newrelic_rpm 9.7.0 → 9.16.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +376 -2
  3. data/README.md +17 -18
  4. data/Rakefile +1 -1
  5. data/lib/boot/strap.rb +101 -0
  6. data/lib/new_relic/agent/agent.rb +4 -1
  7. data/lib/new_relic/agent/agent_helpers/connect.rb +10 -8
  8. data/lib/new_relic/agent/agent_helpers/start_worker_thread.rb +1 -1
  9. data/lib/new_relic/agent/agent_helpers/startup.rb +2 -1
  10. data/lib/new_relic/agent/agent_logger.rb +3 -1
  11. data/lib/new_relic/agent/aws.rb +68 -0
  12. data/lib/new_relic/agent/configuration/default_source.rb +519 -23
  13. data/lib/new_relic/agent/configuration/environment_source.rb +14 -2
  14. data/lib/new_relic/agent/configuration/high_security_source.rb +1 -0
  15. data/lib/new_relic/agent/configuration/manager.rb +51 -8
  16. data/lib/new_relic/agent/configuration/security_policy_source.rb +11 -0
  17. data/lib/new_relic/agent/configuration/yaml_source.rb +2 -0
  18. data/lib/new_relic/agent/connect/request_builder.rb +1 -1
  19. data/lib/new_relic/agent/custom_event_aggregator.rb +27 -1
  20. data/lib/new_relic/agent/database/obfuscation_helpers.rb +11 -11
  21. data/lib/new_relic/agent/database/obfuscator.rb +1 -0
  22. data/lib/new_relic/agent/database.rb +39 -0
  23. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +1 -5
  24. data/lib/new_relic/agent/error_collector.rb +39 -10
  25. data/lib/new_relic/agent/harvester.rb +1 -1
  26. data/lib/new_relic/agent/instrumentation/active_merchant.rb +0 -13
  27. data/lib/new_relic/agent/instrumentation/active_record.rb +1 -8
  28. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +3 -0
  29. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +1 -12
  30. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb +7 -3
  31. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger.rb +0 -2
  32. data/lib/new_relic/agent/instrumentation/active_support_logger.rb +0 -2
  33. data/lib/new_relic/agent/instrumentation/async_http.rb +4 -3
  34. data/lib/new_relic/agent/instrumentation/aws_sdk_lambda/chain.rb +33 -0
  35. data/lib/new_relic/agent/instrumentation/aws_sdk_lambda/instrumentation.rb +93 -0
  36. data/lib/new_relic/agent/instrumentation/aws_sdk_lambda/prepend.rb +23 -0
  37. data/lib/new_relic/agent/instrumentation/aws_sdk_lambda.rb +23 -0
  38. data/lib/new_relic/agent/instrumentation/aws_sqs/chain.rb +37 -0
  39. data/lib/new_relic/agent/instrumentation/aws_sqs/instrumentation.rb +67 -0
  40. data/lib/new_relic/agent/instrumentation/aws_sqs/prepend.rb +21 -0
  41. data/lib/new_relic/agent/instrumentation/aws_sqs.rb +23 -0
  42. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +14 -0
  43. data/lib/new_relic/agent/instrumentation/bunny.rb +3 -4
  44. data/lib/new_relic/agent/instrumentation/concurrent_ruby.rb +1 -2
  45. data/lib/new_relic/agent/instrumentation/curb.rb +3 -4
  46. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +0 -23
  47. data/lib/new_relic/agent/instrumentation/dynamodb/chain.rb +27 -0
  48. data/lib/new_relic/agent/instrumentation/dynamodb/instrumentation.rb +64 -0
  49. data/lib/new_relic/agent/instrumentation/dynamodb/prepend.rb +19 -0
  50. data/lib/new_relic/agent/instrumentation/dynamodb.rb +23 -0
  51. data/lib/new_relic/agent/instrumentation/elasticsearch/instrumentation.rb +58 -8
  52. data/lib/new_relic/agent/instrumentation/elasticsearch.rb +0 -2
  53. data/lib/new_relic/agent/instrumentation/ethon.rb +0 -4
  54. data/lib/new_relic/agent/instrumentation/excon.rb +0 -16
  55. data/lib/new_relic/agent/instrumentation/fiber.rb +0 -2
  56. data/lib/new_relic/agent/instrumentation/grape.rb +1 -1
  57. data/lib/new_relic/agent/instrumentation/grpc/client/instrumentation.rb +0 -1
  58. data/lib/new_relic/agent/instrumentation/grpc_server.rb +1 -1
  59. data/lib/new_relic/agent/instrumentation/httpclient.rb +0 -1
  60. data/lib/new_relic/agent/instrumentation/httprb.rb +0 -1
  61. data/lib/new_relic/agent/instrumentation/httpx.rb +0 -4
  62. data/lib/new_relic/agent/instrumentation/logger.rb +1 -3
  63. data/lib/new_relic/agent/instrumentation/logstasher/chain.rb +21 -0
  64. data/lib/new_relic/agent/instrumentation/logstasher/instrumentation.rb +24 -0
  65. data/lib/new_relic/agent/instrumentation/logstasher/prepend.rb +13 -0
  66. data/lib/new_relic/agent/instrumentation/logstasher.rb +25 -0
  67. data/lib/new_relic/agent/instrumentation/memcache.rb +0 -1
  68. data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +6 -0
  69. data/lib/new_relic/agent/instrumentation/opensearch/chain.rb +21 -0
  70. data/lib/new_relic/agent/instrumentation/opensearch/instrumentation.rb +66 -0
  71. data/lib/new_relic/agent/instrumentation/opensearch/prepend.rb +13 -0
  72. data/lib/new_relic/agent/instrumentation/opensearch.rb +23 -0
  73. data/lib/new_relic/agent/instrumentation/padrino.rb +3 -3
  74. data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +3 -0
  75. data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +9 -5
  76. data/lib/new_relic/agent/instrumentation/rake.rb +0 -1
  77. data/lib/new_relic/agent/instrumentation/rdkafka/chain.rb +72 -0
  78. data/lib/new_relic/agent/instrumentation/rdkafka/instrumentation.rb +70 -0
  79. data/lib/new_relic/agent/instrumentation/rdkafka/prepend.rb +67 -0
  80. data/lib/new_relic/agent/instrumentation/rdkafka.rb +25 -0
  81. data/lib/new_relic/agent/instrumentation/redis/cluster_middleware.rb +26 -0
  82. data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +14 -11
  83. data/lib/new_relic/agent/instrumentation/redis/middleware.rb +3 -0
  84. data/lib/new_relic/agent/instrumentation/redis.rb +11 -5
  85. data/lib/new_relic/agent/instrumentation/resque.rb +0 -4
  86. data/lib/new_relic/agent/instrumentation/roda.rb +4 -4
  87. data/lib/new_relic/agent/instrumentation/ruby_kafka/chain.rb +55 -0
  88. data/lib/new_relic/agent/instrumentation/ruby_kafka/instrumentation.rb +67 -0
  89. data/lib/new_relic/agent/instrumentation/ruby_kafka/prepend.rb +60 -0
  90. data/lib/new_relic/agent/instrumentation/ruby_kafka.rb +25 -0
  91. data/lib/new_relic/agent/instrumentation/ruby_openai/chain.rb +36 -0
  92. data/lib/new_relic/agent/instrumentation/ruby_openai/instrumentation.rb +196 -0
  93. data/lib/new_relic/agent/instrumentation/ruby_openai/prepend.rb +20 -0
  94. data/lib/new_relic/agent/instrumentation/ruby_openai.rb +35 -0
  95. data/lib/new_relic/agent/instrumentation/sidekiq.rb +0 -14
  96. data/lib/new_relic/agent/instrumentation/sinatra.rb +3 -19
  97. data/lib/new_relic/agent/instrumentation/stripe_subscriber.rb +22 -1
  98. data/lib/new_relic/agent/instrumentation/thread.rb +0 -2
  99. data/lib/new_relic/agent/instrumentation/tilt.rb +0 -4
  100. data/lib/new_relic/agent/instrumentation/typhoeus.rb +0 -1
  101. data/lib/new_relic/agent/instrumentation/view_component/instrumentation.rb +13 -6
  102. data/lib/new_relic/agent/instrumentation/view_component.rb +0 -2
  103. data/lib/new_relic/agent/javascript_instrumentor.rb +2 -3
  104. data/lib/new_relic/agent/llm/chat_completion_message.rb +25 -0
  105. data/lib/new_relic/agent/llm/chat_completion_summary.rb +66 -0
  106. data/lib/new_relic/agent/llm/embedding.rb +60 -0
  107. data/lib/new_relic/agent/llm/llm_event.rb +95 -0
  108. data/lib/new_relic/agent/llm/response_headers.rb +80 -0
  109. data/lib/new_relic/agent/llm.rb +49 -0
  110. data/lib/new_relic/agent/local_log_decorator.rb +8 -1
  111. data/lib/new_relic/agent/log_event_aggregator.rb +120 -44
  112. data/lib/new_relic/agent/messaging.rb +11 -5
  113. data/lib/new_relic/agent/new_relic_service.rb +12 -2
  114. data/lib/new_relic/agent/serverless_handler.rb +400 -0
  115. data/lib/new_relic/agent/serverless_handler_event_sources.json +155 -0
  116. data/lib/new_relic/agent/serverless_handler_event_sources.rb +49 -0
  117. data/lib/new_relic/agent/span_event_primitive.rb +8 -10
  118. data/lib/new_relic/agent/system_info.rb +14 -0
  119. data/lib/new_relic/agent/threading/agent_thread.rb +1 -2
  120. data/lib/new_relic/agent/tracer.rb +5 -5
  121. data/lib/new_relic/agent/transaction/abstract_segment.rb +1 -1
  122. data/lib/new_relic/agent/transaction/external_request_segment.rb +0 -10
  123. data/lib/new_relic/agent/transaction/request_attributes.rb +13 -1
  124. data/lib/new_relic/agent/transaction/trace_context.rb +1 -1
  125. data/lib/new_relic/agent/transaction/tracing.rb +2 -2
  126. data/lib/new_relic/agent/transaction.rb +2 -6
  127. data/lib/new_relic/agent/transaction_error_primitive.rb +23 -19
  128. data/lib/new_relic/agent.rb +198 -10
  129. data/lib/new_relic/constants.rb +2 -0
  130. data/lib/new_relic/control/frameworks/grape.rb +14 -0
  131. data/lib/new_relic/control/frameworks/padrino.rb +14 -0
  132. data/lib/new_relic/control/frameworks/rails4.rb +1 -3
  133. data/lib/new_relic/control/instance_methods.rb +8 -0
  134. data/lib/new_relic/control/private_instance_methods.rb +4 -0
  135. data/lib/new_relic/control/security_interface.rb +57 -0
  136. data/lib/new_relic/control.rb +1 -1
  137. data/lib/new_relic/dependency_detection.rb +10 -5
  138. data/lib/new_relic/environment_report.rb +2 -2
  139. data/lib/new_relic/helper.rb +15 -0
  140. data/lib/new_relic/language_support.rb +3 -1
  141. data/lib/new_relic/local_environment.rb +14 -10
  142. data/lib/new_relic/rack/browser_monitoring.rb +28 -12
  143. data/lib/new_relic/supportability_helper.rb +2 -0
  144. data/lib/new_relic/thread_local_storage.rb +31 -0
  145. data/lib/new_relic/version.rb +2 -2
  146. data/lib/sequel/extensions/new_relic_instrumentation.rb +3 -2
  147. data/lib/tasks/config.rake +8 -3
  148. data/lib/tasks/gha.rake +31 -0
  149. data/lib/tasks/helpers/config.html.erb +3 -2
  150. data/lib/tasks/helpers/format.rb +1 -1
  151. data/lib/tasks/helpers/newrelicyml.rb +76 -13
  152. data/lib/tasks/instrumentation_generator/instrumentation.thor +31 -22
  153. data/lib/tasks/instrumentation_generator/templates/chain.tt +0 -1
  154. data/lib/tasks/instrumentation_generator/templates/chain_method.tt +0 -1
  155. data/lib/tasks/instrumentation_generator/templates/dependency_detection.tt +11 -8
  156. data/lib/tasks/instrumentation_generator/templates/newrelic.yml.tt +1 -1
  157. data/newrelic.yml +387 -143
  158. data/newrelic_rpm.gemspec +2 -0
  159. data/test/agent_helper.rb +17 -2
  160. metadata +80 -3
