Class: Sentry::Span
- Inherits:
-
Object
- Object
- Sentry::Span
- Defined in:
- lib/sentry/span.rb
Direct Known Subclasses
Defined Under Namespace
Modules: DataConventions
Constant Summary collapse
- STATUS_MAP =
{ 400 => "invalid_argument", 401 => "unauthenticated", 403 => "permission_denied", 404 => "not_found", 409 => "already_exists", 429 => "resource_exhausted", 499 => "cancelled", 500 => "internal_error", 501 => "unimplemented", 503 => "unavailable", 504 => "deadline_exceeded" }
- DEFAULT_SPAN_ORIGIN =
"manual"
Instance Attribute Summary collapse
-
#data ⇒ Hash
readonly
Span data.
-
#description ⇒ String
readonly
Span description.
-
#op ⇒ String
readonly
Span operation.
-
#origin ⇒ String
readonly
Span origin that tracks what kind of instrumentation created a span.
-
#parent_span_id ⇒ String
readonly
Span parent’s span_id.
-
#sampled ⇒ Boolean?
readonly
Sampling result of the span.
-
#span_id ⇒ String
readonly
An uuid that can be used to identify the span.
-
#span_recorder ⇒ SpanRecorder
The SpanRecorder the current span belongs to.
-
#start_timestamp ⇒ Float
readonly
Starting timestamp of the span.
-
#status ⇒ String
readonly
Span status.
-
#tags ⇒ Hash
readonly
Span tags.
-
#timestamp ⇒ Float
readonly
Finishing timestamp of the span.
-
#trace_id ⇒ String
readonly
An uuid that can be used to identify a trace.
-
#transaction ⇒ Transaction
readonly
The Transaction object the Span belongs to.
Instance Method Summary collapse
- #deep_dup ⇒ Object
-
#finish(end_timestamp: nil) ⇒ self
Finishes the span by adding a timestamp.
-
#get_dynamic_sampling_context ⇒ Hash?
Returns the Dynamic Sampling Context from the transaction baggage.
-
#get_trace_context ⇒ Hash
Returns the span’s context that can be used to embed in an Event.
-
#initialize(transaction:, description: nil, op: nil, status: nil, trace_id: nil, span_id: nil, parent_span_id: nil, sampled: nil, start_timestamp: nil, timestamp: nil, origin: nil) ⇒ Span
constructor
A new instance of Span.
-
#metrics_local_aggregator ⇒ Object
Collects gauge metrics on the span for metric summaries.
- #metrics_summary ⇒ Object
-
#set_data(key, value) ⇒ Object
Inserts a key-value pair to the span’s data payload.
-
#set_description(description) ⇒ Object
Sets the span’s description.
-
#set_http_status(status_code) ⇒ Object
Sets the span’s status with given http status code.
-
#set_op(op) ⇒ Object
Sets the span’s operation.
-
#set_origin(origin) ⇒ Object
Sets the origin of the span.
-
#set_status(status) ⇒ Object
Sets the span’s status.
-
#set_tag(key, value) ⇒ Object
Sets a tag to the span.
-
#set_timestamp(timestamp) ⇒ Object
Sets the span’s finish timestamp.
-
#start_child(**attributes) ⇒ Object
Starts a child span with given attributes.
-
#to_baggage ⇒ String?
Generates a W3C Baggage header string for distributed tracing from the incoming baggage stored on the transaction.
- #to_hash ⇒ Hash
-
#to_sentry_trace ⇒ String
Generates a trace string that can be used to connect other transactions.
-
#with_child_span(**attributes, &block) {|child_span| ... } ⇒ Object
Starts a child span, yield it to the given block, and then finish the span after the block is executed.
Constructor Details
#initialize(transaction:, description: nil, op: nil, status: nil, trace_id: nil, span_id: nil, parent_span_id: nil, sampled: nil, start_timestamp: nil, timestamp: nil, origin: nil) ⇒ Span
Returns a new instance of Span.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/sentry/span.rb', line 112 def initialize( transaction:, description: nil, op: nil, status: nil, trace_id: nil, span_id: nil, parent_span_id: nil, sampled: nil, start_timestamp: nil, timestamp: nil, origin: nil ) @trace_id = trace_id || SecureRandom.uuid.delete("-") @span_id = span_id || SecureRandom.uuid.delete("-").slice(0, 16) @parent_span_id = parent_span_id @sampled = sampled @start_timestamp = || Sentry.utc_now.to_f @timestamp = @description = description @transaction = transaction @op = op @status = status @data = {} @tags = {} @origin = origin || DEFAULT_SPAN_ORIGIN end |
Instance Attribute Details
#data ⇒ Hash (readonly)
Span data
97 98 99 |
# File 'lib/sentry/span.rb', line 97 def data @data end |
#description ⇒ String (readonly)
Span description
85 86 87 |
# File 'lib/sentry/span.rb', line 85 def description @description end |
#op ⇒ String (readonly)
Span operation
88 89 90 |
# File 'lib/sentry/span.rb', line 88 def op @op end |
#origin ⇒ String (readonly)
Span origin that tracks what kind of instrumentation created a span
100 101 102 |
# File 'lib/sentry/span.rb', line 100 def origin @origin end |
#parent_span_id ⇒ String (readonly)
Span parent’s span_id.
73 74 75 |
# File 'lib/sentry/span.rb', line 73 def parent_span_id @parent_span_id end |
#sampled ⇒ Boolean? (readonly)
Sampling result of the span.
76 77 78 |
# File 'lib/sentry/span.rb', line 76 def sampled @sampled end |
#span_id ⇒ String (readonly)
An uuid that can be used to identify the span.
70 71 72 |
# File 'lib/sentry/span.rb', line 70 def span_id @span_id end |
#span_recorder ⇒ SpanRecorder
The SpanRecorder the current span belongs to. SpanRecorder holds all spans under the same Transaction object (including the Transaction itself).
105 106 107 |
# File 'lib/sentry/span.rb', line 105 def span_recorder @span_recorder end |
#start_timestamp ⇒ Float (readonly)
Starting timestamp of the span.
79 80 81 |
# File 'lib/sentry/span.rb', line 79 def @start_timestamp end |
#status ⇒ String (readonly)
Span status
91 92 93 |
# File 'lib/sentry/span.rb', line 91 def status @status end |
#tags ⇒ Hash (readonly)
Span tags
94 95 96 |
# File 'lib/sentry/span.rb', line 94 def @tags end |
#timestamp ⇒ Float (readonly)
Finishing timestamp of the span.
82 83 84 |
# File 'lib/sentry/span.rb', line 82 def @timestamp end |
#trace_id ⇒ String (readonly)
An uuid that can be used to identify a trace.
67 68 69 |
# File 'lib/sentry/span.rb', line 67 def trace_id @trace_id end |
#transaction ⇒ Transaction (readonly)
The Transaction object the Span belongs to. Every span needs to be attached to a Transaction and their child spans will also inherit the same transaction.
110 111 112 |
# File 'lib/sentry/span.rb', line 110 def transaction @transaction end |
Instance Method Details
#deep_dup ⇒ Object
241 242 243 |
# File 'lib/sentry/span.rb', line 241 def deep_dup dup end |
#finish(end_timestamp: nil) ⇒ self
Finishes the span by adding a timestamp.
142 143 144 145 |
# File 'lib/sentry/span.rb', line 142 def finish(end_timestamp: nil) @timestamp = || @timestamp || Sentry.utc_now.to_f self end |
#get_dynamic_sampling_context ⇒ Hash?
Returns the Dynamic Sampling Context from the transaction baggage.
165 166 167 |
# File 'lib/sentry/span.rb', line 165 def get_dynamic_sampling_context transaction.get_baggage&.dynamic_sampling_context end |
#get_trace_context ⇒ Hash
Returns the span’s context that can be used to embed in an Event.
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/sentry/span.rb', line 193 def get_trace_context { trace_id: @trace_id, span_id: @span_id, parent_span_id: @parent_span_id, description: @description, op: @op, status: @status, origin: @origin, data: @data } end |
#metrics_local_aggregator ⇒ Object
Collects gauge metrics on the span for metric summaries.
306 307 308 |
# File 'lib/sentry/span.rb', line 306 def metrics_local_aggregator @metrics_local_aggregator ||= Sentry::Metrics::LocalAggregator.new end |
#metrics_summary ⇒ Object
310 311 312 |
# File 'lib/sentry/span.rb', line 310 def metrics_summary @metrics_local_aggregator&.to_hash end |
#set_data(key, value) ⇒ Object
Inserts a key-value pair to the span’s data payload.
288 289 290 |
# File 'lib/sentry/span.rb', line 288 def set_data(key, value) @data[key] = value end |
#set_description(description) ⇒ Object
Sets the span’s description.
253 254 255 |
# File 'lib/sentry/span.rb', line 253 def set_description(description) @description = description end |
#set_http_status(status_code) ⇒ Object
Sets the span’s status with given http status code.
272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/sentry/span.rb', line 272 def set_http_status(status_code) status_code = status_code.to_i set_data(DataConventions::HTTP_STATUS_CODE, status_code) status = if status_code >= 200 && status_code < 299 "ok" else STATUS_MAP[status_code] end set_status(status) end |
#set_op(op) ⇒ Object
Sets the span’s operation.
247 248 249 |
# File 'lib/sentry/span.rb', line 247 def set_op(op) @op = op end |
#set_origin(origin) ⇒ Object
Sets the origin of the span.
301 302 303 |
# File 'lib/sentry/span.rb', line 301 def set_origin(origin) @origin = origin end |
#set_status(status) ⇒ Object
Sets the span’s status.
260 261 262 |
# File 'lib/sentry/span.rb', line 260 def set_status(status) @status = status end |
#set_tag(key, value) ⇒ Object
Sets a tag to the span.
295 296 297 |
# File 'lib/sentry/span.rb', line 295 def set_tag(key, value) @tags[key] = value end |
#set_timestamp(timestamp) ⇒ Object
Sets the span’s finish timestamp.
266 267 268 |
# File 'lib/sentry/span.rb', line 266 def () @timestamp = end |
#start_child(**attributes) ⇒ Object
Starts a child span with given attributes.
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/sentry/span.rb', line 208 def start_child(**attributes) attributes = attributes.dup.merge(transaction: @transaction, trace_id: @trace_id, parent_span_id: @span_id, sampled: @sampled) new_span = Span.new(**attributes) new_span.span_recorder = span_recorder if span_recorder span_recorder.add(new_span) end new_span end |
#to_baggage ⇒ String?
Generates a W3C Baggage header string for distributed tracing from the incoming baggage stored on the transaction.
159 160 161 |
# File 'lib/sentry/span.rb', line 159 def to_baggage transaction.get_baggage&.serialize end |
#to_hash ⇒ Hash
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/sentry/span.rb', line 170 def to_hash hash = { trace_id: @trace_id, span_id: @span_id, parent_span_id: @parent_span_id, start_timestamp: @start_timestamp, timestamp: @timestamp, description: @description, op: @op, status: @status, tags: @tags, data: @data, origin: @origin } summary = metrics_summary hash[:_metrics_summary] = summary if summary hash end |
#to_sentry_trace ⇒ String
Generates a trace string that can be used to connect other transactions.
149 150 151 152 153 154 |
# File 'lib/sentry/span.rb', line 149 def to_sentry_trace sampled_flag = "" sampled_flag = @sampled ? 1 : 0 unless @sampled.nil? "#{@trace_id}-#{@span_id}-#{sampled_flag}" end |
#with_child_span(**attributes, &block) {|child_span| ... } ⇒ Object
Starts a child span, yield it to the given block, and then finish the span after the block is executed.
229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/sentry/span.rb', line 229 def with_child_span(**attributes, &block) child_span = start_child(**attributes) yield(child_span) child_span.finish rescue child_span.set_http_status(500) child_span.finish raise end |