Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
)
from opentelemetry.instrumentation.langchain.span_utils import (
SpanHolder,
_map_finish_reason,
_set_span_attribute,
extract_model_name_from_response_metadata,
_extract_model_name_from_association_metadata,
Expand Down Expand Up @@ -451,10 +452,14 @@ def _create_llm_span(
_extract_class_name_from_serialized(serialized)
)

_set_span_attribute(span, GenAIAttributes.GEN_AI_SYSTEM, vendor)
_set_span_attribute(span, SpanAttributes.LLM_REQUEST_TYPE, request_type.value)
_set_span_attribute(span, GenAIAttributes.GEN_AI_PROVIDER_NAME, vendor)
operation_name = (
GenAiOperationNameValues.CHAT.value
if request_type == LLMRequestTypeValues.CHAT
else GenAiOperationNameValues.TEXT_COMPLETION.value
)
_set_span_attribute(
span, GenAIAttributes.GEN_AI_OPERATION_NAME, GenAICustomOperationName.LLM_REQUEST.value
span, GenAIAttributes.GEN_AI_OPERATION_NAME, operation_name
)

# we already have an LLM span by this point,
Expand Down Expand Up @@ -732,16 +737,16 @@ def on_llm_end(
span, GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS, completion_tokens
)
_set_span_attribute(
span, SpanAttributes.LLM_USAGE_TOTAL_TOKENS, total_tokens
span, SpanAttributes.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens
)

# Record token usage metrics
vendor = span.attributes.get(GenAIAttributes.GEN_AI_SYSTEM, "Langchain")
vendor = span.attributes.get(GenAIAttributes.GEN_AI_PROVIDER_NAME, "langchain")
if prompt_tokens > 0:
self.token_histogram.record(
prompt_tokens,
attributes={
GenAIAttributes.GEN_AI_SYSTEM: vendor,
GenAIAttributes.GEN_AI_PROVIDER_NAME: vendor,
GenAIAttributes.GEN_AI_TOKEN_TYPE: "input",
GenAIAttributes.GEN_AI_RESPONSE_MODEL: model_name or "unknown",
},
Expand All @@ -751,7 +756,7 @@ def on_llm_end(
self.token_histogram.record(
completion_tokens,
attributes={
GenAIAttributes.GEN_AI_SYSTEM: vendor,
GenAIAttributes.GEN_AI_PROVIDER_NAME: vendor,
GenAIAttributes.GEN_AI_TOKEN_TYPE: "output",
GenAIAttributes.GEN_AI_RESPONSE_MODEL: model_name or "unknown",
},
Expand All @@ -768,11 +773,11 @@ def on_llm_end(

# Record duration before ending span
duration = time.time() - self.spans[run_id].start_time
vendor = span.attributes.get(GenAIAttributes.GEN_AI_SYSTEM, "Langchain")
vendor = span.attributes.get(GenAIAttributes.GEN_AI_PROVIDER_NAME, "langchain")
self.duration_histogram.record(
duration,
attributes={
GenAIAttributes.GEN_AI_SYSTEM: vendor,
GenAIAttributes.GEN_AI_PROVIDER_NAME: vendor,
GenAIAttributes.GEN_AI_RESPONSE_MODEL: model_name or "unknown",
},
)
Expand Down Expand Up @@ -1093,12 +1098,10 @@ def _emit_generation_choice_event(
):
if isinstance(generation, (ChatGeneration, ChatGenerationChunk)):
# Get finish reason
raw_finish_reason = None
if hasattr(generation, "generation_info") and generation.generation_info:
finish_reason = generation.generation_info.get(
"finish_reason", "unknown"
)
else:
finish_reason = "unknown"
raw_finish_reason = generation.generation_info.get("finish_reason")
finish_reason = _map_finish_reason(raw_finish_reason) if raw_finish_reason else None

# Get tool calls
if (
Expand Down Expand Up @@ -1139,12 +1142,10 @@ def _emit_generation_choice_event(
)
elif isinstance(generation, (Generation, GenerationChunk)):
# Get finish reason
raw_finish_reason = None
if hasattr(generation, "generation_info") and generation.generation_info:
finish_reason = generation.generation_info.get(
"finish_reason", "unknown"
)
else:
finish_reason = "unknown"
raw_finish_reason = generation.generation_info.get("finish_reason")
finish_reason = _map_finish_reason(raw_finish_reason) if raw_finish_reason else None

# Emit the event
emit_event(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Roles(Enum):
VALID_MESSAGE_ROLES = {role.value for role in Roles}
"""The valid roles for naming the message event."""

EVENT_ATTRIBUTES = {GenAIAttributes.GEN_AI_SYSTEM: "langchain"}
EVENT_ATTRIBUTES = {GenAIAttributes.GEN_AI_PROVIDER_NAME: "langchain"}
"""The attributes to be used for the event."""


Expand Down
Loading
Loading