Skip to content

fix(integrations): allow explicit op parameter in ai_track #4597

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions sentry_sdk/ai/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def decorator(f):
def sync_wrapped(*args, **kwargs):
# type: (Any, Any) -> Any
curr_pipeline = _ai_pipeline_name.get()
op = span_kwargs.get("op", "ai.run" if curr_pipeline else "ai.pipeline")
op = span_kwargs.pop("op", "ai.run" if curr_pipeline else "ai.pipeline")

with start_span(name=description, op=op, **span_kwargs) as span:
for k, v in kwargs.pop("sentry_tags", {}).items():
Expand Down Expand Up @@ -61,7 +61,7 @@ def sync_wrapped(*args, **kwargs):
async def async_wrapped(*args, **kwargs):
# type: (Any, Any) -> Any
curr_pipeline = _ai_pipeline_name.get()
op = span_kwargs.get("op", "ai.run" if curr_pipeline else "ai.pipeline")
op = span_kwargs.pop("op", "ai.run" if curr_pipeline else "ai.pipeline")

with start_span(name=description, op=op, **span_kwargs) as span:
for k, v in kwargs.pop("sentry_tags", {}).items():
Expand Down
41 changes: 41 additions & 0 deletions tests/test_ai_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,44 @@ async def async_pipeline():
assert ai_pipeline_span["tags"]["user"] == "czyber"
assert ai_pipeline_span["data"]["some_data"] == "value"
assert ai_run_span["description"] == "my async tool"


def test_ai_track_with_explicit_op(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()

@ai_track("my tool", op="custom.operation")
def tool(**kwargs):
pass

with sentry_sdk.start_transaction():
tool()

transaction = events[0]
assert transaction["type"] == "transaction"
assert len(transaction["spans"]) == 1
span = transaction["spans"][0]

assert span["description"] == "my tool"
assert span["op"] == "custom.operation"


@pytest.mark.asyncio
async def test_ai_track_async_with_explicit_op(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()

@ai_track("my async tool", op="custom.async.operation")
async def async_tool(**kwargs):
pass

with sentry_sdk.start_transaction():
await async_tool()

transaction = events[0]
assert transaction["type"] == "transaction"
assert len(transaction["spans"]) == 1
span = transaction["spans"][0]

assert span["description"] == "my async tool"
assert span["op"] == "custom.async.operation"