@@ -16,10 +16,6 @@ DependencyDetection.defer do
16
16
  depends_on { Sinatra::Base.private_method_defined?(:process_route) }
17
17
  depends_on { Sinatra::Base.private_method_defined?(:route_eval) }
18
18
 
19
- executes do
20
- NewRelic::Agent.logger.info('Installing Sinatra instrumentation')
21
- end
22
-
23
19
  executes do
24
20
  if use_prepend?
25
21
  prepend_instrument Sinatra::Base, NewRelic::Agent::Instrumentation::Sinatra::Prepend
@@ -32,27 +28,15 @@ DependencyDetection.defer do
32
28
  end
33
29
 
34
30
  executes do
31
+ supportability_name = NewRelic::Agent::Instrumentation::Sinatra::Tracer::INSTRUMENTATION_NAME
35
32
  # These requires are inside an executes block because they require rack, and
36
33
  # we can't be sure that rack is available when this file is first required.
37
34
  require 'new_relic/rack/agent_hooks'
38
35
  require 'new_relic/rack/browser_monitoring'
39
36
  if use_prepend?
40
- prepend_instrument Sinatra::Base.singleton_class, NewRelic::Agent::Instrumentation::Sinatra::Build::Prepend
37
+ prepend_instrument Sinatra::Base.singleton_class, NewRelic::Agent::Instrumentation::Sinatra::Build::Prepend, supportability_name
41
38
  else
