Couchbase C++ SDK 1.0.2 (rev. 51f4775)
Loading...
Searching...
No Matches
otel_tracer.hxx
Go to the documentation of this file.
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#pragma once
19
21#include <opentelemetry/exporters/ostream/span_exporter.h>
22#include <opentelemetry/sdk/trace/simple_processor.h>
23#include <opentelemetry/sdk/trace/tracer_provider.h>
24#include <opentelemetry/trace/tracer.h>
25
26namespace nostd = opentelemetry::nostd;
28{
30{
31public:
32 explicit otel_request_span(nostd::shared_ptr<opentelemetry::trace::Span> span)
33 : span_(std::move(span))
34 {
35 }
36 void add_tag(const std::string& name, const std::string& value) override
37 {
38 span_->SetAttribute(name, value);
39 }
40 void add_tag(const std::string& name, uint64_t value) override
41 {
42 span_->SetAttribute(name, value);
43 }
44 void end() override
45 {
46 span_->End();
47 }
48 nostd::shared_ptr<opentelemetry::trace::Span> wrapped_span()
49 {
50 return span_;
51 }
52
53private:
54 nostd::shared_ptr<opentelemetry::trace::Span> span_;
55};
56
58{
59public:
60 otel_request_tracer(nostd::shared_ptr<opentelemetry::trace::Tracer> tracer)
61 : tracer_(std::move(tracer))
62 {
63 }
64
65 auto start_span(std::string name, std::shared_ptr<couchbase::tracing::request_span> parent = {})
66 -> std::shared_ptr<couchbase::tracing::request_span> override
67 {
68 auto wrapped_parent = std::dynamic_pointer_cast<otel_request_span>(parent);
69 if (wrapped_parent) {
70 opentelemetry::trace::StartSpanOptions opts;
71 opts.parent = wrapped_parent->wrapped_span()->GetContext();
72 return std::make_shared<otel_request_span>(tracer_->StartSpan(name, opts));
73 }
74 return std::make_shared<otel_request_span>(tracer_->StartSpan(name));
75 }
76
77 auto wrap_span(nostd::shared_ptr<opentelemetry::trace::Span> span)
78 -> std::shared_ptr<couchbase::tracing::otel_request_span>
79 {
80 return std::make_shared<couchbase::tracing::otel_request_span>(span);
81 }
82
83private:
84 nostd::shared_ptr<opentelemetry::trace::Tracer> tracer_;
85};
86} // namespace couchbase::tracing
Definition otel_tracer.hxx:30
void add_tag(const std::string &name, uint64_t value) override
Definition otel_tracer.hxx:40
nostd::shared_ptr< opentelemetry::trace::Span > wrapped_span()
Definition otel_tracer.hxx:48
void end() override
Definition otel_tracer.hxx:44
void add_tag(const std::string &name, const std::string &value) override
Definition otel_tracer.hxx:36
otel_request_span(nostd::shared_ptr< opentelemetry::trace::Span > span)
Definition otel_tracer.hxx:32
Definition otel_tracer.hxx:58
auto start_span(std::string name, std::shared_ptr< couchbase::tracing::request_span > parent={}) -> std::shared_ptr< couchbase::tracing::request_span > override
Definition otel_tracer.hxx:65
auto wrap_span(nostd::shared_ptr< opentelemetry::trace::Span > span) -> std::shared_ptr< couchbase::tracing::otel_request_span >
Definition otel_tracer.hxx:77
otel_request_tracer(nostd::shared_ptr< opentelemetry::trace::Tracer > tracer)
Definition otel_tracer.hxx:60
Definition request_span.hxx:27
auto name() const -> const std::string &
Definition request_span.hxx:49
Definition request_tracer.hxx:28
Definition otel_tracer.hxx:28
@ parent
Notify the context that the process has forked and is the parent.