newrelic_rpm 6.4.0.356 → 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/.gitignore +1 -0
- data/.travis.yml +50 -5
- data/CHANGELOG.md +218 -0
- data/Guardfile +16 -1
- data/lib/new_relic/agent.rb +86 -5
- data/lib/new_relic/agent/agent.rb +124 -56
- data/lib/new_relic/agent/agent_logger.rb +4 -0
- data/lib/new_relic/agent/attribute_filter.rb +7 -7
- data/lib/new_relic/agent/attributes.rb +150 -0
- data/lib/new_relic/agent/autostart.rb +19 -14
- data/lib/new_relic/agent/configuration/default_source.rb +133 -14
- data/lib/new_relic/agent/configuration/event_harvest_config.rb +45 -0
- data/lib/new_relic/agent/configuration/manager.rb +13 -9
- data/lib/new_relic/agent/configuration/server_source.rb +34 -10
- data/lib/new_relic/agent/configuration/yaml_source.rb +11 -6
- data/lib/new_relic/agent/connect/request_builder.rb +8 -15
- data/lib/new_relic/agent/connect/response_handler.rb +1 -1
- data/lib/new_relic/agent/database.rb +1 -2
- data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +2 -2
- data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +8 -8
- 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} +23 -99
- 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/distributed_tracing/trace_context_payload.rb +126 -0
- data/lib/new_relic/agent/error_collector.rb +5 -7
- data/lib/new_relic/agent/error_event_aggregator.rb +5 -2
- data/lib/new_relic/agent/error_trace_aggregator.rb +1 -0
- data/lib/new_relic/agent/event_aggregator.rb +26 -32
- data/lib/new_relic/agent/external.rb +7 -7
- data/lib/new_relic/agent/guid_generator.rb +28 -0
- data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +1 -2
- data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +24 -38
- data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +2 -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/rails_notifications/action_cable.rb +2 -3
- 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/javascript_instrumentor.rb +1 -1
- data/lib/new_relic/agent/logging.rb +139 -0
- 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} +3 -3
- data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +2 -4
- data/lib/new_relic/agent/new_relic_service.rb +7 -5
- data/lib/new_relic/agent/priority_sampled_buffer.rb +2 -0
- data/lib/new_relic/agent/span_event_aggregator.rb +2 -4
- data/lib/new_relic/agent/span_event_primitive.rb +43 -25
- data/lib/new_relic/agent/sql_sampler.rb +3 -3
- data/lib/new_relic/agent/supported_versions.rb +2 -2
- data/lib/new_relic/agent/tracer.rb +35 -3
- data/lib/new_relic/agent/transaction.rb +37 -36
- data/lib/new_relic/agent/transaction/abstract_segment.rb +2 -2
- data/lib/new_relic/agent/transaction/distributed_tracer.rb +171 -0
- data/lib/new_relic/agent/transaction/distributed_tracing.rb +57 -146
- data/lib/new_relic/agent/transaction/external_request_segment.rb +8 -31
- data/lib/new_relic/agent/transaction/message_broker_segment.rb +3 -11
- data/lib/new_relic/agent/transaction/segment.rb +7 -1
- data/lib/new_relic/agent/transaction/trace.rb +2 -4
- data/lib/new_relic/agent/transaction/trace_context.rb +168 -0
- data/lib/new_relic/agent/transaction/trace_node.rb +10 -8
- data/lib/new_relic/agent/transaction_error_primitive.rb +5 -12
- data/lib/new_relic/agent/transaction_event_primitive.rb +28 -39
- data/lib/new_relic/agent/transaction_event_recorder.rb +3 -3
- data/lib/new_relic/cli/commands/deployments.rb +1 -1
- data/lib/new_relic/coerce.rb +31 -6
- data/lib/new_relic/constants.rb +34 -0
- data/lib/new_relic/control/instance_methods.rb +10 -1
- data/lib/new_relic/dependency_detection.rb +4 -4
- data/lib/new_relic/noticed_error.rb +10 -8
- 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 +16 -5
- data/test/agent_helper.rb +115 -9
- data/true +0 -0
- metadata +97 -30
- data/lib/new_relic/agent/configuration/event_data.rb +0 -39
- data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
- data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -40
- data/lib/new_relic/agent/transaction/attributes.rb +0 -154
- data/lib/tasks/versions.html.erb +0 -28
- data/lib/tasks/versions.postface.html +0 -8
- data/lib/tasks/versions.preface.html +0 -9
- data/lib/tasks/versions.rake +0 -65
- data/lib/tasks/versions.txt.erb +0 -14
@@ -5,13 +5,13 @@
|
|
5
5
|
require 'new_relic/agent/instrumentation/queue_time'
|
6
6
|
require 'new_relic/agent/transaction_metrics'
|
7
7
|
require 'new_relic/agent/method_tracer_helpers'
|
8
|
-
require 'new_relic/agent/
|
8
|
+
require 'new_relic/agent/attributes'
|
9
9
|
require 'new_relic/agent/transaction/request_attributes'
|
10
10
|
require 'new_relic/agent/transaction/tracing'
|
11
|
-
require 'new_relic/agent/transaction/
|
12
|
-
require 'new_relic/agent/cross_app_tracing'
|
11
|
+
require 'new_relic/agent/transaction/distributed_tracer'
|
13
12
|
require 'new_relic/agent/transaction_time_aggregator'
|
14
13
|
require 'new_relic/agent/deprecator'
|
14
|
+
require 'new_relic/agent/guid_generator'
|
15
15
|
|
16
16
|
module NewRelic
|
17
17
|
module Agent
|
@@ -21,8 +21,6 @@ module NewRelic
|
|
21
21
|
# @api public
|
22
22
|
class Transaction
|
23
23
|
include Tracing
|
24
|
-
include DistributedTracing
|
25
|
-
include CrossAppTracing
|
26
24
|
|
27
25
|
# for nested transactions
|
28
26
|
SUBTRANSACTION_PREFIX = 'Nested/'.freeze
|
@@ -59,7 +57,8 @@ module NewRelic
|
|
59
57
|
:process_cpu_start,
|
60
58
|
:http_response_code,
|
61
59
|
:response_content_length,
|
62
|
-
:response_content_type
|
60
|
+
:response_content_type,
|
61
|
+
:parent_span_id
|
63
62
|
|
64
63
|
attr_reader :guid,
|
65
64
|
:metrics,
|
@@ -242,7 +241,7 @@ module NewRelic
|
|
242
241
|
|
243
242
|
@exceptions = {}
|
244
243
|
@metrics = TransactionMetrics.new
|
245
|
-
@guid = generate_guid
|
244
|
+
@guid = NewRelic::Agent::GuidGenerator.generate_guid
|
246
245
|
|
247
246
|
@ignore_this_transaction = false
|
248
247
|
@ignore_apdex = options.fetch(:ignore_apdex, false)
|
@@ -265,6 +264,10 @@ module NewRelic
|
|
265
264
|
end
|
266
265
|
end
|
267
266
|
|
267
|
+
def distributed_tracer
|
268
|
+
@distributed_tracer ||= DistributedTracer.new(self)
|
269
|
+
end
|
270
|
+
|
268
271
|
def sampled?
|
269
272
|
return unless Agent.config[:'distributed_tracing.enabled']
|
270
273
|
if @sampled.nil?
|
@@ -273,12 +276,16 @@ module NewRelic
|
|
273
276
|
@sampled
|
274
277
|
end
|
275
278
|
|
279
|
+
def trace_id
|
280
|
+
@trace_id ||= NewRelic::Agent::GuidGenerator.generate_guid 32
|
281
|
+
end
|
282
|
+
|
283
|
+
def trace_id=(value)
|
284
|
+
@trace_id = value
|
285
|
+
end
|
286
|
+
|
276
287
|
def priority
|
277
|
-
|
278
|
-
@priority = rand.round(6)
|
279
|
-
@priority += 1 if sampled?
|
280
|
-
end
|
281
|
-
@priority
|
288
|
+
@priority ||= (sampled? ? 1.0 + rand : rand).round(NewRelic::PRIORITY_PRECISION)
|
282
289
|
end
|
283
290
|
|
284
291
|
def referer
|
@@ -349,8 +356,10 @@ module NewRelic
|
|
349
356
|
end
|
350
357
|
|
351
358
|
def best_name
|
352
|
-
@frozen_name
|
353
|
-
|
359
|
+
@frozen_name ||
|
360
|
+
@overridden_name ||
|
361
|
+
@default_name ||
|
362
|
+
NewRelic::Agent::UNKNOWN_METRIC
|
354
363
|
end
|
355
364
|
|
356
365
|
# For common interface with Trace
|
@@ -523,8 +532,7 @@ module NewRelic
|
|
523
532
|
record_total_time_metrics
|
524
533
|
record_apdex unless ignore_apdex?
|
525
534
|
record_queue_time
|
526
|
-
|
527
|
-
record_distributed_tracing_metrics
|
535
|
+
distributed_tracer.record_metrics
|
528
536
|
|
529
537
|
record_exceptions
|
530
538
|
record_transaction_event
|
@@ -576,11 +584,7 @@ module NewRelic
|
|
576
584
|
attributes.add_intrinsic_attribute(:synthetics_monitor_id, synthetics_monitor_id)
|
577
585
|
end
|
578
586
|
|
579
|
-
|
580
|
-
assign_distributed_trace_intrinsics
|
581
|
-
elsif is_cross_app?
|
582
|
-
assign_cross_app_intrinsics
|
583
|
-
end
|
587
|
+
distributed_tracer.assign_intrinsics
|
584
588
|
end
|
585
589
|
|
586
590
|
def calculate_gc_time
|
@@ -588,6 +592,16 @@ module NewRelic
|
|
588
592
|
NewRelic::Agent::StatsEngine::GCProfiler.record_delta(gc_start_snapshot, gc_stop_snapshot)
|
589
593
|
end
|
590
594
|
|
595
|
+
# This method returns transport_duration in seconds. Transport duration
|
596
|
+
# is stored in milliseconds on the payload, but it's needed in seconds
|
597
|
+
# for metrics and intrinsics.
|
598
|
+
def calculate_transport_duration distributed_trace_payload
|
599
|
+
return unless distributed_trace_payload
|
600
|
+
|
601
|
+
duration = (start_time.to_f * 1000 - distributed_trace_payload.timestamp) / 1000
|
602
|
+
duration < 0 ? 0 : duration
|
603
|
+
end
|
604
|
+
|
591
605
|
# The summary metrics recorded by this method all end up with a duration
|
592
606
|
# equal to the transaction itself, and an exclusive time of zero.
|
593
607
|
def record_summary_metrics(outermost_node_name)
|
@@ -614,14 +628,13 @@ module NewRelic
|
|
614
628
|
:priority => priority
|
615
629
|
}
|
616
630
|
|
617
|
-
|
618
|
-
append_distributed_trace_info(@payload)
|
631
|
+
distributed_tracer.append_payload(@payload)
|
619
632
|
append_apdex_perf_zone(@payload)
|
620
633
|
append_synthetics_to(@payload)
|
621
634
|
end
|
622
635
|
|
623
636
|
def include_guid?
|
624
|
-
is_cross_app? || is_synthetics_request?
|
637
|
+
distributed_tracer.is_cross_app? || is_synthetics_request?
|
625
638
|
end
|
626
639
|
|
627
640
|
def is_synthetics_request?
|
@@ -905,18 +918,6 @@ module NewRelic
|
|
905
918
|
def sql_sampler
|
906
919
|
agent.sql_sampler
|
907
920
|
end
|
908
|
-
|
909
|
-
HEX_DIGITS = (0..15).map{|i| i.to_s(16)}
|
910
|
-
GUID_LENGTH = 16
|
911
|
-
|
912
|
-
# generate a random 64 bit uuid
|
913
|
-
def generate_guid
|
914
|
-
guid = ''
|
915
|
-
GUID_LENGTH.times do |a|
|
916
|
-
guid << HEX_DIGITS[rand(16)]
|
917
|
-
end
|
918
|
-
guid
|
919
|
-
end
|
920
921
|
end
|
921
922
|
end
|
922
923
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
|
4
4
|
|
5
5
|
require 'new_relic/agent/range_extensions'
|
6
|
-
require '
|
6
|
+
require 'new_relic/agent/guid_generator'
|
7
7
|
|
8
8
|
module NewRelic
|
9
9
|
module Agent
|
@@ -27,7 +27,7 @@ module NewRelic
|
|
27
27
|
def initialize name=nil, start_time=nil
|
28
28
|
@name = name
|
29
29
|
@transaction = nil
|
30
|
-
@guid =
|
30
|
+
@guid = NewRelic::Agent::GuidGenerator.generate_guid
|
31
31
|
@parent = nil
|
32
32
|
@params = nil
|
33
33
|
@start_time = start_time if start_time
|
@@ -0,0 +1,171 @@
|
|
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
|
+
# frozen_string_literal: true
|
5
|
+
|
6
|
+
require 'new_relic/agent/transaction/trace_context'
|
7
|
+
require 'new_relic/agent/transaction/distributed_tracing'
|
8
|
+
require 'new_relic/agent/distributed_tracing/cross_app_tracing'
|
9
|
+
|
10
|
+
module NewRelic
|
11
|
+
module Agent
|
12
|
+
class Transaction
|
13
|
+
class DistributedTracer
|
14
|
+
include NewRelic::Agent::CrossAppTracing
|
15
|
+
include DistributedTracing
|
16
|
+
include TraceContext
|
17
|
+
|
18
|
+
attr_reader :transaction
|
19
|
+
attr_accessor :parent_transaction_id
|
20
|
+
|
21
|
+
def parent_guid
|
22
|
+
if trace_context_header_data
|
23
|
+
trace_context_header_data.parent_id
|
24
|
+
elsif distributed_trace_payload
|
25
|
+
distributed_trace_payload.id
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def accept_incoming_request request, transport_type=nil
|
30
|
+
accept_incoming_transport_type request, transport_type
|
31
|
+
if trace_parent_header_present? request
|
32
|
+
accept_trace_context_incoming_request request
|
33
|
+
else
|
34
|
+
accept_distributed_tracing_incoming_request request
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def caller_transport_type
|
39
|
+
@caller_transport_type ||= "Unknown"
|
40
|
+
end
|
41
|
+
|
42
|
+
def accept_transport_type_from_api value
|
43
|
+
@caller_transport_type = DistributedTraceTransportType.from value
|
44
|
+
end
|
45
|
+
|
46
|
+
def accept_incoming_transport_type request, transport_type
|
47
|
+
if transport_type.to_s == NewRelic::EMPTY_STR
|
48
|
+
@caller_transport_type = DistributedTraceTransportType.for_rack_request request
|
49
|
+
else
|
50
|
+
@caller_transport_type = DistributedTraceTransportType.from transport_type
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def initialize transaction
|
55
|
+
@transaction = transaction
|
56
|
+
end
|
57
|
+
|
58
|
+
def record_metrics
|
59
|
+
record_cross_app_metrics
|
60
|
+
DistributedTraceMetrics.record_metrics_for_transaction transaction
|
61
|
+
end
|
62
|
+
|
63
|
+
def append_payload payload
|
64
|
+
append_cat_info payload
|
65
|
+
DistributedTraceIntrinsics.copy_from_transaction \
|
66
|
+
transaction,
|
67
|
+
trace_state_payload || distributed_trace_payload,
|
68
|
+
payload
|
69
|
+
end
|
70
|
+
|
71
|
+
def insert_headers request
|
72
|
+
insert_trace_context_header request
|
73
|
+
insert_distributed_trace_header request
|
74
|
+
insert_cross_app_header request
|
75
|
+
end
|
76
|
+
|
77
|
+
def consume_message_headers headers, tracer_state, transport_type
|
78
|
+
consume_message_distributed_tracing_headers headers, transport_type
|
79
|
+
consume_message_cross_app_tracing_headers headers, tracer_state
|
80
|
+
consume_message_synthetics_headers headers
|
81
|
+
rescue => e
|
82
|
+
NewRelic::Agent.logger.error "Error in consume_message_headers", e
|
83
|
+
end
|
84
|
+
|
85
|
+
def assign_intrinsics
|
86
|
+
if Agent.config[:'distributed_tracing.enabled']
|
87
|
+
DistributedTraceIntrinsics.copy_to_attributes transaction.payload, transaction.attributes
|
88
|
+
elsif is_cross_app?
|
89
|
+
assign_cross_app_intrinsics
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def insert_distributed_trace_header request
|
94
|
+
return unless Agent.config[:'distributed_tracing.enabled']
|
95
|
+
return if Agent.config[:'exclude_newrelic_header']
|
96
|
+
payload = create_distributed_trace_payload
|
97
|
+
request[NewRelic::NEWRELIC_KEY] = payload.http_safe if payload
|
98
|
+
end
|
99
|
+
|
100
|
+
def insert_cat_headers headers
|
101
|
+
return unless CrossAppTracing.cross_app_enabled?
|
102
|
+
@is_cross_app_caller = true
|
103
|
+
insert_message_headers headers,
|
104
|
+
transaction.guid,
|
105
|
+
cat_trip_id,
|
106
|
+
cat_path_hash,
|
107
|
+
transaction.raw_synthetics_header
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
def consume_message_synthetics_headers headers
|
113
|
+
synthetics_header = headers[CrossAppTracing::NR_MESSAGE_BROKER_SYNTHETICS_HEADER]
|
114
|
+
if synthetics_header and
|
115
|
+
incoming_payload = ::JSON.load(deobfuscate(synthetics_header)) and
|
116
|
+
SyntheticsMonitor.is_valid_payload?(incoming_payload) and
|
117
|
+
SyntheticsMonitor.is_supported_version?(incoming_payload) and
|
118
|
+
SyntheticsMonitor.is_trusted?(incoming_payload)
|
119
|
+
|
120
|
+
transaction.raw_synthetics_header = synthetics_header
|
121
|
+
transaction.synthetics_payload = incoming_payload
|
122
|
+
end
|
123
|
+
rescue => e
|
124
|
+
NewRelic::Agent.logger.error "Error in consume_message_synthetics_header", e
|
125
|
+
end
|
126
|
+
|
127
|
+
def consume_message_distributed_tracing_headers headers, transport_type
|
128
|
+
return unless Agent.config[:'distributed_tracing.enabled']
|
129
|
+
|
130
|
+
accept_incoming_transport_type headers, transport_type
|
131
|
+
|
132
|
+
newrelic_trace_key = NewRelic::CANDIDATE_NEWRELIC_KEYS.detect do |key|
|
133
|
+
headers.has_key?(key)
|
134
|
+
end
|
135
|
+
return unless newrelic_trace_key && (payload = headers[newrelic_trace_key])
|
136
|
+
|
137
|
+
accept_distributed_trace_payload payload
|
138
|
+
end
|
139
|
+
|
140
|
+
def consume_message_cross_app_tracing_headers headers, tracer_state
|
141
|
+
return unless CrossAppTracing.cross_app_enabled?
|
142
|
+
return unless CrossAppTracing.message_has_crossapp_request_header?(headers)
|
143
|
+
|
144
|
+
accept_cross_app_payload headers, tracer_state
|
145
|
+
CrossAppTracing.assign_intrinsic_transaction_attributes tracer_state
|
146
|
+
end
|
147
|
+
|
148
|
+
def accept_cross_app_payload headers, tracer_state
|
149
|
+
encoded_id = headers[CrossAppTracing::NR_MESSAGE_BROKER_ID_HEADER]
|
150
|
+
decoded_id = encoded_id.nil? ? EMPTY_STRING : deobfuscate(encoded_id)
|
151
|
+
|
152
|
+
return unless CrossAppTracing.trusted_valid_cross_app_id?(decoded_id)
|
153
|
+
txn_header = headers[CrossAppTracing::NR_MESSAGE_BROKER_TXN_HEADER]
|
154
|
+
txn_info = ::JSON.load(deobfuscate(txn_header))
|
155
|
+
payload = CrossAppPayload.new(decoded_id, transaction, txn_info)
|
156
|
+
|
157
|
+
@cross_app_payload = payload
|
158
|
+
rescue => e
|
159
|
+
NewRelic::Agent.logger.debug("Failure deserializing encoded header in #{self.class}, #{e.class}, #{e.message}")
|
160
|
+
nil
|
161
|
+
end
|
162
|
+
|
163
|
+
def deobfuscate message
|
164
|
+
CrossAppTracing.obfuscator.deobfuscate message
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
@@ -1,39 +1,68 @@
|
|
1
1
|
# encoding: utf-8
|
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
|
+
# frozen_string_literal: true
|
4
5
|
|
5
|
-
require 'new_relic/agent/distributed_trace_payload'
|
6
|
+
require 'new_relic/agent/distributed_tracing/distributed_trace_payload'
|
7
|
+
require 'new_relic/agent/distributed_tracing/distributed_trace_intrinsics'
|
8
|
+
require 'new_relic/agent/distributed_tracing/distributed_trace_metrics'
|
6
9
|
|
7
10
|
module NewRelic
|
8
11
|
module Agent
|
9
12
|
class Transaction
|
10
13
|
module DistributedTracing
|
11
14
|
attr_accessor :distributed_trace_payload
|
15
|
+
attr_writer :distributed_trace_payload_created
|
16
|
+
|
17
|
+
SUPPORTABILITY_DISTRIBUTED_TRACE = "Supportability/DistributedTrace"
|
18
|
+
CREATE_PREFIX = "#{SUPPORTABILITY_DISTRIBUTED_TRACE}/CreatePayload"
|
19
|
+
ACCEPT_PREFIX = "#{SUPPORTABILITY_DISTRIBUTED_TRACE}/AcceptPayload"
|
20
|
+
IGNORE_PREFIX = "#{ACCEPT_PREFIX}/Ignored"
|
21
|
+
|
22
|
+
CREATE_SUCCESS_METRIC = "#{CREATE_PREFIX}/Success"
|
23
|
+
CREATE_EXCEPTION_METRIC = "#{CREATE_PREFIX}/Exception"
|
24
|
+
ACCEPT_SUCCESS_METRIC = "#{ACCEPT_PREFIX}/Success"
|
25
|
+
ACCEPT_EXCEPTION_METRIC = "#{ACCEPT_PREFIX}/Exception"
|
26
|
+
ACCEPT_PARSE_EXCEPTION_METRIC = "#{ACCEPT_PREFIX}/ParseException"
|
27
|
+
|
28
|
+
IGNORE_ACCEPT_AFTER_CREATE_METRIC = "#{IGNORE_PREFIX}/CreateBeforeAccept"
|
29
|
+
IGNORE_MULTIPLE_ACCEPT_METRIC = "#{IGNORE_PREFIX}/Multiple"
|
30
|
+
IGNORE_ACCEPT_NULL_METRIC = "#{IGNORE_PREFIX}/Null"
|
31
|
+
IGNORE_ACCEPT_MAJOR_VERSION_METRIC = "#{IGNORE_PREFIX}/MajorVersion"
|
32
|
+
IGNORE_ACCEPT_UNTRUSTED_ACCOUNT_METRIC = "#{IGNORE_PREFIX}/UntrustedAccount"
|
33
|
+
|
34
|
+
LBRACE = "{"
|
35
|
+
NULL_PAYLOAD = 'null'
|
12
36
|
|
13
|
-
|
14
|
-
|
37
|
+
NEWRELIC_TRACE_KEY = "HTTP_NEWRELIC"
|
38
|
+
|
39
|
+
def accept_distributed_tracing_incoming_request request
|
40
|
+
return unless Agent.config[:'distributed_tracing.enabled']
|
41
|
+
return unless payload = request[NEWRELIC_TRACE_KEY]
|
42
|
+
|
43
|
+
accept_distributed_trace_payload payload
|
15
44
|
end
|
16
45
|
|
17
|
-
|
18
|
-
|
46
|
+
def distributed_trace_payload_created?
|
47
|
+
@distributed_trace_payload_created ||= false
|
48
|
+
end
|
19
49
|
|
20
50
|
def create_distributed_trace_payload
|
21
51
|
return unless Agent.config[:'distributed_tracing.enabled']
|
22
|
-
|
23
|
-
|
24
|
-
|
52
|
+
|
53
|
+
@distributed_trace_payload_created = true
|
54
|
+
payload = DistributedTracePayload.for_transaction transaction
|
55
|
+
NewRelic::Agent.increment_metric CREATE_SUCCESS_METRIC
|
25
56
|
payload
|
26
57
|
rescue => e
|
27
|
-
NewRelic::Agent.increment_metric
|
58
|
+
NewRelic::Agent.increment_metric CREATE_EXCEPTION_METRIC
|
28
59
|
NewRelic::Agent.logger.warn "Failed to create distributed trace payload", e
|
29
60
|
nil
|
30
61
|
end
|
31
62
|
|
32
|
-
SUPPORTABILITY_ACCEPT_PAYLOAD_SUCCESS = "Supportability/DistributedTrace/AcceptPayload/Success".freeze
|
33
|
-
SUPPORTABILITY_ACCEPT_PAYLOAD_EXCEPTION = "Supportability/DistributedTrace/AcceptPayload/Exception".freeze
|
34
|
-
|
35
63
|
def accept_distributed_trace_payload payload
|
36
64
|
return unless Agent.config[:'distributed_tracing.enabled']
|
65
|
+
|
37
66
|
return false if check_payload_ignored(payload)
|
38
67
|
return false unless check_payload_present(payload)
|
39
68
|
return false unless payload = decode_payload(payload)
|
@@ -43,95 +72,37 @@ module NewRelic
|
|
43
72
|
|
44
73
|
assign_payload_and_sampling_params(payload)
|
45
74
|
|
46
|
-
NewRelic::Agent.increment_metric
|
75
|
+
NewRelic::Agent.increment_metric ACCEPT_SUCCESS_METRIC
|
47
76
|
true
|
48
77
|
rescue => e
|
49
|
-
NewRelic::Agent.increment_metric
|
78
|
+
NewRelic::Agent.increment_metric ACCEPT_EXCEPTION_METRIC
|
50
79
|
NewRelic::Agent.logger.warn "Failed to accept distributed trace payload", e
|
51
80
|
false
|
52
81
|
end
|
53
82
|
|
54
|
-
def trace_id
|
55
|
-
if distributed_trace_payload
|
56
|
-
distributed_trace_payload.trace_id
|
57
|
-
else
|
58
|
-
guid
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def parent_id
|
63
|
-
# The payload comes from our parent transaction, so its ID
|
64
|
-
# is our parent ID.
|
65
|
-
#
|
66
|
-
distributed_trace_payload && distributed_trace_payload.transaction_id
|
67
|
-
end
|
68
|
-
|
69
|
-
def distributed_trace_payload_created?
|
70
|
-
@distributed_trace_payload_created ||= false
|
71
|
-
end
|
72
|
-
|
73
|
-
attr_writer :distributed_trace_payload_created
|
74
|
-
|
75
|
-
def append_distributed_trace_info transaction_payload
|
76
|
-
return unless Agent.config[:'distributed_tracing.enabled']
|
77
|
-
if distributed_trace_payload
|
78
|
-
distributed_trace_payload.assign_intrinsics self, transaction_payload
|
79
|
-
else
|
80
|
-
DistributedTracePayload.assign_initial_intrinsics self, transaction_payload
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def assign_distributed_trace_intrinsics
|
85
|
-
return unless Agent.config[:'distributed_tracing.enabled']
|
86
|
-
DistributedTracePayload::INTRINSIC_KEYS.each do |key|
|
87
|
-
next unless @payload.key? key
|
88
|
-
attributes.add_intrinsic_attribute key, @payload[key]
|
89
|
-
end
|
90
|
-
nil
|
91
|
-
end
|
92
|
-
|
93
|
-
# This method returns transport_duration in seconds. Transport duration
|
94
|
-
# is stored in milliseconds on the payload, but it's needed in seconds
|
95
|
-
# for metrics and intrinsics.
|
96
|
-
def transport_duration
|
97
|
-
return unless distributed_trace_payload
|
98
|
-
duration = (start_time.to_f * 1000 - distributed_trace_payload.timestamp) / 1000
|
99
|
-
duration < 0 ? 0 : duration
|
100
|
-
end
|
101
|
-
|
102
83
|
private
|
103
84
|
|
104
|
-
SUPPORTABILITY_CREATE_BEFORE_ACCEPT_PAYLOAD = "Supportability/DistributedTrace/AcceptPayload/Ignored/CreateBeforeAccept".freeze
|
105
|
-
SUPPORTABILITY_MULTIPLE_ACCEPT_PAYLOAD = "Supportability/DistributedTrace/AcceptPayload/Ignored/Multiple".freeze
|
106
|
-
SUPPORTABILITY_PAYLOAD_ACCEPT_IGNORED_NULL = "Supportability/DistributedTrace/AcceptPayload/Ignored/Null".freeze
|
107
|
-
SUPPORTABILITY_PAYLOAD_ACCEPT_IGNORED_BROWSER = "Supportability/DistributedTrace/AcceptPayload/Ignored/BrowserAgentInjected".freeze
|
108
|
-
|
109
85
|
def check_payload_ignored(payload)
|
110
86
|
if distributed_trace_payload
|
111
|
-
NewRelic::Agent.increment_metric
|
87
|
+
NewRelic::Agent.increment_metric IGNORE_MULTIPLE_ACCEPT_METRIC
|
112
88
|
return true
|
113
89
|
elsif distributed_trace_payload_created?
|
114
|
-
NewRelic::Agent.increment_metric
|
90
|
+
NewRelic::Agent.increment_metric IGNORE_ACCEPT_AFTER_CREATE_METRIC
|
115
91
|
return true
|
116
92
|
end
|
117
93
|
false
|
118
94
|
end
|
119
95
|
|
120
|
-
NULL_PAYLOAD = 'null'.freeze
|
121
|
-
|
122
96
|
def check_payload_present(payload)
|
123
97
|
# We might be passed a Ruby `nil` object _or_ the JSON "null"
|
124
98
|
if payload.nil? || payload == NULL_PAYLOAD
|
125
|
-
NewRelic::Agent.increment_metric
|
99
|
+
NewRelic::Agent.increment_metric IGNORE_ACCEPT_NULL_METRIC
|
126
100
|
return nil
|
127
101
|
end
|
128
102
|
|
129
103
|
payload
|
130
104
|
end
|
131
105
|
|
132
|
-
SUPPORTABILITY_PAYLOAD_ACCEPT_IGNORED_PARSE_EXCEPTION = "Supportability/DistributedTrace/AcceptPayload/ParseException".freeze
|
133
|
-
LBRACE = "{".freeze
|
134
|
-
|
135
106
|
def decode_payload(payload)
|
136
107
|
decoded = if payload.start_with? LBRACE
|
137
108
|
DistributedTracePayload.from_json payload
|
@@ -143,7 +114,7 @@ module NewRelic
|
|
143
114
|
|
144
115
|
decoded
|
145
116
|
rescue => e
|
146
|
-
NewRelic::Agent.increment_metric
|
117
|
+
NewRelic::Agent.increment_metric ACCEPT_PARSE_EXCEPTION_METRIC
|
147
118
|
NewRelic::Agent.logger.warn "Error parsing distributed trace payload", e
|
148
119
|
nil
|
149
120
|
end
|
@@ -160,100 +131,40 @@ module NewRelic
|
|
160
131
|
|
161
132
|
true
|
162
133
|
else
|
163
|
-
NewRelic::Agent.increment_metric
|
134
|
+
NewRelic::Agent.increment_metric ACCEPT_PARSE_EXCEPTION_METRIC
|
164
135
|
false
|
165
136
|
end
|
166
137
|
end
|
167
138
|
|
168
|
-
SUPPORTABILITY_PAYLOAD_ACCEPT_IGNORED_MAJOR_VERSION = "Supportability/DistributedTrace/AcceptPayload/Ignored/MajorVersion".freeze
|
169
|
-
|
170
139
|
def check_valid_version(payload)
|
171
140
|
if DistributedTracePayload.major_version_matches?(payload)
|
172
141
|
true
|
173
142
|
else
|
174
|
-
NewRelic::Agent.increment_metric
|
143
|
+
NewRelic::Agent.increment_metric IGNORE_ACCEPT_MAJOR_VERSION_METRIC
|
175
144
|
false
|
176
145
|
end
|
177
146
|
end
|
178
147
|
|
179
|
-
SUPPORTABILITY_PAYLOAD_ACCEPT_UNTRUSTED_ACCOUNT = "Supportability/DistributedTrace/AcceptPayload/Ignored/UntrustedAccount".freeze
|
180
|
-
|
181
148
|
def check_trusted_account(payload)
|
182
149
|
compare_key = payload.trusted_account_key || payload.parent_account_id
|
183
150
|
unless compare_key == NewRelic::Agent.config[:trusted_account_key]
|
184
|
-
NewRelic::Agent.increment_metric
|
151
|
+
NewRelic::Agent.increment_metric IGNORE_ACCEPT_UNTRUSTED_ACCOUNT_METRIC
|
185
152
|
return false
|
186
153
|
end
|
187
154
|
true
|
188
155
|
end
|
189
156
|
|
190
157
|
def assign_payload_and_sampling_params(payload)
|
191
|
-
|
158
|
+
@distributed_trace_payload = payload
|
159
|
+
return if transaction.distributed_tracer.trace_context_header_data
|
160
|
+
transaction.trace_id = payload.trace_id
|
161
|
+
transaction.distributed_tracer.parent_transaction_id = payload.transaction_id
|
162
|
+
transaction.parent_span_id = payload.id
|
192
163
|
|
193
164
|
unless payload.sampled.nil?
|
194
|
-
|
195
|
-
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
ALL_SUFFIX = "all".freeze
|
200
|
-
ALL_WEB_SUFFIX = "allWeb".freeze
|
201
|
-
ALL_OTHER_SUFFIX = "allOther".freeze
|
202
|
-
|
203
|
-
def transaction_type_suffix
|
204
|
-
if Transaction.recording_web_transaction?
|
205
|
-
ALL_WEB_SUFFIX
|
206
|
-
else
|
207
|
-
ALL_OTHER_SUFFIX
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
def record_distributed_tracing_metrics
|
212
|
-
return unless Agent.config[:'distributed_tracing.enabled']
|
213
|
-
|
214
|
-
record_caller_by_duration_metrics
|
215
|
-
record_transport_duration_metrics
|
216
|
-
record_errors_by_caller_metrics
|
217
|
-
end
|
218
|
-
|
219
|
-
DURATION_BY_CALLER_UNKOWN_PREFIX = "DurationByCaller/Unknown/Unknown/Unknown/Unknown".freeze
|
220
|
-
|
221
|
-
def record_caller_by_duration_metrics
|
222
|
-
prefix = if distributed_trace?
|
223
|
-
payload = distributed_trace_payload
|
224
|
-
"DurationByCaller/#{payload.parent_type}/#{payload.parent_account_id}/#{payload.parent_app_id}/#{payload.caller_transport_type}"
|
225
|
-
else
|
226
|
-
DURATION_BY_CALLER_UNKOWN_PREFIX
|
165
|
+
transaction.sampled = payload.sampled
|
166
|
+
transaction.priority = payload.priority if payload.priority
|
227
167
|
end
|
228
|
-
|
229
|
-
metrics.record_unscoped "#{prefix}/#{ALL_SUFFIX}", duration
|
230
|
-
metrics.record_unscoped "#{prefix}/#{transaction_type_suffix}", duration
|
231
|
-
end
|
232
|
-
|
233
|
-
def record_transport_duration_metrics
|
234
|
-
return unless distributed_trace?
|
235
|
-
|
236
|
-
payload = distributed_trace_payload
|
237
|
-
prefix = "TransportDuration/#{payload.parent_type}/#{payload.parent_account_id}/#{payload.parent_app_id}/#{payload.caller_transport_type}"
|
238
|
-
|
239
|
-
metrics.record_unscoped "#{prefix}/#{ALL_SUFFIX}", transport_duration
|
240
|
-
metrics.record_unscoped "#{prefix}/#{transaction_type_suffix}", transport_duration
|
241
|
-
end
|
242
|
-
|
243
|
-
ERRORS_BY_CALLER_UNKOWN_PREFIX = "ErrorsByCaller/Unknown/Unknown/Unknown/Unknown".freeze
|
244
|
-
|
245
|
-
def record_errors_by_caller_metrics
|
246
|
-
return unless exceptions.size > 0
|
247
|
-
|
248
|
-
prefix = if distributed_trace?
|
249
|
-
payload = distributed_trace_payload
|
250
|
-
"ErrorsByCaller/#{payload.parent_type}/#{payload.parent_account_id}/#{payload.parent_app_id}/#{payload.caller_transport_type}"
|
251
|
-
else
|
252
|
-
ERRORS_BY_CALLER_UNKOWN_PREFIX
|
253
|
-
end
|
254
|
-
|
255
|
-
metrics.record_unscoped "#{prefix}/#{ALL_SUFFIX}", 1
|
256
|
-
metrics.record_unscoped "#{prefix}/#{transaction_type_suffix}", 1
|
257
168
|
end
|
258
169
|
end
|
259
170
|
end
|