mirror of
https://github.com/chatwoot/chatwoot.git
synced 2026-06-04 21:02:35 +08:00
# Pull Request Template ## Description We need to pass on trace level attributes down to the spans inside them like tool calls, observations, etc. This way, we can filter observations based on trace level attributes. ## Type of change - [x] 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. Attributes added to observation metadata for easy filtering <img width="1327" height="708" alt="image" src="https://github.com/user-attachments/assets/8f1d1bf8-cde4-481d-a2c2-7920ad2fc52e" /> added a `generation_stage` to differentiate llm_calls that call tools vs those that generate a `final_response` <img width="1806" height="968" alt="CleanShot 2026-06-03 at 15 11 09@2x" src="https://github.com/user-attachments/assets/db1fa8e0-7f2d-404b-a719-27a16d400442" /> propagated attributes to tool calls for future use <img width="903" height="517" alt="image" src="https://github.com/user-attachments/assets/edc61ce8-93db-465c-a66e-043138e2dc15" /> ## 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
81 lines
3.5 KiB
Ruby
81 lines
3.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Integrations::LlmInstrumentationCompletionHelpers
|
|
include Integrations::LlmInstrumentationConstants
|
|
|
|
private
|
|
|
|
def set_embedding_span_attributes(span, params)
|
|
span.set_attribute(ATTR_GEN_AI_PROVIDER, determine_provider(params[:model]))
|
|
span.set_attribute(ATTR_GEN_AI_REQUEST_MODEL, params[:model])
|
|
span.set_attribute('embedding.input_length', params[:input]&.length || 0)
|
|
span.set_attribute(ATTR_LANGFUSE_OBSERVATION_INPUT, params[:input].to_s)
|
|
end
|
|
|
|
def set_audio_transcription_span_attributes(span, params)
|
|
span.set_attribute(ATTR_GEN_AI_PROVIDER, 'openai')
|
|
span.set_attribute(ATTR_GEN_AI_REQUEST_MODEL, params[:model] || 'whisper-1')
|
|
span.set_attribute('audio.duration_seconds', params[:duration]) if params[:duration]
|
|
span.set_attribute(ATTR_LANGFUSE_OBSERVATION_INPUT, params[:file_path].to_s) if params[:file_path]
|
|
end
|
|
|
|
def set_moderation_span_attributes(span, params)
|
|
span.set_attribute(ATTR_GEN_AI_PROVIDER, 'openai')
|
|
span.set_attribute(ATTR_GEN_AI_REQUEST_MODEL, params[:model] || 'text-moderation-latest')
|
|
span.set_attribute('moderation.input_length', params[:input]&.length || 0)
|
|
span.set_attribute(ATTR_LANGFUSE_OBSERVATION_INPUT, params[:input].to_s)
|
|
end
|
|
|
|
def set_embedding_result_attributes(span, result)
|
|
span.set_attribute('embedding.dimensions', result&.length || 0) if result.is_a?(Array)
|
|
span.set_attribute(ATTR_LANGFUSE_OBSERVATION_OUTPUT, "[#{result&.length || 0} dimensions]")
|
|
end
|
|
|
|
def set_transcription_result_attributes(span, result)
|
|
transcribed_text = result.respond_to?(:text) ? result.text : result.to_s
|
|
span.set_attribute('transcription.length', transcribed_text&.length || 0)
|
|
span.set_attribute(ATTR_LANGFUSE_OBSERVATION_OUTPUT, transcribed_text.to_s)
|
|
end
|
|
|
|
def set_moderation_result_attributes(span, result)
|
|
span.set_attribute('moderation.flagged', result.flagged?) if result.respond_to?(:flagged?)
|
|
span.set_attribute('moderation.categories', result.flagged_categories.to_json) if result.respond_to?(:flagged_categories)
|
|
output = {
|
|
flagged: result.respond_to?(:flagged?) ? result.flagged? : nil,
|
|
categories: result.respond_to?(:flagged_categories) ? result.flagged_categories : []
|
|
}
|
|
span.set_attribute(ATTR_LANGFUSE_OBSERVATION_OUTPUT, output.to_json)
|
|
end
|
|
|
|
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.is_a?(String) ? message : message.to_json)
|
|
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
|
|
end
|