|
| 1 | +/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2021 Couchbase, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "otel_tracer.hxx" |
| 19 | + |
| 20 | +#include "../meta/version.hxx" |
| 21 | + |
| 22 | +#if defined(__GNUC__) |
| 23 | +#pragma GCC diagnostic push |
| 24 | +#pragma GCC diagnostic ignored "-Wsign-conversion" |
| 25 | +#endif |
| 26 | +#include <opentelemetry/trace/provider.h> |
| 27 | +#include <opentelemetry/trace/tracer.h> |
| 28 | +#if defined(__GNUC__) |
| 29 | +#pragma GCC diagnostic pop |
| 30 | +#endif |
| 31 | + |
| 32 | +namespace couchbase::core::tracing |
| 33 | +{ |
| 34 | +namespace |
| 35 | +{ |
| 36 | +class otel_request_span : public couchbase::tracing::request_span |
| 37 | +{ |
| 38 | +public: |
| 39 | + explicit otel_request_span(opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span) |
| 40 | + : span_(std::move(span)) |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + void add_tag(const std::string& name, const std::string& value) override |
| 45 | + { |
| 46 | + span_->SetAttribute(name, value); |
| 47 | + } |
| 48 | + |
| 49 | + void add_tag(const std::string& name, std::uint64_t value) override |
| 50 | + { |
| 51 | + span_->SetAttribute(name, value); |
| 52 | + } |
| 53 | + |
| 54 | + void end() override |
| 55 | + { |
| 56 | + span_->End(); |
| 57 | + } |
| 58 | + |
| 59 | + auto wrapped_span() -> opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> |
| 60 | + { |
| 61 | + return span_; |
| 62 | + } |
| 63 | + |
| 64 | +private: |
| 65 | + opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span_; |
| 66 | +}; |
| 67 | + |
| 68 | +} // namespace |
| 69 | + |
| 70 | +class otel_request_tracer_impl |
| 71 | +{ |
| 72 | + friend otel_request_tracer; |
| 73 | + |
| 74 | +public: |
| 75 | + otel_request_tracer_impl() |
| 76 | + : tracer_{ |
| 77 | + opentelemetry::trace::Provider::GetTracerProvider()->GetTracer("couchbase_cxx_sdk", |
| 78 | + meta::sdk_semver()), |
| 79 | + } |
| 80 | + { |
| 81 | + } |
| 82 | + |
| 83 | +private: |
| 84 | + opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> tracer_; |
| 85 | +}; |
| 86 | + |
| 87 | +otel_request_tracer::otel_request_tracer() |
| 88 | + : impl_{ std::make_unique<otel_request_tracer_impl>() } |
| 89 | +{ |
| 90 | +} |
| 91 | + |
| 92 | +otel_request_tracer::~otel_request_tracer() = default; |
| 93 | + |
| 94 | + |
| 95 | +auto |
| 96 | +otel_request_tracer::start_span(std::string name, |
| 97 | + std::shared_ptr<couchbase::tracing::request_span> parent) |
| 98 | + -> std::shared_ptr<couchbase::tracing::request_span> |
| 99 | +{ |
| 100 | + auto wrapped_parent = std::dynamic_pointer_cast<otel_request_span>(parent); |
| 101 | + if (wrapped_parent) { |
| 102 | + opentelemetry::trace::StartSpanOptions opts; |
| 103 | + opts.parent = wrapped_parent->wrapped_span()->GetContext(); |
| 104 | + return std::make_shared<otel_request_span>(impl_->tracer_->StartSpan(name, opts)); |
| 105 | + } |
| 106 | + return std::make_shared<otel_request_span>(impl_->tracer_->StartSpan(name)); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace couchbase::core::tracing |
0 commit comments