Class: Ditliti::TraceContext
- Inherits:
-
Object
- Object
- Ditliti::TraceContext
- Defined in:
- lib/ditliti.rb
Overview
A W3C trace-context (https://www.w3.org/TR/trace-context/) carried
across service boundaries via the traceparent header, so a trace
started in one process is continued - not restarted - in the next.
Instance Attribute Summary collapse
-
#sampled ⇒ Object
readonly
Returns the value of attribute sampled.
-
#span_id ⇒ Object
readonly
Returns the value of attribute span_id.
-
#trace_id ⇒ Object
readonly
Returns the value of attribute trace_id.
Class Method Summary collapse
- .from_traceparent(header) ⇒ Object
- .generate_span_id ⇒ Object
- .generate_trace_id ⇒ Object
- .start ⇒ Object
Instance Method Summary collapse
- #child ⇒ Object
-
#initialize(trace_id, span_id, sampled = true) ⇒ TraceContext
constructor
A new instance of TraceContext.
- #to_traceparent ⇒ Object
Constructor Details
#initialize(trace_id, span_id, sampled = true) ⇒ TraceContext
Returns a new instance of TraceContext.
43 44 45 46 47 |
# File 'lib/ditliti.rb', line 43 def initialize(trace_id, span_id, sampled = true) @trace_id = trace_id @span_id = span_id @sampled = sampled end |
Instance Attribute Details
#sampled ⇒ Object (readonly)
Returns the value of attribute sampled.
13 14 15 |
# File 'lib/ditliti.rb', line 13 def sampled @sampled end |
#span_id ⇒ Object (readonly)
Returns the value of attribute span_id.
13 14 15 |
# File 'lib/ditliti.rb', line 13 def span_id @span_id end |
#trace_id ⇒ Object (readonly)
Returns the value of attribute trace_id.
13 14 15 |
# File 'lib/ditliti.rb', line 13 def trace_id @trace_id end |
Class Method Details
.from_traceparent(header) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ditliti.rb', line 27 def self.from_traceparent(header) return nil if header.nil? || header.strip.empty? parts = header.strip.split("-") return nil unless parts.length == 4 _version, trace_id, span_id, flags = parts return nil unless trace_id =~ /\A[0-9a-f]{32}\z/ && trace_id != "0" * 32 return nil unless span_id =~ /\A[0-9a-f]{16}\z/ && span_id != "0" * 16 flags_value = Integer(flags, 16) rescue nil return nil if flags_value.nil? new(trace_id, span_id, (flags_value & 1) == 1) end |
.generate_span_id ⇒ Object
19 20 21 |
# File 'lib/ditliti.rb', line 19 def self.generate_span_id SecureRandom.hex(8) end |
.generate_trace_id ⇒ Object
15 16 17 |
# File 'lib/ditliti.rb', line 15 def self.generate_trace_id SecureRandom.hex(16) end |
.start ⇒ Object
23 24 25 |
# File 'lib/ditliti.rb', line 23 def self.start new(generate_trace_id, generate_span_id, true) end |
Instance Method Details
#child ⇒ Object
49 50 51 |
# File 'lib/ditliti.rb', line 49 def child self.class.new(trace_id, self.class.generate_span_id, sampled) end |
#to_traceparent ⇒ Object
53 54 55 |
# File 'lib/ditliti.rb', line 53 def to_traceparent "00-#{trace_id}-#{span_id}-#{sampled ? "01" : "00"}" end |