Class: OpenTelemetry::Trace::Propagation::TraceContext::TraceParent

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb

Overview

A TraceParent is an implementation of the W3C trace context specification www.w3.org/TR/trace-context/ SpanContext

Constant Summary collapse

InvalidFormatError =
Class.new(Error)
InvalidVersionError =
Class.new(Error)
InvalidTraceIDError =
Class.new(Error)
InvalidSpanIDError =
Class.new(Error)
TRACE_PARENT_HEADER =
'traceparent'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



96
97
98
# File 'lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb', line 96

def flags
  @flags
end

#span_idObject (readonly)

Returns the value of attribute span_id.



96
97
98
# File 'lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb', line 96

def span_id
  @span_id
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



96
97
98
# File 'lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb', line 96

def trace_id
  @trace_id
end

#versionObject (readonly)

Returns the value of attribute version.



96
97
98
# File 'lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb', line 96

def version
  @version
end

Class Method Details

.from_span_context(ctx) ⇒ TraceParent

Parameters:

Returns:



36
37
38
# File 'lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb', line 36

def from_span_context(ctx)
  new(trace_id: ctx.trace_id, span_id: ctx.span_id, flags: ctx.trace_flags)
end

.from_string(string) ⇒ TraceParent

Deserializes the OpenTelemetry::Trace::Propagation::TraceContext::TraceParent from the string representation

Parameters:

  • string (String)

    The serialized trace parent

Returns:

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb', line 47

def from_string(string)
  matches = match_input(string)

  version = parse_version(matches[:version])
  raise InvalidFormatError if version > SUPPORTED_VERSION && string.length < 55

  trace_id = parse_trace_id(matches[:trace_id])
  span_id = parse_span_id(matches[:span_id])
  flags = parse_flags(matches[:flags])

  new(trace_id: trace_id, span_id: span_id, flags: flags)
end

Instance Method Details

#sampled?Boolean

Returns the sampling choice from the trace_flags

Returns:

  • (Boolean)

    the sampling choice



102
103
104
# File 'lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb', line 102

def sampled?
  flags.sampled?
end

#to_sString

converts this object into a string according to the w3c spec

Returns:

  • (String)

    the serialized trace_parent



108
109
110
# File 'lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb', line 108

def to_s
  "00-#{trace_id.unpack1('H*')}-#{span_id.unpack1('H*')}-#{flag_string}"
end