newrelic_rpm 6.7.0.359 → 6.8.0.360

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +41 -6
  4. data/CHANGELOG.md +76 -0
  5. data/Guardfile +7 -1
  6. data/lib/new_relic/agent.rb +31 -0
  7. data/lib/new_relic/agent/agent.rb +7 -2
  8. data/lib/new_relic/agent/agent_logger.rb +4 -0
  9. data/lib/new_relic/agent/attribute_filter.rb +7 -7
  10. data/lib/new_relic/agent/attributes.rb +152 -0
  11. data/lib/new_relic/agent/autostart.rb +19 -14
  12. data/lib/new_relic/agent/configuration/default_source.rb +72 -5
  13. data/lib/new_relic/agent/configuration/yaml_source.rb +10 -5
  14. data/lib/new_relic/agent/connect/request_builder.rb +3 -11
  15. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +2 -2
  16. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +8 -8
  17. data/lib/new_relic/agent/distributed_trace_intrinsics.rb +90 -0
  18. data/lib/new_relic/agent/distributed_trace_metrics.rb +74 -0
  19. data/lib/new_relic/agent/distributed_trace_monitor.rb +2 -12
  20. data/lib/new_relic/agent/distributed_trace_payload.rb +9 -76
  21. data/lib/new_relic/agent/distributed_trace_transport_type.rb +43 -0
  22. data/lib/new_relic/agent/guid_generator.rb +28 -0
  23. data/lib/new_relic/agent/logging.rb +4 -0
  24. data/lib/new_relic/agent/new_relic_service.rb +7 -5
  25. data/lib/new_relic/agent/span_event_aggregator.rb +1 -0
  26. data/lib/new_relic/agent/span_event_primitive.rb +29 -7
  27. data/lib/new_relic/agent/sql_sampler.rb +1 -1
  28. data/lib/new_relic/agent/trace_context.rb +244 -0
  29. data/lib/new_relic/agent/trace_context_payload.rb +134 -0
  30. data/lib/new_relic/agent/trace_context_request_monitor.rb +42 -0
  31. data/lib/new_relic/agent/transaction.rb +26 -18
  32. data/lib/new_relic/agent/transaction/abstract_segment.rb +2 -2
  33. data/lib/new_relic/agent/transaction/distributed_tracing.rb +20 -101
  34. data/lib/new_relic/agent/transaction/external_request_segment.rb +18 -5
  35. data/lib/new_relic/agent/transaction/segment.rb +7 -1
  36. data/lib/new_relic/agent/transaction/trace_context.rb +159 -0
  37. data/lib/new_relic/agent/transaction/trace_node.rb +8 -3
  38. data/lib/new_relic/agent/transaction_error_primitive.rb +4 -11
  39. data/lib/new_relic/agent/transaction_event_primitive.rb +3 -11
  40. data/lib/new_relic/coerce.rb +29 -6
  41. data/lib/new_relic/control/instance_methods.rb +10 -1
  42. data/lib/new_relic/dependency_detection.rb +4 -4
  43. data/lib/new_relic/noticed_error.rb +8 -4
  44. data/lib/new_relic/version.rb +1 -1
  45. data/newrelic_rpm.gemspec +6 -4
  46. data/test/agent_helper.rb +88 -9
  47. data/true +0 -0
  48. metadata +50 -18
  49. data/lib/new_relic/agent/transaction/attributes.rb +0 -154
  50. data/lib/tasks/versions.html.erb +0 -28
  51. data/lib/tasks/versions.postface.html +0 -8
  52. data/lib/tasks/versions.preface.html +0 -9
  53. data/lib/tasks/versions.rake +0 -65
  54. data/lib/tasks/versions.txt.erb +0 -14
@@ -2,6 +2,7 @@
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
+ require 'new_relic/agent/distributed_trace_transport_type'
5
6
  require 'new_relic/agent/inbound_request_monitor'
6
7
  require 'new_relic/agent/cross_app_tracing'
7
8
 
@@ -21,20 +22,9 @@ module NewRelic
21
22
  return unless txn = Tracer.current_transaction
22
23
 
23
24
  if txn.accept_distributed_trace_payload payload
