Skip to content

Commit 3c72f9d

Browse files
committed
feat: add apmplus metrics
1 parent 54e27f3 commit 3c72f9d

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

veadk/tracing/telemetry/exporters/apmplus_exporter.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ def __init__(
197197
explicit_bucket_boundaries_advisory=_GEN_AI_SERVER_TIME_PER_OUTPUT_TOKEN_BUCKETS,
198198
)
199199

200+
# apmplus metrics for veadk dashboard
201+
self.apmplus_span_latency = self.meter.create_histogram(
202+
name=Meters.APMPLUS_SPAN_LATENCY,
203+
description="Latency of span",
204+
unit="s",
205+
explicit_bucket_boundaries_advisory=_GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS,
206+
)
207+
self.apmplus_tool_token_usage = self.meter.create_histogram(
208+
name=Meters.APMPLUS_TOOL_TOKEN_USAGE,
209+
description="Token consumption of APMPlus tool token",
210+
unit="count",
211+
explicit_bucket_boundaries_advisory=_GEN_AI_CLIENT_TOKEN_USAGE_BUCKETS,
212+
)
213+
214+
200215
def record_call_llm(
201216
self,
202217
invocation_context: InvocationContext,
@@ -269,6 +284,33 @@ def record_call_llm(
269284
# time_per_output_token, attributes=attributes
270285
# )
271286

287+
# add span name attribute
288+
289+
span = trace.get_current_span()
290+
if not span:
291+
return
292+
span_name = "excuteTool memory"
293+
# span_name_attributes = {**attributes, "name": span.name}
294+
span_name_attributes = {**attributes,
295+
# "name": span.name,
296+
"gen_ai_kind":"tool",
297+
"operation":"load_knowledgebase"}
298+
if hasattr(span, "start_time") and self.apmplus_span_latency:
299+
# span 耗时
300+
duration = (time.time_ns() - span.start_time)/1e9 # type: ignore
301+
self.apmplus_span_latency.record(
302+
duration, attributes=span_name_attributes
303+
)
304+
if self.apmplus_tool_token_usage:
305+
tool_token_usage = 122 # tool token 数量,使用文本长度/4
306+
# TODO: 设置 token_type: input, output
307+
tool_token_attributes = {**span_name_attributes, "token_type": "input"}
308+
309+
self.apmplus_tool_token_usage.record(
310+
tool_token_usage, attributes=tool_token_attributes
311+
)
312+
313+
272314
def record_tool_call(
273315
self,
274316
tool: BaseTool,
@@ -277,7 +319,6 @@ def record_tool_call(
277319
):
278320
logger.debug(f"Record tool call work in progress. Tool: {tool.name}")
279321

280-
281322
class APMPlusExporterConfig(BaseModel):
282323
endpoint: str = Field(
283324
default_factory=lambda: settings.apmplus_config.otel_exporter_endpoint,

0 commit comments

Comments
 (0)