Skip to content

Commit 632dc68

Browse files
authored
Add only_if_parent option to POTelSpan and use it in integrations (#3748)
If this option is on, we will only create a new underlying otel span if there's an active valid parent, otherwise we will just return an invalid `NonRecordingSpan` (`INVALID_SPAN`). All internal integration child `start_span` calls have been modified so that now we will only create spans if there is an active root span (transaction) active.
1 parent 4650c7e commit 632dc68

36 files changed

+98
-23
lines changed

sentry_sdk/ai/monitoring.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def sync_wrapped(*args, **kwargs):
3333
curr_pipeline = _ai_pipeline_name.get()
3434
op = span_kwargs.get("op", "ai.run" if curr_pipeline else "ai.pipeline")
3535

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

65-
with start_span(name=description, op=op, **span_kwargs) as span:
67+
with start_span(
68+
name=description, op=op, only_if_parent=True, **span_kwargs
69+
) as span:
6670
for k, v in kwargs.pop("sentry_tags", {}).items():
6771
span.set_tag(k, v)
6872
for k, v in kwargs.pop("sentry_data", {}).items():

sentry_sdk/integrations/aiohttp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ async def on_request_start(session, trace_config_ctx, params):
228228
name="%s %s"
229229
% (method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE),
230230
origin=AioHttpIntegration.origin,
231+
only_if_parent=True,
231232
)
232233

233234
data = {

sentry_sdk/integrations/anthropic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def _sentry_patched_create_common(f, *args, **kwargs):
151151
op=OP.ANTHROPIC_MESSAGES_CREATE,
152152
description="Anthropic messages create",
153153
origin=AnthropicIntegration.origin,
154+
only_if_parent=True,
154155
)
155156
span.__enter__()
156157

sentry_sdk/integrations/arq.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ async def _sentry_enqueue_job(self, function, *args, **kwargs):
7979
return await old_enqueue_job(self, function, *args, **kwargs)
8080

8181
with sentry_sdk.start_span(
82-
op=OP.QUEUE_SUBMIT_ARQ, name=function, origin=ArqIntegration.origin
82+
op=OP.QUEUE_SUBMIT_ARQ,
83+
name=function,
84+
origin=ArqIntegration.origin,
85+
only_if_parent=True,
8386
):
8487
return await old_enqueue_job(self, function, *args, **kwargs)
8588

sentry_sdk/integrations/asyncio.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ async def _coro_creating_hub_and_span():
4848
op=OP.FUNCTION,
4949
name=get_name(coro),
5050
origin=AsyncioIntegration.origin,
51+
only_if_parent=True,
5152
):
5253
try:
5354
result = await coro

sentry_sdk/integrations/asyncpg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ async def _inner(*args: Any, **kwargs: Any) -> T:
169169
op=OP.DB,
170170
name="connect",
171171
origin=AsyncPGIntegration.origin,
172+
only_if_parent=True,
172173
) as span:
173174
data = _get_db_data(
174175
addr=kwargs.get("addr"),

sentry_sdk/integrations/boto3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def _sentry_request_created(service_id, request, operation_name, **kwargs):
7272
op=OP.HTTP_CLIENT,
7373
name=description,
7474
origin=Boto3Integration.origin,
75+
only_if_parent=True,
7576
)
7677

7778
data = {

sentry_sdk/integrations/celery/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ def sentry_publish(self, *args, **kwargs):
495495
op=OP.QUEUE_PUBLISH,
496496
name=task_name,
497497
origin=CeleryIntegration.origin,
498+
only_if_parent=True,
498499
) as span:
499500
if task_id is not None:
500501
span.set_data(SPANDATA.MESSAGING_MESSAGE_ID, task_id)

sentry_sdk/integrations/clickhouse_driver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
8989
op=OP.DB,
9090
name=query,
9191
origin=ClickhouseDriverIntegration.origin,
92+
only_if_parent=True,
9293
)
9394

9495
connection._sentry_span = span # type: ignore[attr-defined]

sentry_sdk/integrations/cohere.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def new_chat(*args, **kwargs):
147147
op=consts.OP.COHERE_CHAT_COMPLETIONS_CREATE,
148148
name="cohere.client.Chat",
149149
origin=CohereIntegration.origin,
150+
only_if_parent=True,
150151
)
151152
span.__enter__()
152153
try:
@@ -233,6 +234,7 @@ def new_embed(*args, **kwargs):
233234
op=consts.OP.COHERE_EMBEDDINGS_CREATE,
234235
name="Cohere Embedding Creation",
235236
origin=CohereIntegration.origin,
237+
only_if_parent=True,
236238
) as span:
237239
if "texts" in kwargs and (
238240
should_send_default_pii() and integration.include_prompts

0 commit comments

Comments
 (0)