24
- txn.distributed_trace_payload.caller_transport_type = transport_type(request)
25
+ txn.distributed_trace_payload.caller_transport_type = DistributedTraceTransportType.for_rack_request(request)
25
26
  end
26
27
  end
27
-
28
- URL_SCHEMES = {
29
- 'http' => 'HTTP'.freeze,
30
- 'https' => 'HTTPS'.freeze
31
- }
32
-
33
- RACK_URL_SCHEME = 'rack.url_scheme'.freeze
34
-
35
- def transport_type(request)
36
- URL_SCHEMES[request[RACK_URL_SCHEME]]
37
- end
38
28
  end
39
29
  end
40
30
  end
@@ -5,6 +5,8 @@ require 'json'
5
5
  require 'base64'
6
6
  require 'set'
7
7
 
8
+ require 'new_relic/agent/distributed_trace_transport_type'
9
+
8
10
  module NewRelic
9
11
  module Agent
10
12
  #
@@ -32,35 +34,6 @@ module NewRelic
32
34
  TIMESTAMP_KEY = 'ti'.freeze
33
35
  PRIORITY_KEY = 'pr'.freeze
34
36
 
35
- # Intrinsic Keys
36
- PARENT_TYPE_INTRINSIC_KEY = "parent.type".freeze
37
- PARENT_APP_INTRINSIC_KEY = "parent.app".freeze
38
- PARENT_ACCOUNT_ID_INTRINSIC_KEY = "parent.account".freeze
39
- PARENT_TRANSPORT_TYPE_INTRINSIC_KEY = "parent.transportType".freeze
40
- PARENT_TRANSPORT_DURATION_INTRINSIC_KEY = "parent.transportDuration".freeze
41
- GUID_INTRINSIC_KEY = "guid".freeze
42
- TRACE_ID_INTRINSIC_KEY = "traceId".freeze
43
- PARENT_ID_INTRINSIC_KEY = "parentId".freeze
44
- PARENT_SPAN_ID_INTRINSIC_KEY = "parentSpanId".freeze
45
- SAMPLED_INTRINSIC_KEY = "sampled".freeze
46
- COMMA = ",".freeze
47
-
48
- INTRINSIC_KEYS = [
49
- PARENT_TYPE_INTRINSIC_KEY,
50
- PARENT_APP_INTRINSIC_KEY,
51
- PARENT_ACCOUNT_ID_INTRINSIC_KEY,
52
- PARENT_TRANSPORT_TYPE_INTRINSIC_KEY,
53
- PARENT_TRANSPORT_DURATION_INTRINSIC_KEY,
54
- GUID_INTRINSIC_KEY,
55
- TRACE_ID_INTRINSIC_KEY,
56
- PARENT_ID_INTRINSIC_KEY,
57
- PARENT_SPAN_ID_INTRINSIC_KEY,
58
- SAMPLED_INTRINSIC_KEY
59
- ].freeze
60
-
61
- # Intrinsic Values
62
- PARENT_TRANSPORT_TYPE_UNKNOWN = 'Unknown'.freeze
63
-
64
37
  class << self
65
38
 
66
39
  def for_transaction transaction
@@ -110,24 +83,12 @@ module NewRelic
110
83
  from_json decoded_payload
111
84
  end
112
85
 
113
- def assign_initial_intrinsics transaction, transaction_payload
114
- transaction_payload[GUID_INTRINSIC_KEY] = transaction.guid
115
- transaction_payload[TRACE_ID_INTRINSIC_KEY] = transaction.trace_id
116
- transaction_payload[SAMPLED_INTRINSIC_KEY] = transaction.sampled?
117
- end
118
-
119
86
  def major_version_matches?(payload)
120
87
  payload.version[0] == VERSION[0]
121
88
  end
122
89
 
123
90
  private
124
91
 
125
- # We use the presence of the account_id and primary_application in the
126
- # config to tell if we have connected yet.
127
- def connected?
128
- Agent.config[:account_id] && Agent.config[:primary_application_id]
129
- end
130
-
131
92
  def assign_trusted_account_key payload, account_id
