GettingStarted.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. Getting started
  2. ---------------
  3. Tracing
  4. ~~~~~~~
  5. When instrumenting libraries and applications, the most simple approach
  6. requires three steps.
  7. Obtain a tracer
  8. ^^^^^^^^^^^^^^^
  9. .. code:: cpp
  10. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  11. auto tracer = provider->GetTracer("foo_library", "1.0.0");
  12. The ``TracerProvider`` acquired in the first step is a singleton object
  13. that is usually provided by the OpenTelemetry C++ SDK. It is used to
  14. provide specific implementations for API interfaces. In case no SDK is
  15. used, the API provides a default no-op implementation of a
  16. ``TracerProvider``.
  17. The ``Tracer`` acquired in the second step is needed to create and start
  18. ``Span``\ s.
  19. Start a span
  20. ^^^^^^^^^^^^
  21. .. code:: cpp
  22. auto span = tracer->StartSpan("HandleRequest");
  23. This creates a span, sets its name to ``"HandleRequest"``, and sets its
  24. start time to the current time. Refer to the API documentation for other
  25. operations that are available to enrich spans with additional data.
  26. Mark a span as active
  27. ^^^^^^^^^^^^^^^^^^^^^
  28. .. code:: cpp
  29. auto scope = tracer->WithActiveSpan(span);
  30. This marks a span as active and returns a ``Scope`` object. The scope object
  31. controls how long a span is active. The span remains active for the lifetime
  32. of the scope object.
  33. The concept of an active span is important, as any span that is created
  34. without explicitly specifying a parent is parented to the currently
  35. active span. A span without a parent is called root span.
  36. Create nested Spans
  37. ^^^^^^^^^^^^^^^^^^^
  38. .. code:: cpp
  39. auto outer_span = tracer->StartSpan("Outer operation");
  40. auto outer_scope = tracer->WithActiveSpan(outer_span);
  41. {
  42. auto inner_span = tracer->StartSpan("Inner operation");
  43. auto inner_scope = tracer->WithActiveSpan(inner_span);
  44. // ... perform inner operation
  45. inner_span->End();
  46. }
  47. // ... perform outer operation
  48. outer_span->End();
  49. Spans can be nested, and have a parent-child relationship with other spans.
  50. When a given span is active, the newly created span inherits the active span's
  51. trace ID, and other context attributes.
  52. Context Propagation
  53. ^^^^^^^^^^^^^^^^^^
  54. .. code:: cpp
  55. // set global propagator
  56. opentelemetry::context::propagation::GlobalTextMapPropagator::SetGlobalPropagator(
  57. nostd::shared_ptr<opentelemetry::context::propagation::TextMapPropagator>(
  58. new opentelemetry::trace::propagation::HttpTraceContext()));
  59. // get global propagator
  60. HttpTextMapCarrier<opentelemetry::ext::http::client::Headers> carrier;
  61. auto propagator =
  62. opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator();
  63. //inject context to headers
  64. auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent();
  65. propagator->Inject(carrier, current_ctx);
  66. //Extract headers to context
  67. auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent();
  68. auto new_context = propagator->Extract(carrier, current_ctx);
  69. auto remote_span = opentelemetry::trace::propagation::GetSpan(new_context);
  70. ``Context`` contains the meta-data of the currently active Span including Span Id,
  71. Trace Id, and flags. Context Propagation is an important mechanism in distributed
  72. tracing to transfer this Context across service boundary often through HTTP headers.
  73. OpenTelemetry provides a text-based approach to propagate context to remote services
  74. using the W3C Trace Context HTTP headers.