newrelic_rpm 6.6.0.358 → 6.11.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of newrelic_rpm might be problematic. Click here for more details.

Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +65 -5
  4. data/CHANGELOG.md +222 -0
  5. data/Gemfile +5 -2
  6. data/Guardfile +18 -1
  7. data/LICENSE +1 -1
  8. data/Rakefile +2 -0
  9. data/lib/new_relic/agent.rb +89 -7
  10. data/lib/new_relic/agent/agent.rb +114 -55
  11. data/lib/new_relic/agent/agent_logger.rb +4 -0
  12. data/lib/new_relic/agent/attribute_filter.rb +7 -7
  13. data/lib/new_relic/agent/attributes.rb +150 -0
  14. data/lib/new_relic/agent/autostart.rb +19 -14
  15. data/lib/new_relic/agent/configuration/default_source.rb +127 -9
  16. data/lib/new_relic/agent/configuration/event_harvest_config.rb +11 -5
  17. data/lib/new_relic/agent/configuration/manager.rb +0 -8
  18. data/lib/new_relic/agent/configuration/server_source.rb +3 -2
  19. data/lib/new_relic/agent/configuration/yaml_source.rb +11 -6
  20. data/lib/new_relic/agent/connect/request_builder.rb +5 -13
  21. data/lib/new_relic/agent/database.rb +1 -2
  22. data/lib/new_relic/agent/datastores/mongo.rb +1 -1
  23. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +2 -2
  24. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +8 -8
  25. data/lib/new_relic/agent/distributed_tracing.rb +155 -6
  26. data/lib/new_relic/agent/{cross_app_payload.rb → distributed_tracing/cross_app_payload.rb} +0 -0
  27. data/lib/new_relic/agent/{cross_app_tracing.rb → distributed_tracing/cross_app_tracing.rb} +60 -45
  28. data/lib/new_relic/agent/distributed_tracing/distributed_trace_intrinsics.rb +80 -0
  29. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
  30. data/lib/new_relic/agent/{distributed_trace_payload.rb → distributed_tracing/distributed_trace_payload.rb} +23 -99
  31. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +39 -0
  32. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +246 -0
  33. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +126 -0
  34. data/lib/new_relic/agent/error_collector.rb +33 -16
  35. data/lib/new_relic/agent/error_event_aggregator.rb +7 -5
  36. data/lib/new_relic/agent/external.rb +7 -7
  37. data/lib/new_relic/agent/guid_generator.rb +28 -0
  38. data/lib/new_relic/agent/hostname.rb +7 -1
  39. data/lib/new_relic/agent/http_clients/abstract.rb +82 -0
  40. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +24 -19
  41. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +28 -13
  42. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +17 -21
  43. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +10 -11
  44. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +16 -4
  45. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +4 -6
  46. data/lib/new_relic/agent/http_clients/uri_util.rb +3 -2
  47. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +5 -7
  48. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +4 -0
  49. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +11 -2
  50. data/lib/new_relic/agent/instrumentation/active_record.rb +4 -2
  51. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +7 -2
  52. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +8 -4
  53. data/lib/new_relic/agent/instrumentation/bunny.rb +45 -28
  54. data/lib/new_relic/agent/instrumentation/curb.rb +59 -18
  55. data/lib/new_relic/agent/instrumentation/data_mapper.rb +3 -1
  56. data/lib/new_relic/agent/instrumentation/excon.rb +1 -1
  57. data/lib/new_relic/agent/instrumentation/excon/connection.rb +6 -3
  58. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +2 -1
  59. data/lib/new_relic/agent/instrumentation/grape.rb +5 -10
  60. data/lib/new_relic/agent/instrumentation/http.rb +6 -3
  61. data/lib/new_relic/agent/instrumentation/httpclient.rb +5 -3
  62. data/lib/new_relic/agent/instrumentation/memcache.rb +3 -1
  63. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +6 -2
  64. data/lib/new_relic/agent/instrumentation/mongo.rb +9 -3
  65. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +13 -0
  66. data/lib/new_relic/agent/instrumentation/net.rb +6 -3
  67. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +25 -1
  68. data/lib/new_relic/agent/instrumentation/redis.rb +9 -3
  69. data/lib/new_relic/agent/instrumentation/resque.rb +3 -0
  70. data/lib/new_relic/agent/instrumentation/sidekiq.rb +0 -1
  71. data/lib/new_relic/agent/instrumentation/typhoeus.rb +23 -6
  72. data/lib/new_relic/agent/logging.rb +139 -0
  73. data/lib/new_relic/agent/messaging.rb +5 -73
  74. data/lib/new_relic/agent/method_tracer.rb +3 -2
  75. data/lib/new_relic/agent/method_tracer_helpers.rb +2 -2
  76. data/lib/new_relic/agent/monitors.rb +27 -0
  77. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +110 -0
  78. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +27 -0
  79. data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +1 -1
  80. data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +2 -4
  81. data/lib/new_relic/agent/new_relic_service.rb +7 -5
  82. data/lib/new_relic/agent/noticible_error.rb +22 -0
  83. data/lib/new_relic/agent/span_event_aggregator.rb +1 -0
  84. data/lib/new_relic/agent/span_event_primitive.rb +82 -53
  85. data/lib/new_relic/agent/sql_sampler.rb +3 -3
  86. data/lib/new_relic/agent/supported_versions.rb +2 -2
  87. data/lib/new_relic/agent/tracer.rb +65 -18
  88. data/lib/new_relic/agent/transaction.rb +84 -79
  89. data/lib/new_relic/agent/transaction/abstract_segment.rb +28 -2
  90. data/lib/new_relic/agent/transaction/distributed_tracer.rb +171 -0
  91. data/lib/new_relic/agent/transaction/distributed_tracing.rb +57 -146
  92. data/lib/new_relic/agent/transaction/external_request_segment.rb +29 -36
  93. data/lib/new_relic/agent/transaction/message_broker_segment.rb +3 -11
  94. data/lib/new_relic/agent/transaction/segment.rb +7 -1
  95. data/lib/new_relic/agent/transaction/trace.rb +2 -4
  96. data/lib/new_relic/agent/transaction/trace_context.rb +168 -0
  97. data/lib/new_relic/agent/transaction/trace_node.rb +10 -8
  98. data/lib/new_relic/agent/transaction_error_primitive.rb +10 -15
  99. data/lib/new_relic/agent/transaction_event_primitive.rb +28 -39
  100. data/lib/new_relic/cli/commands/deployments.rb +1 -1
  101. data/lib/new_relic/cli/commands/install.rb +3 -2
  102. data/lib/new_relic/coerce.rb +31 -6
  103. data/lib/new_relic/constants.rb +34 -0
  104. data/lib/new_relic/control/instance_methods.rb +10 -1
  105. data/lib/new_relic/dependency_detection.rb +4 -4
  106. data/lib/new_relic/noticed_error.rb +38 -17
  107. data/lib/new_relic/rack/browser_monitoring.rb +5 -0
  108. data/lib/new_relic/supportability_helper.rb +14 -0
  109. data/lib/new_relic/version.rb +1 -1
  110. data/lib/tasks/multiverse.rb +25 -0
  111. data/lib/tasks/tests.rake +6 -1
  112. data/newrelic_rpm.gemspec +18 -7
  113. data/test/agent_helper.rb +322 -70
  114. metadata +98 -32
  115. data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
  116. data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -40
  117. data/lib/new_relic/agent/http_clients/abstract_request.rb +0 -31
  118. data/lib/new_relic/agent/transaction/attributes.rb +0 -154
  119. data/lib/tasks/versions.html.erb +0 -28
  120. data/lib/tasks/versions.postface.html +0 -8
  121. data/lib/tasks/versions.preface.html +0 -9
  122. data/lib/tasks/versions.rake +0 -65
  123. data/lib/tasks/versions.txt.erb +0 -14