132
93
  trusted_account_key = Agent.config[:trusted_account_key]
133
94
 
@@ -142,6 +103,10 @@ module NewRelic
142
103
  transaction.current_segment.guid
143
104
  end
144
105
  end
106
+
107
+ def connected?
108
+ Agent.instance.connected?
109
+ end
145
110
  end
146
111
 
147
112
  attr_accessor :version,
@@ -160,12 +125,12 @@ module NewRelic
160
125
 
161
126
  attr_reader :caller_transport_type
162
127
 
163
- def caller_transport_type=(type)
164
- @caller_transport_type = valid_transport_type_for(type)
128
+ def caller_transport_type= type
129
+ @caller_transport_type = DistributedTraceTransportType.from type
165
130
  end
166
131
 
167
132
  def initialize
168
- @caller_transport_type = PARENT_TRANSPORT_TYPE_UNKNOWN
133
+ @caller_transport_type = DistributedTraceTransportType::UNKNOWN
169
134
  end
170
135
 
171
136
  # Represent this payload as a raw JSON string.
@@ -205,38 +170,6 @@ module NewRelic
205
170
  def http_safe
206
171
  Base64.strict_encode64 text
207
172
  end
208
-
209
- def assign_intrinsics transaction, transaction_payload
210
- transaction_payload[PARENT_TYPE_INTRINSIC_KEY] = parent_type
211
- transaction_payload[PARENT_APP_INTRINSIC_KEY] = parent_app_id
212
- transaction_payload[PARENT_ACCOUNT_ID_INTRINSIC_KEY] = parent_account_id
213
- transaction_payload[PARENT_TRANSPORT_TYPE_INTRINSIC_KEY] = caller_transport_type
214
- transaction_payload[PARENT_TRANSPORT_DURATION_INTRINSIC_KEY] = transaction.transport_duration
215
- transaction_payload[GUID_INTRINSIC_KEY] = transaction.guid
216
- transaction_payload[TRACE_ID_INTRINSIC_KEY] = trace_id
217
- transaction_payload[PARENT_ID_INTRINSIC_KEY] = transaction.parent_id if transaction.parent_id
218
- transaction_payload[PARENT_SPAN_ID_INTRINSIC_KEY] = id if id
219
- transaction_payload[SAMPLED_INTRINSIC_KEY] = transaction.sampled?
220
- end
221
-
222
- private
223
-
224
- ALLOWABLE_TRANSPORT_TYPES = Set.new(%w[
225
- Unknown
226
- HTTP
227
- HTTPS
228
- Kafka
229
- JMS
230
- IronMQ
231
- AMQP
232
- Queue
233
- Other
234
- ]).freeze
235
-
236
- def valid_transport_type_for(value)
237
- return value if ALLOWABLE_TRANSPORT_TYPES.include?(value)
238
- PARENT_TRANSPORT_TYPE_UNKNOWN
239
- end
240
173
  end
241
174
  end
242
175
  end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ module NewRelic
6
+ module Agent
7
+ module DistributedTraceTransportType
8
+ extend self
9
+
10
+ UNKNOWN = 'Unknown'.freeze
11
+
12
+ ALLOWABLE_TRANSPORT_TYPES = Set.new(%w[
13
+ Unknown
14
+ HTTP
15
+ HTTPS
16
+ Kafka
17
+ JMS
18
+ IronMQ
19
+ AMQP
20
+ Queue
21
+ Other
22
+ ]).freeze
23
+
24
+ URL_SCHEMES = {
25
+ 'http' => 'HTTP'.freeze,
26
+ 'https' => 'HTTPS'.freeze
27
+ }
28
+
29
+ RACK_URL_SCHEME = 'rack.url_scheme'.freeze
30
+
31
+ def from value
32
+ return value if ALLOWABLE_TRANSPORT_TYPES.include?(value)
33
+
34
+ UNKNOWN
35
+ end
36
+
37
+
38
+ def for_rack_request request
39
+ URL_SCHEMES[request[RACK_URL_SCHEME]]
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ module NewRelic
6
+ module Agent
7
+ module GuidGenerator
8
+
9
+ HEX_DIGITS = (0..15).map{|i| i.to_s(16)}
10
+
11
+ module_function
12
+
13
+ # This method intentionally does not use SecureRandom, because it relies
14
+ # on urandom, which raises an exception in MRI when the interpreter runs
15
+ # out of allocated file descriptors.
16
+ # The guids generated by this method may not be _secure_, but they are
17
+ # random enough for our purposes.
18
+ def generate_guid length = 16
19
+ guid = ''
20
+ length.times do |a|
21
+ guid << HEX_DIGITS[rand(16)]
22
+ end
23
+ guid
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -93,6 +93,10 @@ module NewRelic
93
93
  message.inspect.to_json
