Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add only_if_parent option to POTelSpan and use it in integrations #3748

Merged
merged 1 commit into from
Nov 11, 2024
Merged
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
8 changes: 6 additions & 2 deletions sentry_sdk/ai/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def sync_wrapped(*args, **kwargs):
curr_pipeline = _ai_pipeline_name.get()
op = span_kwargs.get("op", "ai.run" if curr_pipeline else "ai.pipeline")

with start_span(name=description, op=op, **span_kwargs) as span:
with start_span(
name=description, op=op, only_if_parent=True, **span_kwargs
) as span:
for k, v in kwargs.pop("sentry_tags", {}).items():
span.set_tag(k, v)
for k, v in kwargs.pop("sentry_data", {}).items():
Expand Down Expand Up @@ -62,7 +64,9 @@ async def async_wrapped(*args, **kwargs):
curr_pipeline = _ai_pipeline_name.get()
op = span_kwargs.get("op", "ai.run" if curr_pipeline else "ai.pipeline")

with start_span(name=description, op=op, **span_kwargs) as span:
with start_span(
name=description, op=op, only_if_parent=True, **span_kwargs
) as span:
for k, v in kwargs.pop("sentry_tags", {}).items():
span.set_tag(k, v)
for k, v in kwargs.pop("sentry_data", {}).items():
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ async def on_request_start(session, trace_config_ctx, params):
name="%s %s"
% (method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE),
origin=AioHttpIntegration.origin,
only_if_parent=True,
)

data = {
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def _sentry_patched_create_common(f, *args, **kwargs):
op=OP.ANTHROPIC_MESSAGES_CREATE,
description="Anthropic messages create",
origin=AnthropicIntegration.origin,
only_if_parent=True,
)
span.__enter__()

Expand Down
5 changes: 4 additions & 1 deletion sentry_sdk/integrations/arq.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ async def _sentry_enqueue_job(self, function, *args, **kwargs):
return await old_enqueue_job(self, function, *args, **kwargs)

with sentry_sdk.start_span(
op=OP.QUEUE_SUBMIT_ARQ, name=function, origin=ArqIntegration.origin
op=OP.QUEUE_SUBMIT_ARQ,
name=function,
origin=ArqIntegration.origin,
only_if_parent=True,
):
return await old_enqueue_job(self, function, *args, **kwargs)

Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async def _coro_creating_hub_and_span():
op=OP.FUNCTION,
name=get_name(coro),
origin=AsyncioIntegration.origin,
only_if_parent=True,
):
try:
result = await coro
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ async def _inner(*args: Any, **kwargs: Any) -> T:
op=OP.DB,
name="connect",
origin=AsyncPGIntegration.origin,
only_if_parent=True,
) as span:
data = _get_db_data(
addr=kwargs.get("addr"),
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def _sentry_request_created(service_id, request, operation_name, **kwargs):
op=OP.HTTP_CLIENT,
name=description,
origin=Boto3Integration.origin,
only_if_parent=True,
)

data = {
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ def sentry_publish(self, *args, **kwargs):
op=OP.QUEUE_PUBLISH,
name=task_name,
origin=CeleryIntegration.origin,
only_if_parent=True,
) as span:
if task_id is not None:
span.set_data(SPANDATA.MESSAGING_MESSAGE_ID, task_id)
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/clickhouse_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
op=OP.DB,
name=query,
origin=ClickhouseDriverIntegration.origin,
only_if_parent=True,
)

connection._sentry_span = span # type: ignore[attr-defined]
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def new_chat(*args, **kwargs):
op=consts.OP.COHERE_CHAT_COMPLETIONS_CREATE,
name="cohere.client.Chat",
origin=CohereIntegration.origin,
only_if_parent=True,
)
span.__enter__()
try:
Expand Down Expand Up @@ -233,6 +234,7 @@ def new_embed(*args, **kwargs):
op=consts.OP.COHERE_EMBEDDINGS_CREATE,
name="Cohere Embedding Creation",
origin=CohereIntegration.origin,
only_if_parent=True,
) as span:
if "texts" in kwargs and (
should_send_default_pii() and integration.include_prompts
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ def connect(self):
op=OP.DB,
name="connect",
origin=DjangoIntegration.origin_db,
only_if_parent=True,
) as span:
_set_db_data(span, self)
return real_connect(self)
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/django/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ async def sentry_wrapped_callback(request, *args, **kwargs):
op=OP.VIEW_RENDER,
name=request.resolver_match.view_name,
origin=DjangoIntegration.origin,
only_if_parent=True,
):
return await callback(request, *args, **kwargs)

Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/django/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _instrument_call(
op=op,
name=description,
origin=DjangoIntegration.origin,
only_if_parent=True,
) as span:
value = original_method(*args, **kwargs)

Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def _check_middleware_span(old_method):
op=OP.MIDDLEWARE_DJANGO,
name=description,
origin=DjangoIntegration.origin,
only_if_parent=True,
)
middleware_span.set_tag("django.function_name", function_name)
middleware_span.set_tag("django.middleware_name", middleware_name)
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/django/signals_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def wrapper(*args, **kwargs):
op=OP.EVENT_DJANGO,
name=signal_name,
origin=DjangoIntegration.origin,
only_if_parent=True,
) as span:
span.set_data("signal", signal_name)
return receiver(*args, **kwargs)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/django/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def rendered_content(self):
op=OP.TEMPLATE_RENDER,
name=_get_template_name_description(self.template_name),
origin=DjangoIntegration.origin,
only_if_parent=True,
) as span:
if isinstance(self.context_data, dict):
for k, v in self.context_data.items():
Expand Down Expand Up @@ -102,6 +103,7 @@ def render(request, template_name, context=None, *args, **kwargs):
op=OP.TEMPLATE_RENDER,
name=_get_template_name_description(template_name),
origin=DjangoIntegration.origin,
only_if_parent=True,
) as span:
for k, v in context.items():
span.set_data(f"context.{k}", v)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def sentry_patched_render(self):
op=OP.VIEW_RESPONSE_RENDER,
name="serialize response",
origin=DjangoIntegration.origin,
only_if_parent=True,
):
return old_render(self)

