pitrou commented on code in PR #11920: URL: https://github.com/apache/arrow/pull/11920#discussion_r887012449
########## cpp/src/arrow/flight/server_tracing_middleware.cc: ########## @@ -0,0 +1,182 @@ +// 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. + +#include "arrow/flight/server_tracing_middleware.h" + +#include <string> +#include <utility> +#include <vector> + +#include "arrow/flight/transport/grpc/util_internal.h" +#include "arrow/util/make_unique.h" +#include "arrow/util/tracing_internal.h" + +#ifdef ARROW_WITH_OPENTELEMETRY +#include <opentelemetry/context/propagation/global_propagator.h> +#include <opentelemetry/context/propagation/text_map_propagator.h> +#include <opentelemetry/trace/context.h> +#include <opentelemetry/trace/experimental_semantic_conventions.h> +#include <opentelemetry/trace/propagation/http_trace_context.h> +#endif + +namespace arrow { +namespace flight { + +#ifdef ARROW_WITH_OPENTELEMETRY +namespace otel = opentelemetry; +namespace { +class FlightServerCarrier : public otel::context::propagation::TextMapCarrier { + public: + explicit FlightServerCarrier(const CallHeaders& incoming_headers) + : incoming_headers_(incoming_headers) {} + + otel::nostd::string_view Get(otel::nostd::string_view key) const noexcept override { + util::string_view arrow_key(key.data(), key.size()); + auto it = incoming_headers_.find(arrow_key); + if (it == incoming_headers_.end()) return ""; + util::string_view result = it->second; + return {result.data(), result.size()}; + } + + void Set(otel::nostd::string_view, otel::nostd::string_view) noexcept override {} + + const CallHeaders& incoming_headers_; +}; +class KeyValueCarrier : public otel::context::propagation::TextMapCarrier { + public: + explicit KeyValueCarrier(std::vector<TracingServerMiddleware::TraceKey>* items) + : items_(items) {} + otel::nostd::string_view Get(otel::nostd::string_view key) const noexcept override { + return {}; + } + void Set(otel::nostd::string_view key, + otel::nostd::string_view value) noexcept override { + items_->emplace_back(std::string(key), std::string(value)); + } + + private: + std::vector<TracingServerMiddleware::TraceKey>* items_; +}; +} // namespace + +class TracingServerMiddleware::Impl { + public: + Impl(otel::trace::Scope scope, otel::nostd::shared_ptr<otel::trace::Span> span) + : scope_(std::move(scope)), span_(std::move(span)) {} + void CallCompleted(const Status& status) { + if (!status.ok()) { + auto grpc_status = transport::grpc::ToGrpcStatus(status, /*ctx=*/nullptr); + span_->SetStatus(otel::trace::StatusCode::kError, status.ToString()); + span_->SetAttribute(OTEL_GET_TRACE_ATTR(AttrRpcGrpcStatusCode), + static_cast<int32_t>(grpc_status.error_code())); + } else { + span_->SetStatus(otel::trace::StatusCode::kOk, ""); + span_->SetAttribute(OTEL_GET_TRACE_ATTR(AttrRpcGrpcStatusCode), int32_t(0)); + } Review Comment: Newbie question, but will this automatically end the span? ########## cpp/src/arrow/flight/server_tracing_middleware.h: ########## @@ -0,0 +1,72 @@ +// 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. + +// Middleware implementation for propagating OpenTelemetry spans. + +#pragma once + +#include <memory> +#include <string> +#include <vector> + +#include "arrow/flight/server_middleware.h" +#include "arrow/flight/visibility.h" +#include "arrow/status.h" + +namespace arrow { +namespace flight { + +/// \brief Returns a ServerMiddlewareFactory that handles receiving OpenTelemetry spans. +ARROW_FLIGHT_EXPORT std::shared_ptr<ServerMiddlewareFactory> +MakeTracingServerMiddlewareFactory(); + +/// \brief A server middleware that provides access to the +/// OpenTelemetry context, if present. +/// +/// Used to make the OpenTelemetry span available in Python. +class ARROW_FLIGHT_EXPORT TracingServerMiddleware : public ServerMiddleware { + public: + ~TracingServerMiddleware(); + + static constexpr char const kMiddlewareName[] = + "arrow::flight::TracingServerMiddleware"; + + std::string name() const override { return kMiddlewareName; } + void SendingHeaders(AddCallHeaders*) override; + void CallCompleted(const Status&) override; + + struct TraceKey { + std::string key; + std::string value; + + TraceKey() = default; + TraceKey(std::string key, std::string value) + : key(std::move(key)), value(std::move(value)) {} Review Comment: No, but you could try `items_->push_back({std::string(key), std::string(value)})` ########## cpp/src/arrow/flight/types.h: ########## @@ -167,6 +167,9 @@ struct ARROW_FLIGHT_EXPORT Action { /// \brief Opaque result returned after executing an action struct ARROW_FLIGHT_EXPORT Result { + Result() = default; + explicit Result(std::shared_ptr<Buffer> body) : body(std::move(body)) {} Review Comment: Neither this should be necessary? ########## cpp/src/arrow/flight/flight_test.cc: ########## @@ -1534,5 +1556,136 @@ TEST_F(TestCancel, DoExchange) { ARROW_UNUSED(do_exchange_result.writer->Close()); } +class TracingTestServer : public FlightServerBase { + public: + Status DoAction(const ServerCallContext& call_context, const Action&, + std::unique_ptr<ResultStream>* result) override { + std::vector<Result> results; + auto* middleware = + reinterpret_cast<TracingServerMiddleware*>(call_context.GetMiddleware("tracing")); + if (!middleware) return Status::Invalid("Could not find middleware"); +#ifdef ARROW_WITH_OPENTELEMETRY + EXPECT_GT(middleware->GetTraceContext().size(), 0); Review Comment: Can you comment a bit on what this does/tests? ########## cpp/src/arrow/flight/flight_test.cc: ########## @@ -1534,5 +1556,136 @@ TEST_F(TestCancel, DoExchange) { ARROW_UNUSED(do_exchange_result.writer->Close()); } +class TracingTestServer : public FlightServerBase { + public: + Status DoAction(const ServerCallContext& call_context, const Action&, + std::unique_ptr<ResultStream>* result) override { + std::vector<Result> results; + auto* middleware = + reinterpret_cast<TracingServerMiddleware*>(call_context.GetMiddleware("tracing")); + if (!middleware) return Status::Invalid("Could not find middleware"); +#ifdef ARROW_WITH_OPENTELEMETRY + EXPECT_GT(middleware->GetTraceContext().size(), 0); + auto span = arrow::internal::tracing::GetTracer()->GetCurrentSpan(); + const auto context = span->GetContext(); + { + const auto& span_id = context.span_id(); + ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateBuffer(span_id.Id().size())); + std::memcpy(buffer->mutable_data(), span_id.Id().data(), span_id.Id().size()); + results.emplace_back(std::move(buffer)); + } + { + const auto& trace_id = context.trace_id(); + ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateBuffer(trace_id.Id().size())); + std::memcpy(buffer->mutable_data(), trace_id.Id().data(), trace_id.Id().size()); + results.emplace_back(std::move(buffer)); + } +#else + EXPECT_EQ(middleware->GetTraceContext().size(), 0); +#endif + *result = arrow::internal::make_unique<SimpleResultStream>(std::move(results)); + return Status::OK(); + } +}; + +class TestTracing : public ::testing::Test { + public: + void SetUp() { +#ifdef ARROW_WITH_OPENTELEMETRY + // The default tracer always generates no-op spans which have no + // span/trace ID. Set up a different tracer. Note, this needs to + // be run before Arrow uses OTel as GetTracer() gets a tracer once + // and keeps it in a static. + std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>> processors; + auto provider = + opentelemetry::nostd::shared_ptr<opentelemetry::sdk::trace::TracerProvider>( + new opentelemetry::sdk::trace::TracerProvider(std::move(processors))); + opentelemetry::trace::Provider::SetTracerProvider(std::move(provider)); + + opentelemetry::context::propagation::GlobalTextMapPropagator::SetGlobalPropagator( + opentelemetry::nostd::shared_ptr< + opentelemetry::context::propagation::TextMapPropagator>( + new opentelemetry::trace::propagation::HttpTraceContext())); Review Comment: Is this the kind of setup that third party server code would have to write as well? ########## python/pyarrow/tests/test_flight.py: ########## @@ -2177,3 +2177,32 @@ def test_interpreter_shutdown(): See https://issues.apache.org/jira/browse/ARROW-16597. """ util.invoke_script("arrow_16597.py") + + +class TracingFlightServer(FlightServerBase): + """A server that echoes back trace context values.""" + + def do_action(self, context, action): + trace_context = context.get_middleware("tracing").trace_context + # Don't turn this method into a generator since then it'll be + # lazily evaluated, and the trace context will be lost Review Comment: If `trace_context` is a regular Python dict, how would it be lost? ########## cpp/src/arrow/flight/client_tracing_middleware.cc: ########## @@ -0,0 +1,100 @@ +// 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. + +#include "arrow/flight/client_tracing_middleware.h" + +#include <string> +#include <utility> +#include <vector> + +#include "arrow/util/make_unique.h" +#include "arrow/util/tracing_internal.h" + +#ifdef ARROW_WITH_OPENTELEMETRY +#include <opentelemetry/context/propagation/global_propagator.h> +#include <opentelemetry/context/propagation/text_map_propagator.h> +#endif + +namespace arrow { +namespace flight { + +namespace { +#ifdef ARROW_WITH_OPENTELEMETRY +namespace otel = opentelemetry; +class FlightClientCarrier : public otel::context::propagation::TextMapCarrier { + public: + FlightClientCarrier() = default; + + otel::nostd::string_view Get(otel::nostd::string_view key) const noexcept override { + return ""; + } + + void Set(otel::nostd::string_view key, + otel::nostd::string_view value) noexcept override { + context_.emplace_back(key, value); + } + + std::vector<std::pair<std::string, std::string>> context_; +}; + +class TracingClientMiddleware : public ClientMiddleware { + public: + explicit TracingClientMiddleware(FlightClientCarrier carrier) + : carrier_(std::move(carrier)) {} + virtual ~TracingClientMiddleware() = default; + + void SendingHeaders(AddCallHeaders* outgoing_headers) override { + for (const auto& pair : carrier_.context_) { + outgoing_headers->AddHeader(pair.first, pair.second); Review Comment: Hmm, can you add a comment about that? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