42
- chain_instrument NewRelic::Agent::Instrumentation::Sinatra::Build::Chain
39
+ chain_instrument NewRelic::Agent::Instrumentation::Sinatra::Build::Chain, supportability_name
43
40
  end
44
41
  end
45
-
46
- executes do
47
- next unless Gem::Version.new(Sinatra::VERSION) < Gem::Version.new('2.0.0')
48
-
49
- deprecation_msg = 'The Ruby agent is dropping support for Sinatra versions below 2.0.0 ' \
50
- 'in version 9.0.0. Please upgrade your Sinatra version to continue receiving full compatibility. ' \
51
-
52
- NewRelic::Agent.logger.log_once(
53
- :warn,
54
- :deprecated_sinatra_version,
55
- deprecation_msg
56
- )
57
- end
58
42
  end
@@ -10,6 +10,7 @@ module NewRelic
10
10
  EVENT_ATTRIBUTES = %i[http_status method num_retries path request_id].freeze
11
11
  ATTRIBUTE_NAMESPACE = 'stripe.user_data'
12
12
  ATTRIBUTE_FILTER_TYPES = %i[include exclude].freeze
13
+ PATH_PORTION_PATTERN = %r{^/([^/]+/[^/]+)(?:/|\z)}.freeze
13
14
 
