newrelic_rpm 9.5.0 → 9.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +154 -7
- data/CONTRIBUTING.md +0 -7
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/bin/newrelic +2 -9
- data/bin/newrelic_rpm +15 -0
- data/init.rb +2 -2
- data/lib/new_relic/agent/agent.rb +1 -1
- data/lib/new_relic/agent/agent_helpers/shutdown.rb +1 -1
- data/lib/new_relic/agent/agent_helpers/special_startup.rb +1 -1
- data/lib/new_relic/agent/agent_helpers/start_worker_thread.rb +2 -2
- data/lib/new_relic/agent/agent_helpers/startup.rb +2 -2
- data/lib/new_relic/agent/attribute_filter.rb +3 -3
- data/lib/new_relic/agent/configuration/default_source.rb +131 -36
- data/lib/new_relic/agent/configuration/high_security_source.rb +1 -0
- data/lib/new_relic/agent/configuration/manager.rb +13 -9
- data/lib/new_relic/agent/configuration/security_policy_source.rb +11 -0
- data/lib/new_relic/agent/custom_event_aggregator.rb +27 -1
- data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +1 -1
- data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +3 -3
- data/lib/new_relic/agent/error_collector.rb +2 -0
- data/lib/new_relic/agent/event_loop.rb +1 -1
- data/lib/new_relic/agent/http_clients/abstract.rb +4 -0
- data/lib/new_relic/agent/http_clients/async_http_wrappers.rb +80 -0
- data/lib/new_relic/agent/http_clients/curb_wrappers.rb +1 -3
- data/lib/new_relic/agent/http_clients/ethon_wrappers.rb +109 -0
- data/lib/new_relic/agent/http_clients/excon_wrappers.rb +0 -3
- data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +1 -3
- data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +0 -3
- data/lib/new_relic/agent/http_clients/httpx_wrappers.rb +91 -0
- data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +1 -4
- data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +0 -3
- data/lib/new_relic/agent/instrumentation/active_merchant.rb +1 -1
- data/lib/new_relic/agent/instrumentation/active_record_helper.rb +1 -2
- data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/chain.rb +69 -0
- data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb +17 -0
- data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/prepend.rb +37 -0
- data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger.rb +23 -0
- data/lib/new_relic/agent/instrumentation/active_support_logger.rb +3 -1
- data/lib/new_relic/agent/instrumentation/async_http/chain.rb +23 -0
- data/lib/new_relic/agent/instrumentation/async_http/instrumentation.rb +37 -0
- data/lib/new_relic/agent/instrumentation/async_http/prepend.rb +15 -0
- data/lib/new_relic/agent/instrumentation/async_http.rb +28 -0
- data/lib/new_relic/agent/instrumentation/concurrent_ruby.rb +1 -0
- data/lib/new_relic/agent/instrumentation/elasticsearch/instrumentation.rb +1 -1
- data/lib/new_relic/agent/instrumentation/ethon/chain.rb +39 -0
- data/lib/new_relic/agent/instrumentation/ethon/instrumentation.rb +105 -0
- data/lib/new_relic/agent/instrumentation/ethon/prepend.rb +35 -0
- data/lib/new_relic/agent/instrumentation/ethon.rb +39 -0
- data/lib/new_relic/agent/instrumentation/fiber/instrumentation.rb +1 -4
- data/lib/new_relic/agent/instrumentation/grpc_server.rb +1 -1
- data/lib/new_relic/agent/instrumentation/httpx/chain.rb +20 -0
- data/lib/new_relic/agent/instrumentation/httpx/instrumentation.rb +51 -0
- data/lib/new_relic/agent/instrumentation/httpx/prepend.rb +15 -0
- data/lib/new_relic/agent/instrumentation/httpx.rb +27 -0
- data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +1 -3
- data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +7 -1
- data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +1 -0
- data/lib/new_relic/agent/instrumentation/roda/ignorer.rb +45 -0
- data/lib/new_relic/agent/instrumentation/roda/instrumentation.rb +12 -0
- data/lib/new_relic/agent/instrumentation/roda/roda_transaction_namer.rb +1 -2
- data/lib/new_relic/agent/instrumentation/roda.rb +2 -0
- data/lib/new_relic/agent/instrumentation/ruby_openai/chain.rb +36 -0
- data/lib/new_relic/agent/instrumentation/ruby_openai/instrumentation.rb +197 -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 +3 -1
- data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +1 -1
- data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +1 -3
- data/lib/new_relic/agent/instrumentation/sinatra.rb +1 -1
- data/lib/new_relic/agent/instrumentation/thread/instrumentation.rb +1 -4
- data/lib/new_relic/agent/instrumentation/view_component/chain.rb +21 -0
- data/lib/new_relic/agent/instrumentation/view_component/instrumentation.rb +39 -0
- data/lib/new_relic/agent/instrumentation/view_component/prepend.rb +13 -0
- data/lib/new_relic/agent/instrumentation/view_component.rb +26 -0
- data/lib/new_relic/agent/javascript_instrumentor.rb +0 -1
- 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/messaging.rb +2 -2
- data/lib/new_relic/agent/monitors/synthetics_monitor.rb +12 -1
- data/lib/new_relic/agent/new_relic_service/encoders.rb +2 -2
- data/lib/new_relic/agent/new_relic_service.rb +8 -6
- data/lib/new_relic/agent/obfuscator.rb +0 -2
- data/lib/new_relic/agent/pipe_channel_manager.rb +2 -2
- data/lib/new_relic/agent/rules_engine/segment_terms_rule.rb +1 -2
- data/lib/new_relic/agent/rules_engine.rb +1 -1
- data/lib/new_relic/agent/span_event_primitive.rb +16 -4
- data/lib/new_relic/agent/sql_sampler.rb +0 -1
- data/lib/new_relic/agent/system_info.rb +26 -0
- data/lib/new_relic/agent/threading/agent_thread.rb +1 -2
- data/lib/new_relic/agent/tracer.rb +9 -10
- data/lib/new_relic/agent/transaction/abstract_segment.rb +4 -1
- data/lib/new_relic/agent/transaction/external_request_segment.rb +5 -2
- data/lib/new_relic/agent/transaction/message_broker_segment.rb +1 -2
- data/lib/new_relic/agent/transaction/request_attributes.rb +1 -3
- data/lib/new_relic/agent/transaction/tracing.rb +11 -1
- data/lib/new_relic/agent/transaction.rb +25 -2
- data/lib/new_relic/agent/transaction_error_primitive.rb +16 -0
- data/lib/new_relic/agent/transaction_event_primitive.rb +19 -0
- data/lib/new_relic/agent/utilization/gcp.rb +1 -3
- data/lib/new_relic/agent/vm/{mri_vm.rb → c_ruby_vm.rb} +7 -15
- data/lib/new_relic/agent/vm.rb +2 -2
- data/lib/new_relic/agent/worker_loop.rb +1 -1
- data/lib/new_relic/agent.rb +102 -7
- data/lib/new_relic/base64.rb +25 -0
- data/lib/new_relic/cli/command.rb +6 -4
- data/lib/new_relic/constants.rb +5 -0
- data/lib/new_relic/control/frameworks/rails.rb +17 -5
- data/lib/new_relic/control/instrumentation.rb +1 -1
- data/lib/new_relic/language_support.rb +4 -0
- data/lib/new_relic/local_environment.rb +22 -13
- data/lib/new_relic/rack/browser_monitoring.rb +8 -4
- data/lib/new_relic/supportability_helper.rb +3 -1
- data/lib/new_relic/thread_local_storage.rb +31 -0
- data/lib/new_relic/version.rb +1 -1
- data/lib/tasks/config.rake +1 -1
- data/lib/tasks/helpers/config.html.erb +6 -6
- data/lib/tasks/helpers/newrelicyml.rb +1 -1
- data/lib/tasks/instrumentation_generator/instrumentation.thor +3 -3
- data/lib/tasks/instrumentation_generator/templates/chain.tt +0 -1
- data/lib/tasks/instrumentation_generator/templates/chain_method.tt +0 -1
- data/lib/tasks/tests.rake +71 -0
- data/newrelic.yml +76 -36
- data/newrelic_rpm.gemspec +5 -4
- data/test/agent_helper.rb +14 -2
- metadata +43 -7
- data/bin/newrelic_cmd +0 -7
@@ -0,0 +1,197 @@
|
|
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
|
+
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
|
+
|
132
|
+
messages.each do |message|
|
133
|
+
message.id = "#{response_id}-#{message.sequence}"
|
134
|
+
message.request_id = summary.request_id
|
135
|
+
message.response_model = response['model']
|
136
|
+
message.metadata = llm_custom_attributes
|
137
|
+
|
138
|
+
model = message.is_response ? message.response_model : summary.request_model
|
139
|
+
|
140
|
+
message.token_count = calculate_token_count(model, message.content)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def calculate_token_count(model, content)
|
145
|
+
return unless NewRelic::Agent.llm_token_count_callback
|
146
|
+
|
147
|
+
begin
|
148
|
+
count = NewRelic::Agent.llm_token_count_callback.call({model: model, content: content})
|
149
|
+
rescue => e
|
150
|
+
NewRelic::Agent.logger.warn("Error calculating token count using the provided proc. Error: #{e}'")
|
151
|
+
end
|
152
|
+
|
153
|
+
count if count.is_a?(Integer) && count > 0
|
154
|
+
end
|
155
|
+
|
156
|
+
def record_content_enabled?
|
157
|
+
NewRelic::Agent.config[:'ai_monitoring.record_content.enabled']
|
158
|
+
end
|
159
|
+
|
160
|
+
def add_content(message, content)
|
161
|
+
message.content = content if record_content_enabled?
|
162
|
+
end
|
163
|
+
|
164
|
+
def add_input(event, input)
|
165
|
+
event.input = input if record_content_enabled?
|
166
|
+
end
|
167
|
+
|
168
|
+
def llm_custom_attributes
|
169
|
+
NewRelic::Agent::Tracer.current_transaction&.attributes&.custom_attributes&.select { |k| k.to_s.match(/llm.*/) }
|
170
|
+
end
|
171
|
+
|
172
|
+
def record_openai_metric
|
173
|
+
NewRelic::Agent.record_metric(nr_supportability_metric, 0.0)
|
174
|
+
end
|
175
|
+
|
176
|
+
def segment_noticed_error?(segment)
|
177
|
+
segment&.noticed_error
|
178
|
+
end
|
179
|
+
|
180
|
+
def nr_supportability_metric
|
181
|
+
@nr_supportability_metric ||= "Supportability/Ruby/ML/OpenAI/#{::OpenAI::VERSION}"
|
182
|
+
end
|
183
|
+
|
184
|
+
def finish(segment, event)
|
185
|
+
segment&.finish
|
186
|
+
|
187
|
+
return unless event
|
188
|
+
|
189
|
+
if segment
|
190
|
+
event.error = true if segment_noticed_error?(segment)
|
191
|
+
event.duration = segment.duration
|
192
|
+
end
|
193
|
+
|
194
|
+
event.record
|
195
|
+
end
|
196
|
+
end
|
197
|
+
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
|
@@ -33,7 +33,9 @@ DependencyDetection.defer do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
if config.respond_to?(:error_handlers)
|
36
|
-
|
36
|
+
# Sidekiq 3.0.0 - 7.1.4 expect error_handlers to have 2 arguments
|
37
|
+
# Sidekiq 7.1.5+ expect error_handlers to have 3 arguments
|
38
|
+
config.error_handlers << proc do |error, _ctx, *_|
|
37
39
|
NewRelic::Agent.notice_error(error)
|
38
40
|
end
|
39
41
|
end
|
@@ -33,7 +33,7 @@ module NewRelic::Agent::Instrumentation
|
|
33
33
|
set(:newrelic_ignores, Hash.new([])) if !respond_to?(:newrelic_ignores)
|
34
34
|
|
35
35
|
# If we call an ignore without a route, it applies to the whole app
|
36
|
-
routes = [
|
36
|
+
routes = [::NewRelic::ASTERISK] if routes.empty?
|
37
37
|
|
38
38
|
settings.newrelic_ignores[type] += routes.map do |r|
|
39
39
|
# Ugly sending to private Base#compile, but we want to mimic
|
@@ -25,14 +25,12 @@ module NewRelic
|
|
25
25
|
transaction_name(::NewRelic::Agent::UNKNOWN_METRIC, request)
|
26
26
|
end
|
27
27
|
|
28
|
-
ROOT = '/'.freeze
|
29
|
-
|
30
28
|
def transaction_name(route_text, request)
|
31
29
|
verb = http_verb(request)
|
32
30
|
|
33
31
|
route_text = route_text.source if route_text.is_a?(Regexp)
|
34
32
|
name = route_text.gsub(%r{^[/^\\A]*(.*?)[/\$\?\\z]*$}, '\1')
|
35
|
-
name = ROOT if name.empty?
|
33
|
+
name = NewRelic::ROOT if name.empty?
|
36
34
|
name = "#{verb} #{name}" unless verb.nil?
|
37
35
|
name
|
38
36
|
rescue => e
|
@@ -46,7 +46,7 @@ DependencyDetection.defer do
|
|
46
46
|
executes do
|
47
47
|
next unless Gem::Version.new(Sinatra::VERSION) < Gem::Version.new('2.0.0')
|
48
48
|
|
49
|
-
deprecation_msg = 'The Ruby
|
49
|
+
deprecation_msg = 'The Ruby agent is dropping support for Sinatra versions below 2.0.0 ' \
|
50
50
|
'in version 9.0.0. Please upgrade your Sinatra version to continue receiving full compatibility. ' \
|
51
51
|
|
52
52
|
NewRelic::Agent.logger.log_once(
|
@@ -16,10 +16,7 @@ module NewRelic
|
|
16
16
|
def add_thread_tracing(*args, &block)
|
17
17
|
return block if !NewRelic::Agent::Tracer.thread_tracing_enabled?
|
18
18
|
|
19
|
-
NewRelic::Agent::Tracer.thread_block_with_current_transaction(
|
20
|
-
segment_name: 'Ruby/Thread',
|
21
|
-
&block
|
22
|
-
)
|
19
|
+
NewRelic::Agent::Tracer.thread_block_with_current_transaction(&block)
|
23
20
|
end
|
24
21
|
end
|
25
22
|
end
|
@@ -0,0 +1,21 @@
|
|
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 ViewComponent::Chain
|
7
|
+
def self.instrument!
|
8
|
+
::ViewComponent::Base.class_eval do
|
9
|
+
include NewRelic::Agent::Instrumentation::ViewComponent
|
10
|
+
|
11
|
+
alias_method(:render_in_without_tracing, :render_in)
|
12
|
+
|
13
|
+
def render_in(*args)
|
14
|
+
render_in_with_tracing(*args) do
|
15
|
+
render_in_without_tracing(*args)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,39 @@
|
|
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 ViewComponent
|
7
|
+
INSTRUMENTATION_NAME = NewRelic::Agent.base_name(name)
|
8
|
+
|
9
|
+
def render_in_with_tracing(*args)
|
10
|
+
NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)
|
11
|
+
|
12
|
+
begin
|
13
|
+
segment = NewRelic::Agent::Tracer.start_segment(
|
14
|
+
name: metric_name(self.class.identifier, self.class.name)
|
15
|
+
)
|
16
|
+
yield
|
17
|
+
rescue => e
|
18
|
+
NewRelic::Agent.notice_error(e)
|
19
|
+
raise
|
20
|
+
ensure
|
21
|
+
segment&.finish
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def metric_name(identifier, component)
|
26
|
+
"View/#{metric_path(identifier)}/#{component}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def metric_path(identifier)
|
30
|
+
return 'component' unless identifier
|
31
|
+
|
32
|
+
if (parts = identifier.split('/')).size > 1
|
33
|
+
parts[-2..-1].join('/') # Get filepath by assuming the Rails' structure: app/components/home/example_component.rb
|
34
|
+
else
|
35
|
+
NewRelic::Agent::UNKNOWN_METRIC
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,13 @@
|
|
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 ViewComponent::Prepend
|
7
|
+
include NewRelic::Agent::Instrumentation::ViewComponent
|
8
|
+
|
9
|
+
def render_in(*args)
|
10
|
+
render_in_with_tracing(*args) { super }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -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
|
+
require_relative 'view_component/instrumentation'
|
6
|
+
require_relative 'view_component/chain'
|
7
|
+
require_relative 'view_component/prepend'
|
8
|
+
|
9
|
+
DependencyDetection.defer do
|
10
|
+
named :view_component
|
11
|
+
|
12
|
+
depends_on do
|
13
|
+
defined?(ViewComponent) &&
|
14
|
+
ViewComponent::Base.method_defined?(:render_in)
|
15
|
+
end
|
16
|
+
|
17
|
+
executes do
|
18
|
+
NewRelic::Agent.logger.info('Installing ViewComponent instrumentation')
|
19
|
+
|
20
|
+
if use_prepend?
|
21
|
+
prepend_instrument ViewComponent::Base, NewRelic::Agent::Instrumentation::ViewComponent::Prepend
|
22
|
+
else
|
23
|
+
chain_instrument NewRelic::Agent::Instrumentation::ViewComponent::Chain
|
24
|
+
end
|
25
|
+
end
|
26
|
+
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
|
+
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
|