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+
16import time
27from dataclasses import dataclass , field
38from typing import Any , Optional
914from langchain_core .outputs import LLMResult
1015
1116from 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
1318from opentelemetry .instrumentation .utils import _SUPPRESS_INSTRUMENTATION_KEY
1419from opentelemetry .trace import SpanKind , set_span_in_context
1520from opentelemetry .trace .span import Span
@@ -31,7 +36,7 @@ def _set_request_params(span, kwargs, span_holder: SpanHolder):
3136 if (model := kwargs .get (model_tag )) is not None :
3237 span_holder .request_model = model
3338 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 :
3540 span_holder .request_model = model
3641 break
3742 else :
@@ -40,8 +45,8 @@ def _set_request_params(span, kwargs, span_holder: SpanHolder):
4045 if span_holder .request_model is None :
4146 model = None
4247
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 )
4550
4651 if "invocation_params" in kwargs :
4752 params = kwargs ["invocation_params" ].get ("params" ) or kwargs ["invocation_params" ]
@@ -50,13 +55,13 @@ def _set_request_params(span, kwargs, span_holder: SpanHolder):
5055
5156 _set_span_attribute (
5257 span ,
53- Span_Attributes .GEN_AI_REQUEST_MAX_TOKENS ,
58+ SpanAttributes .GEN_AI_REQUEST_MAX_TOKENS ,
5459 params .get ("max_tokens" ) or params .get ("max_new_tokens" ),
5560 )
5661
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" ))
5863
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" ))
6065
6166
6267def _set_span_attribute (span : Span , name : str , value : AttributeValue ):
@@ -196,13 +201,13 @@ def on_chat_model_start(
196201 kind = SpanKind .CLIENT ,
197202 metadata = metadata ,
198203 )
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 )
200205
201206 if "kwargs" in serialized :
202207 _set_request_params (span , serialized ["kwargs" ], self .span_mapping [run_id ])
203208 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" )
206211
207212 def on_llm_start (
208213 self ,
@@ -233,13 +238,13 @@ def on_llm_start(
233238 kind = SpanKind .CLIENT ,
234239 metadata = metadata ,
235240 )
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 )
237242
238243 _set_request_params (span , kwargs , self .span_mapping [run_id ])
239244
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" ))
241246
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" )
243248
244249 def on_llm_end (
245250 self ,
@@ -263,11 +268,11 @@ def on_llm_end(
263268 if response .llm_output is not None :
264269 model_name = response .llm_output .get ("model_name" ) or response .llm_output .get ("model_id" )
265270 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 )
267272
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 )
271276
272277 token_usage = (response .llm_output or {}).get ("token_usage" ) or (response .llm_output or {}).get ("usage" )
273278
@@ -283,9 +288,9 @@ def on_llm_end(
283288 or token_usage .get ("output_tokens" )
284289 )
285290
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 )
287292
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 )
289294
290295 self ._end_span (span , run_id )
291296
@@ -325,7 +330,7 @@ def on_chain_start(
325330 )
326331
327332 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" ])
329334
330335 _set_span_attribute (span , "gen_ai.prompt" , str (inputs ))
331336
@@ -384,18 +389,18 @@ def on_tool_start(
384389 _set_span_attribute (span , "gen_ai.tool.input" , input_str )
385390
386391 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" ))
388393
389394 if serialized .get ("description" ):
390395 _set_span_attribute (
391396 span ,
392- Span_Attributes .GEN_AI_TOOL_DESCRIPTION ,
397+ SpanAttributes .GEN_AI_TOOL_DESCRIPTION ,
393398 serialized .get ("description" ),
394399 )
395400
396- _set_span_attribute (span , Span_Attributes .GEN_AI_TOOL_NAME , name )
401+ _set_span_attribute (span , SpanAttributes .GEN_AI_TOOL_NAME , name )
397402
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" )
399404
400405 def on_tool_end (
401406 self ,
@@ -433,7 +438,7 @@ def on_agent_action(self, action: AgentAction, run_id: UUID, parent_run_id: UUID
433438
434439 _set_span_attribute (span , "gen_ai.agent.tool.input" , tool_input )
435440 _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" )
437442
438443 def on_agent_finish (self , finish : AgentFinish , run_id : UUID , parent_run_id : UUID , ** kwargs : Any ):
439444
0 commit comments