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
@@ -0,0 +1,26 @@
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::Agent::Instrumentation
6
+ module RedisClient
7
+ module ClusterMiddleware
8
+ include NewRelic::Agent::Instrumentation::Redis
9
+
10
+ # Until we decide to move our Redis instrumentation entirely off patches
11
+ # keep the middleware instrumentation for the call and connect methods
12
+ # limited to the redis-clustering instrumentation.
13
+ #
14
+ # Redis's middleware option does not capture errors as high in the stack
15
+ # as our patches. Leaving the patches for call and connect on the main
16
+ # Redis gem limits the feature disparity our customers experience.
17
+ def call(*args, &block)
18
+ call_with_tracing(args[0]) { super }
19
+ end
20
+
21
+ def connect(*args, &block)
22
+ connect_with_tracing { super }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -9,14 +9,14 @@ module NewRelic::Agent::Instrumentation
9
9
  INSTRUMENTATION_NAME = NewRelic::Agent.base_name(name)
10
10
 
11
11
  def connect_with_tracing
12
- with_tracing(Constants::CONNECT, database: db) { yield }
12
+ with_tracing(Constants::CONNECT, database: _nr_db) { yield }
13
13
  end
14
14
 
15
15
  def call_with_tracing(command, &block)
16
16
  operation = command[0]
17
17
  statement = ::NewRelic::Agent::Datastores::Redis.format_command(command)
18
18
 
19
- with_tracing(operation, statement: statement, database: db) { yield }
19
+ with_tracing(operation, statement: statement, database: _nr_db) { yield }
20
20
  end
21
21
 
22
22
  # Used for Redis 4.x and 3.x
@@ -24,22 +24,15 @@ module NewRelic::Agent::Instrumentation
24
24
  operation = pipeline.is_a?(::Redis::Pipeline::Multi) ? Constants::MULTI_OPERATION : Constants::PIPELINE_OPERATION
25
25
  statement = ::NewRelic::Agent::Datastores::Redis.format_pipeline_commands(pipeline.commands)
26
26
 
27
- with_tracing(operation, statement: statement, database: db) { yield }
27
+ with_tracing(operation, statement: statement, database: _nr_db) { yield }
28
28
  end
29
29
 
30
30
  # Used for Redis 5.x+
31
31
  def call_pipelined_with_tracing(pipeline)
32
- db = begin
33
- _nr_redis_client_config.db
34
- rescue StandardError => e
35
- NewRelic::Agent.logger.error("Failed to determine configured Redis db value: #{e.class} - #{e.message}")
36
- nil
37
- end
38
-
39
32
  operation = pipeline.flatten.include?('MULTI') ? Constants::MULTI_OPERATION : Constants::PIPELINE_OPERATION
40
33
  statement = ::NewRelic::Agent::Datastores::Redis.format_pipeline_commands(pipeline)
41
34
 
42
- with_tracing(operation, statement: statement, database: db) { yield }
35
+ with_tracing(operation, statement: statement, database: _nr_db) { yield }
43
36
  end
44
37
 
45
38
  private
@@ -94,5 +87,15 @@ module NewRelic::Agent::Instrumentation
94
87
  config
95
88
  end
96
89
  end
90
+
91
+ def _nr_db
92
+ # db is a method on the Redis client in versions < 5.x
93
+ return db if respond_to?(:db)
94
+ # db is accessible through the RedisClient::Config object in versions > 5.x
95
+ return _nr_redis_client_config.db if _nr_redis_client_config.respond_to?(:db)
96
+ rescue StandardError => e
97
+ NewRelic::Agent.logger.debug("Failed to determine configured Redis db value: #{e.class} - #{e.message}")
98
+ nil
99
+ end
97
100
  end
98
101
  end
@@ -6,6 +6,9 @@ module NewRelic::Agent::Instrumentation
6
6
  module RedisClient
7
7
  module Middleware
8
8
  # This module is used to instrument Redis 5.x+
9
+ #
10
+ # It only instruments call_pipelined because connect and call are accessed
11
+ # too late in the stack to capture all errors
9
12
  include NewRelic::Agent::Instrumentation::Redis
10
13
 
11
14
  def call_pipelined(*args, &block)
@@ -10,6 +10,7 @@ require_relative 'redis/chain'
10
10
  require_relative 'redis/constants'
11
11
  require_relative 'redis/prepend'
