Skip to content

Commit 3b72b0c

Browse files
authored
Merge branch 'potel-base' into ivana/potel/disabled-span-origins
2 parents af02cbb + 03659a4 commit 3b72b0c

36 files changed

+1610
-118
lines changed

.github/workflows/test-integrations-ai.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ jobs:
5959
run: |
6060
set -x # print commands that are executed
6161
./scripts/runtox.sh "py${{ matrix.python-version }}-openai-latest"
62+
- name: Test openai_agents latest
63+
run: |
64+
set -x # print commands that are executed
65+
./scripts/runtox.sh "py${{ matrix.python-version }}-openai_agents-latest"
6266
- name: Test huggingface_hub latest
6367
run: |
6468
set -x # print commands that are executed
@@ -121,6 +125,10 @@ jobs:
121125
run: |
122126
set -x # print commands that are executed
123127
./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-openai"
128+
- name: Test openai_agents pinned
129+
run: |
130+
set -x # print commands that are executed
131+
./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-openai_agents"
124132
- name: Test huggingface_hub pinned
125133
run: |
126134
set -x # print commands that are executed

requirements-aws-lambda-layer.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
certifi
2-
3-
# In Lambda functions botocore is used, and botocore is not
4-
# yet supporting urllib3 1.27.0 never mind 2+.
2+
urllib3
3+
# In Lambda functions botocore is used, and botocore has
4+
# restrictions on urllib3
5+
# https://github.com/boto/botocore/blob/develop/setup.cfg
56
# So we pin this here to make our Lambda layer work with
67
# Lambda Function using Python 3.7+
7-
urllib3<1.27
8+
urllib3<1.27; python_version < "3.10"
89

910
opentelemetry-distro>=0.35b0

scripts/generate-test-files.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ set -xe
66

77
cd "$(dirname "$0")"
88

9+
rm -rf toxgen.venv
910
python -m venv toxgen.venv
1011
. toxgen.venv/bin/activate
1112

12-
pip install -e ..
13-
pip install -r populate_tox/requirements.txt
14-
pip install -r split_tox_gh_actions/requirements.txt
13+
toxgen.venv/bin/pip install -e ..
14+
toxgen.venv/bin/pip install -r populate_tox/requirements.txt
15+
toxgen.venv/bin/pip install -r split_tox_gh_actions/requirements.txt
1516

16-
python populate_tox/populate_tox.py
17-
python split_tox_gh_actions/split_tox_gh_actions.py
17+
toxgen.venv/bin/python populate_tox/populate_tox.py
18+
toxgen.venv/bin/python split_tox_gh_actions/split_tox_gh_actions.py

scripts/populate_tox/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@
152152
"loguru": {
153153
"package": "loguru",
154154
},
155+
"openai_agents": {
156+
"package": "openai-agents",
157+
"deps": {
158+
"*": ["pytest-asyncio"],
159+
},
160+
},
155161
"openfeature": {
156162
"package": "openfeature-sdk",
157163
},

scripts/populate_tox/populate_tox.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from bisect import bisect_left
1111
from collections import defaultdict
1212
from datetime import datetime, timedelta, timezone # noqa: F401
13-
from importlib.metadata import metadata
13+
from importlib.metadata import PackageMetadata, distributions
1414
from packaging.specifiers import SpecifierSet
1515
from packaging.version import Version
1616
from pathlib import Path
@@ -87,6 +87,13 @@
8787
}
8888

8989

90+
def _fetch_sdk_metadata() -> PackageMetadata:
91+
(dist,) = distributions(
92+
name="sentry-sdk", path=[Path(__file__).parent.parent.parent]
93+
)
94+
return dist.metadata
95+
96+
9097
def fetch_url(url: str) -> Optional[dict]:
9198
for attempt in range(3):
9299
pypi_data = requests.get(url)
@@ -592,8 +599,9 @@ def main(fail_on_changes: bool = False) -> None:
592599
)
593600

594601
global MIN_PYTHON_VERSION, MAX_PYTHON_VERSION
602+
meta = _fetch_sdk_metadata()
595603
sdk_python_versions = _parse_python_versions_from_classifiers(
596-
metadata("sentry-sdk").get_all("Classifier")
604+
meta.get_all("Classifier")
597605
)
598606
MIN_PYTHON_VERSION = sdk_python_versions[0]
599607
MAX_PYTHON_VERSION = sdk_python_versions[-1]