94
94
  end
95
95
  end
96
+
97
+ def clear_tags!
98
+ # No-op; just avoiding issues with act-fluent-logger-rails
99
+ end
96
100
  end
97
101
 
98
102
 
@@ -92,14 +92,16 @@ module NewRelic
92
92
  token = Agent.config[:security_policies_token]
93
93
 
94
94
  if token && !token.empty?
95
- response = invoke_remote(:preconnect, [{'security_policies_token' => token}])
95
+ response = invoke_remote(:preconnect, [{'security_policies_token' => token, 'high_security' => false}])
96
96
 
97
97
  validator = SecurityPolicySettings::Validator.new(response)
98
98
  validator.validate_matching_agent_config!
99
99
 
100
100
  response
101
+ elsif Agent.config[:high_security]
102
+ invoke_remote(:preconnect, [{'high_security' => true}])
101
103
  else
102
- invoke_remote(:preconnect, [])
104
+ invoke_remote(:preconnect, [{'high_security' => false}])
103
105
  end
104
106
  end
105
107
 
@@ -196,10 +198,10 @@ module NewRelic
196
198
  encoding = 'identity'
197
199
  if data.size > 64 * 1024
198
200
  encoding = Agent.config[:compressed_content_encoding]
199
- data = if encoding == 'gzip'
200
- Encoders::Compressed::Gzip.encode(data)
201
- else
201
+ data = if encoding == 'deflate'
202
202
  Encoders::Compressed::Deflate.encode(data)
203
+ else
204
+ Encoders::Compressed::Gzip.encode(data)
203
205
  end
204
206
  end
205
207
  check_post_size(data, endpoint)
@@ -12,6 +12,7 @@ module NewRelic
12
12
  class SpanEventAggregator < EventAggregator
13
13
  named :SpanEventAggregator
14
14
  capacity_key :'span_events.max_samples_stored'
15
+
15
16
  enabled_keys :'span_events.enabled',
16
17
  :'distributed_tracing.enabled'
17
18
 
@@ -36,6 +36,8 @@ module NewRelic
36
36
  PEER_HOSTNAME_KEY = 'peer.hostname'.freeze
37
37
  SPAN_KIND_KEY = 'span.kind'.freeze
38
38
  ENTRY_POINT_KEY = 'nr.entryPoint'.freeze
39
+ TRUSTED_PARENT_KEY = "trustedParentId".freeze
40
+ TRACING_VENDORS_KEY = "tracingVendors".freeze
39
41
 
40
42
  # Strings for static values of the event structure
41
43
  EVENT_TYPE = 'Span'.freeze
@@ -51,7 +53,7 @@ module NewRelic
51
53
  intrinsics = intrinsics_for(segment)
52
54
  intrinsics[CATEGORY_KEY] = GENERIC_CATEGORY
53
55
 
54
- [intrinsics, EMPTY_HASH, EMPTY_HASH]
56
+ [intrinsics, custom_attributes(segment.attributes), EMPTY_HASH]
55
57
  end
56
58
 
57
59
  def for_external_request_segment(segment)
@@ -68,7 +70,7 @@ module NewRelic
68
70
  agent_attributes[HTTP_URL_KEY] = truncate(segment.uri)
69
71
  end
70
72
 
71
- [intrinsics, EMPTY_HASH, agent_attributes]
73
+ [intrinsics, custom_attributes(segment.attributes), agent_attributes]
72
74
  end
73
75
 
74
76
  def for_datastore_segment(segment)