@@ -0,0 +1,110 @@
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/tracer'
9
+ require 'new_relic/agent/threading/agent_thread'
10
+
11
+ module NewRelic
12
+ module Agent
13
+ module DistributedTracing
14
+ class CrossAppMonitor < InboundRequestMonitor
15
+
16
+ NEWRELIC_ID_HEADER = 'X-NewRelic-ID'.freeze
17
+ NEWRELIC_TXN_HEADER = 'X-NewRelic-Transaction'.freeze
18
+ NEWRELIC_APPDATA_HEADER = 'X-NewRelic-App-Data'.freeze
19
+
20
+ NEWRELIC_ID_HEADER_KEY = 'HTTP_X_NEWRELIC_ID'.freeze
21
+ NEWRELIC_TXN_HEADER_KEY = 'HTTP_X_NEWRELIC_TRANSACTION'.freeze
22
+ CONTENT_LENGTH_HEADER_KEY = 'HTTP_CONTENT_LENGTH'.freeze
23
+
24
+ def on_finished_configuring(events)
25
+ register_event_listeners(events)
26
+ end
27
+
28
+ def path_hash(txn_name, seed)
29
+ rotated = ((seed << 1) | (seed >> 31)) & 0xffffffff
30
+ app_name = NewRelic::Agent.config[:app_name].first
31
+ identifier = "#{app_name};#{txn_name}"
32
+ sprintf("%08x", rotated ^ hash_transaction_name(identifier))
33
+ end
34
+
35
+ private
36
+
37
+ # Expected sequence of events:
38
+ # :before_call will save our cross application request id to the thread
39
+ # :after_call will write our response headers/metrics and clean up the thread
40
+ def register_event_listeners(events)
41
+ NewRelic::Agent.logger.
42
+ debug("Wiring up Cross Application Tracing to events after finished configuring")
43
+
44
+ events.subscribe(:before_call) do |env| #THREAD_LOCAL_ACCESS
45
+ if id = decoded_id(env) and should_process_request?(id)
46
+ state = NewRelic::Agent::Tracer.state
47
+
48
+ if (txn = state.current_transaction)
49
+ transaction_info = referring_transaction_info(state, env)
50
+
51
+ payload = CrossAppPayload.new(id, txn, transaction_info)
52
+ txn.distributed_tracer.cross_app_payload = payload
53
+ end
54
+
55
+ CrossAppTracing.assign_intrinsic_transaction_attributes state
56
+ end
57
+ end
58
+
59
+ events.subscribe(:after_call) do |env, (_status_code, headers, _body)| #THREAD_LOCAL_ACCESS
60
+ state = NewRelic::Agent::Tracer.state
61
+
62
+ insert_response_header(state, env, headers)
63
+ end
64
+ end
65
+
66
+ def referring_transaction_info(state, request_headers)
67
+ txn_header = request_headers[NEWRELIC_TXN_HEADER_KEY] or return
68
+ deserialize_header(txn_header, NEWRELIC_TXN_HEADER)
69
+ end
70
+
71
+ def insert_response_header(state, request_headers, response_headers)
72
+ txn = state.current_transaction
73
+ unless txn.nil? || txn.distributed_tracer.cross_app_payload.nil?
74
+ txn.freeze_name_and_execute_if_not_ignored do
75
+ content_length = content_length_from_request(request_headers)
76
+ set_response_headers(txn, response_headers, content_length)
77
+ end
78
+ end
79
+ end
80
+
81
+ def should_process_request? id
82
+ CrossAppTracing.cross_app_enabled? && CrossAppTracing.trusts?(id)
83
+ end
84
+
85
+ def set_response_headers(transaction, response_headers, content_length)
86
+ payload = obfuscator.obfuscate(
87
+ ::JSON.dump(
88
+ transaction.distributed_tracer.cross_app_payload.as_json_array(content_length)))
89
+
90
+ response_headers[NEWRELIC_APPDATA_HEADER] = payload
91
+ end
92
+
93
+ def decoded_id(request)
94
+ encoded_id = request[NEWRELIC_ID_HEADER_KEY]
95
+ return "" if encoded_id.nil? || encoded_id.empty?
96
+
97
+ obfuscator.deobfuscate(encoded_id)
98
+ end
99
+
100
+ def content_length_from_request(request)
101
+ request[CONTENT_LENGTH_HEADER_KEY] || -1
102
+ end
103
+
104
+ def hash_transaction_name(identifier)
105
+ Digest::MD5.digest(identifier).unpack("@12N").first & 0xffffffff
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,27 @@
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 DistributedTracing
8
+ class Monitor < InboundRequestMonitor
9
+ def on_finished_configuring(events)
10
+ return unless NewRelic::Agent.config[:'distributed_tracing.enabled']
11
+ events.subscribe(:before_call, &method(:on_before_call))
12
+ end
13
+
14
+ def on_before_call(request)
15
+ unless NewRelic::Agent.config[:'distributed_tracing.enabled']
16
+ NewRelic::Agent.logger.warn "Not configured to accept distributed trace headers"
17
+ return
18
+ end
19
+
20
+ return unless txn = Tracer.current_transaction
21
+
22
+ txn.distributed_tracer.accept_incoming_request request
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -28,7 +28,7 @@ module NewRelic
28
28
  end
