Class: Sentry::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry/span.rb

Direct Known Subclasses

Transaction

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

Instance Method Summary collapse

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.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/sentry/span.rb', line 120

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 || Utils.uuid
  @span_id = span_id || Utils.uuid.slice(0, 16)
  @parent_span_id = parent_span_id
  @sampled = sampled
  @start_timestamp = start_timestamp || Sentry.utc_now.to_f
  @timestamp = timestamp
  @description = description
  @transaction = transaction
  @op = op
  @status = status
  @data = {}
  @tags = {}
  @origin = origin || DEFAULT_SPAN_ORIGIN
end

Instance Attribute Details

#dataHash (readonly)

Span data

Returns:

  • (Hash)


105
106
107
# File 'lib/sentry/span.rb', line 105

def data
  @data
end

#descriptionString (readonly)

Span description

Returns:

  • (String)


93
94
95
# File 'lib/sentry/span.rb', line 93

def description
  @description
end

#opString (readonly)

Span operation

Returns:

  • (String)


96
97
98
# File 'lib/sentry/span.rb', line 96

def op
  @op
end

#originString (readonly)

Span origin that tracks what kind of instrumentation created a span

Returns:

  • (String)


108
109
110
# File 'lib/sentry/span.rb', line 108

def origin
  @origin
end

#parent_span_idString (readonly)

Span parent’s span_id.

Returns:

  • (String)


81
82
83
# File 'lib/sentry/span.rb', line 81

def parent_span_id
  @parent_span_id
end

#sampledBoolean? (readonly)

Sampling result of the span.

Returns:

  • (Boolean, nil)


84
85
86
# File 'lib/sentry/span.rb', line 84

def sampled
  @sampled
end

#span_idString (readonly)

An uuid that can be used to identify the span.

Returns:

  • (String)


78
79
80
# File 'lib/sentry/span.rb', line 78

def span_id
  @span_id
end

#span_recorderSpanRecorder

The SpanRecorder the current span belongs to. SpanRecorder holds all spans under the same Transaction object (including the Transaction itself).

Returns:

  • (SpanRecorder)


113
114
115
# File 'lib/sentry/span.rb', line 113

def span_recorder
  @span_recorder
end

#start_timestampFloat (readonly)

Starting timestamp of the span.

Returns:

  • (Float)


87
88
89
# File 'lib/sentry/span.rb', line 87

def start_timestamp
  @start_timestamp
end

#statusString (readonly)

Span status

Returns:

  • (String)


99
100
101
# File 'lib/sentry/span.rb', line 99

def status
  @status
end

#tagsHash (readonly)

Span tags

Returns:

  • (Hash)


102
103
104
# File 'lib/sentry/span.rb', line 102

def tags
  @tags
end

#timestampFloat (readonly)

Finishing timestamp of the span.

Returns:

  • (Float)


90
91
92
# File 'lib/sentry/span.rb', line 90

def timestamp
  @timestamp
end

#trace_idString (readonly)

An uuid that can be used to identify a trace.

Returns:

  • (String)


75
76
77
# File 'lib/sentry/span.rb', line 75

def trace_id
  @trace_id
end

#transactionTransaction (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.

Returns:



118
119
120
# File 'lib/sentry/span.rb', line 118

def transaction
  @transaction
end

Instance Method Details

#deep_dupObject



244
245
246
# File 'lib/sentry/span.rb', line 244

def deep_dup
  dup
end

#finish(end_timestamp: nil) ⇒ self

Finishes the span by adding a timestamp.

Returns:

  • (self)


150
151
152
153
# File 'lib/sentry/span.rb', line 150

def finish(end_timestamp: nil)
  @timestamp = end_timestamp || @timestamp || Sentry.utc_now.to_f
  self
end

#get_dynamic_sampling_contextHash?

Returns the Dynamic Sampling Context from the transaction baggage.

Returns:

  • (Hash, nil)


173
174
175
# File 'lib/sentry/span.rb', line 173

def get_dynamic_sampling_context
  transaction.get_baggage&.dynamic_sampling_context
end

#get_trace_contextHash

Returns the span’s context that can be used to embed in an Event.

Returns:

  • (Hash)


196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sentry/span.rb', line 196

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

#set_data(key, value) ⇒ Object

Inserts a key-value pair to the span’s data payload.

Parameters:

  • key (String, Symbol)
  • value (Object)


291
292
293
# File 'lib/sentry/span.rb', line 291

def set_data(key, value)
  @data[key] = value
end

#set_description(description) ⇒ Object

Sets the span’s description.

Parameters:

  • description (String)

    description of the span.



256
257
258
# File 'lib/sentry/span.rb', line 256

def set_description(description)
  @description = description
end

#set_http_status(status_code) ⇒ Object

Sets the span’s status with given http status code.

Parameters:

  • status_code (String)

    example: “500”.



275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/sentry/span.rb', line 275

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.

Parameters:

  • op (String)

    operation of the span.



250
251
252
# File 'lib/sentry/span.rb', line 250

def set_op(op)
  @op = op
end

#set_origin(origin) ⇒ Object

Sets the origin of the span.

Parameters:

  • origin (String)


304
305
306
# File 'lib/sentry/span.rb', line 304

def set_origin(origin)
  @origin = origin
end

#set_status(status) ⇒ Object

Sets the span’s status.

Parameters:

  • status (String)

    status of the span.



263
264
265
# File 'lib/sentry/span.rb', line 263

def set_status(status)
  @status = status
end

#set_tag(key, value) ⇒ Object

Sets a tag to the span.

Parameters:

  • key (String, Symbol)
  • value (String)


298
299
300
# File 'lib/sentry/span.rb', line 298

def set_tag(key, value)
  @tags[key] = value
end

#set_timestamp(timestamp) ⇒ Object

Sets the span’s finish timestamp.

Parameters:

  • timestamp (Float)

    finished time in float format (most precise).



269
270
271
# File 'lib/sentry/span.rb', line 269

def set_timestamp(timestamp)
  @timestamp = timestamp
end

#start_child(**attributes) ⇒ Object

Starts a child span with given attributes.

Parameters:

  • attributes (Hash)

    the attributes for the child span.



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/sentry/span.rb', line 211

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_baggageString?

Generates a W3C Baggage header string for distributed tracing from the incoming baggage stored on the transaction.

Returns:

  • (String, nil)


167
168
169
# File 'lib/sentry/span.rb', line 167

def to_baggage
  transaction.get_baggage&.serialize
end

#to_hHash

Returns:

  • (Hash)


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/sentry/span.rb', line 178

def to_h
  {
    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
  }
end

#to_sentry_traceString

Generates a trace string that can be used to connect other transactions.

Returns:

  • (String)


157
158
159
160
161
162
# File 'lib/sentry/span.rb', line 157

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.

Examples:

span.with_child_span do |child_span|
  # things happen here will be recorded in a child span
end

Parameters:

  • attributes (Hash)

    the attributes for the child span.

  • block (Proc)

    the action to be recorded in the child span.

Yield Parameters:



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/sentry/span.rb', line 232

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