NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/trace/propagation/b3_propagator.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <array>
7 
8 #include "detail/hex.h"
9 #include "detail/string.h"
10 #include "opentelemetry/context/propagation/text_map_propagator.h"
11 #include "opentelemetry/trace/context.h"
12 #include "opentelemetry/trace/default_span.h"
13 #include "opentelemetry/version.h"
14 
16 namespace trace
17 {
18 namespace propagation
19 {
20 
22 
23 static const nostd::string_view kB3TraceIdHeader = "X-B3-TraceId";
24 static const nostd::string_view kB3SpanIdHeader = "X-B3-SpanId";
25 static const nostd::string_view kB3SampledHeader = "X-B3-Sampled";
26 
27 /*
28  B3, single header:
29  b3: 80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90
30  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^
31  0 TraceId 31 33 SpanId 48 | 52 ParentSpanId 68
32  50 Debug flag
33  Multiheader version: X-B3-Sampled
34  X-B3-TraceId X-B3-SpanId X-B3-ParentSpanId (ignored)
35 */
36 
37 static const int kTraceIdHexStrLength = 32;
38 static const int kSpanIdHexStrLength = 16;
39 
40 // The B3PropagatorExtractor class provides an interface that enables extracting context from
41 // headers of HTTP requests. HTTP frameworks and clients can integrate with B3Propagator by
42 // providing the object containing the headers, and a getter function for the extraction. Based on:
43 // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md#b3-extract
44 
46 {
47 public:
48  // Returns the context that is stored in the HTTP header carrier.
50  context::Context &context) noexcept override
51  {
52  SpanContext span_context = ExtractImpl(carrier);
53  nostd::shared_ptr<Span> sp{new DefaultSpan(span_context)};
54  if (span_context.IsValid())
55  {
56  return trace::SetSpan(context, sp);
57  }
58  else
59  {
60  return context;
61  }
62  }
63 
65  {
66  uint8_t buf[kTraceIdHexStrLength / 2];
67  detail::HexToBinary(trace_id, buf, sizeof(buf));
68  return TraceId(buf);
69  }
70 
72  {
73  uint8_t buf[kSpanIdHexStrLength / 2];
74  detail::HexToBinary(span_id, buf, sizeof(buf));
75  return SpanId(buf);
76  }
77 
79  {
80  if (trace_flags.length() != 1 || (trace_flags[0] != '1' && trace_flags[0] != 'd'))
81  { // check for invalid length of flags and treat 'd' as sampled
82  return TraceFlags(0);
83  }
85  }
86 
87 private:
88  static SpanContext ExtractImpl(const context::propagation::TextMapCarrier &carrier)
89  {
90  nostd::string_view trace_id_hex;
91  nostd::string_view span_id_hex;
92  nostd::string_view trace_flags_hex;
93 
94  // first let's try a single-header variant
95  auto singleB3Header = carrier.Get(kB3CombinedHeader);
96  if (!singleB3Header.empty())
97  {
98  std::array<nostd::string_view, 3> fields{};
99  // https://github.com/openzipkin/b3-propagation/blob/master/RATIONALE.md
100  if (detail::SplitString(singleB3Header, '-', fields.data(), 3) < 2)
101  {
102  return SpanContext::GetInvalid();
103  }
104 
105  trace_id_hex = fields[0];
106  span_id_hex = fields[1];
107  trace_flags_hex = fields[2];
108  }
109  else
110  {
111  trace_id_hex = carrier.Get(kB3TraceIdHeader);
112  span_id_hex = carrier.Get(kB3SpanIdHeader);
113  trace_flags_hex = carrier.Get(kB3SampledHeader);
114  }
115 
116  if (!detail::IsValidHex(trace_id_hex) || !detail::IsValidHex(span_id_hex))
117  {
118  return SpanContext::GetInvalid();
119  }
120 
121  TraceId trace_id = TraceIdFromHex(trace_id_hex);
122  SpanId span_id = SpanIdFromHex(span_id_hex);
123 
124  if (!trace_id.IsValid() || !span_id.IsValid())
125  {
126  return SpanContext::GetInvalid();
127  }
128 
129  return SpanContext(trace_id, span_id, TraceFlagsFromHex(trace_flags_hex), true);
130  }
131 };
132 
133 // The B3Propagator class provides interface that enables extracting and injecting context into
134 // single header of HTTP Request.
136 {
137 public:
138  // Sets the context for a HTTP header carrier with self defined rules.
140  const context::Context &context) noexcept override
141  {
142  SpanContext span_context = trace::GetSpan(context)->GetContext();
143  if (!span_context.IsValid())
144  {
145  return;
146  }
147 
148  char trace_identity[kTraceIdHexStrLength + kSpanIdHexStrLength + 3];
149  static_assert(sizeof(trace_identity) == 51, "b3 trace identity buffer size mismatch");
151  &trace_identity[0], static_cast<std::size_t>(kTraceIdHexStrLength)});
152  trace_identity[kTraceIdHexStrLength] = '-';
154  &trace_identity[kTraceIdHexStrLength + 1], static_cast<std::size_t>(kSpanIdHexStrLength)});
155  trace_identity[kTraceIdHexStrLength + kSpanIdHexStrLength + 1] = '-';
156  trace_identity[kTraceIdHexStrLength + kSpanIdHexStrLength + 2] =
157  span_context.trace_flags().IsSampled() ? '1' : '0';
158 
159  carrier.Set(kB3CombinedHeader, nostd::string_view(trace_identity, sizeof(trace_identity)));
160  }
161 
162  bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
163  {
164  return callback(kB3CombinedHeader);
165  }
166 };
167 
169 {
170 public:
172  const context::Context &context) noexcept override
173  {
174  SpanContext span_context = GetSpan(context)->GetContext();
175  if (!span_context.IsValid())
176  {
177  return;
178  }
179  char trace_id[32];
180  TraceId(span_context.trace_id()).ToLowerBase16(trace_id);
181  char span_id[16];
182  SpanId(span_context.span_id()).ToLowerBase16(span_id);
183  char trace_flags[2];
184  TraceFlags(span_context.trace_flags()).ToLowerBase16(trace_flags);
185  carrier.Set(kB3TraceIdHeader, nostd::string_view(trace_id, sizeof(trace_id)));
186  carrier.Set(kB3SpanIdHeader, nostd::string_view(span_id, sizeof(span_id)));
187  carrier.Set(kB3SampledHeader, nostd::string_view(trace_flags + 1, 1));
188  }
189 
190  bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
191  {
192  return callback(kB3TraceIdHeader) && callback(kB3SpanIdHeader) && callback(kB3SampledHeader);
193  }
194 };
195 
196 } // namespace propagation
197 } // namespace trace
trace::propagation::kB3SpanIdHeader
static const nostd::string_view kB3SpanIdHeader
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:24
trace::TraceId
Definition: includes/opentelemetry/trace/trace_id.h:19
trace::SetSpan
context::Context SetSpan(context::Context &context, const nostd::shared_ptr< Span > &span) noexcept
Definition: includes/opentelemetry/trace/context.h:37
nostd::function_ref
Definition: includes/opentelemetry/nostd/function_ref.h:17
trace
Definition: includes/opentelemetry/context/context_value.h:18
trace::SpanContext::trace_id
const trace::TraceId & trace_id() const noexcept
Definition: includes/opentelemetry/trace/span_context.h:63
trace::SpanContext
Definition: includes/opentelemetry/trace/span_context.h:24
trace::propagation::B3PropagatorExtractor
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:45
trace::TraceId::ToLowerBase16
void ToLowerBase16(nostd::span< char, 2 *kSize > buffer) const noexcept
Definition: includes/opentelemetry/trace/trace_id.h:35
trace::propagation::B3PropagatorMultiHeader::Fields
bool Fields(nostd::function_ref< bool(nostd::string_view)> callback) const noexcept override
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:190
trace::DefaultSpan
DefaultSpan provides a non-operational Span that propagates the tracer context by wrapping it inside ...
Definition: includes/opentelemetry/trace/default_span.h:21
trace::propagation::B3PropagatorExtractor::TraceFlagsFromHex
static TraceFlags TraceFlagsFromHex(nostd::string_view trace_flags)
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:78
nostd::shared_ptr
Provide a type-erased version of std::shared_ptr that has ABI stability.
Definition: includes/opentelemetry/nostd/shared_ptr.h:28
trace::propagation::kB3TraceIdHeader
static const nostd::string_view kB3TraceIdHeader
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:23
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
trace::propagation::kTraceIdHexStrLength
static const int kTraceIdHexStrLength
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:37
nostd::span
Back port of std::span.
Definition: includes/opentelemetry/nostd/span.h:51
trace::propagation::B3Propagator
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:135
trace::propagation::detail::SplitString
size_t SplitString(nostd::string_view s, char separator, nostd::string_view *results, size_t count)
Splits a string by separator, up to given buffer count words.
Definition: includes/opentelemetry/trace/propagation/detail/string.h:23
context
Definition: includes/opentelemetry/context/context.h:16
trace::propagation::B3PropagatorExtractor::Extract
context::Context Extract(const context::propagation::TextMapCarrier &carrier, context::Context &context) noexcept override
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:49
context::propagation::TextMapPropagator
Definition: includes/opentelemetry/context/propagation/text_map_propagator.h:42
trace::TraceFlags::IsSampled
bool IsSampled() const noexcept
Definition: includes/opentelemetry/trace/trace_flags.h:40
trace::propagation::kB3CombinedHeader
static const nostd::string_view kB3CombinedHeader
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:21
trace::propagation::kB3SampledHeader
static const nostd::string_view kB3SampledHeader
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:25
context::propagation::TextMapCarrier::Set
virtual void Set(nostd::string_view key, nostd::string_view value) noexcept=0
trace::GetSpan
nostd::shared_ptr< Span > GetSpan(const context::Context &context) noexcept
Definition: includes/opentelemetry/trace/context.h:16
trace::propagation::kSpanIdHexStrLength
static const int kSpanIdHexStrLength
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:38
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
trace::propagation::B3PropagatorMultiHeader::Inject
void Inject(context::propagation::TextMapCarrier &carrier, const context::Context &context) noexcept override
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:171
trace::propagation::B3PropagatorExtractor::TraceIdFromHex
static TraceId TraceIdFromHex(nostd::string_view trace_id)
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:64
trace::propagation::B3PropagatorMultiHeader
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:168
trace::SpanContext::trace_flags
const trace::TraceFlags & trace_flags() const noexcept
Definition: includes/opentelemetry/trace/span_context.h:60
trace::propagation::detail::IsValidHex
bool IsValidHex(nostd::string_view s)
Definition: includes/opentelemetry/trace/propagation/detail/hex.h:42
trace::TraceFlags::ToLowerBase16
void ToLowerBase16(nostd::span< char, 2 > buffer) const noexcept
Definition: includes/opentelemetry/trace/trace_flags.h:45
nostd::string_view::length
size_type length() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:60
trace::SpanId::ToLowerBase16
void ToLowerBase16(nostd::span< char, 2 *kSize > buffer) const noexcept
Definition: includes/opentelemetry/trace/span_id.h:29
trace::propagation::B3Propagator::Fields
bool Fields(nostd::function_ref< bool(nostd::string_view)> callback) const noexcept override
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:162
trace::SpanContext::IsValid
bool IsValid() const noexcept
Definition: includes/opentelemetry/trace/span_context.h:57
trace::SpanContext::span_id
const trace::SpanId & span_id() const noexcept
Definition: includes/opentelemetry/trace/span_context.h:66
context::Context
Definition: includes/opentelemetry/context/context.h:23
context::propagation::TextMapCarrier
Definition: includes/opentelemetry/context/propagation/text_map_propagator.h:18
context::propagation::TextMapCarrier::Get
virtual nostd::string_view Get(nostd::string_view key) const noexcept=0
trace::propagation::B3Propagator::Inject
void Inject(context::propagation::TextMapCarrier &carrier, const context::Context &context) noexcept override
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:139
trace::SpanId
Definition: includes/opentelemetry/trace/span_id.h:16
trace::propagation::detail::HexToBinary
bool HexToBinary(nostd::string_view hex, uint8_t *buffer, size_t buffer_size)
Converts a hexadecimal to binary format if the hex string will fit the buffer.
Definition: includes/opentelemetry/trace/propagation/detail/hex.h:51
trace::TraceFlags::kIsSampled
static constexpr uint8_t kIsSampled
Definition: includes/opentelemetry/trace/trace_flags.h:21
trace::propagation::B3PropagatorExtractor::SpanIdFromHex
static SpanId SpanIdFromHex(nostd::string_view span_id)
Definition: includes/opentelemetry/trace/propagation/b3_propagator.h:71
trace::TraceFlags
Definition: includes/opentelemetry/trace/trace_flags.h:18
trace::SpanContext::GetInvalid
static SpanContext GetInvalid() noexcept
Definition: includes/opentelemetry/trace/span_context.h:86
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37