## Description Please include a summary of the change and issue(s) fixed. Also, mention relevant motivation, context, and any dependencies that this change requires. Fixes false new relic alerts set due to hardcoding an error code ## Type of change Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. Before <img width="776" height="666" alt="image" src="https://github.com/user-attachments/assets/f086890d-eaf1-4e83-b383-fe3675b24159" /> the 500 was hardcoded. RubyLLM doesn't send any error codes, so i removed the error code argument and just pass the error message Langfuse gets just the error message <img width="883" height="700" alt="image" src="https://github.com/user-attachments/assets/fc8c3907-b9a5-4c87-bfc6-8e05cfe9c8b0" /> local logs only show error <img width="1434" height="200" alt="image" src="https://github.com/user-attachments/assets/716c6371-78f0-47b8-88a4-03e4196c0e9a" /> Better fix is to handle each case and show the user wherever necessary ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: aakashb95 <aakash@chatwoot.com>
52 lines
1.9 KiB
Ruby
52 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Integrations::LlmInstrumentationHelpers
|
|
include Integrations::LlmInstrumentationConstants
|
|
|
|
private
|
|
|
|
def set_completion_attributes(span, result)
|
|
set_completion_message(span, result)
|
|
set_usage_metrics(span, result)
|
|
set_error_attributes(span, result)
|
|
end
|
|
|
|
def set_completion_message(span, result)
|
|
message = result[:message] || result.dig('choices', 0, 'message', 'content')
|
|
return if message.blank?
|
|
|
|
span.set_attribute(ATTR_GEN_AI_COMPLETION_ROLE, 'assistant')
|
|
span.set_attribute(ATTR_GEN_AI_COMPLETION_CONTENT, message)
|
|
end
|
|
|
|
def set_usage_metrics(span, result)
|
|
usage = result[:usage] || result['usage']
|
|
return if usage.blank?
|
|
|
|
span.set_attribute(ATTR_GEN_AI_USAGE_INPUT_TOKENS, usage['prompt_tokens']) if usage['prompt_tokens']
|
|
span.set_attribute(ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, usage['completion_tokens']) if usage['completion_tokens']
|
|
span.set_attribute(ATTR_GEN_AI_USAGE_TOTAL_TOKENS, usage['total_tokens']) if usage['total_tokens']
|
|
end
|
|
|
|
def set_error_attributes(span, result)
|
|
error = result[:error] || result['error']
|
|
return if error.blank?
|
|
|
|
span.set_attribute(ATTR_GEN_AI_RESPONSE_ERROR, error.to_json)
|
|
span.status = OpenTelemetry::Trace::Status.error(error.to_s.truncate(1000))
|
|
end
|
|
|
|
def set_metadata_attributes(span, params)
|
|
session_id = params[:conversation_id].present? ? "#{params[:account_id]}_#{params[:conversation_id]}" : nil
|
|
span.set_attribute(ATTR_LANGFUSE_USER_ID, params[:account_id].to_s) if params[:account_id]
|
|
span.set_attribute(ATTR_LANGFUSE_SESSION_ID, session_id) if session_id.present?
|
|
span.set_attribute(ATTR_LANGFUSE_TAGS, [params[:feature_name]].to_json)
|
|
|
|
return unless params[:metadata].is_a?(Hash)
|
|
|
|
params[:metadata].each do |key, value|
|
|
span.set_attribute(format(ATTR_LANGFUSE_METADATA, key), value.to_s)
|
|
end
|
|
end
|
|
end
|