Class: OpenTelemetry::Propagator::GoogleCloudTraceContext::CloudTraceContext

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/propagator/google_cloud_trace_context/text_map_propagator.rb

Overview

Provides a class for decoding and encoding x-cloud-trace-context header to/from into trace components

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



47
48
49
# File 'lib/opentelemetry/propagator/google_cloud_trace_context/text_map_propagator.rb', line 47

def flags
  @flags
end

#span_idObject (readonly)

Returns the value of attribute span_id.



47
48
49
# File 'lib/opentelemetry/propagator/google_cloud_trace_context/text_map_propagator.rb', line 47

def span_id
  @span_id
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



47
48
49
# File 'lib/opentelemetry/propagator/google_cloud_trace_context/text_map_propagator.rb', line 47

def trace_id
  @trace_id
end

Class Method Details

.from_span_context(ctx) ⇒ CloudTraceContext

Creates a new OpenTelemetry::Propagator::GoogleCloudTraceContext::CloudTraceContext from a supplied Trace::SpanContext

Parameters:

  • ctx (SpanContext)

    The span context

Returns:



28
29
30
# File 'lib/opentelemetry/propagator/google_cloud_trace_context/text_map_propagator.rb', line 28

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

.from_string(string) ⇒ CloudTraceContext?

Deserializes the OpenTelemetry::Propagator::GoogleCloudTraceContext::CloudTraceContext from the string representation

Parameters:

  • string (String)

    The serialized trace parent

Returns:



35
36
37
38
39
40
41
42
43
44
# File 'lib/opentelemetry/propagator/google_cloud_trace_context/text_map_propagator.rb', line 35

def from_string(string)
  matches = CLOUD_TRACE_CONTEXT_REGEX.match(string)
  return unless matches

  trace_id = Array(matches[:trace_id].downcase).pack('H*')
  span_id = Array(matches[:span_id].to_i.to_s(16)).pack('H*')
  flags = matches[:options] == '1' ? Trace::TraceFlags::SAMPLED : Trace::TraceFlags::DEFAULT

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

Instance Method Details

#to_sString

converts this object into a string according to the w3c spec

Returns:

  • (String)

    the serialized trace_parent



53
54
55
# File 'lib/opentelemetry/propagator/google_cloud_trace_context/text_map_propagator.rb', line 53

def to_s
  "#{trace_id.unpack1('H*')}/#{span_id.unpack1('H*').to_i(16)};o=#{flags.sampled? ? '1' : '0'}"
end