14
15
  def start_segment(event)
15
16
  return unless is_execution_traced?
@@ -39,7 +40,27 @@ module NewRelic
39
40
  end
40
41
 
41
42
  def metric_name(event)
42
- "Stripe#{event.path}/#{event.method}"
43
+ # Grab only the first 2 items from the slash (/) delimited event path.
44
+ # These items are the API version string and the category. Grabbing
45
+ # any more of the path will result in unique method names that will
46
+ # easily grow to be too numerous to sort through in the UI and
47
+ # possibly even violate default New Relic metric count thresholds.
48
+ # See newrelic/newrelic-ruby-agent#2654 and
49
+ # newrelic/newrelic-ruby-agent#2709 for more details.
50
+ #
51
+ # In Ruby v3.4 benchmarks, using regex to get at the first two path
52
+ # elements was seen as more performant than using String#split.
53
+ #
54
+ # Regex legend:
55
+ #
56
+ # ^ = starts with
57
+ # / = a literal '/'
58
+ # () = capture
59
+ # (?:) = don't capture
60
+ # [^/]+ = 1 or more characters that are not '/'
61
+ # /|\z = a literal '/' OR the end of the string
62
+ path_portion = event.path =~ PATH_PORTION_PATTERN ? Regexp.last_match(1) : NewRelic::UNKNOWN
63
+ "Stripe/#{path_portion}/#{event.method}"
43
64
  end
44
65
 
45
66
  def add_stripe_attributes(segment, event)
@@ -9,8 +9,6 @@ DependencyDetection.defer do
9
9
  named :thread
10
10
 
11
11
  executes do
12
- NewRelic::Agent.logger.info('Installing Thread Instrumentation')
13
-
14
12
  if use_prepend?
15
13
  prepend_instrument Thread, NewRelic::Agent::Instrumentation::MonitoredThread::Prepend
16
14
  else
@@ -11,10 +11,6 @@ DependencyDetection.defer do
11
11
 
12
12
  depends_on { defined?(Tilt) }
13
13
 
14
- executes do
15
- NewRelic::Agent.logger.info('Installing Tilt instrumentation')
16
- end
17
-
18
14
  executes do
19
15
  if use_prepend?
20
16
  prepend_instrument Tilt::Template, NewRelic::Agent::Instrumentation::Tilt::Prepend
@@ -18,7 +18,6 @@ DependencyDetection.defer do
18
18
  end
19
19
 
20
20
  executes do
21
- NewRelic::Agent.logger.info('Installing Typhoeus instrumentation')
22
21
  require 'new_relic/agent/distributed_tracing/cross_app_tracing'
23
22
  require 'new_relic/agent/http_clients/typhoeus_wrappers'
24
23
  end
@@ -10,19 +10,26 @@ module NewRelic::Agent::Instrumentation
10
10
  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)
11
11
 
12
12
  begin
13
- segment = NewRelic::Agent::Tracer.start_segment(
14
- name: metric_name(self.class.identifier, self.class.name)
15
- )
13
+ segment = NewRelic::Agent::Tracer.start_segment(name: metric_name)
16
14
  yield
17
15
  rescue => e
18
- ::NewRelic::Agent.logger.debug('Error capturing ViewComponent segment', e)
16
+ NewRelic::Agent.notice_error(e)
17
+ raise
19
18
  ensure
20
19
  segment&.finish
21
20
  end
22
21
  end
23
22
 
24
- def metric_name(identifier, component)
25
- "View/#{metric_path(identifier)}/#{component}"
23
+ def metric_name
24
+ # ViewComponent determines a component's identifier differently depending on the version
25
+ # https://github.com/ViewComponent/view_component/pull/2153
26
+ component_identifier = defined?(self.class.source_location) ? self.class.source_location : self.class.identifier
27
+
28
+ "View/#{metric_path(component_identifier)}/#{self.class.name}"
29
+ rescue => e
30
+ NewRelic::Agent.logger.error('Error identifying View Component metric name', e)
31
+
32
+ 'View/component'
26
33
  end