@@ -96,7 +98,7 @@ module NewRelic
96
98
  agent_attributes[DB_STATEMENT_KEY] = truncate(segment.nosql_statement, 2000)
97
99
  end
98
100
 
99
- [intrinsics, EMPTY_HASH, agent_attributes]
101
+ [intrinsics, custom_attributes(segment.attributes), agent_attributes]
100
102
  end
101
103
 
102
104
  private
@@ -115,19 +117,39 @@ module NewRelic
115
117
  NAME_KEY => segment.name
116
118
  }
117
119
 
120
+ if segment.parent.nil?
121
+ intrinsics[ENTRY_POINT_KEY] = true
122
+ if segment.transaction
123
+ if segment.transaction.trace_context_header_data
124
+ intrinsics[TRACING_VENDORS_KEY] = segment.transaction.trace_context_header_data.trace_state_vendors
125
+ end
126
+ if segment.transaction.trace_state_payload
127
+ intrinsics[TRUSTED_PARENT_KEY] = segment.transaction.trace_state_payload.id
128
+ end
129
+ end
130
+ end
131
+
118
132
  parent_id = parent_guid(segment)
119
133
  intrinsics[PARENT_ID_KEY] = parent_id if parent_id
120
-
121
- intrinsics[ENTRY_POINT_KEY] = true unless segment.parent
122
-
123
134
  intrinsics
124
135
  end
125
136
 
137
+ def custom_attributes attributes
138
+ if attributes
139
+ result = attributes.custom_attributes_for(NewRelic::Agent::AttributeFilter::DST_SPAN_EVENTS)
140
+ result.freeze
141
+ else
142
+ EMPTY_HASH
143
+ end
144
+ end
145
+
126
146
  def parent_guid(segment)
127
147
  if segment.parent
128
148
  segment.parent.guid
129
- elsif segment.transaction && segment.transaction.distributed_trace?
149
+ elsif segment.transaction && segment.transaction.distributed_trace_payload
130
150
  segment.transaction.distributed_trace_payload.id
151
+ elsif segment.transaction && segment.transaction.trace_context_header_data
152
+ segment.transaction.trace_context_header_data.parent_id
131
153
  end
132
154
  end
133
155
 
@@ -162,7 +162,7 @@ module NewRelic
162
162
  if transaction && transaction.distributed_trace_payload
163
163
  params = {}
164
164
  payload = transaction.distributed_trace_payload
165
- payload.assign_intrinsics transaction, params
165
+ DistributedTraceIntrinsics.copy_from_transaction transaction, payload, params
166
166
  params[PRIORITY] = transaction.priority
167
167
  end
168
168
  params
