Class: Datadog::OpenTracer::Tracer
- Inherits:
-
OpenTracing::Tracer
- Object
- OpenTracing::Tracer
- Datadog::OpenTracer::Tracer
- Defined in:
- lib/datadog/opentracer/tracer.rb
Overview
OpenTracing adapter for Datadog::Tracer
Instance Attribute Summary collapse
- #datadog_tracer ⇒ Datadog::Tracer readonly
Instance Method Summary collapse
-
#extract(format, carrier) ⇒ SpanContext?
Extract a SpanContext in the given format from the given carrier.
-
#initialize(**options) ⇒ Tracer
constructor
A new instance of Tracer.
-
#inject(span_context, format, carrier) ⇒ Object
Inject a SpanContext into the given carrier.
-
#scope_manager ⇒ ScopeManager
The current ScopeManager.
-
#start_active_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: nil, ignore_active_scope: false, finish_on_close: true) {|Scope| ... } ⇒ Scope
Returns a newly started and activated Scope.
-
#start_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: nil, ignore_active_scope: false) ⇒ Span
Like #start_active_span, but the returned Span has not been registered via the ScopeManager.
Constructor Details
Instance Attribute Details
#datadog_tracer ⇒ Datadog::Tracer (readonly)
13 14 15 |
# File 'lib/datadog/opentracer/tracer.rb', line 13 def datadog_tracer @datadog_tracer end |
Instance Method Details
#extract(format, carrier) ⇒ SpanContext?
Extract a SpanContext in the given format from the given carrier.
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/datadog/opentracer/tracer.rb', line 184 def extract(format, carrier) case format when OpenTracing::FORMAT_TEXT_MAP TextMapPropagator.extract(carrier) when OpenTracing::FORMAT_BINARY BinaryPropagator.extract(carrier) when OpenTracing::FORMAT_RACK RackPropagator.extract(carrier) else warn 'Unknown extract format' nil end end |
#inject(span_context, format, carrier) ⇒ Object
Inject a SpanContext into the given carrier.
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/datadog/opentracer/tracer.rb', line 166 def inject(span_context, format, carrier) case format when OpenTracing::FORMAT_TEXT_MAP TextMapPropagator.inject(span_context, carrier) when OpenTracing::FORMAT_BINARY BinaryPropagator.inject(span_context, carrier) when OpenTracing::FORMAT_RACK RackPropagator.inject(span_context, carrier) else warn 'Unknown inject format' end end |
#scope_manager ⇒ ScopeManager
Returns the current ScopeManager.
23 24 25 |
# File 'lib/datadog/opentracer/tracer.rb', line 23 def scope_manager @scope_manager ||= ThreadLocalScopeManager.new end |
#start_active_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: nil, ignore_active_scope: false, finish_on_close: true) {|Scope| ... } ⇒ Scope
Returns a newly started and activated Scope.
If ‘scope_manager.active` is not nil, no explicit references are provided, and `ignore_active_scope` is false, then an inferred OpenTracing::Reference#CHILD_OF reference is created to the `scope_manager.active`’s SpanContext when #start_active_span is invoked.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/datadog/opentracer/tracer.rb', line 53 def start_active_span( operation_name, child_of: nil, references: nil, start_time: Time.now, tags: nil, ignore_active_scope: false, finish_on_close: true ) # When meant to automatically determine the parent, # Use the active scope first, otherwise fall back to any # context generated by Datadog, so as to append to it and gain # the benefit of any out-of-the-box tracing from Datadog preceding # the OpenTracer::Tracer. # # We do this here instead of in #start_span because #start_span generates # spans that are not assigned to a scope, a.k.a not supposed to be used by # subsequent spans implicitly. By using the existing Datadog context, the span # effectively ends up "assigned to a scope", by virtue of being added to the # Context. Hence, it would behave more like an active span, which is why it # should only be here. unless child_of || ignore_active_scope child_of = if scope_manager.active scope_manager.active.span.context else SpanContextFactory.build(datadog_context: datadog_tracer.send(:call_context)) end end # Create the span, and auto-add it to the Datadog context. span = start_span( operation_name, child_of: child_of, references: references, start_time: start_time, tags: , ignore_active_scope: ignore_active_scope ) # Overwrite the tracer context with the OpenTracing managed context. # This is mostly for the benefit of any out-of-the-box tracing from Datadog, # such that spans generated by that tracing will be attached to the OpenTracer # parent span. datadog_tracer.provider.context = span.context.datadog_context scope_manager.activate(span, finish_on_close: finish_on_close).tap do |scope| if block_given? begin yield(scope) ensure scope.close end end end end |
#start_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: nil, ignore_active_scope: false) ⇒ Span
Like #start_active_span, but the returned Span has not been registered via the ScopeManager.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/datadog/opentracer/tracer.rb', line 127 def start_span( operation_name, child_of: nil, references: nil, start_time: Time.now, tags: nil, ignore_active_scope: false ) # Derive the OpenTracer::SpanContext to inherit from. parent_span_context = inherited_span_context(child_of, ignore_active_scope: ignore_active_scope) # Retrieve Datadog::Context from parent SpanContext. datadog_context = parent_span_context.nil? ? Datadog::Tracing::Context.new : parent_span_context.datadog_context datadog_trace_digest = parent_span_context && parent_span_context.datadog_trace_digest # Build the new Datadog span datadog_span = datadog_tracer.trace( operation_name, continue_from: datadog_trace_digest, _context: datadog_context, start_time: start_time, tags: || {} ) span_context = SpanContextFactory.build( datadog_context: datadog_context || datadog_tracer.send(:call_context), baggage: parent_span_context ? parent_span_context.baggage.dup : {} ) # Wrap the Datadog span and OpenTracer::Span context in a OpenTracer::Span Span.new(datadog_span: datadog_span, span_context: span_context) end |