27
34
 
28
35
  def metric_path(identifier)
@@ -15,8 +15,6 @@ DependencyDetection.defer do
15
15
  end
16
16
 
17
17
  executes do
18
- NewRelic::Agent.logger.info('Installing ViewComponent instrumentation')
19
-
20
18
  if use_prepend?
21
19
  prepend_instrument ViewComponent::Base, NewRelic::Agent::Instrumentation::ViewComponent::Prepend
22
20
  else
@@ -164,9 +164,8 @@ module NewRelic
164
164
 
165
165
  def add_ssl_for_http(data)
166
166
  ssl_for_http = NewRelic::Agent.config[:'browser_monitoring.ssl_for_http']
167
- unless ssl_for_http.nil?
168
- data[SSL_FOR_HTTP_KEY] = ssl_for_http
169
- end
167
+
168
+ data[SSL_FOR_HTTP_KEY] = ssl_for_http if ssl_for_http
170
169
  end
171
170
 
172
171
  def add_attributes(data, txn)
@@ -0,0 +1,25 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic
6
+ module Agent
7
+ module Llm
8
+ class ChatCompletionMessage < LlmEvent
9
+ ATTRIBUTES = %i[content role sequence completion_id token_count
10
+ is_response]
11
+ EVENT_NAME = 'LlmChatCompletionMessage'
12
+
13
+ attr_accessor(*ATTRIBUTES)
14
+
15
+ def attributes
16
+ LlmEvent::ATTRIBUTES + ATTRIBUTES
17
+ end
18
+
19
+ def event_name
20
+ EVENT_NAME
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,66 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require_relative 'response_headers'
6
+
7
+ module NewRelic
8
+ module Agent
9
+ module Llm
10
+ class ChatCompletionSummary < LlmEvent
11
+ include ResponseHeaders
12
+
13
+ ATTRIBUTES = %i[request_max_tokens response_number_of_messages
14
+ request_model response_choices_finish_reason request_temperature
15
+ duration error]
16
+ ATTRIBUTE_NAME_EXCEPTIONS = {
17
+ response_number_of_messages: 'response.number_of_messages',
18
+ request_model: 'request.model',
19
+ response_choices_finish_reason: 'response.choices.finish_reason',
20
+ request_temperature: 'request.temperature'
21
+ }
22
+ ERROR_COMPLETION_ID = 'completion_id'
23
+ EVENT_NAME = 'LlmChatCompletionSummary'
24
+
25
+ attr_accessor(*ATTRIBUTES)
26
+
27
+ def attributes
28
+ LlmEvent::ATTRIBUTES + ResponseHeaders::ATTRIBUTES + ATTRIBUTES
29
+ end
30
+
31
+ def attribute_name_exceptions
32
+ # TODO: OLD RUBIES < 2.6
33
+ # Hash#merge accepts multiple arguments in 2.6
34
+ # Remove condition once support for Ruby <2.6 is dropped
35
+ if RUBY_VERSION >= '2.6.0'
36
+ LlmEvent::ATTRIBUTE_NAME_EXCEPTIONS.merge(ResponseHeaders::ATTRIBUTE_NAME_EXCEPTIONS, ATTRIBUTE_NAME_EXCEPTIONS)
37
+ else
38
+ LlmEvent::ATTRIBUTE_NAME_EXCEPTIONS.merge(ResponseHeaders::ATTRIBUTE_NAME_EXCEPTIONS).merge(ATTRIBUTE_NAME_EXCEPTIONS)
39
+ end
40
+ end
41
+
42
+ def event_name
43
+ EVENT_NAME
44
+ end
45
+
46
+ def error_attributes(exception)
47
+ attrs = {ERROR_COMPLETION_ID => id}
48
+
49
+ error_attributes_from_response(exception, attrs)
50
+ end
51
+
52
+ private
53
+
54
+ def error_attributes_from_response(exception, attrs)
55
+ return attrs unless exception.respond_to?(:response)
56
+
57
+ attrs[ERROR_ATTRIBUTE_STATUS_CODE] = exception.response.dig(:status)
58
+ attrs[ERROR_ATTRIBUTE_CODE] = exception.response.dig(:body, ERROR_STRING, CODE_STRING)
59
+ attrs[ERROR_ATTRIBUTE_PARAM] = exception.response.dig(:body, ERROR_STRING, PARAM_STRING)
60
+
61
+ attrs
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,60 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic
6
+ module Agent
7
+ module Llm
8
+ class Embedding < LlmEvent
9
+ include ResponseHeaders
10
+
11
+ ATTRIBUTES = %i[input request_model token_count duration error].freeze
12
+ ATTRIBUTE_NAME_EXCEPTIONS = {
13
+ request_model: 'request.model'
14
+ }.freeze
15
+ ERROR_EMBEDDING_ID = 'embedding_id'
16
+ EVENT_NAME = 'LlmEmbedding'
17
+
18
+ attr_accessor(*ATTRIBUTES)
19
+
20
+ def attributes
21
+ LlmEvent::ATTRIBUTES + ResponseHeaders::ATTRIBUTES + ATTRIBUTES
22
+ end
23
+
24
+ def attribute_name_exceptions
25
+ # TODO: OLD RUBIES < 2.6
26
+ # Hash#merge accepts multiple arguments in 2.6
27
+ # Remove condition once support for Ruby <2.6 is dropped
28
+ if RUBY_VERSION >= '2.6.0'
29
+ LlmEvent::ATTRIBUTE_NAME_EXCEPTIONS.merge(ResponseHeaders::ATTRIBUTE_NAME_EXCEPTIONS, ATTRIBUTE_NAME_EXCEPTIONS)
30
+ else
31
+ LlmEvent::ATTRIBUTE_NAME_EXCEPTIONS.merge(ResponseHeaders::ATTRIBUTE_NAME_EXCEPTIONS).merge(ATTRIBUTE_NAME_EXCEPTIONS)
32
+ end
33
+ end
34
+
35
+ def event_name
36
+ EVENT_NAME
37
+ end
38
+
39
+ def error_attributes(exception)
40
+ attrs = {}
41
+ attrs[ERROR_EMBEDDING_ID] = id
42
+
43
+ error_attributes_from_response(exception, attrs)
44
+ end
45
+
46
+ private
47
+
48
+ def error_attributes_from_response(exception, attrs)
49
+ return attrs unless exception.respond_to?(:response)
50
+
51
+ attrs[ERROR_ATTRIBUTE_STATUS_CODE] = exception.response.dig(:status)
52
+ attrs[ERROR_ATTRIBUTE_CODE] = exception.response.dig(:body, ERROR_STRING, CODE_STRING)
53
+ attrs[ERROR_ATTRIBUTE_PARAM] = exception.response.dig(:body, ERROR_STRING, PARAM_STRING)
54
+
55
+ attrs
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,95 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic
6
+ module Agent
7
+ module Llm
8
+ class LlmEvent
9
+ # Every subclass must define its own ATTRIBUTES constant, an array of symbols representing
10
+ # that class's unique attributes
11
+ ATTRIBUTES = %i[id request_id span_id trace_id response_model vendor
12
+ ingest_source metadata]
13
+ # These attributes should not be passed as arguments to initialize and will be set by the agent
14
+ AGENT_DEFINED_ATTRIBUTES = %i[span_id trace_id ingest_source]
15
+ # Some attributes have names that can't be written as symbols used for metaprogramming.
16
+ # The ATTRIBUTE_NAME_EXCEPTIONS hash should use the symbolized version of the name as the key
17
+ # and the string version expected by the UI as the value.
18
+ ATTRIBUTE_NAME_EXCEPTIONS = {response_model: 'response.model'}
19
+ INGEST_SOURCE = 'Ruby'
20
+ ERROR_ATTRIBUTE_STATUS_CODE = 'http.statusCode'
21
+ ERROR_ATTRIBUTE_CODE = 'error.code'
22
+ ERROR_ATTRIBUTE_PARAM = 'error.param'
23
+ ERROR_STRING = 'error'
24
+ CODE_STRING = 'code'
25
+ PARAM_STRING = 'param'
26
+
27
+ attr_accessor(*ATTRIBUTES)
28
+
29
+ def self.set_llm_agent_attribute_on_transaction
30
+ NewRelic::Agent::Transaction.add_agent_attribute(:llm, true, NewRelic::Agent::AttributeFilter::DST_TRANSACTION_EVENTS)
31
+ end
32
+
33
+ # This initialize method is used for all subclasses.
34
+ # It leverages the subclass's `attributes` method to iterate through
35
+ # all the attributes for that subclass.
36
+ # It assigns instance variables for all arguments passed to the method.
37
+ # It also assigns agent-defined attributes.
38
+ def initialize(opts = {})
39
+ (attributes - AGENT_DEFINED_ATTRIBUTES).each do |attr|
40
+ instance_variable_set(:"@#{attr}", opts[attr]) if opts.key?(attr)
41
+ end
42
+
43
+ @id = id || NewRelic::Agent::GuidGenerator.generate_guid
44
+ @span_id = NewRelic::Agent::Tracer.current_span_id
45
+ @trace_id = NewRelic::Agent::Tracer.current_trace_id
46
+ @ingest_source = INGEST_SOURCE
47
+ end
48
+
49
+ # All subclasses use event_attributes to get a full hash of all
50
+ # attributes and their values
51
+ def event_attributes
52
+ attributes_hash = attributes.each_with_object({}) do |attr, hash|
53
+ hash[replace_attr_with_string(attr)] = instance_variable_get(:"@#{attr}")
54
+ end
55
+ attributes_hash.merge!(metadata) && attributes_hash.delete(:metadata) if !metadata.nil?
56
+
57
+ attributes_hash
58
+ end
59
+
60
+ # Subclasses define an attributes method to concatenate attributes
61
+ # defined across their ancestors and other modules
62
+ def attributes
63
+ ATTRIBUTES
64
+ end
65
+
66
+ # Subclasses that record events will override this method
67
+ def event_name
68
+ end
69
+
70
+ # Some attribute names include periods, which aren't valid values for
71
+ # Ruby method names. This method returns a Hash with the key as the
72
+ # Ruby symbolized version of the attribute and the value as the
73
+ # period-delimited string expected upstream.
74
+ def attribute_name_exceptions
75
+ ATTRIBUTE_NAME_EXCEPTIONS
76
+ end
77
+
78
+ def record
79
+ NewRelic::Agent.record_custom_event(event_name, event_attributes)
80
+ end
81
+
82
+ # Subclasses that add attributes to noticed errors will override this method
83
+ def error_attributes(exception)
84
+ NewRelic::EMPTY_HASH
85
+ end
86
+
87
+ private
88
+
89
+ def replace_attr_with_string(attr)
90
+ attribute_name_exceptions.fetch(attr, attr)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,80 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic
6
+ module Agent
7
+ module Llm
8
+ module ResponseHeaders
9
+ ATTRIBUTES = %i[response_organization llm_version ratelimit_limit_requests
10
+ ratelimit_limit_tokens ratelimit_remaining_requests
11
+ ratelimit_remaining_tokens ratelimit_reset_requests
12
+ ratelimit_reset_tokens ratelimit_limit_tokens_usage_based
13
+ ratelimit_reset_tokens_usage_based
14
+ ratelimit_remaining_tokens_usage_based].freeze
15
+
16
+ ATTRIBUTE_NAME_EXCEPTIONS = {
17
+ response_organization: 'response.organization',
18
+ llm_version: 'response.headers.llmVersion',
19
+ ratelimit_limit_requests: 'response.headers.ratelimitLimitRequests',
20
+ ratelimit_limit_tokens: 'response.headers.ratelimitLimitTokens',
21
+ ratelimit_remaining_requests: 'response.headers.ratelimitRemainingRequests',
22
+ ratelimit_remaining_tokens: 'response.headers.ratelimitRemainingTokens',
23
+ ratelimit_reset_requests: 'response.headers.ratelimitResetRequests',
24
+ ratelimit_reset_tokens: 'response.headers.ratelimitResetTokens',
25
+ ratelimit_limit_tokens_usage_based: 'response.headers.ratelimitLimitTokensUsageBased',
26
+ ratelimit_reset_tokens_usage_based: 'response.headers.ratelimitResetTokensUsageBased',
27
+ ratelimit_remaining_tokens_usage_based: 'response.headers.ratelimitRemainingTokensUsageBased'
28
+ }.freeze
29
+
30
+ OPENAI_ORGANIZATION = 'openai-organization'
31
+ OPENAI_VERSION = 'openai-version'
32
+ X_RATELIMIT_LIMIT_REQUESTS = 'x-ratelimit-limit-requests'
33
+ X_RATELIMIT_LIMIT_TOKENS = 'x-ratelimit-limit-tokens'
34
+ X_RATELIMIT_REMAINING_REQUESTS = 'x-ratelimit-remaining-requests'
35
+ X_RATELIMIT_REMAINING_TOKENS = 'x-ratelimit-remaining-tokens'
36
+ X_RATELIMIT_RESET_REQUESTS = 'x-ratelimit-reset-requests'
37
+ X_RATELIMIT_RESET_TOKENS = 'x-ratelimit-reset-tokens'
38
+ X_RATELIMIT_LIMIT_TOKENS_USAGE_BASED = 'x-ratelimit-limit-tokens-usage-based'
39
+ X_RATELIMIT_RESET_TOKENS_USAGE_BASED = 'x-ratelimit-reset-tokens-usage-based'
40
+ X_RATELIMIT_REMAINING_TOKENS_USAGE_BASED = 'x-ratelimit-remaining-tokens-usage-based'
41
+ X_REQUEST_ID = 'x-request-id'
42
+
43
+ attr_accessor(*ATTRIBUTES)
44
+
45
+ # Headers is a hash of Net::HTTP response headers
46
+ def populate_openai_response_headers(headers)
47
+ # Embedding, ChatCompletionSummary, and ChatCompletionMessage all need
48
+ # request_id, so it's defined in LlmEvent. ChatCompletionMessage
49
+ # adds the attribute via ChatCompletionSummary.
50
+ self.request_id = headers[X_REQUEST_ID]&.first
51
+ self.response_organization = headers[OPENAI_ORGANIZATION]&.first
52
+ self.llm_version = headers[OPENAI_VERSION]&.first
53
+ self.ratelimit_limit_requests = headers[X_RATELIMIT_LIMIT_REQUESTS]&.first.to_i
54
+ self.ratelimit_limit_tokens = headers[X_RATELIMIT_LIMIT_TOKENS]&.first.to_i
55
+ remaining_headers(headers)
56
+ reset_headers(headers)
57
+ tokens_usage_based_headers(headers)
58
+ end
59
+
60
+ private
61
+
62
+ def remaining_headers(headers)
63
+ self.ratelimit_remaining_requests = headers[X_RATELIMIT_REMAINING_REQUESTS]&.first.to_i
64
+ self.ratelimit_remaining_tokens = headers[X_RATELIMIT_REMAINING_TOKENS]&.first.to_i
65
+ end
66
+
67
+ def reset_headers(headers)
68
+ self.ratelimit_reset_requests = headers[X_RATELIMIT_RESET_REQUESTS]&.first
69
+ self.ratelimit_reset_tokens = headers[X_RATELIMIT_RESET_TOKENS]&.first
70
+ end
71
+
72
+ def tokens_usage_based_headers(headers)
73
+ self.ratelimit_limit_tokens_usage_based = headers[X_RATELIMIT_LIMIT_TOKENS_USAGE_BASED]&.first.to_i
74
+ self.ratelimit_reset_tokens_usage_based = headers[X_RATELIMIT_RESET_TOKENS_USAGE_BASED]&.first
75
+ self.ratelimit_remaining_tokens_usage_based = headers[X_RATELIMIT_REMAINING_TOKENS_USAGE_BASED]&.first.to_i
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,49 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require_relative 'llm/llm_event'
6
+ require_relative 'llm/chat_completion_message'
7
+ require_relative 'llm/chat_completion_summary'
8
+ require_relative 'llm/embedding'
9
+ require_relative 'llm/response_headers'
10
+
11
+ module NewRelic
12
+ module Agent
13
+ class LLM
14
+ INPUT = 'input'
15
+ CONTENT = 'content'
16
+ SEGMENT_PATTERN = %r{Llm/.+/OpenAI/.+}.freeze
17
+
18
+ def self.instrumentation_enabled?
19
+ NewRelic::Agent.config[:'ai_monitoring.enabled']
20
+ end
21
+
22
+ # LLM content-related attributes are exempt from the 4095 byte limit
23
+ def self.exempt_event_attribute?(type, key)
24
+ return false unless instrumentation_enabled?
25
+
26
+ (type == NewRelic::Agent::Llm::Embedding::EVENT_NAME && key == INPUT) ||
27
+ (type == NewRelic::Agent::Llm::ChatCompletionMessage::EVENT_NAME && key == CONTENT)
28
+ end
29
+
30
+ def self.openai?
31
+ @openai ||= %i[prepend chain].include?(NewRelic::Agent.config[:'instrumentation.ruby_openai']) &&
32
+ NewRelic::Agent.config[:'ai_monitoring.enabled']
33
+ end
34
+
35
+ # Used in NetHTTP instrumentation
36
+ def self.openai_parent?(segment)
37
+ return false unless openai?
38
+
39
+ segment&.parent&.name&.match?(SEGMENT_PATTERN)
40
+ end
41
+
42
+ def self.populate_openai_response_headers(response, parent)
43
+ return unless parent.instance_variable_defined?(:@llm_event)
44
+
45
+ parent.llm_event.populate_openai_response_headers(response.to_hash)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -12,6 +12,12 @@ module NewRelic
12
12
  return message unless decorating_enabled?
13
13
 
14
14
  metadata = NewRelic::Agent.linking_metadata
15
+
16
+ if message.is_a?(Hash)
17
+ message.merge!(metadata) unless message.frozen?
18
+ return
19
+ end
20
+
15
21
  formatted_metadata = " NR-LINKING|#{metadata[ENTITY_GUID_KEY]}|#{metadata[HOSTNAME_KEY]}|" \
16
22
  "#{metadata[TRACE_ID_KEY]}|#{metadata[SPAN_ID_KEY]}|" \
17
23
  "#{escape_entity_name(metadata[ENTITY_NAME_KEY])}|"
@@ -23,7 +29,8 @@ module NewRelic
23
29
 
24
30
  def decorating_enabled?
25
31
  NewRelic::Agent.config[:'application_logging.enabled'] &&
26
- NewRelic::Agent::Instrumentation::Logger.enabled? &&
32
+ (NewRelic::Agent::Instrumentation::Logger.enabled? ||
33
+ NewRelic::Agent::Instrumentation::LogStasher.enabled?) &&
27
34
  NewRelic::Agent.config[:'application_logging.local_decorating.enabled']
28
35
  end
29
36