12
12
  require_relative 'redis/middleware'
13
+ require_relative 'redis/cluster_middleware'
13
14
 
14
15
  DependencyDetection.defer do
15
16
  # Why not :redis? newrelic-redis used that name, so avoid conflicting
@@ -30,15 +31,20 @@ DependencyDetection.defer do
30
31
  end
31
32
 
32
33
  executes do
33
- NewRelic::Agent.logger.info('Installing Redis Instrumentation')
34
34
  if NewRelic::Agent::Instrumentation::Redis::Constants::HAS_REDIS_CLIENT
35
35
  RedisClient.register(NewRelic::Agent::Instrumentation::RedisClient::Middleware)
36
+
37
+ if defined?(Redis::Cluster::Client)
38
+ RedisClient.register(NewRelic::Agent::Instrumentation::RedisClient::ClusterMiddleware)
39
+ end
36
40
  end
37
41
 
38
- if use_prepend?
39
- prepend_instrument Redis::Client, NewRelic::Agent::Instrumentation::Redis::Prepend
40
- else
41
- chain_instrument NewRelic::Agent::Instrumentation::Redis::Chain
42
+ unless defined?(Redis::Cluster::Client)
43
+ if use_prepend?
44
+ prepend_instrument Redis::Client, NewRelic::Agent::Instrumentation::Redis::Prepend
45
+ else
46
+ chain_instrument NewRelic::Agent::Instrumentation::Redis::Chain
47
+ end
42
48
  end
43
49
  end
44
50
  end
@@ -18,10 +18,6 @@ DependencyDetection.defer do
18
18
  defined?(Airbrake) && defined?(Airbrake::AIRBRAKE_VERSION) && Gem::Version.create(Airbrake::AIRBRAKE_VERSION) < Gem::Version.create('11.0.3')
19
19
  end
20
20
 
21
- executes do
22
- NewRelic::Agent.logger.info('Installing Resque instrumentation')
23
- end
24
-
25
21
  executes do
26
22
  if NewRelic::Agent.config[:'resque.use_ruby_dns'] && NewRelic::Agent.config[:dispatcher] == :resque
27
23
  NewRelic::Agent.logger.info('Requiring resolv-replace')
@@ -20,15 +20,15 @@ DependencyDetection.defer do
20
20
  require_relative '../../rack/agent_hooks'
21
21
  require_relative '../../rack/browser_monitoring'
22
22
 
23
- NewRelic::Agent.logger.info('Installing Roda instrumentation')
24
-
25
23
  if use_prepend?
26
24
  require_relative 'roda/prepend'
27
- prepend_instrument Roda.singleton_class, NewRelic::Agent::Instrumentation::Roda::Build::Prepend
25
+
26
+ supportability_name = NewRelic::Agent::Instrumentation::Roda::Tracer::INSTRUMENTATION_NAME
27
+ prepend_instrument Roda.singleton_class, NewRelic::Agent::Instrumentation::Roda::Build::Prepend, supportability_name
28
28
  prepend_instrument Roda, NewRelic::Agent::Instrumentation::Roda::Prepend
29
29
  else
30
30
  require_relative 'roda/chain'
31
- chain_instrument NewRelic::Agent::Instrumentation::Roda::Build::Chain
31
+ chain_instrument NewRelic::Agent::Instrumentation::Roda::Build::Chain, supportability_name
32
32
  chain_instrument NewRelic::Agent::Instrumentation::Roda::Chain
33
33
  end
34
34
  Roda.class_eval { extend NewRelic::Agent::Instrumentation::Roda::Ignorer }