29
29
 
30
30
  def setup_obfuscator
31
- @obfuscator = NewRelic::Agent::Obfuscator.new(NewRelic::Agent.config[:encoding_key])
31
+ @obfuscator = Obfuscator.new(Agent.config[:encoding_key])
32
32
  end
33
33
 
34
34
  def deserialize_header(encoded_header, key)
@@ -2,8 +2,6 @@
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/inbound_request_monitor'
6
-
7
5
  module NewRelic
8
6
  module Agent
9
7
  class SyntheticsMonitor < InboundRequestMonitor
@@ -40,7 +38,7 @@ module NewRelic
40
38
 
41
39
  def is_trusted?(incoming_payload)
42
40
  account_id = incoming_payload[1]
43
- NewRelic::Agent.config[:trusted_account_ids].include?(account_id)
41
+ Agent.config[:trusted_account_ids].include?(account_id)
44
42
  end
45
43
 
46
44
  def is_valid_payload?(incoming_payload)
@@ -48,7 +46,7 @@ module NewRelic
48
46
  end
49
47
 
50
48
  def reject_messaging_synthetics_header headers
51
- headers.reject {|k,_| k == NewRelic::Agent::CrossAppTracing::NR_MESSAGE_BROKER_SYNTHETICS_HEADER}
49
+ headers.reject {|k,_| k == CrossAppTracing::NR_MESSAGE_BROKER_SYNTHETICS_HEADER}
52
50
  end
