From bb26b4b34d3a6e1fea0b50cca014c3fa81ca395e Mon Sep 17 00:00:00 2001 From: benibus Date: Thu, 29 Feb 2024 16:55:42 -0500 Subject: [PATCH] Move some utils to internal header --- cpp/src/arrow/telemetry/logging.cc | 66 +-------------- cpp/src/arrow/telemetry/logging.h | 3 +- cpp/src/arrow/telemetry/util_internal.h | 103 ++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 63 deletions(-) create mode 100644 cpp/src/arrow/telemetry/util_internal.h diff --git a/cpp/src/arrow/telemetry/logging.cc b/cpp/src/arrow/telemetry/logging.cc index b8d5dadeffec0..a19bbfa7653cf 100644 --- a/cpp/src/arrow/telemetry/logging.cc +++ b/cpp/src/arrow/telemetry/logging.cc @@ -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" @@ -75,69 +75,12 @@ std::unique_ptr MakeNoopLogger() { return std::make_unique() #ifdef ARROW_WITH_OPENTELEMETRY -namespace otel = ::opentelemetry; namespace SemanticConventions = otel::sdk::resource::SemanticConventions; namespace { -template -using otel_shared_ptr = otel::nostd::shared_ptr; -template -using otel_span = otel::nostd::span; -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 v) { return ToOtelSpan(v); } - OtelValue operator()(span v) { return ToOtelSpan(v); } - OtelValue operator()(span v) { return ToOtelSpan(v); } - OtelValue operator()(span v) { return ToOtelSpan(v); } - OtelValue operator()(span v) { return ToOtelSpan(v); } - OtelValue operator()(span v) { return ToOtelSpan(v); } - OtelValue operator()(span v) { - return ToOtelStringSpan(v); - } - OtelValue operator()(span v) { - return ToOtelStringSpan(v); - } - OtelValue operator()(span v) { - return ToOtelStringSpan(v); - } - - private: - template - OtelValue ToOtelSpan(span vals) const { - return otel_span(vals.begin(), vals.end()); - } - - template - OtelValue ToOtelStringSpan(span 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(output_views_.data(), length); - } - - std::vector output_views_; -}; - class OtlpOStreamLogRecordExporter final : public otel::sdk::logs::LogRecordExporter { public: explicit OtlpOStreamLogRecordExporter(std::ostream* sink) : sink_(sink) { @@ -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)); @@ -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)); diff --git a/cpp/src/arrow/telemetry/logging.h b/cpp/src/arrow/telemetry/logging.h index e6f70ee7aeb8b..d661dd5bdf4d0 100644 --- a/cpp/src/arrow/telemetry/logging.h +++ b/cpp/src/arrow/telemetry/logging.h @@ -23,7 +23,6 @@ #include #include -#include "arrow/telemetry/util.h" #include "arrow/util/config.h" #include "arrow/util/logging.h" #include "arrow/util/macros.h" @@ -32,6 +31,8 @@ namespace arrow { namespace telemetry { +class AttributeHolder; + using LogLevel = util::ArrowLogLevel; /// \brief Attributes to be set in an OpenTelemetry resource diff --git a/cpp/src/arrow/telemetry/util_internal.h b/cpp/src/arrow/telemetry/util_internal.h new file mode 100644 index 0000000000000..be324fb5053eb --- /dev/null +++ b/cpp/src/arrow/telemetry/util_internal.h @@ -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 +#include +#include +#include +#endif + +namespace arrow { +namespace telemetry { + +#ifdef ARROW_WITH_OPENTELEMETRY +using util::span; + +namespace otel = ::opentelemetry; + +template +using otel_shared_ptr = otel::nostd::shared_ptr; +template +using otel_span = otel::nostd::span; +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 v) { return ToOtelSpan(v); } + OtelValue operator()(span v) { return ToOtelSpan(v); } + OtelValue operator()(span v) { return ToOtelSpan(v); } + OtelValue operator()(span v) { return ToOtelSpan(v); } + OtelValue operator()(span v) { return ToOtelSpan(v); } + OtelValue operator()(span v) { return ToOtelSpan(v); } + OtelValue operator()(span v) { + return ToOtelStringSpan(v); + } + OtelValue operator()(span v) { + return ToOtelStringSpan(v); + } + OtelValue operator()(span v) { + return ToOtelStringSpan(v); + } + + private: + template + OtelValue ToOtelSpan(span vals) const { + return otel_span(vals.begin(), vals.end()); + } + + template + OtelValue ToOtelStringSpan(span 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(output_views_.data(), length); + } + + std::vector output_views_; +}; +#endif + +} // namespace telemetry +} // namespace arrow