Class: NewRelic::Agent::Llm::LlmEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/llm/llm_event.rb

Constant Summary collapse

ATTRIBUTES =

Every subclass must define its own ATTRIBUTES constant, an array of symbols representing that class’s unique attributes

%i[id request_id span_id trace_id response_model vendor
ingest_source metadata]
AGENT_DEFINED_ATTRIBUTES =

These attributes should not be passed as arguments to initialize and will be set by the agent

%i[span_id trace_id ingest_source]
ATTRIBUTE_NAME_EXCEPTIONS =

Some attributes have names that can’t be written as symbols used for metaprogramming. The ATTRIBUTE_NAME_EXCEPTIONS hash should use the symbolized version of the name as the key and the string version expected by the UI as the value.

{response_model: 'response.model'}
INGEST_SOURCE =
'Ruby'
ERROR_ATTRIBUTE_STATUS_CODE =
'http.statusCode'
ERROR_ATTRIBUTE_CODE =
'error.code'
ERROR_ATTRIBUTE_PARAM =
'error.param'
ERROR_STRING =
'error'
CODE_STRING =
'code'
PARAM_STRING =
'param'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ LlmEvent

This initialize method is used for all subclasses. It leverages the subclass’s ‘attributes` method to iterate through all the attributes for that subclass. It assigns instance variables for all arguments passed to the method. It also assigns agent-defined attributes.



38
39
40
41
42
43
44
45
46
47
# File 'lib/new_relic/agent/llm/llm_event.rb', line 38

def initialize(opts = {})
  (attributes - AGENT_DEFINED_ATTRIBUTES).each do |attr|
    instance_variable_set(:"@#{attr}", opts[attr]) if opts.key?(attr)
  end

  @id = id || NewRelic::Agent::GuidGenerator.generate_guid
  @span_id = NewRelic::Agent::Tracer.current_span_id
  @trace_id = NewRelic::Agent::Tracer.current_trace_id
  @ingest_source = INGEST_SOURCE
end

Class Method Details

.set_llm_agent_attribute_on_transactionObject



29
30
31
# File 'lib/new_relic/agent/llm/llm_event.rb', line 29

def self.set_llm_agent_attribute_on_transaction
  NewRelic::Agent::Transaction.add_agent_attribute(:llm, true, NewRelic::Agent::AttributeFilter::DST_TRANSACTION_EVENTS)
end

Instance Method Details

#attribute_name_exceptionsObject

Some attribute names include periods, which aren’t valid values for Ruby method names. This method returns a Hash with the key as the Ruby symbolized version of the attribute and the value as the period-delimited string expected upstream.



74
75
76
# File 'lib/new_relic/agent/llm/llm_event.rb', line 74

def attribute_name_exceptions
  ATTRIBUTE_NAME_EXCEPTIONS
end

#attributesObject

Subclasses define an attributes method to concatenate attributes defined across their ancestors and other modules



62
63
64
# File 'lib/new_relic/agent/llm/llm_event.rb', line 62

def attributes
  ATTRIBUTES
end

#error_attributes(exception) ⇒ Object

Subclasses that add attributes to noticed errors will override this method



83
84
85
# File 'lib/new_relic/agent/llm/llm_event.rb', line 83

def error_attributes(exception)
  NewRelic::EMPTY_HASH
end

#event_attributesObject

All subclasses use event_attributes to get a full hash of all attributes and their values



51
52
53
54
55
56
57
58
# File 'lib/new_relic/agent/llm/llm_event.rb', line 51

def event_attributes
  attributes_hash = attributes.each_with_object({}) do |attr, hash|
    hash[replace_attr_with_string(attr)] = instance_variable_get(:"@#{attr}")
  end
  attributes_hash.merge!() && attributes_hash.delete(:metadata) if !.nil?

  attributes_hash
end

#event_nameObject

Subclasses that record events will override this method



67
68
# File 'lib/new_relic/agent/llm/llm_event.rb', line 67

def event_name
end

#recordObject



78
79
80
# File 'lib/new_relic/agent/llm/llm_event.rb', line 78

def record
  NewRelic::Agent.record_custom_event(event_name, event_attributes)
end