53
51
 
54
52
  end
@@ -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)
@@ -0,0 +1,22 @@
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
+ module NewRelic
7
+ module Agent
8
+
9
+ # This class is used for instrumentations that have exceptions or error classes
10
+ # not derived from Ruby's usual Exception or StandardError or in situations
11
+ # where we do not have such Exception object to work with.
12
+ class NoticibleError
13
+ attr_reader :class_name, :message
14
+
15
+ def initialize class_name, message
16
+ @class_name = class_name
17
+ @message = message
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -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
 
@@ -1,12 +1,13 @@
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
6
  # This module builds the data structures necessary to create a Span
6
7
  # event for a segment.
7
8
 
8
9
  require 'new_relic/agent/payload_metric_mapping'
9
- require 'new_relic/agent/distributed_trace_payload'
10
+ require 'new_relic/agent/distributed_tracing/distributed_trace_payload'
10
11
 
11
12
  module NewRelic
12
13
  module Agent
@@ -15,70 +16,80 @@ module NewRelic
15
16
  extend self
16
17
 
17
18
  # Strings for static keys of the event structure
18
- TYPE_KEY = 'type'.freeze
19
- TRACE_ID_KEY = 'traceId'.freeze
20
- GUID_KEY = 'guid'.freeze
21
- PARENT_ID_KEY = 'parentId'.freeze
22
- GRANDPARENT_ID_KEY = 'grandparentId'.freeze
23
- TRANSACTION_ID_KEY = 'transactionId'.freeze
24
- SAMPLED_KEY = 'sampled'.freeze
25
- PRIORITY_KEY = 'priority'.freeze
26
- TIMESTAMP_KEY = 'timestamp'.freeze
27
- DURATION_KEY = 'duration'.freeze
28
- NAME_KEY = 'name'.freeze
29
- CATEGORY_KEY = 'category'.freeze
30
- HTTP_URL_KEY = 'http.url'.freeze
31
- HTTP_METHOD_KEY = 'http.method'.freeze
32
- COMPONENT_KEY = 'component'.freeze
33
- DB_INSTANCE_KEY = 'db.instance'.freeze
34
- DB_STATEMENT_KEY = 'db.statement'.freeze
35
- PEER_ADDRESS_KEY = 'peer.address'.freeze
36
- PEER_HOSTNAME_KEY = 'peer.hostname'.freeze
37
- SPAN_KIND_KEY = 'span.kind'.freeze
38
- ENTRY_POINT_KEY = 'nr.entryPoint'.freeze
19
+ ELLIPSIS = '...'
20
+ TYPE_KEY = 'type'
21
+ TRACE_ID_KEY = 'traceId'
22
+ GUID_KEY = 'guid'
23
+ PARENT_ID_KEY = 'parentId'
24
+ GRANDPARENT_ID_KEY = 'grandparentId'
25
+ TRANSACTION_ID_KEY = 'transactionId'
26
+ SAMPLED_KEY = 'sampled'
27
+ PRIORITY_KEY = 'priority'
28
+ TIMESTAMP_KEY = 'timestamp'
29
+ DURATION_KEY = 'duration'
30
+ NAME_KEY = 'name'
31
+ CATEGORY_KEY = 'category'
32
+ HTTP_URL_KEY = 'http.url'
33
+ HTTP_METHOD_KEY = 'http.method'
34
+ HTTP_STATUS_CODE_KEY = 'http.statusCode'
35
+ COMPONENT_KEY = 'component'
36
+ DB_INSTANCE_KEY = 'db.instance'
37
+ DB_STATEMENT_KEY = 'db.statement'
38
+ PEER_ADDRESS_KEY = 'peer.address'
39
+ PEER_HOSTNAME_KEY = 'peer.hostname'
40
+ SPAN_KIND_KEY = 'span.kind'
41
+ ENTRY_POINT_KEY = 'nr.entryPoint'
42
+ TRUSTED_PARENT_KEY = "trustedParentId"
43
+ TRACING_VENDORS_KEY = "tracingVendors"
39
44
 