@@ -0,0 +1,55 @@
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::Agent::Instrumentation
6
+ module RubyKafka::Chain
7
+ def self.instrument!
8
+ ::Kafka::Producer.class_eval do
9
+ include NewRelic::Agent::Instrumentation::RubyKafka
10
+
11
+ alias_method(:produce_without_new_relic, :produce)
12
+
13
+ def produce(value, **kwargs)
14
+ produce_with_new_relic(value, **kwargs) do |headers|
15
+ kwargs[:headers] = headers
16
+ produce_without_new_relic(value, **kwargs)
17
+ end
18
+ end
19
+ end
20
+
21
+ ::Kafka::Consumer.class_eval do
22
+ include NewRelic::Agent::Instrumentation::RubyKafka
23
+
24
+ alias_method(:each_message_without_new_relic, :each_message)
25
+
26
+ def each_message(*args)
27
+ each_message_without_new_relic(*args) do |message|
28
+ each_message_with_new_relic(message) do
29
+ yield(message)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ ::Kafka::Client.class_eval do
36
+ include NewRelic::Agent::Instrumentation::RubyKafkaConfig
37
+
38
+ alias_method(:producer_without_new_relic, :producer)
39
+ alias_method(:consumer_without_new_relic, :consumer)
40
+
41
+ def producer(**kwargs)
42
+ producer_without_new_relic(**kwargs).tap do |producer|
43
+ set_nr_config(producer)
44
+ end
45
+ end
46
+
47
+ def consumer(**kwargs)
48
+ consumer_without_new_relic(**kwargs).tap do |consumer|
49
+ set_nr_config(consumer)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,67 @@
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 'new_relic/agent/messaging'
6
+
7
+ module NewRelic::Agent::Instrumentation
8
+ module RubyKafka
9
+ MESSAGING_LIBRARY = 'Kafka'
10
+ PRODUCE = 'Produce'
11
+ CONSUME = 'Consume'
12
+
13
+ INSTRUMENTATION_NAME = 'ruby-kafka'
14
+
15
+ def produce_with_new_relic(value, **kwargs)
16
+ NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)
17
+
18
+ topic_name = kwargs[:topic]
19
+ segment = NewRelic::Agent::Tracer.start_message_broker_segment(
20
+ action: :produce,
21
+ library: MESSAGING_LIBRARY,
22
+ destination_type: :topic,
23
+ destination_name: topic_name
24
+ )
25
+ create_kafka_metrics(action: PRODUCE, topic: topic_name)
26
+
27
+ headers = kwargs[:headers] || {}
28
+ ::NewRelic::Agent::DistributedTracing.insert_distributed_trace_headers(headers)
29
+
30
+ NewRelic::Agent::Tracer.capture_segment_error(segment) { yield(headers) }
31
+ ensure
32
+ segment&.finish
33
+ end
34
+
35
+ def each_message_with_new_relic(message)
36
+ NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)
37
+
38
+ headers = message&.headers || {}
39
+ topic_name = message&.topic
40
+
41
+ NewRelic::Agent::Messaging.wrap_message_broker_consume_transaction(
42
+ library: MESSAGING_LIBRARY,
43
+ destination_type: :topic,
44
+ destination_name: topic_name,
45
+ headers: headers,
46
+ action: :consume
47
+ ) do
48
+ create_kafka_metrics(action: CONSUME, topic: topic_name)
49
+ yield
50
+ end
51
+ end
52
+
53
+ def create_kafka_metrics(action:, topic:)
54
+ @nr_config.each do |seed_broker|
55
+ host = "#{seed_broker&.host}:#{seed_broker&.port}"
56
+ NewRelic::Agent.record_metric("MessageBroker/Kafka/Nodes/#{host}/#{action}/#{topic}", 1)
57
+ NewRelic::Agent.record_metric("MessageBroker/Kafka/Nodes/#{host}", 1)
58
+ end
59
+ end
60
+ end
61
+
62
+ module RubyKafkaConfig
63
+ def set_nr_config(producer_or_consumer)
64
+ producer_or_consumer.instance_variable_set(:@nr_config, @seed_brokers)
65
+ end
66
+ end
67
+ 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::Agent::Instrumentation
6
+ module RubyKafkaProducer
7
+ module Prepend
8
+ include NewRelic::Agent::Instrumentation::RubyKafka
9
+
10
+ def produce(value, **kwargs)
11
+ produce_with_new_relic(value, **kwargs) do |headers|
12
+ kwargs[:headers] = headers
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ module RubyKafkaConsumer
20
+ module Prepend
21
+ include NewRelic::Agent::Instrumentation::RubyKafka
22
+
23
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3')
24
+ def each_message(**args)
25
+ super do |message|
26
+ each_message_with_new_relic(message) do
27
+ yield(message)
28
+ end
29
+ end
30
+ end
31
+ else
32
+ def each_message(*args)
33
+ super do |message|
34
+ each_message_with_new_relic(message) do
35
+ yield(message)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ module RubyKafkaClient
44
+ module Prepend
45
+ include NewRelic::Agent::Instrumentation::RubyKafkaConfig
46
+
47
+ def producer(**kwargs)
48
+ super.tap do |producer|
49
+ set_nr_config(producer)
50
+ end
51
+ end
52
+
53
+ def consumer(**kwargs)
54
+ super.tap do |consumer|
55
+ set_nr_config(consumer)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -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
+ require_relative 'ruby_kafka/instrumentation'
6
+ require_relative 'ruby_kafka/chain'
7
+ require_relative 'ruby_kafka/prepend'
8
+
9
+ DependencyDetection.defer do
10
+ named :'ruby_kafka'
11
+
12
+ depends_on do
13
+ defined?(Kafka)
14
+ end
15
+
16
+ executes do
17
+ if use_prepend?
18
+ prepend_instrument Kafka::Producer, NewRelic::Agent::Instrumentation::RubyKafkaProducer::Prepend
19
+ prepend_instrument Kafka::Consumer, NewRelic::Agent::Instrumentation::RubyKafkaConsumer::Prepend
20
+ prepend_instrument Kafka::Client, NewRelic::Agent::Instrumentation::RubyKafkaClient::Prepend
21
+ else
22
+ chain_instrument NewRelic::Agent::Instrumentation::RubyKafka::Chain
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,36 @@
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::Agent::Instrumentation
6
+ module OpenAI::Chain
7
+ def self.instrument!
8
+ ::OpenAI::Client.class_eval do
9
+ include NewRelic::Agent::Instrumentation::OpenAI
10
+
11
+ alias_method(:json_post_without_new_relic, :json_post)
12
+
13
+ # In versions 4.0.0+ json_post is an instance method
14
+ # defined in the OpenAI::HTTP module, included by the
15
+ # OpenAI::Client class
16
+ def json_post(**kwargs)
17
+ json_post_with_new_relic(**kwargs) do
18
+ json_post_without_new_relic(**kwargs)
19
+ end
20
+ end
21
+
22
+ # In versions below 4.0.0 json_post is a class method
23
+ # on OpenAI::Client
24
+ class << self
25
+ alias_method(:json_post_without_new_relic, :json_post)
26
+
27
+ def json_post(**kwargs)
28
+ json_post_with_new_relic(**kwargs) do
29
+ json_post_without_new_relic(**kwargs)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,196 @@
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::Agent::Instrumentation
6
+ module OpenAI
7
+ VENDOR = 'openAI' # AIM expects this capitalization style for the UI
8
+ INSTRUMENTATION_NAME = NewRelic::Agent.base_name(name)
9
+ EMBEDDINGS_PATH = '/embeddings'
10
+ CHAT_COMPLETIONS_PATH = '/chat/completions'
11
+ EMBEDDINGS_SEGMENT_NAME = 'Llm/embedding/OpenAI/embeddings'
12
+ CHAT_COMPLETIONS_SEGMENT_NAME = 'Llm/completion/OpenAI/chat'
13
+
14
+ def json_post_with_new_relic(path:, parameters:)
15
+ return yield unless path == EMBEDDINGS_PATH || path == CHAT_COMPLETIONS_PATH
16
+
17
+ NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)
18
+ NewRelic::Agent::Llm::LlmEvent.set_llm_agent_attribute_on_transaction
19
+ record_openai_metric
20
+
21
+ if path == EMBEDDINGS_PATH
22
+ embeddings_instrumentation(parameters) { yield }
23
+ elsif path == CHAT_COMPLETIONS_PATH
24
+ chat_completions_instrumentation(parameters) { yield }
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def embeddings_instrumentation(parameters)
31
+ segment = NewRelic::Agent::Tracer.start_segment(name: EMBEDDINGS_SEGMENT_NAME)
32
+ event = create_embeddings_event(parameters)
33
+ segment.llm_event = event
34
+ begin
35
+ response = NewRelic::Agent::Tracer.capture_segment_error(segment) { yield }
36
+ # TODO: Remove !response.include?('error') when we drop support for versions below 4.0.0
37
+ add_embeddings_response_params(response, event) if response && !response.include?('error')
38
+
39
+ response
40
+ ensure
41
+ finish(segment, event)
42
+ end
43
+ end
44
+
45
+ def chat_completions_instrumentation(parameters)
46
+ segment = NewRelic::Agent::Tracer.start_segment(name: CHAT_COMPLETIONS_SEGMENT_NAME)
47
+ event = create_chat_completion_summary(parameters)
48
+ segment.llm_event = event
49
+ messages = create_chat_completion_messages(parameters, event.id)
50
+
51
+ begin
52
+ response = NewRelic::Agent::Tracer.capture_segment_error(segment) { yield }
53
+ # TODO: Remove !response.include?('error') when we drop support for versions below 4.0.0
54
+ if response && !response.include?('error')
55
+ add_chat_completion_response_params(parameters, response, event)
56
+ messages = update_chat_completion_messages(messages, response, event)
57
+ end
58
+
59
+ response
60
+ ensure
61
+ finish(segment, event)
62
+ messages&.each { |m| m.record }
63
+ end
64
+ end
65
+
66
+ def create_chat_completion_summary(parameters)
67
+ NewRelic::Agent::Llm::ChatCompletionSummary.new(
68
+ vendor: VENDOR,
69
+ request_max_tokens: (parameters[:max_tokens] || parameters['max_tokens'])&.to_i,
70
+ request_model: parameters[:model] || parameters['model'],
71
+ request_temperature: (parameters[:temperature] || parameters['temperature'])&.to_f,
72
+ metadata: llm_custom_attributes
73
+ )
74
+ end
75
+
76
+ def create_embeddings_event(parameters)
77
+ event = NewRelic::Agent::Llm::Embedding.new(
78
+ vendor: VENDOR,
79
+ request_model: parameters[:model] || parameters['model'],
80
+ metadata: llm_custom_attributes
81
+ )
82
+ add_input(event, (parameters[:input] || parameters['input']))
83
+
84
+ event
85
+ end
86
+
87
+ def add_chat_completion_response_params(parameters, response, event)
88
+ event.response_number_of_messages = (parameters[:messages] || parameters['messages']).size + response['choices'].size
89
+ # The response hash always returns keys as strings, so we don't need to run an || check here
90
+ event.response_model = response['model']
91
+ event.response_choices_finish_reason = response['choices'][0]['finish_reason']
92
+ end
93
+
94
+ def add_embeddings_response_params(response, event)
95
+ event.response_model = response['model']
96
+ event.token_count = calculate_token_count(event.request_model, event.input)
97
+ end
98
+
99
+ def create_chat_completion_messages(parameters, summary_id)
100
+ (parameters[:messages] || parameters['messages']).map.with_index do |message, index|
101
+ msg = NewRelic::Agent::Llm::ChatCompletionMessage.new(
102
+ role: message[:role] || message['role'],
103
+ sequence: index,
104
+ completion_id: summary_id,
105
+ vendor: VENDOR
106
+ )
107
+ add_content(msg, (message[:content] || message['content']))
108
+
109
+ msg
110
+ end
111
+ end
112
+
113
+ def create_chat_completion_response_messages(response, sequence_origin, summary_id)
114
+ response['choices'].map.with_index(sequence_origin) do |choice, index|
115
+ msg = NewRelic::Agent::Llm::ChatCompletionMessage.new(
116
+ role: choice['message']['role'],
117
+ sequence: index,
118
+ completion_id: summary_id,
119
+ vendor: VENDOR,
120
+ is_response: true
121
+ )
122
+ add_content(msg, choice['message']['content'])
123
+
124
+ msg
125
+ end
126
+ end
127
+
128
+ def update_chat_completion_messages(messages, response, summary)
129
+ messages += create_chat_completion_response_messages(response, messages.size, summary.id)
130
+ response_id = response['id'] || NewRelic::Agent::GuidGenerator.generate_guid
131
+ messages.each do |message|
132
+ message.id = "#{response_id}-#{message.sequence}"
133
+ message.request_id = summary.request_id
134
+ message.response_model = response['model']
135
+ message.metadata = llm_custom_attributes
136
+
137
+ model = message.is_response ? message.response_model : summary.request_model
138
+
139
+ message.token_count = calculate_token_count(model, message.content)
140
+ end
141
+ end
142
+
143
+ def calculate_token_count(model, content)
144
+ return unless NewRelic::Agent.llm_token_count_callback
145
+
146
+ begin
147
+ count = NewRelic::Agent.llm_token_count_callback.call({model: model, content: content})
148
+ rescue => e
149
+ NewRelic::Agent.logger.warn("Error calculating token count using the provided proc. Error: #{e}'")
150
+ end
151
+
152
+ count if count.is_a?(Integer) && count > 0
153
+ end
154
+
155
+ def record_content_enabled?
156
+ NewRelic::Agent.config[:'ai_monitoring.record_content.enabled']
157
+ end
158
+
159
+ def add_content(message, content)
160
+ message.content = content if record_content_enabled?
161
+ end
162
+
163
+ def add_input(event, input)
164
+ event.input = input if record_content_enabled?
165
+ end
166
+
167
+ def llm_custom_attributes
168
+ NewRelic::Agent::Tracer.current_transaction&.attributes&.custom_attributes&.select { |k| k.to_s.match(/llm.*/) }
169
+ end
170
+
171
+ def record_openai_metric
172
+ NewRelic::Agent.record_metric(nr_supportability_metric, 0.0)
173
+ end
174
+
175
+ def segment_noticed_error?(segment)
176
+ segment&.noticed_error
177
+ end
178
+
179
+ def nr_supportability_metric
180
+ @nr_supportability_metric ||= "Supportability/Ruby/ML/OpenAI/#{::OpenAI::VERSION}"
181
+ end
182
+
183
+ def finish(segment, event)
184
+ segment&.finish
185
+
186
+ return unless event
187
+
188
+ if segment
189
+ event.error = true if segment_noticed_error?(segment)
190
+ event.duration = segment.duration
191
+ end
192
+
193
+ event.record
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,20 @@
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::Agent::Instrumentation
6
+ module OpenAI::Prepend
7
+ include NewRelic::Agent::Instrumentation::OpenAI
8
+
9
+ # In versions 4.0.0+ json_post is an instance method defined in the
10
+ # OpenAI::HTTP module, included by the OpenAI::Client class.
11
+ #
12
+ # In versions below 4.0.0 json_post is a class method on OpenAI::Client.
13
+ #
14
+ # Dependency detection will apply the instrumentation to the correct scope,
15
+ # so we don't need to change the code here.
16
+ def json_post(**kwargs)
17
+ json_post_with_new_relic(**kwargs) { super }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,35 @@
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 'ruby_openai/instrumentation'
6
+ require_relative 'ruby_openai/chain'
7
+ require_relative 'ruby_openai/prepend'
8
+
9
+ DependencyDetection.defer do
10
+ named :'ruby_openai'
11
+
12
+ depends_on do
13
+ NewRelic::Agent.config[:'ai_monitoring.enabled'] &&
14
+ defined?(OpenAI) && defined?(OpenAI::Client) &&
15
+ Gem::Version.new(OpenAI::VERSION) >= Gem::Version.new('3.4.0')
16
+ end
17
+
18
+ executes do
19
+ if use_prepend?
20
+ # TODO: Remove condition when we drop support for versions below 5.0.0
21
+ if Gem::Version.new(OpenAI::VERSION) >= Gem::Version.new('5.0.0')
22
+ prepend_instrument OpenAI::Client,
23
+ NewRelic::Agent::Instrumentation::OpenAI::Prepend,
24
+ NewRelic::Agent::Instrumentation::OpenAI::VENDOR
25
+ else
26
+ prepend_instrument OpenAI::Client.singleton_class,
27
+ NewRelic::Agent::Instrumentation::OpenAI::Prepend,
28
+ NewRelic::Agent::Instrumentation::OpenAI::VENDOR
29
+ end
30
+ else
31
+ chain_instrument NewRelic::Agent::Instrumentation::OpenAI::Chain,
32
+ NewRelic::Agent::Instrumentation::OpenAI::VENDOR
33
+ end
34
+ end
35
+ end
@@ -41,18 +41,4 @@ DependencyDetection.defer do
41
41
  end
42
42
  end
43
43
  end
44
-
45
- executes do
46
- next unless Gem::Version.new(Sidekiq::VERSION) < Gem::Version.new('5.0.0')
47
-
48
- deprecation_msg = 'Instrumentation for Sidekiq versions below 5.0.0 is deprecated ' \
49
- 'and will be dropped entirely in a future major New Relic Ruby agent release.' \
50
- 'Please upgrade your Sidekiq version to continue receiving full support. '
51
-
52
- NewRelic::Agent.logger.log_once(
53
- :warn,
54
- :deprecated_sidekiq_version,
55
- deprecation_msg
56
- )
57
- end
58
44
  end