Module: LogBrew::Traceparent

Defined in:
lib/logbrew/traceparent.rb

Overview

Dependency-free W3C traceparent helpers for explicit app-owned propagation.

Class Method Summary collapse

Class Method Details

.create(trace_id:, span_id:, trace_flags: "01") ⇒ Object



44
45
46
47
48
49
50
# File 'lib/logbrew/traceparent.rb', line 44

def create(trace_id:, span_id:, trace_flags: "01")
  normalized_trace_id = normalize_trace_id(trace_id)
  normalized_span_id = normalize_span_id("traceparent span id", span_id)
  flags = normalize_trace_flags(trace_flags)

  "#{VERSION}-#{normalized_trace_id}-#{normalized_span_id}-#{flags}"
end

.create_headers(trace_id:, span_id:, trace_flags: "01") ⇒ Object



52
53
54
# File 'lib/logbrew/traceparent.rb', line 52

def create_headers(trace_id:, span_id:, trace_flags: "01")
  { "traceparent" => create(trace_id: trace_id, span_id: span_id, trace_flags: trace_flags) }
end

.parse(traceparent) ⇒ Object

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/logbrew/traceparent.rb', line 24

def parse(traceparent)
  Validation.require_non_empty("traceparent", traceparent)
  parts = traceparent.to_s.strip.downcase.split("-")
  raise SdkError.new("validation_error", "traceparent must have four fields") unless parts.length == 4

  version, trace_id, parent_span_id, trace_flags = parts
  require_version(version)
  require_trace_id(trace_id)
  require_span_id("traceparent parent span id", parent_span_id)
  flags = normalize_trace_flags(trace_flags)

  TraceparentContext.new(
    version: version,
    trace_id: trace_id,
    parent_span_id: parent_span_id,
    trace_flags: flags,
    sampled: (flags.to_i(16) & 1) == 1
  )
end

.span_attributes_from_traceparent(traceparent, input) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/logbrew/traceparent.rb', line 56

def span_attributes_from_traceparent(traceparent, input)
  context = traceparent.is_a?(TraceparentContext) ? traceparent : parse(traceparent)
  attributes = {
    "name" => required_name(input.name),
    "traceId" => context.trace_id,
    "spanId" => normalize_span_id("span spanId", input.span_id),
    "parentSpanId" => context.parent_span_id,
    "status" => normalize_status(input.status)
  }

  unless input.duration_ms.nil?
    duration_ms = Validation.require_finite_number("span durationMs", input.duration_ms)
    raise SdkError.new("validation_error", "span durationMs must be non-negative") if duration_ms.negative?

    attributes["durationMs"] = duration_ms
  end

   = Validation.(input.)
  attributes["metadata"] =  unless .nil?
  attributes
end