Expand Down Expand Up @@ -90,6 +91,7 @@ def sentry_wrapped_callback(request, *args, **kwargs):
op=OP.VIEW_RENDER,
name=request.resolver_match.view_name,
origin=DjangoIntegration.origin,
only_if_parent=True,
):
return callback(request, *args, **kwargs)

Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/graphene.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def graphql_span(schema, source, kwargs):
if scope.span:
_graphql_span = scope.span.start_child(op=op, name=operation_name)
else:
_graphql_span = sentry_sdk.start_span(op=op, name=operation_name)
_graphql_span = sentry_sdk.start_span(
op=op, name=operation_name, only_if_parent=True
)

_graphql_span.set_data("graphql.document", source)
_graphql_span.set_data("graphql.operation.name", operation_name)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/grpc/aio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def intercept_unary_unary(
op=OP.GRPC_CLIENT,
name="unary unary call to %s" % method.decode(),
origin=SPAN_ORIGIN,
only_if_parent=True,
) as span:
span.set_data("type", "unary unary")
span.set_data("method", method)
Expand Down Expand Up @@ -82,6 +83,7 @@ async def intercept_unary_stream(
op=OP.GRPC_CLIENT,
name="unary stream call to %s" % method.decode(),
origin=SPAN_ORIGIN,
only_if_parent=True,
) as span:
span.set_data("type", "unary stream")
span.set_data("method", method)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/grpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
op=OP.GRPC_CLIENT,
name="unary unary call to %s" % method,
origin=SPAN_ORIGIN,
only_if_parent=True,
) as span:
span.set_data("type", "unary unary")
span.set_data("method", method)
Expand All @@ -52,6 +53,7 @@ def intercept_unary_stream(self, continuation, client_call_details, request):
op=OP.GRPC_CLIENT,
name="unary stream call to %s" % method,
origin=SPAN_ORIGIN,
only_if_parent=True,
) as span:
span.set_data("type", "unary stream")
span.set_data("method", method)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def send(self, request, **kwargs):
parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE,
),
origin=HttpxIntegration.origin,
only_if_parent=True,
) as span:
data = {
SPANDATA.HTTP_METHOD: request.method,
Expand Down Expand Up @@ -129,6 +130,7 @@ async def send(self, request, **kwargs):
parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE,
),
origin=HttpxIntegration.origin,
only_if_parent=True,
) as span:
data = {
SPANDATA.HTTP_METHOD: request.method,
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/huey.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def _sentry_enqueue(self, task):
op=OP.QUEUE_SUBMIT_HUEY,
name=task.name,
origin=HueyIntegration.origin,
only_if_parent=True,
):
if not isinstance(task, PeriodicTask):
# Attach trace propagation data to task kwargs. We do
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/huggingface_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def new_text_generation(*args, **kwargs):
op=consts.OP.HUGGINGFACE_HUB_CHAT_COMPLETIONS_CREATE,
name="Text Generation",
origin=HuggingfaceHubIntegration.origin,
only_if_parent=True,
)
span.__enter__()
try:
Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ def _create_span(self, run_id, parent_id, **kwargs):
watched_span = WatchedSpan(parent_span.span.start_child(**kwargs))
parent_span.children.append(watched_span)
if watched_span is None:
watched_span = WatchedSpan(sentry_sdk.start_span(**kwargs))
watched_span = WatchedSpan(
sentry_sdk.start_span(only_if_parent=True, **kwargs)
)

if kwargs.get("op", "").startswith("ai.pipeline."):
if kwargs.get("name"):
Expand Down
3 changes: 3 additions & 0 deletions sentry_sdk/integrations/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ async def _create_span_call(self, scope, receive, send):
op=OP.MIDDLEWARE_LITESTAR,
name=middleware_name,
origin=LitestarIntegration.origin,
only_if_parent=True,
) as middleware_span:
middleware_span.set_tag("litestar.middleware_name", middleware_name)

