Class: AllStak::Modules::Tracing
- Inherits:
-
Object
- Object
- AllStak::Modules::Tracing
- Defined in:
- lib/allstak/modules/tracing.rb
Overview
Distributed tracing — spans with parent-child hierarchy via Thread-local state.
Constant Summary collapse
- PATH =
"/ingest/v1/spans".freeze
- VALID_STATUSES =
%w[ok error timeout].freeze
Instance Method Summary collapse
- #current_span_id ⇒ Object
- #current_trace_id ⇒ Object
- #flush ⇒ Object
-
#in_span(operation, description: "", tags: nil) ⇒ Object
Block-form helper: automatically finishes the span on return, on raise, or on non-local flow (e.g. Sinatra’s ‘throw :halt`).
-
#initialize(transport, config, logger) ⇒ Tracing
constructor
A new instance of Tracing.
- #reset_trace ⇒ Object
- #set_trace_id(trace_id) ⇒ Object
- #shutdown ⇒ Object
- #start_span(operation, description: "", tags: nil) ⇒ Object
Constructor Details
#initialize(transport, config, logger) ⇒ Tracing
Returns a new instance of Tracing.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/allstak/modules/tracing.rb', line 10 def initialize(transport, config, logger) @transport = transport @config = config @logger = logger @buffer = Transport::FlushBuffer.new( name: "tracing", max_size: config.buffer_size, interval_ms: config.flush_interval_ms, flush_proc: method(:flush_batch), logger: logger ) end |
Instance Method Details
#current_span_id ⇒ Object
31 32 33 34 |
# File 'lib/allstak/modules/tracing.rb', line 31 def current_span_id stack = Thread.current[:allstak_span_stack] stack&.last end |
#current_trace_id ⇒ Object
23 24 25 |
# File 'lib/allstak/modules/tracing.rb', line 23 def current_trace_id Thread.current[:allstak_trace_id] ||= SecureRandom.hex(16) end |
#flush ⇒ Object
78 79 80 |
# File 'lib/allstak/modules/tracing.rb', line 78 def flush @buffer.flush end |
#in_span(operation, description: "", tags: nil) ⇒ Object
Block-form helper: automatically finishes the span on return, on raise, or on non-local flow (e.g. Sinatra’s ‘throw :halt`).
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/allstak/modules/tracing.rb', line 65 def in_span(operation, description: "", tags: nil) span = start_span(operation, description: description, tags: ) status = "ok" begin return yield(span) rescue => e status = "error" raise ensure span.finish(status) unless span.finished? end end |
#reset_trace ⇒ Object
36 37 38 39 |
# File 'lib/allstak/modules/tracing.rb', line 36 def reset_trace Thread.current[:allstak_trace_id] = nil Thread.current[:allstak_span_stack] = nil end |
#set_trace_id(trace_id) ⇒ Object
27 28 29 |
# File 'lib/allstak/modules/tracing.rb', line 27 def set_trace_id(trace_id) Thread.current[:allstak_trace_id] = trace_id end |
#shutdown ⇒ Object
82 83 84 |
# File 'lib/allstak/modules/tracing.rb', line 82 def shutdown @buffer.shutdown end |
#start_span(operation, description: "", tags: nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/allstak/modules/tracing.rb', line 41 def start_span(operation, description: "", tags: nil) trace_id = current_trace_id span_id = SecureRandom.hex(8) parent = current_span_id || "" Thread.current[:allstak_span_stack] ||= [] Thread.current[:allstak_span_stack] << span_id Span.new( trace_id: trace_id, span_id: span_id, parent_span_id: parent, operation: operation, description: description, service: @config.service_name, environment: @config.environment || "", release: (@config.respond_to?(:release) ? @config.release : nil) || "", tags: || {}, start_time_millis: (Time.now.to_f * 1000).to_i, on_finish: method(:on_span_finish) ) end |