/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * Copyright 2021 Couchbase, Inc. * * Licensed 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 #include #include #include #include #include namespace nostd = opentelemetry::nostd; namespace couchbase::tracing { class otel_request_span : public couchbase::tracing::request_span { public: explicit otel_request_span(nostd::shared_ptr span) : span_(std::move(span)) { } void add_tag(const std::string& name, const std::string& value) override { span_->SetAttribute(name, value); } void add_tag(const std::string& name, uint64_t value) override { span_->SetAttribute(name, value); } void end() override { span_->End(); } nostd::shared_ptr wrapped_span() { return span_; } private: nostd::shared_ptr span_; }; class otel_request_tracer : public couchbase::tracing::request_tracer { public: otel_request_tracer(nostd::shared_ptr tracer) : tracer_(std::move(tracer)) { } std::shared_ptr start_span(std::string name, std::shared_ptr parent = {}) override { auto wrapped_parent = std::dynamic_pointer_cast(parent); if (wrapped_parent) { opentelemetry::trace::StartSpanOptions opts; opts.parent = wrapped_parent->wrapped_span()->GetContext(); return std::make_shared(tracer_->StartSpan(name, opts)); } return std::make_shared(tracer_->StartSpan(name)); } std::shared_ptr wrap_span(nostd::shared_ptr span) { return std::make_shared(span); } private: nostd::shared_ptr tracer_; }; } // namespace couchbase::tracing