Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
63 changes: 35 additions & 28 deletions sentry_sdk/integrations/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from functools import wraps
from typing import TYPE_CHECKING, cast

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.ai.utils import (
GEN_AI_ALLOWED_MESSAGE_ROLES,
Expand Down Expand Up @@ -531,19 +533,19 @@ def _set_common_input_data(
"""
Set input data for the span based on the provided keyword arguments for the anthropic message creation.
"""
span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, "anthropic")
span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME, "anthropic")
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME, "chat")

if max_tokens is not None and _is_given(max_tokens):
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)
if model is not None and _is_given(model):
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model)
if temperature is not None and _is_given(temperature):
span.set_attribute(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE, temperature)
if top_k is not None and _is_given(top_k):
span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_K, top_k)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_K, top_k)
if top_p is not None and _is_given(top_p):
span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P, top_p)

if sentry_sdk.get_client().options["data_collection"]["gen_ai"]["inputs"]:
if tools is not None and _is_given(tools) and len(tools) > 0: # type: ignore
Expand All @@ -556,7 +558,7 @@ def _set_common_input_data(

if isinstance(system, str) or isinstance(system, Iterable):
span.set_attribute(
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS,
json.dumps(_transform_system_instructions(system)),
)

Expand Down Expand Up @@ -621,7 +623,9 @@ def _set_create_input_data(
"""
Set input data for the span based on the provided keyword arguments for the anthropic message creation.
"""
span.set_attribute(SPANDATA.GEN_AI_RESPONSE_STREAMING, kwargs.get("stream", False))
span.set_attribute(
ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING, kwargs.get("stream", False)
)

_set_common_input_data(
span=span,
Expand Down Expand Up @@ -713,11 +717,13 @@ def _set_output_data(
"""
Set output data for the span based on the AI response."""
if model is not None:
span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, model)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, model)
if response_id is not None:
span.set_attribute(SPANDATA.GEN_AI_RESPONSE_ID, response_id)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID, response_id)
if finish_reason is not None:
span.set_attribute(SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, [finish_reason])
span.set_attribute(
ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS, [finish_reason]
)

if sentry_sdk.get_client().options["data_collection"]["gen_ai"]["outputs"]:
parts: "list[Union[TextPart, ReasoningPart]]" = []
Expand All @@ -742,27 +748,28 @@ def _set_output_data(
if len(parts) > 0:
set_data_normalized(
span,
SPANDATA.GEN_AI_OUTPUT_MESSAGES,
ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES,
[{"role": "assistant", "parts": parts}],
unpack=False,
)

if input_tokens is not None:
span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)

if cache_read_input_tokens is not None:
span.set_attribute(
SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_input_tokens
ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
cache_read_input_tokens,
)

if cache_write_input_tokens is not None:
span.set_attribute(
SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
cache_write_input_tokens,
)

if output_tokens is not None:
span.set_attribute(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)

if output_tokens_reasoning is not None:
span.set_attribute(
Expand All @@ -771,7 +778,7 @@ def _set_output_data(

if input_tokens is not None and output_tokens is not None:
span.set_attribute(
SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, input_tokens + output_tokens
ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, input_tokens + output_tokens
)


Expand Down Expand Up @@ -799,8 +806,8 @@ def _sentry_patched_create_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any
span = sentry_sdk.start_span(
name=f"chat {model}".strip(),
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": AnthropicIntegration.origin,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: AnthropicIntegration.origin,
},
)

Expand Down Expand Up @@ -900,8 +907,8 @@ async def _sentry_patched_create_async(
span = sentry_sdk.start_span(
name=f"chat {model}".strip(),
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": AnthropicIntegration.origin,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: AnthropicIntegration.origin,
},
)

Expand Down Expand Up @@ -1155,9 +1162,9 @@ def _sentry_patched_enter(self: "MessageStreamManager") -> "MessageStream":
if patched_self._model is None
else f"chat {patched_self._model}".strip(),
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": AnthropicIntegration.origin,
SPANDATA.GEN_AI_RESPONSE_STREAMING: True,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: AnthropicIntegration.origin,
ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True,
},
)

Expand Down Expand Up @@ -1259,9 +1266,9 @@ async def _sentry_patched_aenter(
if patched_self._model is None
else f"chat {patched_self._model}".strip(),
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": AnthropicIntegration.origin,
SPANDATA.GEN_AI_RESPONSE_STREAMING: True,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: AnthropicIntegration.origin,
ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True,
},
)

Expand Down
27 changes: 15 additions & 12 deletions sentry_sdk/integrations/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from functools import wraps
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

