-
Notifications
You must be signed in to change notification settings - Fork 18.5k
fix(ops): add streaming metrics and LLM span for agent-chat traces #28320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a84bfdd
563f643
6e87f22
3d27bdc
9f415d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -222,6 +222,59 @@ def build_message_span( | |||||||||||||||||||||||||||
| links=links, | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||
| def build_message_llm_span( | ||||||||||||||||||||||||||||
| trace_info: MessageTraceInfo, trace_id: int, parent_span_id: int, user_id: str | ||||||||||||||||||||||||||||
| ) -> SpanData: | ||||||||||||||||||||||||||||
| """Build LLM span for message traces with detailed LLM attributes.""" | ||||||||||||||||||||||||||||
| status = Status(StatusCode.OK) | ||||||||||||||||||||||||||||
| if trace_info.error: | ||||||||||||||||||||||||||||
| status = Status(StatusCode.ERROR, trace_info.error) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Extract model information from `metadata`` or `message_data` | ||||||||||||||||||||||||||||
| trace_metadata = trace_info.metadata or {} | ||||||||||||||||||||||||||||
| message_data = trace_info.message_data or {} | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| model_provider = trace_metadata.get("ls_provider") or ( | ||||||||||||||||||||||||||||
| message_data.get("model_provider", "") if isinstance(message_data, dict) else "" | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| model_name = trace_metadata.get("ls_model_name") or ( | ||||||||||||||||||||||||||||
| message_data.get("model_id", "") if isinstance(message_data, dict) else "" | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
Comment on lines
+238
to
+243
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for extracting
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is purely a style preference I'd prefer to keep it as-is unless there's a functional issue. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| inputs_str = str(trace_info.inputs or "") | ||||||||||||||||||||||||||||
| outputs_str = str(trace_info.outputs or "") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| attributes = { | ||||||||||||||||||||||||||||
| GEN_AI_SESSION_ID: trace_metadata.get("conversation_id", ""), | ||||||||||||||||||||||||||||
| GEN_AI_USER_ID: str(user_id), | ||||||||||||||||||||||||||||
| GEN_AI_SPAN_KIND: GenAISpanKind.GENERATION.value, | ||||||||||||||||||||||||||||
| GEN_AI_FRAMEWORK: "dify", | ||||||||||||||||||||||||||||
| GEN_AI_MODEL_NAME: str(model_name), | ||||||||||||||||||||||||||||
| GEN_AI_PROVIDER: str(model_provider), | ||||||||||||||||||||||||||||
| GEN_AI_USAGE_INPUT_TOKENS: str(trace_info.message_tokens or 0), | ||||||||||||||||||||||||||||
| GEN_AI_USAGE_OUTPUT_TOKENS: str(trace_info.answer_tokens or 0), | ||||||||||||||||||||||||||||
| GEN_AI_USAGE_TOTAL_TOKENS: str(trace_info.total_tokens or 0), | ||||||||||||||||||||||||||||
| GEN_AI_PROMPT: inputs_str, | ||||||||||||||||||||||||||||
| GEN_AI_COMPLETION: outputs_str, | ||||||||||||||||||||||||||||
| INPUT_VALUE: inputs_str, | ||||||||||||||||||||||||||||
| OUTPUT_VALUE: outputs_str, | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if trace_info.is_streaming_request: | ||||||||||||||||||||||||||||
| attributes[GEN_AI_IS_STREAMING_REQUEST] = "true" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return SpanData( | ||||||||||||||||||||||||||||
| trace_id=trace_id, | ||||||||||||||||||||||||||||
| parent_span_id=parent_span_id, | ||||||||||||||||||||||||||||
| span_id=TencentTraceUtils.convert_to_span_id(trace_info.message_id, "llm"), | ||||||||||||||||||||||||||||
| name="GENERATION", | ||||||||||||||||||||||||||||
| start_time=TencentSpanBuilder._get_time_nanoseconds(trace_info.start_time), | ||||||||||||||||||||||||||||
| end_time=TencentSpanBuilder._get_time_nanoseconds(trace_info.end_time), | ||||||||||||||||||||||||||||
| attributes=attributes, | ||||||||||||||||||||||||||||
| status=status, | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||
| def build_tool_span(trace_info: ToolTraceInfo, trace_id: int, parent_span_id: int) -> SpanData: | ||||||||||||||||||||||||||||
| """Build tool span.""" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition
self._task_state.is_streaming_response and self._task_state.first_token_timeis redundant. Based on the logic in_process_stream_response,is_streaming_responseis alwaysTruewhenfirst_token_timeis set. You can simplify this condition to make the code more concise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to keep the explicit check because the dual condition provides better protection.