Class: MetrixWire::Trace
- Inherits:
-
Object
- Object
- MetrixWire::Trace
- 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
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#method ⇒ Object
Returns the value of attribute method.
-
#route ⇒ Object
Returns the value of attribute route.
-
#spans ⇒ Object
readonly
Returns the value of attribute spans.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#started_mono ⇒ Object
readonly
Returns the value of attribute started_mono.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #add_span(span) ⇒ Object
-
#capture_exception(err) ⇒ Object
Attach an exception to this trace and flag it as an error.
- #duration_ms ⇒ Object
-
#finalize_duration! ⇒ Object
Freeze the duration at the moment the trace closes.
-
#initialize(route:, method:, started_at: nil, started_mono: nil) ⇒ Trace
constructor
A new instance of Trace.
- #set_meta(key, value) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(route:, method:, started_at: nil, started_mono: nil) ⇒ Trace
Returns a new instance of Trace.
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
#meta ⇒ Object
Returns the value of attribute meta.
9 10 11 |
# File 'lib/metrixwire/trace.rb', line 9 def @meta end |
#method ⇒ Object
Returns the value of attribute method.
9 10 11 |
# File 'lib/metrixwire/trace.rb', line 9 def method @method end |
#route ⇒ Object
Returns the value of attribute route.
9 10 11 |
# File 'lib/metrixwire/trace.rb', line 9 def route @route end |
#spans ⇒ Object (readonly)
Returns the value of attribute spans.
10 11 12 |
# File 'lib/metrixwire/trace.rb', line 10 def spans @spans end |
#started_at ⇒ Object (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_mono ⇒ Object (readonly)
Returns the value of attribute started_mono.
10 11 12 |
# File 'lib/metrixwire/trace.rb', line 10 def started_mono @started_mono end |
#status ⇒ Object
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.(err) @status = "error" rescue StandardError # instrumentation must never throw end |
#duration_ms ⇒ Object
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 (key, value) @meta[key] = value end |
#to_h ⇒ Object
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 |