40
45
  # Strings for static values of the event structure
41
- EVENT_TYPE = 'Span'.freeze
42
- GENERIC_CATEGORY = 'generic'.freeze
43
- HTTP_CATEGORY = 'http'.freeze
44
- DATASTORE_CATEGORY = 'datastore'.freeze
45
- CLIENT = 'client'.freeze
46
-
47
- # To avoid allocations when we have empty custom or agent attributes
48
- EMPTY_HASH = {}.freeze
46
+ EVENT_TYPE = 'Span'
47
+ GENERIC_CATEGORY = 'generic'
48
+ HTTP_CATEGORY = 'http'
49
+ DATASTORE_CATEGORY = 'datastore'
50
+ CLIENT = 'client'
51
+
52
+ # Builds a Hash of error attributes as well as the Span ID when
53
+ # an error is present. Otherwise, returns nil when no error present.
54
+ def error_attributes segment
55
+ return if Agent.config[:high_security] || !segment.noticed_error
56
+ segment.noticed_error.build_error_attributes
57
+ segment.noticed_error_attributes
58
+ end
49
59
 
50
- def for_segment(segment)
60
+ def for_segment segment
51
61
  intrinsics = intrinsics_for(segment)
52
62
  intrinsics[CATEGORY_KEY] = GENERIC_CATEGORY
53
63
 
54
- [intrinsics, EMPTY_HASH, EMPTY_HASH]
64
+ [intrinsics, custom_attributes(segment.attributes), error_attributes(segment) || NewRelic::EMPTY_HASH]
55
65
  end
56
66
 
57
- def for_external_request_segment(segment)
67
+ def for_external_request_segment segment
58
68
  intrinsics = intrinsics_for(segment)
59
69
 
60
70
  intrinsics[COMPONENT_KEY] = segment.library
61
71
  intrinsics[HTTP_METHOD_KEY] = segment.procedure
72
+ intrinsics[HTTP_STATUS_CODE_KEY] = segment.http_status_code if segment.http_status_code
62
73
  intrinsics[CATEGORY_KEY] = HTTP_CATEGORY
63
74
  intrinsics[SPAN_KIND_KEY] = CLIENT
64
75
 
65
- agent_attributes = {}
76
+ agent_attributes = error_attributes(segment) || {}
66
77
 
67
78
  if allowed?(HTTP_URL_KEY)
68
79
  agent_attributes[HTTP_URL_KEY] = truncate(segment.uri)
69
80
  end
70
81
 
71
- [intrinsics, EMPTY_HASH, agent_attributes]
82
+ [intrinsics, custom_attributes(segment.attributes), agent_attributes]
72
83
  end
73
84
 
