Class: Dontbugme::Trace

Inherits:
Object
  • Object
show all
Defined in:
lib/dontbugme/trace.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, identifier:, metadata: {}) ⇒ Trace

Returns a new instance of Trace.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dontbugme/trace.rb', line 10

def initialize(kind:, identifier:, metadata: {})
  @id = "tr_#{SecureRandom.hex(6)}"
  @kind = kind.to_sym
  @identifier = identifier.to_s
  @metadata = 
  @correlation_id = [:correlation_id] || ['correlation_id']
  @spans = []
  @started_at_utc = Time.now.utc
  @started_at_monotonic = now_monotonic_ms
  @finished_at = nil
  @status = :success
  @error = nil
  @truncated_spans_count = 0
end

Instance Attribute Details

#correlation_idObject (readonly)

Returns the value of attribute correlation_id.



7
8
9
# File 'lib/dontbugme/trace.rb', line 7

def correlation_id
  @correlation_id
end

#errorObject

Returns the value of attribute error.



8
9
10
# File 'lib/dontbugme/trace.rb', line 8

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/dontbugme/trace.rb', line 7

def id
  @id
end

#identifierObject (readonly)

Returns the value of attribute identifier.



7
8
9
# File 'lib/dontbugme/trace.rb', line 7

def identifier
  @identifier
end

#kindObject (readonly)

Returns the value of attribute kind.



7
8
9
# File 'lib/dontbugme/trace.rb', line 7

def kind
  @kind
end

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/dontbugme/trace.rb', line 7

def 
  @metadata
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/dontbugme/trace.rb', line 8

def status
  @status
end

#truncated_spans_countObject

Returns the value of attribute truncated_spans_count.



8
9
10
# File 'lib/dontbugme/trace.rb', line 8

def truncated_spans_count
  @truncated_spans_count
end

Class Method Details

.from_h(hash) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dontbugme/trace.rb', line 94

def self.from_h(hash)
  trace = allocate
  trace.instance_variable_set(:@id, hash[:id] || hash['id'])
  trace.instance_variable_set(:@kind, (hash[:kind] || hash['kind']).to_sym)
  trace.instance_variable_set(:@identifier, hash[:identifier] || hash['identifier'])
  trace.instance_variable_set(:@metadata, (hash[:metadata] || hash['metadata'] || {}).transform_keys(&:to_sym))
  trace.instance_variable_set(:@correlation_id, hash[:correlation_id] || hash['correlation_id'])
  trace.instance_variable_set(:@status, (hash[:status] || hash['status'] || :success).to_sym)
  trace.instance_variable_set(:@error, hash[:error] || hash['error'])
  trace.instance_variable_set(:@truncated_spans_count, hash[:truncated_spans_count] || hash['truncated_spans_count'] || 0)

  spans_data = hash[:spans] || hash['spans'] || []
  trace.instance_variable_set(:@spans, spans_data.map { |s| Span.from_h(s) })

  started = hash[:started_at] || hash['started_at']
  trace.instance_variable_set(:@started_at_utc, started ? Time.parse(started.to_s) : nil)
  trace.instance_variable_set(:@started_at_monotonic, 0)
  trace.instance_variable_set(:@finished_at, nil)
  trace.instance_variable_set(:@duration_ms_stored, hash[:duration_ms] || hash['duration_ms'])
  trace
end

Instance Method Details

#add_span(span) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dontbugme/trace.rb', line 29

def add_span(span)
  config = Dontbugme.config
  max_spans = config.max_spans_per_trace

  if @spans.size >= max_spans
    @truncated_spans_count += 1
    return
  end

  @spans << span
end

#duration_msObject



69
70
71
72
73
74
# File 'lib/dontbugme/trace.rb', line 69

def duration_ms
  return @duration_ms_stored if defined?(@duration_ms_stored) && @duration_ms_stored
  return nil unless @finished_at

  (@finished_at - @started_at_monotonic).round(2)
end

#finish!(error: nil) ⇒ Object



49
50
51
52
53
# File 'lib/dontbugme/trace.rb', line 49

def finish!(error: nil)
  @finished_at = now_monotonic_ms
  @status = error ? :error : :success
  @error = error ? format_error(error) : nil
end

#merge_tags!(**tags) ⇒ Object



25
26
27
# File 'lib/dontbugme/trace.rb', line 25

def merge_tags!(**tags)
  @metadata.merge!(tags.transform_keys(&:to_sym))
end

#raw_spansObject



45
46
47
# File 'lib/dontbugme/trace.rb', line 45

def raw_spans
  @spans.dup
end

#spansObject



41
42
43
# File 'lib/dontbugme/trace.rb', line 41

def spans
  @span_collection ||= SpanCollection.new(@spans)
end

#spans_by_category(cat) ⇒ Object

Convenience for trace.spans by category



117
118
119
# File 'lib/dontbugme/trace.rb', line 117

def spans_by_category(cat)
  spans.select { |s| s.category == cat.to_sym }
end

#started_at_monotonicObject

Monotonic start time, used for computing span offsets during recording



60
61
62
# File 'lib/dontbugme/trace.rb', line 60

def started_at_monotonic
  @started_at_monotonic
end

#started_at_timeObject

Wall-clock start time for computing span offsets from ActiveSupport::Notifications



65
66
67
# File 'lib/dontbugme/trace.rb', line 65

def started_at_time
  @started_at_utc
end

#started_at_utcObject



55
56
57
# File 'lib/dontbugme/trace.rb', line 55

def started_at_utc
  @started_at_utc
end

#to_hObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dontbugme/trace.rb', line 76

def to_h
  finished_at_time = @finished_at ? (@started_at_utc + (duration_ms || 0) / 1000.0) : nil
  {
    id: id,
    kind: kind,
    identifier: identifier,
    started_at: format_time(started_at_utc),
    finished_at: finished_at_time ? format_time(finished_at_time) : nil,
    duration_ms: duration_ms,
    status: status,
    error: error,
    metadata: ,
    correlation_id: correlation_id,
    spans: raw_spans.map(&:to_h),
    truncated_spans_count: truncated_spans_count
  }
end

#to_timeline(only: nil, slow: nil) ⇒ Object



121
122
123
# File 'lib/dontbugme/trace.rb', line 121

def to_timeline(only: nil, slow: nil)
  Formatters::Timeline.format(self, only: only, slow: slow)
end