-
Notifications
You must be signed in to change notification settings - Fork 375
/
correlation.rb
112 lines (103 loc) · 3.89 KB
/
correlation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require_relative '../core'
require_relative 'utils'
require_relative 'metadata/ext'
module Datadog
module Tracing
# Contains behavior for managing correlations with tracing
# e.g. Retrieve a correlation to the current trace for logging, etc.
module Correlation
# Represents current trace state with key identifiers
# @public_api
class Identifier
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
attr_reader \
:env,
:service,
:span_id,
:span_name,
:span_resource,
:span_service,
:span_type,
:trace_id,
:trace_name,
:trace_resource,
:trace_service,
:version
# @!visibility private
def initialize(
env: nil,
service: nil,
span_id: nil,
span_name: nil,
span_resource: nil,
span_service: nil,
span_type: nil,
trace_id: nil,
trace_name: nil,
trace_resource: nil,
trace_service: nil,
version: nil
)
# Dup and freeze strings so they aren't modified by reference.
@env = Core::Utils::SafeDup.frozen_or_dup(env || Datadog.configuration.env).freeze
@service = Core::Utils::SafeDup.frozen_or_dup(service || Datadog.configuration.service).freeze
@span_id = span_id || 0
@span_name = Core::Utils::SafeDup.frozen_or_dup(span_name).freeze
@span_resource = Core::Utils::SafeDup.frozen_or_dup(span_resource).freeze
@span_service = Core::Utils::SafeDup.frozen_or_dup(span_service).freeze
@span_type = Core::Utils::SafeDup.frozen_or_dup(span_type).freeze
@trace_id = trace_id || 0
@trace_name = Core::Utils::SafeDup.frozen_or_dup(trace_name).freeze
@trace_resource = Core::Utils::SafeDup.frozen_or_dup(trace_resource).freeze
@trace_service = Core::Utils::SafeDup.frozen_or_dup(trace_service).freeze
@version = Core::Utils::SafeDup.frozen_or_dup(version || Datadog.configuration.version).freeze
end
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}=#{logging_trace_id}"
attributes << "#{LOG_ATTR_SPAN_ID}=#{span_id}"
attributes.join(' ')
end
end
private
def logging_trace_id
@logging_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
end
module_function
# Produces a CorrelationIdentifier from the TraceDigest provided
#
# DEV: can we memoize this object, give it can be common to
# use a correlation multiple times, specially in the context of logging?
# @!visibility private
def identifier_from_digest(digest)
return Identifier.new unless digest
Identifier.new(
span_id: digest.span_id,
span_name: digest.span_name,
span_resource: digest.span_resource,
span_service: digest.span_service,
span_type: digest.span_type,
trace_id: digest.trace_id,
trace_name: digest.trace_name,
trace_resource: digest.trace_resource,
trace_service: digest.trace_service
)
end
end
end
end