newrelic_rpm 6.8.0.360 → 6.9.0.363
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +16 -16
- data/CHANGELOG.md +64 -0
- data/lib/new_relic/agent.rb +6 -5
- data/lib/new_relic/agent/agent.rb +43 -36
- data/lib/new_relic/agent/attributes.rb +2 -4
- data/lib/new_relic/agent/configuration/default_source.rb +23 -22
- data/lib/new_relic/agent/configuration/server_source.rb +1 -1
- data/lib/new_relic/agent/configuration/yaml_source.rb +1 -1
- data/lib/new_relic/agent/database.rb +1 -2
- data/lib/new_relic/agent/distributed_tracing.rb +155 -6
- data/lib/new_relic/agent/{cross_app_payload.rb → distributed_tracing/cross_app_payload.rb} +0 -0
- data/lib/new_relic/agent/{cross_app_tracing.rb → distributed_tracing/cross_app_tracing.rb} +60 -45
- data/lib/new_relic/agent/distributed_tracing/distributed_trace_intrinsics.rb +80 -0
- data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
- data/lib/new_relic/agent/{distributed_trace_payload.rb → distributed_tracing/distributed_trace_payload.rb} +19 -28
- data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +39 -0
- data/lib/new_relic/agent/distributed_tracing/trace_context.rb +246 -0
- data/lib/new_relic/agent/{trace_context_payload.rb → distributed_tracing/trace_context_payload.rb} +3 -11
- data/lib/new_relic/agent/error_collector.rb +3 -5
- data/lib/new_relic/agent/error_event_aggregator.rb +3 -1
- data/lib/new_relic/agent/external.rb +7 -7
- data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +1 -2
- data/lib/new_relic/agent/instrumentation/bunny.rb +1 -1
- data/lib/new_relic/agent/instrumentation/curb.rb +1 -1
- data/lib/new_relic/agent/instrumentation/excon.rb +1 -1
- data/lib/new_relic/agent/instrumentation/grape.rb +5 -10
- data/lib/new_relic/agent/instrumentation/http.rb +1 -1
- data/lib/new_relic/agent/instrumentation/httpclient.rb +1 -1
- data/lib/new_relic/agent/instrumentation/net.rb +1 -1
- data/lib/new_relic/agent/instrumentation/resque.rb +3 -0
- data/lib/new_relic/agent/instrumentation/typhoeus.rb +1 -1
- data/lib/new_relic/agent/logging.rb +13 -3
- data/lib/new_relic/agent/messaging.rb +5 -73
- data/lib/new_relic/agent/method_tracer.rb +3 -2
- data/lib/new_relic/agent/method_tracer_helpers.rb +1 -1
- data/lib/new_relic/agent/monitors.rb +27 -0
- data/lib/new_relic/agent/monitors/cross_app_monitor.rb +110 -0
- data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +27 -0
- data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +1 -1
- data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +2 -4
- data/lib/new_relic/agent/span_event_primitive.rb +25 -29
- data/lib/new_relic/agent/sql_sampler.rb +2 -2
- data/lib/new_relic/agent/supported_versions.rb +2 -2
- data/lib/new_relic/agent/tracer.rb +3 -3
- data/lib/new_relic/agent/transaction.rb +21 -28
- data/lib/new_relic/agent/transaction/distributed_tracer.rb +171 -0
- data/lib/new_relic/agent/transaction/distributed_tracing.rb +61 -69
- data/lib/new_relic/agent/transaction/external_request_segment.rb +8 -44
- data/lib/new_relic/agent/transaction/message_broker_segment.rb +3 -11
- data/lib/new_relic/agent/transaction/trace.rb +2 -4
- data/lib/new_relic/agent/transaction/trace_context.rb +88 -79
- data/lib/new_relic/agent/transaction/trace_node.rb +2 -5
- data/lib/new_relic/agent/transaction_error_primitive.rb +2 -2
- data/lib/new_relic/agent/transaction_event_primitive.rb +26 -29
- data/lib/new_relic/coerce.rb +5 -3
- data/lib/new_relic/constants.rb +34 -0
- data/lib/new_relic/noticed_error.rb +2 -4
- data/lib/new_relic/rack/browser_monitoring.rb +4 -0
- data/lib/new_relic/supportability_helper.rb +14 -0
- data/lib/new_relic/version.rb +1 -1
- data/lib/tasks/tests.rake +6 -1
- data/newrelic_rpm.gemspec +4 -2
- data/test/agent_helper.rb +21 -1
- metadata +49 -19
- data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
- data/lib/new_relic/agent/distributed_trace_intrinsics.rb +0 -90
- data/lib/new_relic/agent/distributed_trace_metrics.rb +0 -74
- data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -30
- data/lib/new_relic/agent/distributed_trace_transport_type.rb +0 -43
- data/lib/new_relic/agent/trace_context.rb +0 -244
- data/lib/new_relic/agent/trace_context_request_monitor.rb +0 -42
@@ -1,110 +0,0 @@
|
|
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 'digest'
|
6
|
-
require 'json'
|
7
|
-
|
8
|
-
require 'new_relic/agent/inbound_request_monitor'
|
9
|
-
require 'new_relic/agent/tracer'
|
10
|
-
require 'new_relic/agent/threading/agent_thread'
|
11
|
-
require 'new_relic/agent/cross_app_payload'
|
12
|
-
|
13
|
-
module NewRelic
|
14
|
-
module Agent
|
15
|
-
class CrossAppMonitor < InboundRequestMonitor
|
16
|
-
|
17
|
-
NEWRELIC_ID_HEADER = 'X-NewRelic-ID'.freeze
|
18
|
-
NEWRELIC_TXN_HEADER = 'X-NewRelic-Transaction'.freeze
|
19
|
-
NEWRELIC_APPDATA_HEADER = 'X-NewRelic-App-Data'.freeze
|
20
|
-
|
21
|
-
NEWRELIC_ID_HEADER_KEY = 'HTTP_X_NEWRELIC_ID'.freeze
|
22
|
-
NEWRELIC_TXN_HEADER_KEY = 'HTTP_X_NEWRELIC_TRANSACTION'.freeze
|
23
|
-
CONTENT_LENGTH_HEADER_KEY = 'HTTP_CONTENT_LENGTH'.freeze
|
24
|
-
|
25
|
-
def on_finished_configuring(events)
|
26
|
-
register_event_listeners(events)
|
27
|
-
end
|
28
|
-
|
29
|
-
def path_hash(txn_name, seed)
|
30
|
-
rotated = ((seed << 1) | (seed >> 31)) & 0xffffffff
|
31
|
-
app_name = NewRelic::Agent.config[:app_name].first
|
32
|
-
identifier = "#{app_name};#{txn_name}"
|
33
|
-
sprintf("%08x", rotated ^ hash_transaction_name(identifier))
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
# Expected sequence of events:
|
39
|
-
# :before_call will save our cross application request id to the thread
|
40
|
-
# :after_call will write our response headers/metrics and clean up the thread
|
41
|
-
def register_event_listeners(events)
|
42
|
-
NewRelic::Agent.logger.
|
43
|
-
debug("Wiring up Cross Application Tracing to events after finished configuring")
|
44
|
-
|
45
|
-
events.subscribe(:before_call) do |env| #THREAD_LOCAL_ACCESS
|
46
|
-
if id = decoded_id(env) and should_process_request?(id)
|
47
|
-
state = NewRelic::Agent::Tracer.state
|
48
|
-
|
49
|
-
if (transaction = state.current_transaction)
|
50
|
-
transaction_info = referring_transaction_info(state, env)
|
51
|
-
|
52
|
-
payload = CrossAppPayload.new(id, transaction, transaction_info)
|
53
|
-
transaction.cross_app_payload = payload
|
54
|
-
end
|
55
|
-
|
56
|
-
CrossAppTracing.assign_intrinsic_transaction_attributes state
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
events.subscribe(:after_call) do |env, (_status_code, headers, _body)| #THREAD_LOCAL_ACCESS
|
61
|
-
state = NewRelic::Agent::Tracer.state
|
62
|
-
|
63
|
-
insert_response_header(state, env, headers)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def referring_transaction_info(state, request_headers)
|
68
|
-
txn_header = request_headers[NEWRELIC_TXN_HEADER_KEY] or return
|
69
|
-
deserialize_header(txn_header, NEWRELIC_TXN_HEADER)
|
70
|
-
end
|
71
|
-
|
72
|
-
def insert_response_header(state, request_headers, response_headers)
|
73
|
-
txn = state.current_transaction
|
74
|
-
unless txn.nil? || txn.cross_app_payload.nil?
|
75
|
-
txn.freeze_name_and_execute_if_not_ignored do
|
76
|
-
content_length = content_length_from_request(request_headers)
|
77
|
-
set_response_headers(txn, response_headers, content_length)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def should_process_request? id
|
83
|
-
CrossAppTracing.cross_app_enabled? && CrossAppTracing.trusts?(id)
|
84
|
-
end
|
85
|
-
|
86
|
-
def set_response_headers(transaction, response_headers, content_length)
|
87
|
-
payload = obfuscator.obfuscate(
|
88
|
-
::JSON.dump(
|
89
|
-
transaction.cross_app_payload.as_json_array(content_length)))
|
90
|
-
|
91
|
-
response_headers[NEWRELIC_APPDATA_HEADER] = payload
|
92
|
-
end
|
93
|
-
|
94
|
-
def decoded_id(request)
|
95
|
-
encoded_id = request[NEWRELIC_ID_HEADER_KEY]
|
96
|
-
return "" if encoded_id.nil? || encoded_id.empty?
|
97
|
-
|
98
|
-
obfuscator.deobfuscate(encoded_id)
|
99
|
-
end
|
100
|
-
|
101
|
-
def content_length_from_request(request)
|
102
|
-
request[CONTENT_LENGTH_HEADER_KEY] || -1
|
103
|
-
end
|
104
|
-
|
105
|
-
def hash_transaction_name(identifier)
|
106
|
-
Digest::MD5.digest(identifier).unpack("@12N").first & 0xffffffff
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
@@ -1,90 +0,0 @@
|
|
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/distributed_trace_transport_type'
|
6
|
-
|
7
|
-
module NewRelic
|
8
|
-
module Agent
|
9
|
-
module DistributedTraceIntrinsics
|
10
|
-
extend self
|
11
|
-
|
12
|
-
# Intrinsic Keys
|
13
|
-
PARENT_TYPE_KEY = "parent.type".freeze
|
14
|
-
PARENT_APP_KEY = "parent.app".freeze
|
15
|
-
PARENT_ACCOUNT_ID_KEY = "parent.account".freeze
|
16
|
-
PARENT_TRANSPORT_TYPE_KEY = "parent.transportType".freeze
|
17
|
-
PARENT_TRANSPORT_DURATION_KEY = "parent.transportDuration".freeze
|
18
|
-
GUID_KEY = "guid".freeze
|
19
|
-
TRACE_ID_KEY = "traceId".freeze
|
20
|
-
PARENT_TRANSACTION_ID_KEY = "parentId".freeze
|
21
|
-
PARENT_SPAN_ID_KEY = "parentSpanId".freeze
|
22
|
-
SAMPLED_KEY = "sampled".freeze
|
23
|
-
|
24
|
-
INTRINSIC_KEYS = [
|
25
|
-
PARENT_TYPE_KEY,
|
26
|
-
PARENT_APP_KEY,
|
27
|
-
PARENT_ACCOUNT_ID_KEY,
|
28
|
-
PARENT_TRANSPORT_TYPE_KEY,
|
29
|
-
PARENT_TRANSPORT_DURATION_KEY,
|
30
|
-
GUID_KEY,
|
31
|
-
TRACE_ID_KEY,
|
32
|
-
PARENT_TRANSACTION_ID_KEY,
|
33
|
-
PARENT_SPAN_ID_KEY,
|
34
|
-
SAMPLED_KEY
|
35
|
-
].freeze
|
36
|
-
|
37
|
-
# This method extracts intrinsics from the transaction_payload and
|
38
|
-
# inserts them into the specified destination.
|
39
|
-
def copy_to_hash transaction_payload, destination
|
40
|
-
return unless enabled?
|
41
|
-
INTRINSIC_KEYS.each do |key|
|
42
|
-
value = transaction_payload[key]
|
43
|
-
destination[key] = value unless value.nil?
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# This method extracts intrinsics from the transaction_payload and
|
48
|
-
# inserts them as intrinsics in the specified transaction_attributes
|
49
|
-
def copy_to_attributes transaction_payload, destination
|
50
|
-
return unless enabled?
|
51
|
-
INTRINSIC_KEYS.each do |key|
|
52
|
-
next unless transaction_payload.key? key
|
53
|
-
destination.add_intrinsic_attribute key, transaction_payload[key]
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# This method takes all distributed tracing intrinsics from the transaction
|
58
|
-
# and the distributed_trace_payload, and populates them into the destination
|
59
|
-
def copy_from_transaction transaction, distributed_trace_payload, destination
|
60
|
-
destination[GUID_KEY] = transaction.guid
|
61
|
-
destination[SAMPLED_KEY] = transaction.sampled?
|
62
|
-
destination[TRACE_ID_KEY] = transaction.trace_id
|
63
|
-
|
64
|
-
if transaction.parent_span_id
|
65
|
-
destination[PARENT_SPAN_ID_KEY] = transaction.parent_span_id
|
66
|
-
end
|
67
|
-
|
68
|
-
if distributed_trace_payload
|
69
|
-
destination[PARENT_TYPE_KEY] = distributed_trace_payload.parent_type
|
70
|
-
destination[PARENT_APP_KEY] = distributed_trace_payload.parent_app_id
|
71
|
-
destination[PARENT_ACCOUNT_ID_KEY] = distributed_trace_payload.parent_account_id
|
72
|
-
destination[PARENT_TRANSPORT_TYPE_KEY] = DistributedTraceTransportType.from distributed_trace_payload.caller_transport_type
|
73
|
-
|
74
|
-
destination[PARENT_TRANSPORT_DURATION_KEY] = transaction.calculate_transport_duration distributed_trace_payload
|
75
|
-
|
76
|
-
if transaction.parent_transaction_id
|
77
|
-
destination[PARENT_TRANSACTION_ID_KEY] = transaction.parent_transaction_id
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
private
|
83
|
-
|
84
|
-
def enabled?
|
85
|
-
return Agent.config[:'distributed_tracing.enabled']
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
@@ -1,74 +0,0 @@
|
|
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
|
-
|
6
|
-
module NewRelic
|
7
|
-
module Agent
|
8
|
-
module DistributedTraceMetrics
|
9
|
-
extend self
|
10
|
-
|
11
|
-
ALL_SUFFIX = "all".freeze
|
12
|
-
ALL_WEB_SUFFIX = "allWeb".freeze
|
13
|
-
ALL_OTHER_SUFFIX = "allOther".freeze
|
14
|
-
|
15
|
-
DURATION_BY_CALLER_UNKOWN_PREFIX = "DurationByCaller/Unknown/Unknown/Unknown/Unknown".freeze
|
16
|
-
ERRORS_BY_CALLER_UNKOWN_PREFIX = "ErrorsByCaller/Unknown/Unknown/Unknown/Unknown".freeze
|
17
|
-
|
18
|
-
def transaction_type_suffix
|
19
|
-
if Transaction.recording_web_transaction?
|
20
|
-
ALL_WEB_SUFFIX
|
21
|
-
else
|
22
|
-
ALL_OTHER_SUFFIX
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def record_metrics_for_transaction transaction
|
27
|
-
return unless Agent.config[:'distributed_tracing.enabled']
|
28
|
-
payload = if transaction.distributed_trace_payload
|
29
|
-
transaction.distributed_trace_payload
|
30
|
-
elsif transaction.trace_state_payload
|
31
|
-
transaction.trace_state_payload
|
32
|
-
end
|
33
|
-
|
34
|
-
record_caller_by_duration_metrics transaction, payload
|
35
|
-
record_transport_duration_metrics transaction, payload
|
36
|
-
record_errors_by_caller_metrics transaction, payload
|
37
|
-
end
|
38
|
-
|
39
|
-
def record_caller_by_duration_metrics transaction, payload
|
40
|
-
prefix = if payload
|
41
|
-
"DurationByCaller/#{payload.parent_type}/#{payload.parent_account_id}/#{payload.parent_app_id}/#{payload.caller_transport_type}"
|
42
|
-
else
|
43
|
-
DURATION_BY_CALLER_UNKOWN_PREFIX
|
44
|
-
end
|
45
|
-
|
46
|
-
transaction.metrics.record_unscoped "#{prefix}/#{ALL_SUFFIX}", transaction.duration
|
47
|
-
transaction.metrics.record_unscoped "#{prefix}/#{transaction_type_suffix}", transaction.duration
|
48
|
-
end
|
49
|
-
|
50
|
-
def record_transport_duration_metrics transaction, payload
|
51
|
-
return unless payload
|
52
|
-
|
53
|
-
prefix = "TransportDuration/#{payload.parent_type}/#{payload.parent_account_id}/#{payload.parent_app_id}/#{payload.caller_transport_type}"
|
54
|
-
duration = transaction.calculate_transport_duration payload
|
55
|
-
|
56
|
-
transaction.metrics.record_unscoped "#{prefix}/#{ALL_SUFFIX}", duration
|
57
|
-
transaction.metrics.record_unscoped "#{prefix}/#{transaction_type_suffix}", duration
|
58
|
-
end
|
59
|
-
|
60
|
-
def record_errors_by_caller_metrics transaction, payload
|
61
|
-
return unless transaction.exceptions.size > 0
|
62
|
-
|
63
|
-
prefix = if payload
|
64
|
-
"ErrorsByCaller/#{payload.parent_type}/#{payload.parent_account_id}/#{payload.parent_app_id}/#{payload.caller_transport_type}"
|
65
|
-
else
|
66
|
-
ERRORS_BY_CALLER_UNKOWN_PREFIX
|
67
|
-
end
|
68
|
-
|
69
|
-
transaction.metrics.record_unscoped "#{prefix}/#{ALL_SUFFIX}", 1
|
70
|
-
transaction.metrics.record_unscoped "#{prefix}/#{transaction_type_suffix}", 1
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
@@ -1,30 +0,0 @@
|
|
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/distributed_trace_transport_type'
|
6
|
-
require 'new_relic/agent/inbound_request_monitor'
|
7
|
-
require 'new_relic/agent/cross_app_tracing'
|
8
|
-
|
9
|
-
module NewRelic
|
10
|
-
module Agent
|
11
|
-
class DistributedTraceMonitor < InboundRequestMonitor
|
12
|
-
def on_finished_configuring(events)
|
13
|
-
return unless NewRelic::Agent.config[:'distributed_tracing.enabled']
|
14
|
-
events.subscribe(:before_call, &method(:on_before_call))
|
15
|
-
end
|
16
|
-
|
17
|
-
NEWRELIC_TRACE_KEY = 'HTTP_NEWRELIC'
|
18
|
-
|
19
|
-
def on_before_call(request)
|
20
|
-
return unless NewRelic::Agent.config[:'distributed_tracing.enabled']
|
21
|
-
return unless payload = request[NEWRELIC_TRACE_KEY]
|
22
|
-
return unless txn = Tracer.current_transaction
|
23
|
-
|
24
|
-
if txn.accept_distributed_trace_payload payload
|
25
|
-
txn.distributed_trace_payload.caller_transport_type = DistributedTraceTransportType.for_rack_request(request)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,43 +0,0 @@
|
|
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
|
@@ -1,244 +0,0 @@
|
|
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
|