Class: Datadog::Tracing::Correlation::Identifier

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/correlation.rb

Overview

Represents current trace state with key identifiers

Constant Summary collapse

LOG_ATTR_ENV =
'dd.env'.freeze
LOG_ATTR_SERVICE =
'dd.service'.freeze
LOG_ATTR_SPAN_ID =
'dd.span_id'.freeze
LOG_ATTR_TRACE_ID =
'dd.trace_id'.freeze
LOG_ATTR_VERSION =
'dd.version'.freeze
LOG_ATTR_SOURCE =
'ddsource'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def env
  @env
end

#serviceObject (readonly)

Returns the value of attribute service.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def service
  @service
end

#span_idObject (readonly)

Returns the value of attribute span_id.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def span_id
  @span_id
end

#span_nameObject (readonly)

Returns the value of attribute span_name.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def span_name
  @span_name
end

#span_resourceObject (readonly)

Returns the value of attribute span_resource.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def span_resource
  @span_resource
end

#span_serviceObject (readonly)

Returns the value of attribute span_service.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def span_service
  @span_service
end

#span_typeObject (readonly)

Returns the value of attribute span_type.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def span_type
  @span_type
end

#trace_nameObject (readonly)

Returns the value of attribute trace_name.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def trace_name
  @trace_name
end

#trace_resourceObject (readonly)

Returns the value of attribute trace_resource.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def trace_resource
  @trace_resource
end

#trace_serviceObject (readonly)

Returns the value of attribute trace_service.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def trace_service
  @trace_service
end

#versionObject (readonly)

Returns the value of attribute version.



20
21
22
# File 'lib/datadog/tracing/correlation.rb', line 20

def version
  @version
end

Instance Method Details

#to_hObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/datadog/tracing/correlation.rb', line 63

def to_h
  @to_h ||= {
    # Adds IDs as tags to log output
    dd: {
      # To preserve precision during JSON serialization, use strings for large numbers
      env: env.to_s,
      service: service.to_s,
      version: version.to_s,
      trace_id: trace_id.to_s,
      span_id: span_id.to_s
    },
    ddsource: Core::Logging::Ext::DD_SOURCE
  }
end

#to_log_formatObject

This method (#to_log_format) implements an algorithm by prefixing keys for nested values but the algorithm makes the constants implicit. Hence, we use it for validation during test.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/datadog/tracing/correlation.rb', line 80

def to_log_format
  @log_format ||= begin
    attributes = []
    attributes << "#{LOG_ATTR_ENV}=#{env}" unless env.nil?
    attributes << "#{LOG_ATTR_SERVICE}=#{service}"
    attributes << "#{LOG_ATTR_VERSION}=#{version}" unless version.nil?
    attributes << "#{LOG_ATTR_TRACE_ID}=#{trace_id}"
    attributes << "#{LOG_ATTR_SPAN_ID}=#{span_id}"
    attributes << "#{LOG_ATTR_SOURCE}=#{Core::Logging::Ext::DD_SOURCE}"
    attributes.join(' ')
  end
end

#trace_idObject

DEV-2.0: This public method was returning an Integer, but with 128 bit trace id it would return a String.



94
95
96
97
98
99
100
101
# File 'lib/datadog/tracing/correlation.rb', line 94

def trace_id
  if Datadog.configuration.tracing.trace_id_128_bit_logging_enabled &&
      !Tracing::Utils::TraceId.to_high_order(@trace_id).zero?
    Kernel.format('%032x', @trace_id)
  else
    Tracing::Utils::TraceId.to_low_order(@trace_id)
  end
end