Class: Datadog::Context
- Inherits:
-
Object
- Object
- Datadog::Context
- Defined in:
- lib/ddtrace/context.rb
Overview
Context is used to keep track of a hierarchy of spans for the current execution flow. During each logical execution, the same Context is used to represent a single logical trace, even if the trace is built asynchronously.
A single code execution may use multiple Context if part of the execution must not be related to the current tracing. As example, a delayed job may compose a standalone trace instead of being related to the same trace that generates the job itself. On the other hand, if it’s part of the same Context, it will be related to the original trace.
This data structure is thread-safe.
Instance Method Summary collapse
-
#add_span(span) ⇒ Object
Add a span to the context trace list, keeping it as the last active span.
-
#close_span(span) ⇒ Object
Mark a span as a finished, increasing the internal counter to prevent cycles inside _trace list.
-
#current_span ⇒ Object
Return the last active span that corresponds to the last inserted item in the trace list.
-
#finished? ⇒ Boolean
Returns if the trace for the current Context is finished or not.
-
#get ⇒ Object
Returns both the trace list generated in the current context and if the context is sampled or not.
-
#initialize ⇒ Context
constructor
Initialize a new thread-safe Context.
-
#sampled? ⇒ Boolean
Returns true if the context is sampled, that is, if it should be kept and sent to the trace agent.
-
#to_s ⇒ Object
Return a string representation of the context.
Constructor Details
#initialize ⇒ Context
Initialize a new thread-safe Context.
18 19 20 21 |
# File 'lib/ddtrace/context.rb', line 18 def initialize @mutex = Mutex.new reset end |
Instance Method Details
#add_span(span) ⇒ Object
Add a span to the context trace list, keeping it as the last active span.
41 42 43 44 45 46 47 48 |
# File 'lib/ddtrace/context.rb', line 41 def add_span(span) @mutex.synchronize do @current_span = span @sampled = span.sampled @trace << span span.context = self end end |
#close_span(span) ⇒ Object
Mark a span as a finished, increasing the internal counter to prevent cycles inside _trace list.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ddtrace/context.rb', line 52 def close_span(span) @mutex.synchronize do @finished_spans += 1 # Current span is only meaningful for linear tree-like traces, # in other cases, this is just broken and one should rely # on per-instrumentation code to retrieve handle parent/child relations. @current_span = span.parent return if span.tracer.nil? return unless Datadog::Tracer.debug_logging if span.parent.nil? && !check_finished_spans opened_spans = @trace.length - @finished_spans Datadog::Tracer.log.debug("root span #{span.name} closed but has #{opened_spans} unfinished spans:") @trace.each do |s| Datadog::Tracer.log.debug("unfinished span: #{s}") unless s.finished? end end end end |
#current_span ⇒ Object
Return the last active span that corresponds to the last inserted item in the trace list. This cannot be considered as the current active span in asynchronous environments, because some spans can be closed earlier while child spans still need to finish their traced execution.
34 35 36 37 38 |
# File 'lib/ddtrace/context.rb', line 34 def current_span @mutex.synchronize do return @current_span end end |
#finished? ⇒ Boolean
Returns if the trace for the current Context is finished or not. A Context is considered finished if all spans in this context are finished.
79 80 81 82 83 |
# File 'lib/ddtrace/context.rb', line 79 def finished? @mutex.synchronize do return check_finished_spans end end |
#get ⇒ Object
Returns both the trace list generated in the current context and if the context is sampled or not. It returns nil, nil if the “Context“ is not finished. If a trace is returned, the Context will be reset so that it can be re-used immediately.
This operation is thread-safe.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ddtrace/context.rb', line 99 def get @mutex.synchronize do return nil, nil unless check_finished_spans trace = @trace sampled = @sampled reset return trace, sampled end end |
#sampled? ⇒ Boolean
Returns true if the context is sampled, that is, if it should be kept and sent to the trace agent.
87 88 89 90 91 |
# File 'lib/ddtrace/context.rb', line 87 def sampled? @mutex.synchronize do return @sampled end end |
#to_s ⇒ Object
Return a string representation of the context.
111 112 113 114 115 116 |
# File 'lib/ddtrace/context.rb', line 111 def to_s @mutex.synchronize do # rubocop:disable Metrics/LineLength "Context(trace.length:#{@trace.length},sampled:#{@sampled},finished_spans:#{@finished_spans},current_span:#{@current_span})" end end |