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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +376 -2
- data/README.md +17 -18
- data/Rakefile +1 -1
- data/lib/boot/strap.rb +101 -0
- data/lib/new_relic/agent/agent.rb +4 -1
- data/lib/new_relic/agent/agent_helpers/connect.rb +10 -8
- data/lib/new_relic/agent/agent_helpers/start_worker_thread.rb +1 -1
- data/lib/new_relic/agent/agent_helpers/startup.rb +2 -1
- data/lib/new_relic/agent/agent_logger.rb +3 -1
- data/lib/new_relic/agent/aws.rb +68 -0
- data/lib/new_relic/agent/configuration/default_source.rb +519 -23
- data/lib/new_relic/agent/configuration/environment_source.rb +14 -2
- data/lib/new_relic/agent/configuration/high_security_source.rb +1 -0
- data/lib/new_relic/agent/configuration/manager.rb +51 -8
- data/lib/new_relic/agent/configuration/security_policy_source.rb +11 -0
- data/lib/new_relic/agent/configuration/yaml_source.rb +2 -0
- data/lib/new_relic/agent/connect/request_builder.rb +1 -1
- data/lib/new_relic/agent/custom_event_aggregator.rb +27 -1
- data/lib/new_relic/agent/database/obfuscation_helpers.rb +11 -11
- data/lib/new_relic/agent/database/obfuscator.rb +1 -0
- data/lib/new_relic/agent/database.rb +39 -0
- data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +1 -5
- data/lib/new_relic/agent/error_collector.rb +39 -10
- data/lib/new_relic/agent/harvester.rb +1 -1
- data/lib/new_relic/agent/instrumentation/active_merchant.rb +0 -13
- data/lib/new_relic/agent/instrumentation/active_record.rb +1 -8
- data/lib/new_relic/agent/instrumentation/active_record_helper.rb +3 -0
- data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +1 -12
- data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb +7 -3
- data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger.rb +0 -2
- data/lib/new_relic/agent/instrumentation/active_support_logger.rb +0 -2
- data/lib/new_relic/agent/instrumentation/async_http.rb +4 -3
- data/lib/new_relic/agent/instrumentation/aws_sdk_lambda/chain.rb +33 -0
- data/lib/new_relic/agent/instrumentation/aws_sdk_lambda/instrumentation.rb +93 -0
- data/lib/new_relic/agent/instrumentation/aws_sdk_lambda/prepend.rb +23 -0
- data/lib/new_relic/agent/instrumentation/aws_sdk_lambda.rb +23 -0
- data/lib/new_relic/agent/instrumentation/aws_sqs/chain.rb +37 -0
- data/lib/new_relic/agent/instrumentation/aws_sqs/instrumentation.rb +67 -0
- data/lib/new_relic/agent/instrumentation/aws_sqs/prepend.rb +21 -0
- data/lib/new_relic/agent/instrumentation/aws_sqs.rb +23 -0
- data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +14 -0
- data/lib/new_relic/agent/instrumentation/bunny.rb +3 -4
- data/lib/new_relic/agent/instrumentation/concurrent_ruby.rb +1 -2
- data/lib/new_relic/agent/instrumentation/curb.rb +3 -4
- data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +0 -23
- data/lib/new_relic/agent/instrumentation/dynamodb/chain.rb +27 -0
- data/lib/new_relic/agent/instrumentation/dynamodb/instrumentation.rb +64 -0
- data/lib/new_relic/agent/instrumentation/dynamodb/prepend.rb +19 -0
- data/lib/new_relic/agent/instrumentation/dynamodb.rb +23 -0
- data/lib/new_relic/agent/instrumentation/elasticsearch/instrumentation.rb +58 -8
- data/lib/new_relic/agent/instrumentation/elasticsearch.rb +0 -2
- data/lib/new_relic/agent/instrumentation/ethon.rb +0 -4
- data/lib/new_relic/agent/instrumentation/excon.rb +0 -16
- data/lib/new_relic/agent/instrumentation/fiber.rb +0 -2
- data/lib/new_relic/agent/instrumentation/grape.rb +1 -1
- data/lib/new_relic/agent/instrumentation/grpc/client/instrumentation.rb +0 -1
- data/lib/new_relic/agent/instrumentation/grpc_server.rb +1 -1
- data/lib/new_relic/agent/instrumentation/httpclient.rb +0 -1
- data/lib/new_relic/agent/instrumentation/httprb.rb +0 -1
- data/lib/new_relic/agent/instrumentation/httpx.rb +0 -4
- data/lib/new_relic/agent/instrumentation/logger.rb +1 -3
- data/lib/new_relic/agent/instrumentation/logstasher/chain.rb +21 -0
- data/lib/new_relic/agent/instrumentation/logstasher/instrumentation.rb +24 -0
- data/lib/new_relic/agent/instrumentation/logstasher/prepend.rb +13 -0
- data/lib/new_relic/agent/instrumentation/logstasher.rb +25 -0
- data/lib/new_relic/agent/instrumentation/memcache.rb +0 -1
- data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +6 -0
- data/lib/new_relic/agent/instrumentation/opensearch/chain.rb +21 -0
- data/lib/new_relic/agent/instrumentation/opensearch/instrumentation.rb +66 -0
- data/lib/new_relic/agent/instrumentation/opensearch/prepend.rb +13 -0
- data/lib/new_relic/agent/instrumentation/opensearch.rb +23 -0
- data/lib/new_relic/agent/instrumentation/padrino.rb +3 -3
- data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +3 -0
- data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +9 -5
- data/lib/new_relic/agent/instrumentation/rake.rb +0 -1
- data/lib/new_relic/agent/instrumentation/rdkafka/chain.rb +72 -0
- data/lib/new_relic/agent/instrumentation/rdkafka/instrumentation.rb +70 -0
- data/lib/new_relic/agent/instrumentation/rdkafka/prepend.rb +67 -0
- data/lib/new_relic/agent/instrumentation/rdkafka.rb +25 -0
- data/lib/new_relic/agent/instrumentation/redis/cluster_middleware.rb +26 -0
- data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +14 -11
- data/lib/new_relic/agent/instrumentation/redis/middleware.rb +3 -0
- data/lib/new_relic/agent/instrumentation/redis.rb +11 -5
- data/lib/new_relic/agent/instrumentation/resque.rb +0 -4
- data/lib/new_relic/agent/instrumentation/roda.rb +4 -4
- data/lib/new_relic/agent/instrumentation/ruby_kafka/chain.rb +55 -0
- data/lib/new_relic/agent/instrumentation/ruby_kafka/instrumentation.rb +67 -0
- data/lib/new_relic/agent/instrumentation/ruby_kafka/prepend.rb +60 -0
- data/lib/new_relic/agent/instrumentation/ruby_kafka.rb +25 -0
- data/lib/new_relic/agent/instrumentation/ruby_openai/chain.rb +36 -0
- data/lib/new_relic/agent/instrumentation/ruby_openai/instrumentation.rb +196 -0
- data/lib/new_relic/agent/instrumentation/ruby_openai/prepend.rb +20 -0
- data/lib/new_relic/agent/instrumentation/ruby_openai.rb +35 -0
- data/lib/new_relic/agent/instrumentation/sidekiq.rb +0 -14
- data/lib/new_relic/agent/instrumentation/sinatra.rb +3 -19
- data/lib/new_relic/agent/instrumentation/stripe_subscriber.rb +22 -1
- data/lib/new_relic/agent/instrumentation/thread.rb +0 -2
- data/lib/new_relic/agent/instrumentation/tilt.rb +0 -4
- data/lib/new_relic/agent/instrumentation/typhoeus.rb +0 -1
- data/lib/new_relic/agent/instrumentation/view_component/instrumentation.rb +13 -6
- data/lib/new_relic/agent/instrumentation/view_component.rb +0 -2
- data/lib/new_relic/agent/javascript_instrumentor.rb +2 -3
- data/lib/new_relic/agent/llm/chat_completion_message.rb +25 -0
- data/lib/new_relic/agent/llm/chat_completion_summary.rb +66 -0
- data/lib/new_relic/agent/llm/embedding.rb +60 -0
- data/lib/new_relic/agent/llm/llm_event.rb +95 -0
- data/lib/new_relic/agent/llm/response_headers.rb +80 -0
- data/lib/new_relic/agent/llm.rb +49 -0
- data/lib/new_relic/agent/local_log_decorator.rb +8 -1
- data/lib/new_relic/agent/log_event_aggregator.rb +120 -44
- data/lib/new_relic/agent/messaging.rb +11 -5
- data/lib/new_relic/agent/new_relic_service.rb +12 -2
- data/lib/new_relic/agent/serverless_handler.rb +400 -0
- data/lib/new_relic/agent/serverless_handler_event_sources.json +155 -0
- data/lib/new_relic/agent/serverless_handler_event_sources.rb +49 -0
- data/lib/new_relic/agent/span_event_primitive.rb +8 -10
- data/lib/new_relic/agent/system_info.rb +14 -0
- data/lib/new_relic/agent/threading/agent_thread.rb +1 -2
- data/lib/new_relic/agent/tracer.rb +5 -5
- data/lib/new_relic/agent/transaction/abstract_segment.rb +1 -1
- data/lib/new_relic/agent/transaction/external_request_segment.rb +0 -10
- data/lib/new_relic/agent/transaction/request_attributes.rb +13 -1
- data/lib/new_relic/agent/transaction/trace_context.rb +1 -1
- data/lib/new_relic/agent/transaction/tracing.rb +2 -2
- data/lib/new_relic/agent/transaction.rb +2 -6
- data/lib/new_relic/agent/transaction_error_primitive.rb +23 -19
- data/lib/new_relic/agent.rb +198 -10
- data/lib/new_relic/constants.rb +2 -0
- data/lib/new_relic/control/frameworks/grape.rb +14 -0
- data/lib/new_relic/control/frameworks/padrino.rb +14 -0
- data/lib/new_relic/control/frameworks/rails4.rb +1 -3
- data/lib/new_relic/control/instance_methods.rb +8 -0
- data/lib/new_relic/control/private_instance_methods.rb +4 -0
- data/lib/new_relic/control/security_interface.rb +57 -0
- data/lib/new_relic/control.rb +1 -1
- data/lib/new_relic/dependency_detection.rb +10 -5
- data/lib/new_relic/environment_report.rb +2 -2
- data/lib/new_relic/helper.rb +15 -0
- data/lib/new_relic/language_support.rb +3 -1
- data/lib/new_relic/local_environment.rb +14 -10
- data/lib/new_relic/rack/browser_monitoring.rb +28 -12
- data/lib/new_relic/supportability_helper.rb +2 -0
- data/lib/new_relic/thread_local_storage.rb +31 -0
- data/lib/new_relic/version.rb +2 -2
- data/lib/sequel/extensions/new_relic_instrumentation.rb +3 -2
- data/lib/tasks/config.rake +8 -3
- data/lib/tasks/gha.rake +31 -0
- data/lib/tasks/helpers/config.html.erb +3 -2
- data/lib/tasks/helpers/format.rb +1 -1
- data/lib/tasks/helpers/newrelicyml.rb +76 -13
- data/lib/tasks/instrumentation_generator/instrumentation.thor +31 -22
- data/lib/tasks/instrumentation_generator/templates/chain.tt +0 -1
- data/lib/tasks/instrumentation_generator/templates/chain_method.tt +0 -1
- data/lib/tasks/instrumentation_generator/templates/dependency_detection.tt +11 -8
- data/lib/tasks/instrumentation_generator/templates/newrelic.yml.tt +1 -1
- data/newrelic.yml +387 -143
- data/newrelic_rpm.gemspec +2 -0
- data/test/agent_helper.rb +17 -2
- 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
|
-
|
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)
|
@@ -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
|
@@ -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
|
-
|
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
|
25
|
-
|
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)
|
@@ -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
|
-
|
168
|
-
|
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
|
|