Skip to content

Commit 470bbbb

Browse files
merge master
2 parents ef3ddc6 + 7449603 commit 470bbbb

File tree

14 files changed

+353
-120
lines changed

14 files changed

+353
-120
lines changed

.github/release.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# This configuration is used by Craft to categorize changelog entries based on
2-
# PR labels. To avoid some manual work, there is a PR labeling GitHub action in
3-
# .github/workflows/pr-labeler.yml that adds a changelog label to PRs based on
4-
# the title.
5-
61
changelog:
72
exclude:
83
labels:
@@ -16,19 +11,29 @@ changelog:
1611
- Feature
1712
- Improvement
1813
- New Integration
14+
commit_patterns:
15+
- "^feat(\([a-zA-Z0-9_-]+\))?:"
1916
- title: Bug Fixes 🐛
2017
labels:
2118
- "Changelog: Bugfix"
2219
- Bug
20+
commit_patterns:
21+
- "^(fix|bugfix)(\([a-zA-Z0-9_-]+\))?:"
2322
- title: Deprecations 🏗️
2423
labels:
2524
- "Changelog: Deprecation"
25+
commit_patterns:
26+
- "deprecat" # deprecation, deprecated
2627
- title: Documentation 📚
2728
labels:
2829
- "Changelog: Docs"
2930
- Docs
3031
- "Component: Docs"
32+
commit_patterns:
33+
- "^docs(\([a-zA-Z0-9_-]+\))?:"
3134
- title: Internal Changes 🔧
3235
labels:
3336
- "Changelog: Internal"
3437
- Quality Improvement
38+
commit_patterns:
39+
- "^(build|ref|chore|ci|tests|test)(\([a-zA-Z0-9_-]+\))?:"

.github/workflows/pr-labeler.yml

Lines changed: 0 additions & 72 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
python-version: ${{ matrix.python-version }}
4646
allow-prereleases: true
4747
- name: Start Redis
48-
uses: supercharge/redis-github-action@1.8.1
48+
uses: supercharge/redis-github-action@v2
4949
- name: Install Java
5050
uses: actions/setup-java@v5
5151
with:

.github/workflows/update-tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
echo "date=$DATE" >> $GITHUB_OUTPUT
5555
5656
- name: Create pull request
57-
uses: actions/github-script@v8.0.0
57+
uses: actions/github-script@v8
5858
with:
5959
script: |
6060
const branchName = '${{ steps.create-branch.outputs.branch_name }}';

scripts/split_tox_gh_actions/templates/test_group.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
{% if needs_redis %}
5858
- name: Start Redis
59-
uses: supercharge/redis-github-action@1.8.1
59+
uses: supercharge/redis-github-action@2
6060
{% endif %}
6161

6262
{% if needs_java %}

