4 OpenTelemetry C++ SDK provides the reference implementation of OpenTelemetry C++ API,
5 and also provides implementation for Processor, Sampler, and core Exporters as per the
12 An exporter is responsible for sending the telemetry data to a particular backend.
13 OpenTelemetry offers six tracing exporters out of the box:
15 - In-Memory Exporter: keeps the data in memory, useful for debugging.
16 - Jaeger Exporter: prepares and sends the collected telemetry data to a Jaeger backend via UDP and HTTP.
17 - Zipkin Exporter: prepares and sends the collected telemetry data to a Zipkin backend via the Zipkin APIs.
18 - Logging Exporter: saves the telemetry data into log streams.
19 - OpenTelemetry(otlp) Exporter: sends the data to the OpenTelemetry Collector using protobuf/gRPC or protobuf/HTTP.
20 - ETW Exporter: sends the telemetry data to Event Tracing for Windows (ETW).
24 //namespace alias used in sample code here.
25 namespace sdktrace = opentelemetry::sdk::trace;
28 auto ostream_exporter =
29 std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::trace::OStreamSpanExporter);
32 auto memory_exporter =
33 std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::memory::InMemorySpanExporter);
36 opentelemetry::exporter::zipkin::ZipkinExporterOptions opts;
37 opts.endpoint = "http://localhost:9411/api/v2/spans" ; // or export OTEL_EXPORTER_ZIPKIN_ENDPOINT="..."
38 opts.service_name = "default_service" ;
39 auto zipkin_exporter =
40 std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::zipkin::ZipkinExporter(opts));
42 // Jaeger UDP exporter
43 opentelemetry::exporter::jaeger::JaegerExporterOptions opts;
44 opts.endpoint = "localhost";
45 opts.server_port = 6831;
46 auto jaeger_udp_exporter =
47 std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::jaeger::JaegerExporter(opts));
49 // Jaeger HTTP exporter
50 opentelemetry::exporter::jaeger::JaegerExporterOptions opts;
51 opts.transport_format = opentelemetry::exporter::jaeger::TransportFormat::kThriftHttp;
52 opts.endpoint = "localhost";
53 opts.server_port = 14268;
54 opts.headers = {{}}; // optional headers
55 auto jaeger_http_exporter =
56 std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::jaeger::JaegerExporter(opts));
60 opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts;
61 opts.endpoint = "localhost::4317";
62 opts.use_ssl_credentials = true;
63 opts.ssl_credentials_cacert_as_string = "ssl-certificate";
64 auto otlp_grpc_exporter =
65 std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::otlp::OtlpGrpcExporter(opts));
68 opentelemetry::exporter::otlp::OtlpHttpExporterOptions opts;
69 opts.url = "http://localhost:4318/v1/traces";
70 auto otlp_http_exporter =
71 std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::otlp::OtlpHttpExporter(opts));
77 Span Processor is initialised with an Exporter. Different Span Processors are offered by OpenTelemetry C++ SDK:
79 - SimpleSpanProcessor: immediately forwards ended spans to the exporter.
80 - BatchSpanProcessor: batches the ended spans and send them to exporter in bulk.
81 - MultiSpanProcessor: Allows multiple span processors to be active and configured at the same time.
86 auto simple_processor = std::unique_ptr<sdktrace::SpanProcessor>(
87 new sdktrace::SimpleSpanProcessor(std::move(ostream_exporter)));
90 sdktrace::BatchSpanProcessorOptions options{};
91 auto batch_processor = std::unique_ptr<sdktrace::SpanProcessor>(
92 new sdktrace::BatchSpanProcessor(std::move(memory_exporter), options));
95 std::vector<std::unique_ptr<SpanProcessor>>
96 processors{std::move(simple_processor), std::move(batch_processor)};
97 auto multi_processor = std::unique_ptr<sdktrace::SpanProcessor>(
98 new sdktrace::MultiSpanProcessor(std::move(processors));
103 A Resource is an immutable representation of the entity producing telemetry as key-value pair.
104 The OpenTelemetry C++ SDK allow for creation of Resources and for associating them with telemetry.
108 auto resource_attributes = opentelemetry::sdk::resource::ResourceAttributes
110 {"service.name", "shoppingcart"},
111 {"service.instance.id", "instance-12"}
113 auto resource = opentelemetry::sdk::resource::Resource::Create(resource_attributes);
114 auto received_attributes = resource.GetAttributes();
115 // received_attributes contains
116 // - service.name = shoppingcart
117 // - service.instance.id = instance-12
118 // - telemetry.sdk.name = opentelemetry
119 // - telemetry.sdk.language = cpp
120 // - telemetry.sdk.version = <current sdk version>
122 It is possible to define the custom resource detectors by inhering from
123 `opentelemetry::sdk::Resource::ResourceDetector` class.
128 Sampling is mechanism to control/reducing the number of samples of traces collected and sent to the backend.
129 OpenTelemetry C++ SDK offers four samplers out of the box:
131 - AlwaysOnSampler which samples every trace regardless of upstream sampling decisions.
132 - AlwaysOffSampler which doesn’t sample any trace, regardless of upstream sampling decisions.
133 - ParentBased which uses the parent span to make sampling decisions, if present.
134 - TraceIdRatioBased which samples a configurable percentage of traces.
139 auto always_on_sampler = std::unique_ptr<sdktrace::AlwaysOnSampler>
140 (new sdktrace::AlwaysOnSampler);
143 auto always_off_sampler = std::unique_ptr<sdktrace::AlwaysOffSampler>
144 (new sdktrace::AlwaysOffSampler);
147 auto parent_based_sampler = std::unique_ptr<sdktrace::ParentBasedSampler>
148 (new sdktrace::ParentBasedSampler);
150 //TraceIdRatioBasedSampler - Sample 50% generated spans
152 auto always_off_sampler = std::unique_ptr<sdktrace::TraceIdRatioBasedSampler>
153 (new sdktrace::TraceIdRatioBasedSampler(ratio));
158 SDK configuration are shared between `TracerProvider` and all it's `Tracer` instances through `TracerContext`.
162 auto tracer_context = std::make_shared<sdktrace::TracerContext>
163 (std::move(multi_processor), resource, std::move(always_on_sampler));
168 `TracerProvider` instance holds the SDK configurations ( Span Processors, Samplers, Resource). There is single
169 global TracerProvider instance for an application, and it is created at the start of application.
170 There are two different mechanisms to create TraceProvider instance
172 - Using constructor which takes already created TracerContext shared object as parameter.
173 - Using consructor which takes SDK configurations as parameter.
177 // Created using `TracerContext` instance
178 auto tracer_provider = nostd::shared_ptr<sdktrace::TracerProvider>
179 (new sdktrace::TracerProvider(tracer_context));
181 // Create using SDK configurations as parameter
182 auto tracer_provider = nostd::shared_ptr<sdktrace::TracerProvider>
183 (std::move(simple_processor), resource, std::move(always_on_sampler));
185 // set the global tracer TraceProvider
186 opentelemetry::trace::Provider::SetTracerProvider(tracer_provider);
189 Logging and Error Handling
190 ^^^^^^^^^^^^^^^^^^^^^^^^^^
192 OpenTelemetry C++ SDK provides mechanism for application owner to add customer log and error handler.
193 The default log handler is redirected to standard output ( using std::cout ).
195 The logging macro supports logging using C++ stream format, and key-value pair.
196 The log handler is meant to capture errors and warnings arising from SDK, not supposed to be used for the application errors.
197 The different log levels are supported - Error, Warn, Info and Debug. The default log level is Warn ( to dump both Error and Warn)
198 and it can be changed at compile time.
202 OTEL_INTERNAL_LOG_ERROR(" Connection failed. Error string " << error_str << " Error Num: " << errorno);
203 opentelemetry::sdk::common::AttributeMap error_attributes = {
204 {"url", url}, {"content-length", len}, {"content-type", type}};
205 OTEL_INTERNAL_LOG_ERROR(" Connection failed." , error_attributes);
206 opentelemetry::sdk::common::AttributeMap http_attributes = {
207 {"url", url}, {"content-length", len}, {"content-type", type}};
208 OTEL_INTERNAL_LOG_DEBUG(" Connection Established Successfully. Headers:", http_attributes);
210 The custom log handler can be defined by inheriting from `opentelemetry::sdk::common::internal_log::LogHandler` class.
214 class CustomLogHandler : public opentelemetry::sdk::common::internal_log::LogHandler
216 void Handle(opentelemetry::sdk::common::internal_log::LogLevel level,
220 const opentelemetry::sdk::common::AttributeMap &attributes) noexcept override
223 // add implementation here
226 opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogHandler(CustomLogHandler());
227 opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(opentelemetry::sdk::common::internal_log::LogLevel::Debug);