Expand All @@ -153,6 +154,7 @@ async def _sentry_receive(*args, **kwargs):
op=OP.MIDDLEWARE_LITESTAR_RECEIVE,
name=getattr(receive, "__qualname__", str(receive)),
origin=LitestarIntegration.origin,
only_if_parent=True,
) as span:
span.set_tag("litestar.middleware_name", middleware_name)
return await receive(*args, **kwargs)
Expand All @@ -170,6 +172,7 @@ async def _sentry_send(message):
op=OP.MIDDLEWARE_LITESTAR_SEND,
name=getattr(send, "__qualname__", str(send)),
origin=LitestarIntegration.origin,
only_if_parent=True,
) as span:
span.set_tag("litestar.middleware_name", middleware_name)
return await send(message)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def _new_chat_completion_common(f, *args, **kwargs):
op=consts.OP.OPENAI_CHAT_COMPLETIONS_CREATE,
description="Chat Completion",
origin=OpenAIIntegration.origin,
only_if_parent=True,
)
span.__enter__()

Expand Down Expand Up @@ -324,6 +325,7 @@ def _new_embeddings_create_common(f, *args, **kwargs):
op=consts.OP.OPENAI_EMBEDDINGS_CREATE,
description="OpenAI Embedding Creation",
origin=OpenAIIntegration.origin,
only_if_parent=True,
) as span:
if "input" in kwargs and (
should_send_default_pii() and integration.include_prompts
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def started(self, event):
op=OP.DB,
name=query,
origin=PyMongoIntegration.origin,
only_if_parent=True,
)

with capture_internal_exceptions():
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def _remote_method_with_header_propagation(*args, **kwargs):
op=OP.QUEUE_SUBMIT_RAY,
name=qualname_from_function(f),
origin=RayIntegration.origin,
only_if_parent=True,
) as span:
tracing = {
k: v
Expand Down
3 changes: 3 additions & 0 deletions sentry_sdk/integrations/redis/_async_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async def _sentry_execute(self, *args, **kwargs):
op=OP.DB_REDIS,
name="redis.pipeline.execute",
origin=SPAN_ORIGIN,
only_if_parent=True,
) as span:
with capture_internal_exceptions():
span_data = get_db_data_fn(self)
Expand Down Expand Up @@ -84,6 +85,7 @@ async def _sentry_execute_command(self, name, *args, **kwargs):
op=cache_properties["op"],
name=cache_properties["description"],
origin=SPAN_ORIGIN,
only_if_parent=True,
)
cache_span.__enter__()

Expand All @@ -93,6 +95,7 @@ async def _sentry_execute_command(self, name, *args, **kwargs):
op=db_properties["op"],
name=db_properties["description"],
origin=SPAN_ORIGIN,
only_if_parent=True,
)
db_span.__enter__()

Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/redis/_sync_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def sentry_patched_execute(self, *args, **kwargs):
op=OP.DB_REDIS,
name="redis.pipeline.execute",
origin=SPAN_ORIGIN,
only_if_parent=True,
) as span:
with capture_internal_exceptions():
span_data = get_db_data_fn(self)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def create_connection(
op=OP.SOCKET_CONNECTION,
name=_get_span_description(address[0], address[1]),
origin=SocketIntegration.origin,
only_if_parent=True,
) as span:
span.set_data("address", address)
span.set_data("timeout", timeout)
Expand All @@ -83,6 +84,7 @@ def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
op=OP.SOCKET_DNS,
name=_get_span_description(host, port),
origin=SocketIntegration.origin,
only_if_parent=True,
) as span:
span.set_data("host", host)
span.set_data("port", port)
Expand Down
3 changes: 3 additions & 0 deletions sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ async def _create_span_call(app, scope, receive, send, **kwargs):
op=OP.MIDDLEWARE_STARLETTE,
name=middleware_name,
origin=StarletteIntegration.origin,
only_if_parent=True,
) as middleware_span:
middleware_span.set_tag("starlette.middleware_name", middleware_name)

Expand All @@ -174,6 +175,7 @@ async def _sentry_receive(*args, **kwargs):
op=OP.MIDDLEWARE_STARLETTE_RECEIVE,
name=getattr(receive, "__qualname__", str(receive)),
origin=StarletteIntegration.origin,
only_if_parent=True,
) as span:
span.set_tag("starlette.middleware_name", middleware_name)
return await receive(*args, **kwargs)
Expand All @@ -189,6 +191,7 @@ async def _sentry_send(*args, **kwargs):
op=OP.MIDDLEWARE_STARLETTE_SEND,
name=getattr(send, "__qualname__", str(send)),
origin=StarletteIntegration.origin,
only_if_parent=True,
) as span:
span.set_tag("starlette.middleware_name", middleware_name)
return await send(*args, **kwargs)
Expand Down
Loading
Loading