|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/flight/client_tracing_middleware.h" |
| 19 | + |
| 20 | +#include <memory> |
| 21 | +#include <string> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#include "arrow/util/tracing_internal.h" |
| 26 | + |
| 27 | +#ifdef ARROW_WITH_OPENTELEMETRY |
| 28 | +#include <opentelemetry/context/propagation/global_propagator.h> |
| 29 | +#include <opentelemetry/context/propagation/text_map_propagator.h> |
| 30 | +#endif |
| 31 | + |
| 32 | +namespace arrow { |
| 33 | +namespace flight { |
| 34 | + |
| 35 | +namespace { |
| 36 | +#ifdef ARROW_WITH_OPENTELEMETRY |
| 37 | +namespace otel = opentelemetry; |
| 38 | +class FlightClientCarrier : public otel::context::propagation::TextMapCarrier { |
| 39 | + public: |
| 40 | + FlightClientCarrier() = default; |
| 41 | + |
| 42 | + otel::nostd::string_view Get(otel::nostd::string_view key) const noexcept override { |
| 43 | + return ""; |
| 44 | + } |
| 45 | + |
| 46 | + void Set(otel::nostd::string_view key, |
| 47 | + otel::nostd::string_view value) noexcept override { |
| 48 | + context_.emplace_back(key, value); |
| 49 | + } |
| 50 | + |
| 51 | + std::vector<std::pair<std::string, std::string>> context_; |
| 52 | +}; |
| 53 | + |
| 54 | +class TracingClientMiddleware : public ClientMiddleware { |
| 55 | + public: |
| 56 | + explicit TracingClientMiddleware(FlightClientCarrier carrier) |
| 57 | + : carrier_(std::move(carrier)) {} |
| 58 | + virtual ~TracingClientMiddleware() = default; |
| 59 | + |
| 60 | + void SendingHeaders(AddCallHeaders* outgoing_headers) override { |
| 61 | + // The exact headers added are not arbitrary and are defined in |
| 62 | + // the OpenTelemetry specification (see |
| 63 | + // open-telemetry/opentelemetry-specification api-propagators.md) |
| 64 | + for (const auto& pair : carrier_.context_) { |
| 65 | + outgoing_headers->AddHeader(pair.first, pair.second); |
| 66 | + } |
| 67 | + } |
| 68 | + void ReceivedHeaders(const CallHeaders&) override {} |
| 69 | + void CallCompleted(const Status&) override {} |
| 70 | + |
| 71 | + private: |
| 72 | + FlightClientCarrier carrier_; |
| 73 | +}; |
| 74 | + |
| 75 | +class TracingClientMiddlewareFactory : public ClientMiddlewareFactory { |
| 76 | + public: |
| 77 | + virtual ~TracingClientMiddlewareFactory() = default; |
| 78 | + void StartCall(const CallInfo& info, |
| 79 | + std::unique_ptr<ClientMiddleware>* middleware) override { |
| 80 | + FlightClientCarrier carrier; |
| 81 | + auto context = otel::context::RuntimeContext::GetCurrent(); |
| 82 | + auto propagator = |
| 83 | + otel::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); |
| 84 | + propagator->Inject(carrier, context); |
| 85 | + *middleware = std::make_unique<TracingClientMiddleware>(std::move(carrier)); |
| 86 | + } |
| 87 | +}; |
| 88 | +#else |
| 89 | +class TracingClientMiddlewareFactory : public ClientMiddlewareFactory { |
| 90 | + public: |
| 91 | + virtual ~TracingClientMiddlewareFactory() = default; |
| 92 | + void StartCall(const CallInfo&, std::unique_ptr<ClientMiddleware>*) override {} |
| 93 | +}; |
| 94 | +#endif |
| 95 | +} // namespace |
| 96 | + |
| 97 | +std::shared_ptr<ClientMiddlewareFactory> MakeTracingClientMiddlewareFactory() { |
| 98 | + return std::make_shared<TracingClientMiddlewareFactory>(); |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace flight |
| 102 | +} // namespace arrow |
0 commit comments