@@ -124,6 +124,12 @@ class Meters:
124124 "gen_ai.chat_completions.streaming_time_per_output_token"
125125 )
126126
127+ # apmplus metrics
128+ # span duration
129+ APMPLUS_SPAN_LATENCY = "apmplus_span_latency"
130+ # tool token usage
131+ APMPLUS_TOOL_TOKEN_USAGE = "apmplus_tool_token_usage"
132+
127133
128134class MeterUploader :
129135 def __init__ (
@@ -195,6 +201,21 @@ def __init__(
195201 explicit_bucket_boundaries_advisory = _GEN_AI_SERVER_TIME_PER_OUTPUT_TOKEN_BUCKETS ,
196202 )
197203
204+ # apmplus metrics for veadk dashboard
205+ self .apmplus_span_latency = self .meter .create_histogram (
206+ name = Meters .APMPLUS_SPAN_LATENCY ,
207+ description = "Latency of span" ,
208+ unit = "s" ,
209+ explicit_bucket_boundaries_advisory = _GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS ,
210+ )
211+ self .apmplus_tool_token_usage = self .meter .create_histogram (
212+ name = Meters .APMPLUS_TOOL_TOKEN_USAGE ,
213+ description = "Token consumption of APMPlus tool token" ,
214+ unit = "count" ,
215+ explicit_bucket_boundaries_advisory = _GEN_AI_CLIENT_TOKEN_USAGE_BUCKETS ,
216+ )
217+
218+
198219 def record (
199220 self ,
200221 invocation_context : InvocationContext ,
@@ -267,6 +288,32 @@ def record(
267288 # time_per_output_token, attributes=attributes
268289 # )
269290
291+ # add span name attribute
292+
293+ span = trace .get_current_span ()
294+ if not span :
295+ return
296+ span_name = "excuteTool memory"
297+ # span_name_attributes = {**attributes, "name": span.name}
298+ span_name_attributes = {** attributes ,
299+ # "name": span.name,
300+ "gen_ai_kind" :"tool" ,
301+ "operation" :"load_knowledgebase" }
302+ if hasattr (span , "start_time" ) and self .apmplus_span_latency :
303+ # span 耗时
304+ duration = (time .time_ns () - span .start_time )/ 1e9 # type: ignore
305+ self .apmplus_span_latency .record (
306+ duration , attributes = span_name_attributes
307+ )
308+ if self .apmplus_tool_token_usage :
309+ tool_token_usage = 122 # tool token 数量,使用文本长度/4
310+ # TODO: 设置 token_type: input, output
311+ tool_token_attributes = {** span_name_attributes , "token_type" : "input" }
312+
313+ self .apmplus_tool_token_usage .record (
314+ tool_token_usage , attributes = tool_token_attributes
315+ )
316+
270317
271318class APMPlusExporterConfig (BaseModel ):
272319 endpoint : str = Field (
0 commit comments