Skip to content

Commit

Permalink
Move some utils to internal header
Browse files Browse the repository at this point in the history
  • Loading branch information
benibus committed Feb 29, 2024
1 parent be71c78 commit bb26b4b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 63 deletions.
66 changes: 4 additions & 62 deletions cpp/src/arrow/telemetry/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "arrow/result.h"
#include "arrow/telemetry/logging.h"
#include "arrow/telemetry/util.h"
#include "arrow/telemetry/util_internal.h"
#include "arrow/util/io_util.h"
#include "arrow/util/logging.h"

Expand Down Expand Up @@ -75,69 +75,12 @@ std::unique_ptr<Logger> MakeNoopLogger() { return std::make_unique<NoopLogger>()

#ifdef ARROW_WITH_OPENTELEMETRY

namespace otel = ::opentelemetry;
namespace SemanticConventions = otel::sdk::resource::SemanticConventions;

namespace {

template <typename T>
using otel_shared_ptr = otel::nostd::shared_ptr<T>;
template <typename T>
using otel_span = otel::nostd::span<T>;
using otel_string_view = otel::nostd::string_view;

using util::span;

constexpr const char kLoggingBackendEnvVar[] = "ARROW_LOGGING_BACKEND";

struct AttributeConverter {
using OtelValue = otel::common::AttributeValue;

OtelValue operator()(bool v) { return OtelValue(v); }
OtelValue operator()(int32_t v) { return OtelValue(v); }
OtelValue operator()(uint32_t v) { return OtelValue(v); }
OtelValue operator()(int64_t v) { return OtelValue(v); }
OtelValue operator()(double v) { return OtelValue(v); }
OtelValue operator()(const char* v) { return OtelValue(otel_string_view(v)); }
OtelValue operator()(std::string_view v) {
return OtelValue(otel_string_view(v.data(), v.length()));
}
OtelValue operator()(span<const uint8_t> v) { return ToOtelSpan<uint8_t>(v); }
OtelValue operator()(span<const int32_t> v) { return ToOtelSpan<int32_t>(v); }
OtelValue operator()(span<const uint32_t> v) { return ToOtelSpan<uint32_t>(v); }
OtelValue operator()(span<const int64_t> v) { return ToOtelSpan<int64_t>(v); }
OtelValue operator()(span<const uint64_t> v) { return ToOtelSpan<uint64_t>(v); }
OtelValue operator()(span<const double> v) { return ToOtelSpan<double>(v); }
OtelValue operator()(span<const char* const> v) {
return ToOtelStringSpan<const char*>(v);
}
OtelValue operator()(span<const std::string> v) {
return ToOtelStringSpan<std::string>(v);
}
OtelValue operator()(span<const std::string_view> v) {
return ToOtelStringSpan<std::string_view>(v);
}

private:
template <typename T, typename U = T>
OtelValue ToOtelSpan(span<const U> vals) const {
return otel_span<const T>(vals.begin(), vals.end());
}

template <typename T>
OtelValue ToOtelStringSpan(span<const T> vals) {
const size_t length = vals.size();
output_views_.resize(length);
for (size_t i = 0; i < length; ++i) {
const std::string_view s{vals[i]};
output_views_[i] = otel_string_view(s.data(), s.length());
}
return otel_span<const otel_string_view>(output_views_.data(), length);
}

std::vector<otel_string_view> output_views_;
};

class OtlpOStreamLogRecordExporter final : public otel::sdk::logs::LogRecordExporter {
public:
explicit OtlpOStreamLogRecordExporter(std::ostream* sink) : sink_(sink) {
Expand Down Expand Up @@ -324,8 +267,7 @@ class OtelLogger : public Logger {
if (desc.attributes && desc.attributes->num_attributes() > 0) {
auto callback = [&log](std::string_view k, const AttributeValue& v) -> bool {
AttributeConverter converter{};
log->SetAttribute(otel_string_view(k.data(), k.length()),
std::visit(converter, v));
log->SetAttribute(ToOtel(k), std::visit(converter, v));
return true;
};
desc.attributes->ForEach(std::move(callback));
Expand All @@ -343,11 +285,11 @@ class OtelLogger : public Logger {

if (desc.body) {
auto body = *desc.body;
log->SetBody(otel_string_view(body.data(), body.length()));
log->SetBody(ToOtel(body));
}

if (const auto& event = desc.event_id; event.is_valid()) {
log->SetEventId(event.id, otel_string_view(event.name.data(), event.name.length()));
log->SetEventId(event.id, ToOtel(event.name));
}

logger_->EmitLogRecord(std::move(log));
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/arrow/telemetry/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <string>
#include <string_view>

#include "arrow/telemetry/util.h"
#include "arrow/util/config.h"
#include "arrow/util/logging.h"
#include "arrow/util/macros.h"
Expand All @@ -32,6 +31,8 @@
namespace arrow {
namespace telemetry {

class AttributeHolder;

using LogLevel = util::ArrowLogLevel;

/// \brief Attributes to be set in an OpenTelemetry resource
Expand Down
103 changes: 103 additions & 0 deletions cpp/src/arrow/telemetry/util_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

// Pick up ARROW_WITH_OPENTELEMETRY first
#include "arrow/util/config.h"

#include "arrow/telemetry/util.h"

#ifdef ARROW_WITH_OPENTELEMETRY
#include <opentelemetry/common/attribute_value.h>
#include <opentelemetry/nostd/shared_ptr.h>
#include <opentelemetry/nostd/span.h>
#include <opentelemetry/nostd/string_view.h>
#endif

namespace arrow {
namespace telemetry {

#ifdef ARROW_WITH_OPENTELEMETRY
using util::span;

namespace otel = ::opentelemetry;

template <typename T>
using otel_shared_ptr = otel::nostd::shared_ptr<T>;
template <typename T>
using otel_span = otel::nostd::span<T>;
using otel_string_view = otel::nostd::string_view;

inline otel_string_view ToOtel(std::string_view in) {
return otel_string_view(in.data(), in.length());
}

/// \brief Converts AttributeValues to their equivalent OTel types (compatible with
/// std::visit)
///
/// NOTE: This class is stateful and allocates/owns memory when converting string spans,
/// so one should ensure that the output spans are copied before the converter is reused
/// or freed.
struct AttributeConverter {
using OtelValue = otel::common::AttributeValue;

OtelValue operator()(bool v) { return OtelValue(v); }
OtelValue operator()(int32_t v) { return OtelValue(v); }
OtelValue operator()(uint32_t v) { return OtelValue(v); }
OtelValue operator()(int64_t v) { return OtelValue(v); }
OtelValue operator()(double v) { return OtelValue(v); }
OtelValue operator()(const char* v) { return OtelValue(otel_string_view(v)); }
OtelValue operator()(std::string_view v) { return OtelValue(ToOtel(v)); }
OtelValue operator()(span<const uint8_t> v) { return ToOtelSpan<uint8_t>(v); }
OtelValue operator()(span<const int32_t> v) { return ToOtelSpan<int32_t>(v); }
OtelValue operator()(span<const uint32_t> v) { return ToOtelSpan<uint32_t>(v); }
OtelValue operator()(span<const int64_t> v) { return ToOtelSpan<int64_t>(v); }
OtelValue operator()(span<const uint64_t> v) { return ToOtelSpan<uint64_t>(v); }
OtelValue operator()(span<const double> v) { return ToOtelSpan<double>(v); }
OtelValue operator()(span<const char* const> v) {
return ToOtelStringSpan<const char*>(v);
}
OtelValue operator()(span<const std::string> v) {
return ToOtelStringSpan<std::string>(v);
}
OtelValue operator()(span<const std::string_view> v) {
return ToOtelStringSpan<std::string_view>(v);
}

private:
template <typename T, typename U = T>
OtelValue ToOtelSpan(span<const U> vals) const {
return otel_span<const T>(vals.begin(), vals.end());
}

template <typename T>
OtelValue ToOtelStringSpan(span<const T> vals) {
const size_t length = vals.size();
output_views_.resize(length);
for (size_t i = 0; i < length; ++i) {
output_views_[i] = ToOtel(std::string_view(vals[i]));
}
return otel_span<const otel_string_view>(output_views_.data(), length);
}

std::vector<otel_string_view> output_views_;
};
#endif

} // namespace telemetry
} // namespace arrow

0 comments on commit bb26b4b

Please sign in to comment.