Skip to content

Commit ae42ef0

Browse files
committed
fix tool common attr setting
1 parent 8581516 commit ae42ef0

File tree

4 files changed

+46
-38
lines changed

4 files changed

+46
-38
lines changed

veadk/tracing/base_tracer.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,6 @@
1919
logger = get_logger(__name__)
2020

2121

22-
def replace_bytes_with_empty(data):
23-
"""
24-
Recursively traverse the data structure and replace all bytes types with empty strings.
25-
Supports handling any nested structure of lists and dictionaries.
26-
"""
27-
if isinstance(data, dict):
28-
# Handle dictionary: Recursively process each value
29-
return {k: replace_bytes_with_empty(v) for k, v in data.items()}
30-
elif isinstance(data, list):
31-
# Handle list: Recursively process each element
32-
return [replace_bytes_with_empty(item) for item in data]
33-
elif isinstance(data, bytes):
34-
# When encountering the bytes type, replace it with an empty string
35-
return "<image data>"
36-
else:
37-
# Keep other types unchanged
38-
return data
39-
40-
4122
class BaseTracer(ABC):
4223
def __init__(self, name: str):
4324
self.name = name

veadk/tracing/telemetry/attributes/extractors/llm_attributes_extractors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ def llm_output_value(params: LLMAttributesParams) -> ExtractorResponse:
395395
# attributes
396396
"gen_ai.prompt": llm_gen_ai_prompt,
397397
"gen_ai.completion": llm_gen_ai_completion,
398-
"input.value": llm_input_value, # TLS required
399-
"output.value": llm_output_value, # TLS required
398+
# "input.value": llm_input_value,
399+
# "output.value": llm_output_value,
400400
# ===== usage =====
401401
"gen_ai.usage.input_tokens": llm_gen_ai_usage_input_tokens,
402402
"gen_ai.usage.output_tokens": llm_gen_ai_usage_output_tokens,

veadk/tracing/telemetry/attributes/extractors/tool_attributes_extractors.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def tool_gen_ai_tool_message(params: ToolAttributesParams) -> ExtractorResponse:
3838
return ExtractorResponse(type="event", content=tool_input)
3939

4040

41-
def tool_cozeloop_input(params: ToolAttributesParams) -> ExtractorResponse:
41+
def tool_gen_ai_tool_input(params: ToolAttributesParams) -> ExtractorResponse:
4242
tool_input = {
4343
"name": params.tool.name,
4444
"description": params.tool.description,
@@ -53,7 +53,7 @@ def tool_gen_ai_tool_name(params: ToolAttributesParams) -> ExtractorResponse:
5353
return ExtractorResponse(content=params.tool.name or "<unknown_tool_name>")
5454

5555

56-
def tool_cozeloop_output(params: ToolAttributesParams) -> ExtractorResponse:
56+
def tool_gen_ai_tool_output(params: ToolAttributesParams) -> ExtractorResponse:
5757
function_response = params.function_response_event.get_function_responses()[
5858
0
5959
].model_dump()
@@ -70,6 +70,8 @@ def tool_cozeloop_output(params: ToolAttributesParams) -> ExtractorResponse:
7070
TOOL_ATTRIBUTES = {
7171
"gen_ai.operation.name": tool_gen_ai_operation_name,
7272
"gen_ai.tool.name": tool_gen_ai_tool_name, # TLS required
73-
"cozeloop.input": tool_cozeloop_input, # CozeLoop required
74-
"cozeloop.output": tool_cozeloop_output, # CozeLoop required
73+
"gen_ai.tool.input": tool_gen_ai_tool_input, # TLS required
74+
"gen_ai.tool.output": tool_gen_ai_tool_output, # TLS required
75+
"cozeloop.input": tool_gen_ai_tool_input, # CozeLoop required
76+
"cozeloop.output": tool_gen_ai_tool_output, # CozeLoop required
7577
}

veadk/tracing/telemetry/telemetry.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ def upload_metrics(
5151
exporter.meter_uploader.record(llm_request, llm_response)
5252

5353

54-
def trace_send_data(): ...
55-
56-
57-
def set_common_attributes(
54+
def set_common_attributes_on_model_span(
5855
invocation_context: InvocationContext, current_span: _Span, **kwargs
5956
) -> None:
6057
if current_span.context:
@@ -66,7 +63,7 @@ def set_common_attributes(
6663
return
6764

6865
try:
69-
spans = _INMEMORY_EXPORTER_INSTANCE.processor.spans # # type: ignore
66+
spans = _INMEMORY_EXPORTER_INSTANCE.processor.spans # type: ignore
7067

7168
spans_in_current_trace = [
7269
span
@@ -76,24 +73,52 @@ def set_common_attributes(
7673

7774
common_attributes = ATTRIBUTES.get("common", {})
7875
for span in spans_in_current_trace:
79-
if span.name.startswith("invocation"):
80-
span.set_attribute("gen_ai.operation.name", "chain")
81-
elif span.name.startswith("agent_run"):
82-
span.set_attribute("gen_ai.operation.name", "agent")
83-
for attr_name, attr_extractor in common_attributes.items():
84-
value = attr_extractor(**kwargs)
85-
span.set_attribute(attr_name, value)
76+
if span.is_recording():
77+
if span.name.startswith("invocation"):
78+
span.set_attribute("gen_ai.operation.name", "chain")
79+
elif span.name.startswith("agent_run"):
80+
span.set_attribute("gen_ai.operation.name", "agent")
81+
for attr_name, attr_extractor in common_attributes.items():
82+
value = attr_extractor(**kwargs)
83+
span.set_attribute(attr_name, value)
8684
except Exception as e:
8785
logger.error(f"Failed to set common attributes for spans: {e}")
8886

8987

88+
def set_common_attributes_on_tool_span(current_span: _Span) -> None:
89+
# find parent span (generally a llm span)
90+
if not current_span.context:
91+
logger.warning(
92+
f"Get tool span's context failed. Skip setting common attributes for span {current_span.name}"
93+
)
94+
return
95+
96+
if not current_span.parent:
97+
logger.warning(
98+
f"Get tool span's parent failed. Skip setting common attributes for span {current_span.name}"
99+
)
100+
return
101+
102+
parent_span_id = current_span.parent.span_id
103+
for span in _INMEMORY_EXPORTER_INSTANCE.processor.spans: # type: ignore
104+
if span.context.span_id == parent_span_id:
105+
common_attributes = ATTRIBUTES.get("common", {})
106+
for attr_name in common_attributes.keys():
107+
current_span.set_attribute(attr_name, span.attributes[attr_name])
108+
109+
110+
def trace_send_data(): ...
111+
112+
90113
def trace_tool_call(
91114
tool: BaseTool,
92115
args: dict[str, Any],
93116
function_response_event: Event,
94117
) -> None:
95118
span = trace.get_current_span()
96119

120+
set_common_attributes_on_tool_span(current_span=span) # type: ignore
121+
97122
tool_attributes_mapping = ATTRIBUTES.get("tool", {})
98123
params = ToolAttributesParams(tool, args, function_response_event)
99124

@@ -112,7 +137,7 @@ def trace_call_llm(
112137

113138
from veadk.agent import Agent
114139

115-
set_common_attributes(
140+
set_common_attributes_on_model_span(
116141
invocation_context=invocation_context,
117142
current_span=span, # type: ignore
118143
agent_name=invocation_context.agent.name,

0 commit comments

Comments
 (0)