74
- def for_datastore_segment(segment)
85
+ def for_datastore_segment segment
75
86
  intrinsics = intrinsics_for(segment)
76
87
 
77
88
  intrinsics[COMPONENT_KEY] = segment.product
78
89
  intrinsics[SPAN_KIND_KEY] = CLIENT
79
90
  intrinsics[CATEGORY_KEY] = DATASTORE_CATEGORY
80
91
 
81
- agent_attributes = {}
92
+ agent_attributes = error_attributes(segment) || {}
82
93
 
83
94
  if segment.database_name && allowed?(DB_INSTANCE_KEY)
84
95
  agent_attributes[DB_INSTANCE_KEY] = truncate(segment.database_name)
@@ -96,14 +107,13 @@ module NewRelic
96
107
  agent_attributes[DB_STATEMENT_KEY] = truncate(segment.nosql_statement, 2000)
97
108
  end
98
109
 
99
- [intrinsics, EMPTY_HASH, agent_attributes]
110
+ [intrinsics, custom_attributes(segment.attributes), agent_attributes]
100
111
  end
101
112
 
102
113
  private
103
114
 
104
- def intrinsics_for(segment)
105
- intrinsics =
106
- {
115
+ def intrinsics_for segment
116
+ intrinsics = {
107
117
  TYPE_KEY => EVENT_TYPE,
108
118
  TRACE_ID_KEY => segment.transaction.trace_id,
109
119
  GUID_KEY => segment.guid,
@@ -115,29 +125,48 @@ module NewRelic
115
125
  NAME_KEY => segment.name
116
126
  }
117
127
 
118
- parent_id = parent_guid(segment)
119
- intrinsics[PARENT_ID_KEY] = parent_id if parent_id
120
-
121
- intrinsics[ENTRY_POINT_KEY] = true unless segment.parent
128
+ if segment.parent.nil?
129
+ intrinsics[ENTRY_POINT_KEY] = true
130
+ if txn = segment.transaction
131
+ if header_data = txn.distributed_tracer.trace_context_header_data
132
+ if trace_state_vendors = header_data.trace_state_vendors
133
+ intrinsics[TRACING_VENDORS_KEY] = trace_state_vendors unless trace_state_vendors == NewRelic::EMPTY_STR
134
+ end
135
+ end
136
+ if trace_state_payload = txn.distributed_tracer.trace_state_payload
137
+ intrinsics[TRUSTED_PARENT_KEY] = trace_state_payload.id if trace_state_payload.id
138
+ end
139
+ end
140
+ end
122
141
 
142
+ if parent_id = parent_guid(segment)
143
+ intrinsics[PARENT_ID_KEY] = parent_id
144
+ end
123
145
  intrinsics
124
146
  end
125
147
 
126
- def parent_guid(segment)
148
+ def custom_attributes attributes
149
+ if attributes
150
+ result = attributes.custom_attributes_for(NewRelic::Agent::AttributeFilter::DST_SPAN_EVENTS)
151
+ result.freeze
152
+ else
153
+ NewRelic::EMPTY_HASH
154
+ end
155
+ end
156
+
157
+ def parent_guid segment
127
158
  if segment.parent
128
159
  segment.parent.guid
129
- elsif segment.transaction && segment.transaction.distributed_trace?
130
- segment.transaction.distributed_trace_payload.id
160
+ elsif txn = segment.transaction
161
+ txn.distributed_tracer.parent_guid
131
162
  end
132
163
  end
133
164
 
134
- def milliseconds_since_epoch(segment)
165
+ def milliseconds_since_epoch segment
135
166
  Integer(segment.start_time.to_f * 1000.0)
136
167
  end
137
168
 
138
- ELLIPSIS = '...'.freeze
139
-
140
- def truncate(value, max_size=255)
169
+ def truncate value, max_size=255
141
170
  value = value.to_s
142
171
  if value.bytesize > max_size
143
172
  value.byteslice(0, max_size - 2).chop! << ELLIPSIS
@@ -146,7 +175,7 @@ module NewRelic
146
175
  end
147
176
  end
148
177
 
149
- def allowed?(key)
178
+ def allowed? key
150
179
  NewRelic::Agent.instance.attribute_filter.allows_key?(key, AttributeFilter::DST_SPAN_EVENTS)
151
180
  end
152
181
  end