sentry_sdk/consts.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ def __init__(
10241024
enable_metrics=True, # type: bool
10251025
before_send_metric=None, # type: Optional[Callable[[Metric, Hint], Optional[Metric]]]
10261026
org_id=None, # type: Optional[str]
1027+
strict_trace_continuation=False, # type: bool
10271028
):
10281029
# type: (...) -> None
10291030
"""Initialize the Sentry SDK with the given parameters. All parameters described here can be used in a call to `sentry_sdk.init()`.
@@ -1427,6 +1428,15 @@ def __init__(
14271428
If `trace_ignore_status_codes` is not provided, requests with any status code
14281429
may be traced.
14291430
1431+
:param strict_trace_continuation: If set to `True`, the SDK will only continue a trace if the `org_id` of the incoming trace found in the
1432+
`baggage` header matches the `org_id` of the current Sentry client and only if BOTH are present.
1433+
1434+
If set to `False`, consistency of `org_id` will only be enforced if both are present. If either are missing, the trace will be continued.
1435+
1436+
The client's organization ID is extracted from the DSN or can be set with the `org_id` option.
1437+
If the organization IDs do not match, the SDK will start a new trace instead of continuing the incoming one.
1438+
This is useful to prevent traces of unknown third-party services from being continued in your application.
1439+
14301440
:param org_id: An optional organization ID. The SDK will try to extract if from the DSN in most cases
14311441
but you can provide it explicitly for self-hosted and Relay setups. This value is used for
14321442
trace propagation and for features like `strict_trace_continuation`.

sentry_sdk/integrations/openai_agents/spans/invoke_agent.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
get_start_span_function,
44
set_data_normalized,
55
normalize_message_roles,
6+
truncate_and_annotate_messages,
67
)
78
from sentry_sdk.consts import OP, SPANDATA
89
from sentry_sdk.scope import should_send_default_pii
@@ -61,12 +62,17 @@ def invoke_agent_span(context, agent, kwargs):
6162

6263
if len(messages) > 0:
6364
normalized_messages = normalize_message_roles(messages)
64-
set_data_normalized(
65-
span,
66-
SPANDATA.GEN_AI_REQUEST_MESSAGES,
67-
normalized_messages,
68-
unpack=False,
65+
scope = sentry_sdk.get_current_scope()
66+
messages_data = truncate_and_annotate_messages(
67+
normalized_messages, span, scope
6968
)
69+
if messages_data is not None:
70+
set_data_normalized(
71+
span,
72+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
73+
messages_data,
74+
unpack=False,
75+
)
7076

7177
_set_agent_data(span, agent)
7278

sentry_sdk/integrations/openai_agents/utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
normalize_message_roles,
55
set_data_normalized,
66
normalize_message_role,
7+
truncate_and_annotate_messages,
78
)
89
from sentry_sdk.consts import SPANDATA, SPANSTATUS, OP
910
from sentry_sdk.integrations import DidNotEnable
@@ -157,12 +158,16 @@ def _set_input_data(span, get_response_kwargs):
157158
}
158159
)
159160

160-
set_data_normalized(
161-
span,
162-
SPANDATA.GEN_AI_REQUEST_MESSAGES,
163-
normalize_message_roles(request_messages),
164-
unpack=False,
165-
)
161+
normalized_messages = normalize_message_roles(request_messages)
162+
scope = sentry_sdk.get_current_scope()
163+
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
164+
if messages_data is not None:
165+
set_data_normalized(
166+
span,
167+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
168+
messages_data,
169+
unpack=False,
170+
)
166171

167172

168173
def _set_output_data(span, result):

sentry_sdk/tracing_utils.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from sentry_sdk.utils import (
1616
capture_internal_exceptions,
1717
filename_for_module,
18-
Dsn,
1918
logger,
2019
match_regex_list,
2120
qualname_from_function,
@@ -453,15 +452,23 @@ def from_incoming_data(cls, incoming_data):
453452

454453
sentry_trace_header = normalized_data.get(SENTRY_TRACE_HEADER_NAME)
455454
sentrytrace_data = extract_sentrytrace_data(sentry_trace_header)
455+
456+
# nothing to propagate if no sentry-trace
456457
if sentrytrace_data is None:
457458
return None
458459

460+
baggage_header = normalized_data.get(BAGGAGE_HEADER_NAME)
461+
baggage = (
462+
Baggage.from_incoming_header(baggage_header) if baggage_header else None
463+
)
464+
465+
if not _should_continue_trace(baggage):
466+
return None
467+
459468
propagation_context = PropagationContext()
460469
propagation_context.update(sentrytrace_data)
461-
462-
baggage_header = normalized_data.get(BAGGAGE_HEADER_NAME)
463-
if baggage_header:
464-
propagation_context.baggage = Baggage.from_incoming_header(baggage_header)
470+
if baggage:
471+
propagation_context.baggage = baggage
465472

466473
propagation_context._fill_sample_rand()
467474

@@ -1230,6 +1237,41 @@ def _set_output_attributes(span, template, send_pii, result):
12301237
span.update_data(_get_output_attributes(template, send_pii, result) or {})
12311238

12321239

1240+
def _should_continue_trace(baggage):
1241+
# type: (Optional[Baggage]) -> bool
1242+
"""
1243+
Check if we should continue the incoming trace according to the strict_trace_continuation spec.
1244+
https://develop.sentry.dev/sdk/telemetry/traces/#stricttracecontinuation
1245+
"""
1246+
1247+
client = sentry_sdk.get_client()
1248+
parsed_dsn = client.parsed_dsn
1249+
client_org_id = parsed_dsn.org_id if parsed_dsn else None
1250+
baggage_org_id = baggage.sentry_items.get("org_id") if baggage else None
1251+
1252+
if (
1253+
client_org_id is not None
1254+
and baggage_org_id is not None
1255+
and client_org_id != baggage_org_id
1256+
):
1257+
logger.debug(
1258+
f"Starting a new trace because org IDs don't match (incoming baggage org_id: {baggage_org_id}, SDK org_id: {client_org_id})"
1259+
)
1260+
return False
1261+
1262+
strict_trace_continuation = client.options.get("strict_trace_continuation", False) # type: bool
1263+
if strict_trace_continuation:
1264+
if (baggage_org_id is not None and client_org_id is None) or (
1265+
baggage_org_id is None and client_org_id is not None
1266+
):
1267+
logger.debug(
1268+
f"Starting a new trace because strict trace continuation is enabled and one org ID is missing (incoming baggage org_id: {baggage_org_id}, SDK org_id: {client_org_id})"
1269+
)
1270+
return False
1271+
1272+
return True
1273+
1274+
12331275
# Circular imports
12341276
from sentry_sdk.tracing import (
12351277
BAGGAGE_HEADER_NAME,

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,19 @@ def capture_envelope(self, _: Envelope) -> None:
221221
pass
222222

223223

224+
class TestTransportWithOptions(Transport):
225+
"""TestTransport above does not pass in the options and for some tests we need them"""
226+
227+
__test__ = False
228+
229+
def __init__(self, options=None):
230+
Transport.__init__(self, options)
231+
232+
def capture_envelope(self, _: Envelope) -> None:
233+
"""No-op capture_envelope for tests"""
234+
pass
235+
236+
224237
@pytest.fixture
225238
def capture_events(monkeypatch):
226239
def inner():

0 commit comments

Comments
 (0)