@@ -0,0 +1,244 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'new_relic/agent/trace_context_payload'
6
+
7
+ module NewRelic
8
+ module Agent
9
+ class TraceContext
10
+ VERSION = 0x0
11
+ TRACE_PARENT = 'traceparent'.freeze
12
+ TRACE_STATE = 'tracestate'.freeze
13
+
14
+ TRACE_PARENT_RACK = 'HTTP_TRACEPARENT'.freeze
15
+ TRACE_STATE_RACK = 'HTTP_TRACESTATE'.freeze
16
+
17
+ FORMAT_HTTP = 0
18
+ FORMAT_RACK = 1
19
+
20
+ TRACE_PARENT_REGEX = /\A(?<version>[a-f\d]{2})-(?<trace_id>[a-f\d]{32})-(?<parent_id>[a-f\d]{16})-(?<trace_flags>\d{2})(?<undefined_fields>-[a-zA-Z\d-]*)?\z/.freeze
21
+
22
+ COMMA = ','.freeze
23
+ EMPTY_STRING = ''.freeze
24
+ EQUALS = '='.freeze
25
+ TRACE_ID_KEY = 'trace_id'.freeze
26
+ PARENT_ID_KEY = 'parent_id'.freeze
27
+ VERSION_KEY = 'version'.freeze
28
+ UNDEFINED_FIELDS_KEY = 'undefined_fields'.freeze
29
+ INVALID_TRACE_ID = ('0' * 32).freeze
30
+ INVALID_PARENT_ID = ('0' * 16).freeze
31
+ INVALID_VERSION = 'ff'.freeze
32
+
33
+ MAX_TRACE_STATE_SIZE = 512 # bytes
34
+ MAX_TRACE_STATE_ENTRY_SIZE = 128 # bytes
35
+
36
+ SUPPORTABILITY_TRACE_PARENT_PARSE_EXCEPTION = "Supportability/TraceContext/TraceParent/Parse/Exception".freeze
37
+ SUPPORTABILITY_TRACE_STATE_PARSE_EXCEPTION = "Supportability/TraceContext/TraceState/Parse/Exception".freeze
38
+
39
+ class << self
40
+ def insert format: FORMAT_HTTP,
41
+ carrier: nil,
42
+ parent_id: nil,
43
+ trace_id: nil,
44
+ trace_flags: nil,
45
+ trace_state: nil
46
+
47
+ trace_parent_header = trace_parent_header_for_format format
48
+ carrier[trace_parent_header] = format_trace_parent \
49
+ trace_id: trace_id,
50
+ parent_id: parent_id,
51
+ trace_flags: trace_flags
52
+
53
+ trace_state_header = trace_state_header_for_format format
54
+ carrier[trace_state_header] = trace_state if trace_state && !trace_state.empty?
55
+ end
56
+
57
+ def parse format: FORMAT_HTTP,
58
+ carrier: nil,
59
+ trace_state_entry_key: nil
60
+ trace_parent = extract_traceparent(format, carrier)
61
+ unless trace_parent_valid? trace_parent
62
+ NewRelic::Agent.increment_metric SUPPORTABILITY_TRACE_PARENT_PARSE_EXCEPTION
63
+ return
64
+ end
65
+
66
+ begin
67
+ if header_data = extract_tracestate(format, carrier, trace_state_entry_key)
68
+ header_data.trace_parent = trace_parent
69
+ header_data
70
+ end
71
+ rescue Exception
72
+ NewRelic::Agent.increment_metric SUPPORTABILITY_TRACE_STATE_PARSE_EXCEPTION
73
+ end
74
+ end
75
+
76
+ def create_trace_state_entry entry_key, payload
77
+ "#{entry_key}=#{payload}"
78
+ end
79
+
80
+ private
81
+
82
+ TRACE_PARENT_FORMAT_STRING = "%02x-%s-%s-%02x".freeze
83
+
84
+ def format_trace_parent trace_id: nil,
85
+ parent_id: nil,
86
+ trace_flags: nil
87
+ sprintf TRACE_PARENT_FORMAT_STRING,
88
+ VERSION,
89
+ trace_id,
90
+ parent_id,
91
+ trace_flags
92
+ end
93
+
94
+ def extract_traceparent format, carrier
95
+ header_name = trace_parent_header_for_format format
96
+ return unless header = carrier[header_name]
97
+ if matchdata = header.match(TRACE_PARENT_REGEX)
98
+ TRACE_PARENT_REGEX.named_captures.inject({}) do |hash, (name, (index))|
99
+ hash[name] = matchdata[index]
100
+ hash
101
+ end
102
+ end
103
+ end
104
+
105
+ def trace_parent_valid? trace_parent
106
+ return false if trace_parent.nil?
107
+ return false if trace_parent[TRACE_ID_KEY] == INVALID_TRACE_ID
108
+ return false if trace_parent[PARENT_ID_KEY] == INVALID_PARENT_ID
109
+ return false if trace_parent[VERSION_KEY] == INVALID_VERSION
110
+ return false if trace_parent[VERSION_KEY].to_i(16) == VERSION && !trace_parent[UNDEFINED_FIELDS_KEY].nil?
111
+
112
+ true
113
+ end
114
+
115
+ def trace_parent_header_for_format format
116
+ if format == FORMAT_RACK
117
+ TRACE_PARENT_RACK
118
+ else
119
+ TRACE_PARENT
120
+ end
121
+ end
122
+
123
+ def trace_state_header_for_format format
124
+ if format == FORMAT_RACK
125
+ TRACE_STATE_RACK
126
+ else
127
+ TRACE_STATE
128
+ end
129
+ end
130
+
131
+ def extract_tracestate format, carrier, trace_state_entry_key
132
+ header_name = trace_state_header_for_format format
133
+ header = carrier[header_name]
134
+ return HeaderData.create if header.nil? || header.empty?
135
+
136
+ payload = nil
137
+ trace_state_size = 0
138
+ trace_state_vendors = ''
139
+ trace_state = header.split(COMMA).map(&:strip)
140
+ trace_state.reject! do |entry|
141
+ vendor_id = entry.slice 0, entry.index(EQUALS)
142
+ if vendor_id == trace_state_entry_key
143
+ payload = entry.slice! trace_state_entry_key.size + 1, entry.size
144
+ true
145
+ else
146
+ trace_state_size += entry.size
147
+ trace_state_vendors << vendor_id << COMMA
148
+ false
149
+ end
150
+ end
151
+
152
+ trace_state_vendors.chomp! COMMA unless trace_state_vendors.empty?
153
+
154
+ HeaderData.create trace_state_payload: payload ? decode_payload(payload) : nil,
155
+ trace_state_entries: trace_state,
156
+ trace_state_size: trace_state_size,
157
+ trace_state_vendors: trace_state_vendors
158
+ end
159
+
160
+ def decode_payload payload
161
+ TraceContextPayload.from_s payload
162
+ end
163
+ end
164
+
165
+ class HeaderData
166
+ class << self
167
+ def create trace_parent: nil,
168
+ trace_state_payload: nil,
169
+ trace_state_entries: nil,
170
+ trace_state_size: 0,
171
+ trace_state_vendors: nil
172
+ new trace_parent, \
173
+ trace_state_payload, \
174
+ trace_state_entries, \
175
+ trace_state_size, \
176
+ trace_state_vendors
177
+ end
178
+ end
179
+
180
+ def initialize trace_parent, trace_state_payload, trace_state_entries, trace_state_size, trace_state_vendors
181
+ @trace_parent = trace_parent
182
+ @trace_state_entries = trace_state_entries
183
+ @trace_state_payload = trace_state_payload
184
+ @trace_state_size = trace_state_size
185
+ @trace_state_vendors = trace_state_vendors
186
+ end
187
+
188
+ attr_accessor :trace_parent, :trace_state_payload, :trace_state_vendors
189
+
190
+ def trace_state trace_state_entry
191
+ @trace_state ||= join_trace_state trace_state_entry.size
192
+ @trace_state_entries = nil
193
+
194
+ trace_state_entry << @trace_state
195
+ end
196
+
197
+ def trace_id
198
+ @trace_parent[TRACE_ID_KEY]
199
+ end
200
+
201
+ def parent_id
202
+ @trace_parent[PARENT_ID_KEY]
203
+ end
204
+
205
+ private
206
+
207
+ def join_trace_state trace_state_entry_size
208
+ return @trace_state || EMPTY_STRING if @trace_state_entries.nil? || @trace_state_entries.empty?
209
+
210
+ max_size = MAX_TRACE_STATE_SIZE - trace_state_entry_size
211
+ return @trace_state_entries.join(COMMA).prepend(COMMA) if @trace_state_size < max_size
212
+
213
+ joined_trace_state = ''
214
+
215
+ used_size = 0
216
+
217
+ @trace_state_entries.each do |entry|
218
+ entry_size = entry.size + 1
219
+ break if used_size + entry_size > max_size
220
+ next if entry_size > MAX_TRACE_STATE_ENTRY_SIZE
221
+
222
+ joined_trace_state << COMMA << entry
223
+ used_size += entry_size
224
+ end
225
+
226
+ joined_trace_state
227
+ end
228
+
229
+ end
230
+
231
+ module AccountHelpers
232
+ extend self
233
+
234
+ def trace_state_entry_key
235
+ @trace_state_entry_key ||= if Agent.config[:trusted_account_key]
236
+ "#{Agent.config[:trusted_account_key]}@nr".freeze
237
+ elsif Agent.config[:account_id]
238
+ "#{Agent.config[:account_id]}@nr".freeze
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end
244
+ end