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

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

Overview

Represents current trace state with key identifiers

Constant Summary collapse

LOG_ATTR_ENV =

Returns:

'dd.env'
LOG_ATTR_SERVICE =

Returns:

'dd.service'
LOG_ATTR_SPAN_ID =

Returns:

'dd.span_id'
LOG_ATTR_TRACE_ID =

Returns:

'dd.trace_id'
LOG_ATTR_VERSION =

Returns:

'dd.version'
LOG_ATTR_SOURCE =

Returns:

  • ("ddsource")
'ddsource'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: nil, service: nil, span_id: nil, trace_id: nil, version: nil) ⇒ Identifier

Returns a new instance of Identifier.

Parameters:

  • env: (Object, nil) (defaults to: nil)
  • service: (Object, nil) (defaults to: nil)
  • span_id: (Object, nil) (defaults to: nil)
  • trace_id: (Object, nil) (defaults to: nil)
  • version: (Object, nil) (defaults to: nil)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/datadog/tracing/correlation.rb', line 30

def initialize(
  env: nil,
  service: nil,
  span_id: nil,
  trace_id: nil,
  version: nil
)
  # Dup and freeze strings so they aren't modified by reference.
  @env = env || Datadog.configuration.env
  @service = service || Datadog.configuration.service
  @span_id = (span_id || 0).to_s
  @trace_id = trace_id || 0
  @version = version || Datadog.configuration.version
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



23
24
25
# File 'lib/datadog/tracing/correlation.rb', line 23

def env
  @env
end

#serviceObject (readonly)

Returns the value of attribute service.



23
24
25
# File 'lib/datadog/tracing/correlation.rb', line 23

def service
  @service
end

#span_idObject (readonly)

Returns the value of attribute span_id.



23
24
25
# File 'lib/datadog/tracing/correlation.rb', line 23

def span_id
  @span_id
end

#versionObject (readonly)

Returns the value of attribute version.



23
24
25
# File 'lib/datadog/tracing/correlation.rb', line 23

def version
  @version
end

Instance Method Details

#to_hObject

Returns:

  • (Object)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datadog/tracing/correlation.rb', line 45

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.



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

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

Returns:

  • (Object)


75
76
77
# File 'lib/datadog/tracing/correlation.rb', line 75

def trace_id
  Correlation.format_trace_id(@trace_id)
end