Class: MetrixWire::Trace

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

Overview

One request / unit of work. Holds its spans and trace-level meta. Serialized to the exact ingest wire shape. Nothing here is part of the public API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route:, method:, started_at: nil, started_mono: nil) ⇒ Trace

Returns a new instance of Trace.

Parameters:

  • started_mono (Float) (defaults to: nil)

    monotonic clock at open (for duration)



13
14
15
16
17
18
19
20
21
22
# File 'lib/metrixwire/trace.rb', line 13

def initialize(route:, method:, started_at: nil, started_mono: nil)
  @route = route
  @method = method
  @started_mono = started_mono || MetrixWire.monotonic_ms
  @started_at = started_at || MetrixWire.iso8601(Time.now)
  @status = "success"
  @spans = []
  @meta = {}
  @duration_ms = nil
end

Instance Attribute Details

#metaObject

Returns the value of attribute meta.



9
10
11
# File 'lib/metrixwire/trace.rb', line 9

def meta
  @meta
end

#methodObject

Returns the value of attribute method.



9
10
11
# File 'lib/metrixwire/trace.rb', line 9

def method
  @method
end

#routeObject

Returns the value of attribute route.



9
10
11
# File 'lib/metrixwire/trace.rb', line 9

def route
  @route
end

#spansObject (readonly)

Returns the value of attribute spans.



10
11
12
# File 'lib/metrixwire/trace.rb', line 10

def spans
  @spans
end

#started_atObject (readonly)

Returns the value of attribute started_at.



10
11
12
# File 'lib/metrixwire/trace.rb', line 10

def started_at
  @started_at
end

#started_monoObject (readonly)

Returns the value of attribute started_mono.



10
11
12
# File 'lib/metrixwire/trace.rb', line 10

def started_mono
  @started_mono
end

#statusObject

Returns the value of attribute status.



9
10
11
# File 'lib/metrixwire/trace.rb', line 9

def status
  @status
end

Instance Method Details

#add_span(span) ⇒ Object



24
25
26
# File 'lib/metrixwire/trace.rb', line 24

def add_span(span)
  @spans << span
end

#capture_exception(err) ⇒ Object

Attach an exception to this trace and flag it as an error. Never throws.



33
34
35
36
37
38
# File 'lib/metrixwire/trace.rb', line 33

def capture_exception(err)
  @meta[:exception] = MetrixWire.exception_meta(err)
  @status = "error"
rescue StandardError
  # instrumentation must never throw
end

#duration_msObject



40
41
42
# File 'lib/metrixwire/trace.rb', line 40

def duration_ms
  @duration_ms ||= [(MetrixWire.monotonic_ms - @started_mono), 0].max.round
end

#finalize_duration!Object

Freeze the duration at the moment the trace closes.



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

def finalize_duration!
  @duration_ms = [(MetrixWire.monotonic_ms - @started_mono), 0].max.round
end

#set_meta(key, value) ⇒ Object



28
29
30
# File 'lib/metrixwire/trace.rb', line 28

def set_meta(key, value)
  @meta[key] = value
end

#to_hObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/metrixwire/trace.rb', line 49

def to_h
  out = {
    route: @route,
    method: @method,
    startedAt: @started_at,
    durationMs: duration_ms,
    status: @status,
    spans: @spans.map(&:to_h)
  }
  out[:meta] = @meta if @meta && !@meta.empty?
  out
end