GettingStarted.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. Getting started
  2. ^^^^^^^^^^^^^^^
  3. OpenTelemetry C++ SDK provides the reference implementation of OpenTelemetry C++ API,
  4. and also provides implementation for Processor, Sampler, and core Exporters as per the
  5. specification.
  6. Exporter
  7. ^^^^^^^^
  8. An exporter is responsible for sending the telemetry data to a particular backend.
  9. OpenTelemetry offers six tracing exporters out of the box:
  10. - In-Memory Exporter: keeps the data in memory, useful for debugging.
  11. - Zipkin Exporter: prepares and sends the collected telemetry data to a Zipkin backend via the Zipkin APIs.
  12. - Logging Exporter: saves the telemetry data into log streams.
  13. - OpenTelemetry(otlp) Exporter: sends the data to the OpenTelemetry Collector using protobuf/gRPC or protobuf/HTTP.
  14. - ETW Exporter: sends the telemetry data to Event Tracing for Windows (ETW).
  15. .. code:: cpp
  16. //namespace alias used in sample code here.
  17. namespace sdktrace = opentelemetry::sdk::trace;
  18. // logging exporter
  19. auto ostream_exporter =
  20. std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::trace::OStreamSpanExporter);
  21. // memory exporter
  22. auto memory_exporter =
  23. std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::memory::InMemorySpanExporter);
  24. // zipkin exporter
  25. opentelemetry::exporter::zipkin::ZipkinExporterOptions opts;
  26. opts.endpoint = "http://localhost:9411/api/v2/spans" ; // or export OTEL_EXPORTER_ZIPKIN_ENDPOINT="..."
  27. opts.service_name = "default_service" ;
  28. auto zipkin_exporter =
  29. std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::zipkin::ZipkinExporter(opts));
  30. // otlp grpc exporter
  31. opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts;
  32. opts.endpoint = "localhost:4317";
  33. opts.use_ssl_credentials = true;
  34. opts.ssl_credentials_cacert_as_string = "ssl-certificate";
  35. auto otlp_grpc_exporter =
  36. std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::otlp::OtlpGrpcExporter(opts));
  37. // otlp http exporter
  38. opentelemetry::exporter::otlp::OtlpHttpExporterOptions opts;
  39. opts.url = "http://localhost:4318/v1/traces";
  40. auto otlp_http_exporter =
  41. std::unique_ptr<sdktrace::SpanExporter>(new opentelemetry::exporter::otlp::OtlpHttpExporter(opts));
  42. Span Processor
  43. ^^^^^^^^^^^^^^
  44. Span Processor is initialised with an Exporter. Different Span Processors are offered by OpenTelemetry C++ SDK:
  45. - SimpleSpanProcessor: immediately forwards ended spans to the exporter.
  46. - BatchSpanProcessor: batches the ended spans and send them to exporter in bulk.
  47. - MultiSpanProcessor: Allows multiple span processors to be active and configured at the same time.
  48. .. code:: cpp
  49. // simple processor
  50. auto simple_processor = std::unique_ptr<sdktrace::SpanProcessor>(
  51. new sdktrace::SimpleSpanProcessor(std::move(ostream_exporter)));
  52. // batch processor
  53. sdktrace::BatchSpanProcessorOptions options{};
  54. auto batch_processor = std::unique_ptr<sdktrace::SpanProcessor>(
  55. new sdktrace::BatchSpanProcessor(std::move(memory_exporter), options));
  56. // multi-processor
  57. std::vector<std::unique_ptr<SpanProcessor>>
  58. processors{std::move(simple_processor), std::move(batch_processor)};
  59. auto multi_processor = std::unique_ptr<sdktrace::SpanProcessor>(
  60. new sdktrace::MultiSpanProcessor(std::move(processors));
  61. Resource
  62. ^^^^^^^^
  63. A Resource is an immutable representation of the entity producing telemetry as key-value pair.
  64. The OpenTelemetry C++ SDK allow for creation of Resources and for associating them with telemetry.
  65. .. code:: cpp
  66. auto resource_attributes = opentelemetry::sdk::resource::ResourceAttributes
  67. {
  68. {"service.name", "shoppingcart"},
  69. {"service.instance.id", "instance-12"}
  70. };
  71. auto resource = opentelemetry::sdk::resource::Resource::Create(resource_attributes);
  72. auto received_attributes = resource.GetAttributes();
  73. // received_attributes contains
  74. // - service.name = shoppingcart
  75. // - service.instance.id = instance-12
  76. // - telemetry.sdk.name = opentelemetry
  77. // - telemetry.sdk.language = cpp
  78. // - telemetry.sdk.version = <current sdk version>
  79. It is possible to define the custom resource detectors by inhering from
  80. `opentelemetry::sdk::Resource::ResourceDetector` class.
  81. Sampler
  82. ^^^^^^^
  83. Sampling is mechanism to control/reducing the number of samples of traces collected and sent to the backend.
  84. OpenTelemetry C++ SDK offers four samplers out of the box:
  85. - AlwaysOnSampler which samples every trace regardless of upstream sampling decisions.
  86. - AlwaysOffSampler which doesn’t sample any trace, regardless of upstream sampling decisions.
  87. - ParentBased which uses the parent span to make sampling decisions, if present.
  88. - TraceIdRatioBased which samples a configurable percentage of traces.
  89. .. code:: cpp
  90. //AlwaysOnSampler
  91. auto always_on_sampler = std::unique_ptr<sdktrace::AlwaysOnSampler>
  92. (new sdktrace::AlwaysOnSampler);
  93. //AlwaysOffSampler
  94. auto always_off_sampler = std::unique_ptr<sdktrace::AlwaysOffSampler>
  95. (new sdktrace::AlwaysOffSampler);
  96. //ParentBasedSampler
  97. auto parent_based_sampler = std::unique_ptr<sdktrace::ParentBasedSampler>
  98. (new sdktrace::ParentBasedSampler);
  99. //TraceIdRatioBasedSampler - Sample 50% generated spans
  100. double ratio = 0.5;
  101. auto always_off_sampler = std::unique_ptr<sdktrace::TraceIdRatioBasedSampler>
  102. (new sdktrace::TraceIdRatioBasedSampler(ratio));
  103. TracerContext
  104. ^^^^^^^^^^^^^
  105. SDK configuration are shared between `TracerProvider` and all it's `Tracer` instances through `TracerContext`.
  106. .. code:: cpp
  107. auto tracer_context = std::make_shared<sdktrace::TracerContext>
  108. (std::move(multi_processor), resource, std::move(always_on_sampler));
  109. TracerProvider
  110. ^^^^^^^^^^^^^^
  111. `TracerProvider` instance holds the SDK configurations ( Span Processors, Samplers, Resource). There is single
  112. global TracerProvider instance for an application, and it is created at the start of application.
  113. There are two different mechanisms to create TraceProvider instance
  114. - Using constructor which takes already created TracerContext shared object as parameter.
  115. - Using consructor which takes SDK configurations as parameter.
  116. .. code:: cpp
  117. // Created using `TracerContext` instance
  118. auto tracer_provider = nostd::shared_ptr<sdktrace::TracerProvider>
  119. (new sdktrace::TracerProvider(tracer_context));
  120. // Create using SDK configurations as parameter
  121. auto tracer_provider = nostd::shared_ptr<sdktrace::TracerProvider>
  122. (std::move(simple_processor), resource, std::move(always_on_sampler));
  123. // set the global tracer TraceProvider
  124. opentelemetry::trace::Provider::SetTracerProvider(tracer_provider);
  125. Logging and Error Handling
  126. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  127. OpenTelemetry C++ SDK provides mechanism for application owner to add customer log and error handler.
  128. The default log handler is redirected to standard output ( using std::cout ).
  129. The logging macro supports logging using C++ stream format, and key-value pair.
  130. The log handler is meant to capture errors and warnings arising from SDK, not supposed to be used for the application errors.
  131. The different log levels are supported - Error, Warn, Info and Debug. The default log level is Warn ( to dump both Error and Warn)
  132. and it can be changed at compile time.
  133. .. code:: cpp
  134. OTEL_INTERNAL_LOG_ERROR(" Connection failed. Error string " << error_str << " Error Num: " << errorno);
  135. opentelemetry::sdk::common::AttributeMap error_attributes = {
  136. {"url", url}, {"content-length", len}, {"content-type", type}};
  137. OTEL_INTERNAL_LOG_ERROR(" Connection failed." , error_attributes);
  138. opentelemetry::sdk::common::AttributeMap http_attributes = {
  139. {"url", url}, {"content-length", len}, {"content-type", type}};
  140. OTEL_INTERNAL_LOG_DEBUG(" Connection Established Successfully. Headers:", http_attributes);
  141. The custom log handler can be defined by inheriting from `opentelemetry::sdk::common::internal_log::LogHandler` class.
  142. .. code:: cpp
  143. class CustomLogHandler : public opentelemetry::sdk::common::internal_log::LogHandler
  144. {
  145. void Handle(opentelemetry::sdk::common::internal_log::LogLevel level,
  146. const char \*file,
  147. int line,
  148. const char \*msg,
  149. const opentelemetry::sdk::common::AttributeMap &attributes) noexcept override
  150. {
  151. // add implementation here
  152. }
  153. };
  154. opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogHandler(CustomLogHandler());
  155. opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(opentelemetry::sdk::common::internal_log::LogLevel::Debug);