Class: Sentry::Span

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

Direct Known Subclasses

Transaction

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"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description: nil, op: nil, status: nil, trace_id: nil, parent_span_id: nil, sampled: nil, start_timestamp: nil, timestamp: nil) ⇒ Span

Returns a new instance of Span.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sentry/span.rb', line 24

def initialize(description: nil, op: nil, status: nil, trace_id: nil, parent_span_id: nil, sampled: nil, start_timestamp: nil, timestamp: nil)
  @trace_id = trace_id || SecureRandom.uuid.delete("-")
  @span_id = SecureRandom.hex(8)
  @parent_span_id = parent_span_id
  @sampled = sampled
  @start_timestamp = start_timestamp || Sentry.utc_now.to_f
  @timestamp = timestamp
  @description = description
  @op = op
  @status = status
  @data = {}
  @tags = {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/sentry/span.rb', line 21

def data
  @data
end

#descriptionObject (readonly)

Returns the value of attribute description.



21
22
23
# File 'lib/sentry/span.rb', line 21

def description
  @description
end

#opObject (readonly)

Returns the value of attribute op.



21
22
23
# File 'lib/sentry/span.rb', line 21

def op
  @op
end

#parent_span_idObject (readonly)

Returns the value of attribute parent_span_id.



21
22
23
# File 'lib/sentry/span.rb', line 21

def parent_span_id
  @parent_span_id
end

#sampledObject (readonly)

Returns the value of attribute sampled.



21
22
23
# File 'lib/sentry/span.rb', line 21

def sampled
  @sampled
end

#span_idObject (readonly)

Returns the value of attribute span_id.



21
22
23
# File 'lib/sentry/span.rb', line 21

def span_id
  @span_id
end

#span_recorderObject

Returns the value of attribute span_recorder.



22
23
24
# File 'lib/sentry/span.rb', line 22

def span_recorder
  @span_recorder
end

#start_timestampObject (readonly)

Returns the value of attribute start_timestamp.



21
22
23
# File 'lib/sentry/span.rb', line 21

def start_timestamp
  @start_timestamp
end

#statusObject (readonly)

Returns the value of attribute status.



21
22
23
# File 'lib/sentry/span.rb', line 21

def status
  @status
end

#tagsObject (readonly)

Returns the value of attribute tags.



21
22
23
# File 'lib/sentry/span.rb', line 21

def tags
  @tags
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



21
22
23
# File 'lib/sentry/span.rb', line 21

def timestamp
  @timestamp
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



21
22
23
# File 'lib/sentry/span.rb', line 21

def trace_id
  @trace_id
end

Instance Method Details

#deep_dupObject



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

def deep_dup
  dup
end

#finishObject



38
39
40
41
42
43
44
# File 'lib/sentry/span.rb', line 38

def finish
  # already finished
  return if @timestamp

  @timestamp = Sentry.utc_now.to_f
  self
end

#get_trace_contextObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/sentry/span.rb', line 68

def get_trace_context
  {
    trace_id: @trace_id,
    span_id: @span_id,
    parent_span_id: @parent_span_id,
    description: @description,
    op: @op,
    status: @status
  }
end

#set_data(key, value) ⇒ Object



125
126
127
# File 'lib/sentry/span.rb', line 125

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

#set_description(description) ⇒ Object



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

def set_description(description)
  @description = description
end

#set_http_status(status_code) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sentry/span.rb', line 112

def set_http_status(status_code)
  status_code = status_code.to_i
  set_data("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



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

def set_op(op)
  @op = op
end

#set_status(status) ⇒ Object



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

def set_status(status)
  @status = status
end

#set_tag(key, value) ⇒ Object



129
130
131
# File 'lib/sentry/span.rb', line 129

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

#set_timestamp(timestamp) ⇒ Object



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

def set_timestamp(timestamp)
  @timestamp = timestamp
end

#start_child(**options) ⇒ Object



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

def start_child(**options)
  options = options.dup.merge(trace_id: @trace_id, parent_span_id: @span_id, sampled: @sampled)
  Span.new(**options)
end

#to_hashObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sentry/span.rb', line 53

def to_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
  }
end

#to_sentry_traceObject



46
47
48
49
50
51
# File 'lib/sentry/span.rb', line 46

def to_sentry_trace
  sampled_flag = ""
  sampled_flag = @sampled ? 1 : 0 unless @sampled.nil?

  "#{@trace_id}-#{@span_id}-#{sampled_flag}"
end

#with_child_span(**options) {|child_span| ... } ⇒ Object

Yields:

  • (child_span)


84
85
86
87
88
89
90
# File 'lib/sentry/span.rb', line 84

def with_child_span(**options, &block)
  child_span = start_child(**options)

  yield(child_span)

  child_span.finish
end