scripts/populate_tox/tox.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ setenv =
372372
litestar: TESTPATH=tests/integrations/litestar
373373
loguru: TESTPATH=tests/integrations/loguru
374374
openai: TESTPATH=tests/integrations/openai
375+
openai_agents: TESTPATH=tests/integrations/openai_agents
375376
openfeature: TESTPATH=tests/integrations/openfeature
376377
pure_eval: TESTPATH=tests/integrations/pure_eval
377378
pymongo: TESTPATH=tests/integrations/pymongo

scripts/split_tox_gh_actions/split_tox_gh_actions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"cohere",
6464
"langchain",
6565
"openai",
66+
"openai_agents",
6667
"huggingface_hub",
6768
],
6869
"Cloud": [

sentry_sdk/ai/monitoring.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,19 @@ def record_token_usage(
103103
ai_pipeline_name = get_ai_pipeline_name()
104104
if ai_pipeline_name:
105105
span.set_attribute(SPANDATA.AI_PIPELINE_NAME, ai_pipeline_name)
106+
106107
if prompt_tokens is not None:
107-
span.set_attribute(SPANDATA.AI_PROMPT_TOKENS_USED, prompt_tokens)
108+
span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, prompt_tokens)
109+
108110
if completion_tokens is not None:
109-
span.set_attribute(SPANDATA.AI_COMPLETION_TOKENS_USED, completion_tokens)
111+
span.set_attribute(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, completion_tokens)
112+
110113
if (
111114
total_tokens is None
112115
and prompt_tokens is not None
113116
and completion_tokens is not None
114117
):
115118
total_tokens = prompt_tokens + completion_tokens
119+
116120
if total_tokens is not None:
117-
span.set_attribute(SPANDATA.AI_TOTAL_TOKENS_USED, total_tokens)
121+
span.set_attribute(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens)

sentry_sdk/integrations/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def iter_default_integrations(
147147
"launchdarkly": (9, 8, 0),
148148
"loguru": (0, 7, 0),
149149
"openai": (1, 0, 0),
150+
"openai_agents": (0, 0, 19),
150151
"openfeature": (0, 7, 1),
151152
"quart": (0, 16, 0),
152153
"ray": (2, 7, 0),
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from sentry_sdk.integrations import DidNotEnable, Integration
2+
3+
from .patches import (
4+
_create_get_model_wrapper,
5+
_create_get_all_tools_wrapper,
6+
_create_run_wrapper,
7+
_patch_agent_run,
8+
)
9+
10+
try:
11+
import agents
12+
13+
except ImportError:
14+
raise DidNotEnable("OpenAI Agents not installed")
15+
16+
17+
def _patch_runner() -> None:
18+
# Create the root span for one full agent run (including eventual handoffs)
19+
# Note agents.run.DEFAULT_AGENT_RUNNER.run_sync is a wrapper around
20+
# agents.run.DEFAULT_AGENT_RUNNER.run. It does not need to be wrapped separately.
21+
# TODO-anton: Also patch streaming runner: agents.Runner.run_streamed
22+
agents.run.DEFAULT_AGENT_RUNNER.run = _create_run_wrapper(
23+
agents.run.DEFAULT_AGENT_RUNNER.run
24+
)
25+
26+
# Creating the actual spans for each agent run.
27+
_patch_agent_run()
28+
29+
30+
def _patch_model() -> None:
31+
agents.run.AgentRunner._get_model = classmethod(
32+
_create_get_model_wrapper(agents.run.AgentRunner._get_model),
33+
)
34+
35+
36+
def _patch_tools() -> None:
37+
agents.run.AgentRunner._get_all_tools = classmethod(
38+
_create_get_all_tools_wrapper(agents.run.AgentRunner._get_all_tools),
39+
)
40+
41+
42+
class OpenAIAgentsIntegration(Integration):
43+
identifier = "openai_agents"
44+
45+
@staticmethod
46+
def setup_once() -> None:
47+
_patch_tools()
48+
_patch_model()
49+
_patch_runner()

0 commit comments

Comments
 (0)