newrelic_rpm 9.7.1 → 9.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +47 -1
  3. data/README.md +1 -1
  4. data/lib/new_relic/agent/agent.rb +4 -1
  5. data/lib/new_relic/agent/agent_helpers/connect.rb +10 -8
  6. data/lib/new_relic/agent/agent_helpers/start_worker_thread.rb +1 -1
  7. data/lib/new_relic/agent/agent_helpers/startup.rb +2 -1
  8. data/lib/new_relic/agent/agent_logger.rb +2 -1
  9. data/lib/new_relic/agent/configuration/default_source.rb +67 -3
  10. data/lib/new_relic/agent/configuration/environment_source.rb +9 -1
  11. data/lib/new_relic/agent/configuration/high_security_source.rb +1 -0
  12. data/lib/new_relic/agent/configuration/manager.rb +28 -8
  13. data/lib/new_relic/agent/configuration/security_policy_source.rb +11 -0
  14. data/lib/new_relic/agent/configuration/yaml_source.rb +2 -0
  15. data/lib/new_relic/agent/connect/request_builder.rb +1 -1
  16. data/lib/new_relic/agent/custom_event_aggregator.rb +4 -4
  17. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +1 -5
  18. data/lib/new_relic/agent/error_collector.rb +2 -0
  19. data/lib/new_relic/agent/harvester.rb +1 -1
  20. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb +7 -3
  21. data/lib/new_relic/agent/instrumentation/concurrent_ruby.rb +1 -0
  22. data/lib/new_relic/agent/instrumentation/elasticsearch/instrumentation.rb +6 -1
  23. data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +6 -0
  24. data/lib/new_relic/agent/instrumentation/ruby_openai/chain.rb +36 -0
  25. data/lib/new_relic/agent/instrumentation/ruby_openai/instrumentation.rb +196 -0
  26. data/lib/new_relic/agent/instrumentation/ruby_openai/prepend.rb +20 -0
  27. data/lib/new_relic/agent/instrumentation/ruby_openai.rb +35 -0
  28. data/lib/new_relic/agent/llm/chat_completion_message.rb +25 -0
  29. data/lib/new_relic/agent/llm/chat_completion_summary.rb +66 -0
  30. data/lib/new_relic/agent/llm/embedding.rb +60 -0
  31. data/lib/new_relic/agent/llm/llm_event.rb +95 -0
  32. data/lib/new_relic/agent/llm/response_headers.rb +80 -0
  33. data/lib/new_relic/agent/llm.rb +49 -0
  34. data/lib/new_relic/agent/log_event_aggregator.rb +1 -16
  35. data/lib/new_relic/agent/new_relic_service.rb +12 -2
  36. data/lib/new_relic/agent/serverless_handler.rb +171 -0
  37. data/lib/new_relic/agent/threading/agent_thread.rb +1 -2
  38. data/lib/new_relic/agent/tracer.rb +5 -5
  39. data/lib/new_relic/agent/transaction/abstract_segment.rb +1 -1
  40. data/lib/new_relic/agent/transaction/tracing.rb +2 -2
  41. data/lib/new_relic/agent/transaction_error_primitive.rb +23 -19
  42. data/lib/new_relic/agent.rb +102 -8
  43. data/lib/new_relic/constants.rb +2 -0
  44. data/lib/new_relic/control/instance_methods.rb +7 -0
  45. data/lib/new_relic/local_environment.rb +13 -6
  46. data/lib/new_relic/rack/browser_monitoring.rb +8 -4
  47. data/lib/new_relic/supportability_helper.rb +2 -0
  48. data/lib/new_relic/thread_local_storage.rb +31 -0
  49. data/lib/new_relic/version.rb +2 -2
  50. data/lib/tasks/config.rake +2 -1
  51. data/newrelic.yml +27 -1
  52. metadata +14 -2
@@ -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
@@ -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
@@ -247,21 +247,6 @@ module NewRelic
247
247
  message.byteslice(0...MAX_BYTES)
248
248
  end
249
249
 
250
- def minimum_log_level
251
- if Logger::Severity.constants.include?(configured_log_level_constant)
252
- configured_log_level_constant
253
- else
254
- NewRelic::Agent.logger.log_once(
255
- :error,
256
- 'Invalid application_logging.forwarding.log_level ' \
257
- "'#{NewRelic::Agent.config[LOG_LEVEL_KEY]}' specified! " \
258
- "Must be one of #{Logger::Severity.constants.join('|')}. " \
259
- "Using default level of 'debug'"
260
- )
261
- :DEBUG
262
- end
263
- end
264
-
265
250
  def configured_log_level_constant
266
251
  format_log_level_constant(NewRelic::Agent.config[LOG_LEVEL_KEY])
267
252
  end
@@ -275,7 +260,7 @@ module NewRelic
275
260
  # always record custom log levels
276
261
  return false unless Logger::Severity.constants.include?(severity_constant)
277
262
 
278
- Logger::Severity.const_get(severity_constant) < Logger::Severity.const_get(minimum_log_level)
263
+ Logger::Severity.const_get(severity_constant) < Logger::Severity.const_get(configured_log_level_constant)
279
264
  end
280
265
  end
281
266
  end