1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ # pylint: disable=no-self-use
5
+
1
6
import time
2
7
from dataclasses import dataclass , field
3
8
from typing import Any , Optional
9
14
from langchain_core .outputs import LLMResult
10
15
11
16
from opentelemetry import context as context_api
12
- from opentelemetry .instrumentation .langchain_v2 .span_attributes import GenAIOperationValues , Span_Attributes
17
+ from opentelemetry .instrumentation .langchain_v2 .span_attributes import GenAIOperationValues , SpanAttributes
13
18
from opentelemetry .instrumentation .utils import _SUPPRESS_INSTRUMENTATION_KEY
14
19
from opentelemetry .trace import SpanKind , set_span_in_context
15
20
from opentelemetry .trace .span import Span
@@ -31,7 +36,7 @@ def _set_request_params(span, kwargs, span_holder: SpanHolder):
31
36
if (model := kwargs .get (model_tag )) is not None :
32
37
span_holder .request_model = model
33
38
break
34
- elif (model := (kwargs .get ("invocation_params" ) or {}).get (model_tag )) is not None :
39
+ if (model := (kwargs .get ("invocation_params" ) or {}).get (model_tag )) is not None :
35
40
span_holder .request_model = model
36
41
break
37
42
else :
@@ -40,8 +45,8 @@ def _set_request_params(span, kwargs, span_holder: SpanHolder):
40
45
if span_holder .request_model is None :
41
46
model = None
42
47
43
- _set_span_attribute (span , Span_Attributes .GEN_AI_REQUEST_MODEL , model )
44
- _set_span_attribute (span , Span_Attributes .GEN_AI_RESPONSE_MODEL , model )
48
+ _set_span_attribute (span , SpanAttributes .GEN_AI_REQUEST_MODEL , model )
49
+ _set_span_attribute (span , SpanAttributes .GEN_AI_RESPONSE_MODEL , model )
45
50
46
51
if "invocation_params" in kwargs :
47
52
params = kwargs ["invocation_params" ].get ("params" ) or kwargs ["invocation_params" ]
@@ -50,13 +55,13 @@ def _set_request_params(span, kwargs, span_holder: SpanHolder):
50
55
51
56
_set_span_attribute (
52
57
span ,
53
- Span_Attributes .GEN_AI_REQUEST_MAX_TOKENS ,
58
+ SpanAttributes .GEN_AI_REQUEST_MAX_TOKENS ,
54
59
params .get ("max_tokens" ) or params .get ("max_new_tokens" ),
55
60
)
56
61
57
- _set_span_attribute (span , Span_Attributes .GEN_AI_REQUEST_TEMPERATURE , params .get ("temperature" ))
62
+ _set_span_attribute (span , SpanAttributes .GEN_AI_REQUEST_TEMPERATURE , params .get ("temperature" ))
58
63
59
- _set_span_attribute (span , Span_Attributes .GEN_AI_REQUEST_TOP_P , params .get ("top_p" ))
64
+ _set_span_attribute (span , SpanAttributes .GEN_AI_REQUEST_TOP_P , params .get ("top_p" ))
60
65
61
66
62
67
def _set_span_attribute (span : Span , name : str , value : AttributeValue ):
@@ -196,13 +201,13 @@ def on_chat_model_start(
196
201
kind = SpanKind .CLIENT ,
197
202
metadata = metadata ,
198
203
)
199
- _set_span_attribute (span , Span_Attributes .GEN_AI_OPERATION_NAME , GenAIOperationValues .CHAT )
204
+ _set_span_attribute (span , SpanAttributes .GEN_AI_OPERATION_NAME , GenAIOperationValues .CHAT )
200
205
201
206
if "kwargs" in serialized :
202
207
_set_request_params (span , serialized ["kwargs" ], self .span_mapping [run_id ])
203
208
if "name" in serialized :
204
- _set_span_attribute (span , Span_Attributes .GEN_AI_SYSTEM , serialized .get ("name" ))
205
- _set_span_attribute (span , Span_Attributes .GEN_AI_OPERATION_NAME , "chat" )
209
+ _set_span_attribute (span , SpanAttributes .GEN_AI_SYSTEM , serialized .get ("name" ))
210
+ _set_span_attribute (span , SpanAttributes .GEN_AI_OPERATION_NAME , "chat" )
206
211
207
212
def on_llm_start (
208
213
self ,
@@ -233,13 +238,13 @@ def on_llm_start(
233
238
kind = SpanKind .CLIENT ,
234
239
metadata = metadata ,
235
240
)
236
- _set_span_attribute (span , Span_Attributes .GEN_AI_OPERATION_NAME , GenAIOperationValues .CHAT )
241
+ _set_span_attribute (span , SpanAttributes .GEN_AI_OPERATION_NAME , GenAIOperationValues .CHAT )
237
242
238
243
_set_request_params (span , kwargs , self .span_mapping [run_id ])
239
244
240
- _set_span_attribute (span , Span_Attributes .GEN_AI_SYSTEM , serialized .get ("name" ))
245
+ _set_span_attribute (span , SpanAttributes .GEN_AI_SYSTEM , serialized .get ("name" ))
241
246
242
- _set_span_attribute (span , Span_Attributes .GEN_AI_OPERATION_NAME , "text_completion" )
247
+ _set_span_attribute (span , SpanAttributes .GEN_AI_OPERATION_NAME , "text_completion" )
243
248
244
249
def on_llm_end (
245
250
self ,
@@ -263,11 +268,11 @@ def on_llm_end(
263
268
if response .llm_output is not None :
264
269
model_name = response .llm_output .get ("model_name" ) or response .llm_output .get ("model_id" )
265
270
if model_name is not None :
266
- _set_span_attribute (span , Span_Attributes .GEN_AI_RESPONSE_MODEL , model_name )
271
+ _set_span_attribute (span , SpanAttributes .GEN_AI_RESPONSE_MODEL , model_name )
267
272
268
- id = response .llm_output .get ("id" )
269
- if id is not None and id != "" :
270
- _set_span_attribute (span , Span_Attributes .GEN_AI_RESPONSE_ID , id )
273
+ item_id = response .llm_output .get ("id" )
274
+ if item_id is not None and item_id != "" :
275
+ _set_span_attribute (span , SpanAttributes .GEN_AI_RESPONSE_ID , item_id )
271
276
272
277
token_usage = (response .llm_output or {}).get ("token_usage" ) or (response .llm_output or {}).get ("usage" )
273
278
@@ -283,9 +288,9 @@ def on_llm_end(
283
288
or token_usage .get ("output_tokens" )
284
289
)
285
290
286
- _set_span_attribute (span , Span_Attributes .GEN_AI_USAGE_INPUT_TOKENS , prompt_tokens )
291
+ _set_span_attribute (span , SpanAttributes .GEN_AI_USAGE_INPUT_TOKENS , prompt_tokens )
287
292
288
- _set_span_attribute (span , Span_Attributes .GEN_AI_USAGE_OUTPUT_TOKENS , completion_tokens )
293
+ _set_span_attribute (span , SpanAttributes .GEN_AI_USAGE_OUTPUT_TOKENS , completion_tokens )
289
294
290
295
self ._end_span (span , run_id )
291
296
@@ -325,7 +330,7 @@ def on_chain_start(
325
330
)
326
331
327
332
if "agent_name" in metadata :
328
- _set_span_attribute (span , Span_Attributes .GEN_AI_AGENT_NAME , metadata ["agent_name" ])
333
+ _set_span_attribute (span , SpanAttributes .GEN_AI_AGENT_NAME , metadata ["agent_name" ])
329
334
330
335
_set_span_attribute (span , "gen_ai.prompt" , str (inputs ))
331
336
@@ -384,18 +389,18 @@ def on_tool_start(
384
389
_set_span_attribute (span , "gen_ai.tool.input" , input_str )
385
390
386
391
if serialized .get ("id" ):
387
- _set_span_attribute (span , Span_Attributes .GEN_AI_TOOL_CALL_ID , serialized .get ("id" ))
392
+ _set_span_attribute (span , SpanAttributes .GEN_AI_TOOL_CALL_ID , serialized .get ("id" ))
388
393
389
394
if serialized .get ("description" ):
390
395
_set_span_attribute (
391
396
span ,
392
- Span_Attributes .GEN_AI_TOOL_DESCRIPTION ,
397
+ SpanAttributes .GEN_AI_TOOL_DESCRIPTION ,
393
398
serialized .get ("description" ),
394
399
)
395
400
396
- _set_span_attribute (span , Span_Attributes .GEN_AI_TOOL_NAME , name )
401
+ _set_span_attribute (span , SpanAttributes .GEN_AI_TOOL_NAME , name )
397
402
398
- _set_span_attribute (span , Span_Attributes .GEN_AI_OPERATION_NAME , "execute_tool" )
403
+ _set_span_attribute (span , SpanAttributes .GEN_AI_OPERATION_NAME , "execute_tool" )
399
404
400
405
def on_tool_end (
401
406
self ,
@@ -433,7 +438,7 @@ def on_agent_action(self, action: AgentAction, run_id: UUID, parent_run_id: UUID
433
438
434
439
_set_span_attribute (span , "gen_ai.agent.tool.input" , tool_input )
435
440
_set_span_attribute (span , "gen_ai.agent.tool.name" , tool )
436
- _set_span_attribute (span , Span_Attributes .GEN_AI_OPERATION_NAME , "invoke_agent" )
441
+ _set_span_attribute (span , SpanAttributes .GEN_AI_OPERATION_NAME , "invoke_agent" )
437
442
438
443
def on_agent_finish (self , finish : AgentFinish , run_id : UUID , parent_run_id : UUID , ** kwargs : Any ):
439
444
0 commit comments