from sentry_sdk import consts
from sentry_sdk.ai.utils import set_data_normalized
from sentry_sdk.consts import SPANDATA
Expand Down Expand Up @@ -120,13 +122,13 @@ def collect_chat_response_fields(
if hasattr(res.meta, "billed_units"):
if res.meta.billed_units.input_tokens is not None:
span.set_attribute(
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS,
ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS,
res.meta.billed_units.input_tokens,
)

if res.meta.billed_units.output_tokens is not None:
span.set_attribute(
SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS,
ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS,
res.meta.billed_units.output_tokens,
)

Expand All @@ -135,19 +137,20 @@ def collect_chat_response_fields(
and res.meta.billed_units.output_tokens is not None
):
span.set_attribute(
SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS,
ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS,
res.meta.billed_units.input_tokens
+ res.meta.billed_units.output_tokens,
)
elif hasattr(res.meta, "tokens"):
if res.meta.tokens.input_tokens is not None:
span.set_attribute(
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, res.meta.tokens.input_tokens
ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS,
res.meta.tokens.input_tokens,
)

if res.meta.tokens.output_tokens is not None:
span.set_attribute(
SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS,
ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS,
res.meta.tokens.output_tokens,
)

Expand All @@ -156,7 +159,7 @@ def collect_chat_response_fields(
and res.meta.tokens.output_tokens is not None
):
span.set_attribute(
SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS,
ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS,
res.meta.tokens.input_tokens + res.meta.tokens.output_tokens,
)

Expand All @@ -179,8 +182,8 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any":
span = sentry_sdk.start_span(
name="cohere.client.Chat",
attributes={
"sentry.op": consts.OP.GEN_AI_CHAT,
"sentry.origin": CohereIntegration.origin,
ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: CohereIntegration.origin,
},
)

Expand Down Expand Up @@ -273,8 +276,8 @@ def new_embed(*args: "Any", **kwargs: "Any") -> "Any":
with sentry_sdk.start_span(
name="Cohere Embedding Creation",
attributes={
"sentry.op": consts.OP.GEN_AI_EMBEDDINGS,
"sentry.origin": CohereIntegration.origin,
ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_EMBEDDINGS,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: CohereIntegration.origin,
},
) as span:
if (
Expand Down Expand Up @@ -310,12 +313,12 @@ def new_embed(*args: "Any", **kwargs: "Any") -> "Any":
):
if res.meta.billed_units.input_tokens is not None:
span.set_attribute(
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS,
ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS,
res.meta.billed_units.input_tokens,
)

span.set_attribute(
SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS,
ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS,
res.meta.billed_units.input_tokens,
)
return res
Expand Down
68 changes: 35 additions & 33 deletions sentry_sdk/integrations/google_genai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
List,
)

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
from sentry_sdk.traces import SpanStatus
from sentry_sdk.utils import parse_version
Expand Down Expand Up @@ -80,12 +82,12 @@ def new_generate_content_stream(
chat_span = sentry_sdk.start_span(
name=f"chat {model_name}",
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name,
SPANDATA.GEN_AI_RESPONSE_STREAMING: True,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat",
ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name,
ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True,
},
)

Expand Down Expand Up @@ -144,12 +146,12 @@ async def new_async_generate_content_stream(
chat_span = sentry_sdk.start_span(
name=f"chat {model_name}",
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name,
SPANDATA.GEN_AI_RESPONSE_STREAMING: True,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat",
ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name,
ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True,
},
)

Expand Down Expand Up @@ -204,11 +206,11 @@ def new_generate_content(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
with sentry_sdk.start_span(
name=f"chat {model_name}",
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat",
ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name,
},
) as chat_span:
set_span_data_for_request(
Expand Down Expand Up @@ -247,11 +249,11 @@ async def new_async_generate_content(
with sentry_sdk.start_span(
name=f"chat {model_name}",
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat",
ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name,
},
) as chat_span:
set_span_data_for_request(
Expand Down Expand Up @@ -284,11 +286,11 @@ def new_embed_content(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
with sentry_sdk.start_span(
name=f"embeddings {model_name}",
attributes={
"sentry.op": OP.GEN_AI_EMBEDDINGS,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "embeddings",
SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_EMBEDDINGS,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "embeddings",
ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name,
},
) as span:
set_span_data_for_embed_request(span, integration, contents, kwargs)
Expand Down Expand Up @@ -322,11 +324,11 @@ async def new_async_embed_content(
with sentry_sdk.start_span(
name=f"embeddings {model_name}",
attributes={
"sentry.op": OP.GEN_AI_EMBEDDINGS,
"sentry.origin": ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "embeddings",
SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
SPANDATA.GEN_AI_REQUEST_MODEL: model_name,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_EMBEDDINGS,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "embeddings",
ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name,
},
) as span:
set_span_data_for_embed_request(span, integration, contents, kwargs)
Expand Down
Loading
Loading