Class: OpenCensus::Trace::SpanContext
- Inherits:
-
Object
- Object
- OpenCensus::Trace::SpanContext
- Defined in:
- lib/opencensus/trace/span_context.rb
Overview
SpanContext represents the context within which a span may be created. It includes the ID of the parent trace, the ID of the parent span, and sampling state.
Defined Under Namespace
Classes: TraceData
Constant Summary collapse
- MAX_TRACE_ID =
Maximum integer value for a ‘trace_id`
0xffffffffffffffffffffffffffffffff
- MAX_SPAN_ID =
Maximum integer value for a ‘span_id`
0xffffffffffffffff
Instance Attribute Summary collapse
-
#parent ⇒ SpanContext?
readonly
The parent of this context, or ‘nil` if this is a root context.
-
#same_process_as_parent ⇒ boolean?
readonly
Whether the parent of spans created by this context is local, or ‘nil` if this context creates root spans or this information is unknown.
-
#span_id ⇒ String
readonly
The span ID as a 16-character hex string, or the empty string if the context refers to the root of the trace.
Class Method Summary collapse
-
.create_root(trace_context: nil, same_process_as_parent: nil) ⇒ SpanContext
Create a new root SpanContext object, given either a Trace-Context header value by itself, or an entire Rack environment.
Instance Method Summary collapse
-
#build_contained_spans(max_attributes: nil, max_stack_frames: nil, max_annotations: nil, max_message_events: nil, max_links: nil, max_string_length: nil) ⇒ Array<Span>
Builds spans under this context, and returns an array of built ‘Span` objects.
-
#contained_span_builders ⇒ Array<SpanBuilder>
Returns all SpanBuilder objects created by this context or any descendant context.
-
#contains?(context) ⇒ boolean
Returns true if this context equals or is an ancestor of the given context.
-
#end_span(span) ⇒ Object
Finish the given span, which must have been created by this span context.
-
#in_span(name, kind: nil, skip_frames: 0, sampler: nil) ⇒ Object
Create a new span in this context.
-
#initialize(trace_data, parent, span_id, same_process_as_parent) ⇒ SpanContext
constructor
Initialize a SpanContext object.
-
#root ⇒ SpanContext
The root context, which may be this context or one of its ancestors.
-
#root? ⇒ boolean
Returns true if this is a root span context.
-
#sampled? ⇒ boolean
Whether the context (e.g. the parent span) has been sampled.
-
#start_span(name, kind: nil, skip_frames: 0, sampler: nil) ⇒ SpanBuilder
Create a new span in this context.
-
#this_span ⇒ SpanBuilder?
Returns the span that defines this context; that is, the span that is the parent of spans created by this context.
-
#trace_context ⇒ TraceContextData
Returns the trace context for this span.
-
#trace_id ⇒ String
The trace ID, as a 32-character hex string.
-
#trace_options ⇒ Integer
The original trace options byte used to create this span context.
Constructor Details
#initialize(trace_data, parent, span_id, same_process_as_parent) ⇒ SpanContext
Initialize a SpanContext object. This low-level constructor is used internally only. Generally, you should create a SpanContext using the ‘SpanContext.create_root` method.
287 288 289 290 291 292 |
# File 'lib/opencensus/trace/span_context.rb', line 287 def initialize trace_data, parent, span_id, same_process_as_parent @trace_data = trace_data @parent = parent @span_id = span_id @same_process_as_parent = same_process_as_parent end |
Instance Attribute Details
#parent ⇒ SpanContext? (readonly)
The parent of this context, or ‘nil` if this is a root context.
80 81 82 |
# File 'lib/opencensus/trace/span_context.rb', line 80 def parent @parent end |
#same_process_as_parent ⇒ boolean? (readonly)
Whether the parent of spans created by this context is local, or ‘nil` if this context creates root spans or this information is unknown.
145 146 147 |
# File 'lib/opencensus/trace/span_context.rb', line 145 def same_process_as_parent @same_process_as_parent end |
#span_id ⇒ String (readonly)
The span ID as a 16-character hex string, or the empty string if the context refers to the root of the trace.
137 138 139 |
# File 'lib/opencensus/trace/span_context.rb', line 137 def span_id @span_id end |
Class Method Details
.create_root(trace_context: nil, same_process_as_parent: nil) ⇒ SpanContext
Create a new root SpanContext object, given either a Trace-Context header value by itself, or an entire Rack environment. If a valid Trace-Context header can be obtained from either source, it is used to generate the SpanContext. Otherwise, a new root context with a unique ‘trace_id` and a root `span_id` of “” is used.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/opencensus/trace/span_context.rb', line 60 def create_root trace_context: nil, same_process_as_parent: nil if trace_context trace_data = TraceData.new \ trace_context.trace_id, trace_context., {} new trace_data, nil, trace_context.span_id, same_process_as_parent else trace_id = rand 1..MAX_TRACE_ID trace_id = trace_id.to_s(16).rjust(32, "0") trace_data = TraceData.new trace_id, 0, {} new trace_data, nil, "", nil end end |
Instance Method Details
#build_contained_spans(max_attributes: nil, max_stack_frames: nil, max_annotations: nil, max_message_events: nil, max_links: nil, max_string_length: nil) ⇒ Array<Span>
Builds spans under this context, and returns an array of built ‘Span` objects. Builds only spans that are both finished and sampled, and ignores others. The order of the generated spans is undefined.
Does not build any ancestor spans. If you want the entire span tree built, call this method on the ‘#root` context.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/opencensus/trace/span_context.rb', line 261 def build_contained_spans max_attributes: nil, max_stack_frames: nil, max_annotations: nil, max_message_events: nil, max_links: nil, max_string_length: nil sampled_span_builders = contained_span_builders.find_all do |sb| sb.finished? && sb.sampled end sampled_span_builders.map do |sb| sb.to_span max_attributes: max_attributes, max_stack_frames: max_stack_frames, max_annotations: max_annotations, max_message_events: , max_links: max_links, max_string_length: max_string_length end end |
#contained_span_builders ⇒ Array<SpanBuilder>
Returns all SpanBuilder objects created by this context or any descendant context. The order of the returned spans is undefined.
316 317 318 319 320 321 322 323 |
# File 'lib/opencensus/trace/span_context.rb', line 316 def contained_span_builders builders = @trace_data.span_map.values if root? builders else builders.find_all { |sb| contains? sb.context.parent } end end |
#contains?(context) ⇒ boolean
Returns true if this context equals or is an ancestor of the given context.
301 302 303 304 305 306 307 |
# File 'lib/opencensus/trace/span_context.rb', line 301 def contains? context until context.nil? return true if context == self context = context.parent end false end |
#end_span(span) ⇒ Object
Finish the given span, which must have been created by this span context.
219 220 221 222 223 224 |
# File 'lib/opencensus/trace/span_context.rb', line 219 def end_span span unless span.context.parent == self raise "The given span was not created by this context" end span.finish! end |
#in_span(name, kind: nil, skip_frames: 0, sampler: nil) ⇒ Object
Create a new span in this context. You must pass a name for the span. All other span attributes should be set using the SpanBuilder methods.
The span will be started automatically with the current timestamp. The SpanBuilder will then be passed to the block you provide. The span will be finished automatically at the end of the block.
203 204 205 206 207 208 209 210 211 |
# File 'lib/opencensus/trace/span_context.rb', line 203 def in_span name, kind: nil, skip_frames: 0, sampler: nil span = start_span name, kind: kind, skip_frames: skip_frames + 1, sampler: sampler begin yield span ensure end_span span end end |
#root ⇒ SpanContext
The root context, which may be this context or one of its ancestors.
96 97 98 99 100 101 102 |
# File 'lib/opencensus/trace/span_context.rb', line 96 def root root = self until (parent = root.parent).nil? root = parent end root end |
#root? ⇒ boolean
Returns true if this is a root span context
87 88 89 |
# File 'lib/opencensus/trace/span_context.rb', line 87 def root? parent.nil? end |
#sampled? ⇒ boolean
Whether the context (e.g. the parent span) has been sampled. This information may be used in sampling decisions for new spans.
153 154 155 156 157 158 159 160 |
# File 'lib/opencensus/trace/span_context.rb', line 153 def sampled? span = this_span if span span.sampled else & 0x01 != 0 end end |
#start_span(name, kind: nil, skip_frames: 0, sampler: nil) ⇒ SpanBuilder
Create a new span in this context. You must pass a name for the span. All other span attributes should be set using the SpanBuilder methods. The span will be started automatically with the current timestamp. However, you are responsible for finishing the span yourself.
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/opencensus/trace/span_context.rb', line 177 def start_span name, kind: nil, skip_frames: 0, sampler: nil child_context = create_child sampler ||= OpenCensus::Trace.config.default_sampler sampled = sampler.call span_context: self span = SpanBuilder.new child_context, sampled, skip_frames: skip_frames + 1 span.name = name span.kind = kind if kind span.start! @trace_data.span_map[child_context.span_id] = span end |
#this_span ⇒ SpanBuilder?
Returns the span that defines this context; that is, the span that is the parent of spans created by this context. Returns ‘nil` if this context is the root and doesn’t correspond to an actual span, or if the corresponding span is remote.
234 235 236 |
# File 'lib/opencensus/trace/span_context.rb', line 234 def this_span get_span @span_id end |
#trace_context ⇒ TraceContextData
Returns the trace context for this span.
109 110 111 |
# File 'lib/opencensus/trace/span_context.rb', line 109 def trace_context TraceContextData.new trace_id, @span_id, end |
#trace_id ⇒ String
The trace ID, as a 32-character hex string.
118 119 120 |
# File 'lib/opencensus/trace/span_context.rb', line 118 def trace_id @trace_data.trace_id end |
#trace_options ⇒ Integer
The original trace options byte used to create this span context.
127 128 129 |
# File 'lib/opencensus/trace/span_context.rb', line 127 def @trace_data. end |