Skip to content

Commit 515ac72

Browse files
authored
Merge branch 'potel-base' into antonpirker/potel/pymongo
2 parents b36a759 + de7e58b commit 515ac72

File tree

10 files changed

+67
-28
lines changed

10 files changed

+67
-28
lines changed

sentry_sdk/integrations/opentelemetry/integration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from sentry_sdk.integrations import DidNotEnable, Integration
8+
from sentry_sdk.integrations.opentelemetry.scope import setup_initial_scopes
89
from sentry_sdk.integrations.opentelemetry.propagator import SentryPropagator
910
from sentry_sdk.integrations.opentelemetry.span_processor import (
1011
SentrySpanProcessor,
@@ -74,6 +75,7 @@ def _setup_scope_context_management():
7475
import opentelemetry.context
7576

7677
opentelemetry.context._RUNTIME_CONTEXT = SentryContextVarsRuntimeContext()
78+
setup_initial_scopes()
7779

7880

7981
def _setup_sentry_tracing():

sentry_sdk/integrations/opentelemetry/propagator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def inject(self, carrier, context=None, setter=default_setter):
9999

100100
# TODO-neel-potel check trace_propagation_targets
101101
# TODO-neel-potel test propagator works with twp
102-
for (key, value) in current_scope.iter_trace_propagation_headers():
102+
for key, value in current_scope.iter_trace_propagation_headers():
103103
setter.set(carrier, key, value)
104104

105105
@property

sentry_sdk/integrations/opentelemetry/sampler.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from typing import TYPE_CHECKING
1818

1919
if TYPE_CHECKING:
20-
from typing import Optional, Sequence, Union
20+
from typing import Any, Optional, Sequence, Union
2121
from opentelemetry.context import Context
2222
from opentelemetry.trace import Link, SpanKind
2323
from opentelemetry.trace.span import SpanContext
@@ -152,15 +152,9 @@ def should_sample(
152152
has_traces_sampler = callable(client.options.get("traces_sampler"))
153153

154154
if is_root_span and has_traces_sampler:
155-
sampling_context = {
156-
"transaction_context": {
157-
"name": name,
158-
"op": attributes.get(SentrySpanAttribute.OP),
159-
"source": attributes.get(SentrySpanAttribute.SOURCE),
160-
},
161-
"parent_sampled": get_parent_sampled(parent_span_context, trace_id),
162-
}
163-
sampling_context.update(attributes)
155+
sampling_context = create_sampling_context(
156+
name, attributes, parent_span_context, trace_id
157+
)
164158
sample_rate = client.options["traces_sampler"](sampling_context)
165159
else:
166160
# Check if there is a parent with a sampling decision
@@ -193,3 +187,19 @@ def should_sample(
193187

194188
def get_description(self) -> str:
195189
return self.__class__.__name__
190+
191+
192+
def create_sampling_context(name, attributes, parent_span_context, trace_id):
193+
# type: (str, Attributes, SpanContext, str) -> dict[str, Any]
194+
sampling_context = {
195+
"transaction_context": {
196+
"name": name,
197+
"op": attributes.get(SentrySpanAttribute.OP),
198+
"source": attributes.get(SentrySpanAttribute.SOURCE),
199+
},
200+
"parent_sampled": get_parent_sampled(parent_span_context, trace_id),
201+
}
202+
203+
sampling_context.update(attributes)
204+
205+
return sampling_context

sentry_sdk/integrations/opentelemetry/scope.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from typing import cast
22
from contextlib import contextmanager
33

4-
from opentelemetry.context import get_value, set_value, attach, detach, get_current
4+
from opentelemetry.context import (
5+
Context,
6+
get_value,
7+
set_value,
8+
attach,
9+
detach,
10+
get_current,
11+
)
512
from opentelemetry.trace import (
613
SpanContext,
714
NonRecordingSpan,
@@ -136,13 +143,13 @@ def start_span(self, **kwargs):
136143
_INITIAL_ISOLATION_SCOPE = None
137144

138145

139-
def _setup_initial_scopes():
146+
def setup_initial_scopes():
140147
global _INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE
141148
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)
142149
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION)
143150

144-
145-
_setup_initial_scopes()
151+
scopes = (_INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE)
152+
attach(set_value(SENTRY_SCOPES_KEY, scopes))
146153

147154

148155
@contextmanager

sentry_sdk/integrations/opentelemetry/span_processor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
get_profiler_id,
2121
)
2222
from sentry_sdk.profiler.transaction_profiler import Profile
23+
from sentry_sdk.integrations.opentelemetry.sampler import create_sampling_context
2324
from sentry_sdk.integrations.opentelemetry.utils import (
2425
is_sentry_span,
2526
convert_from_otel_timestamp,
@@ -126,8 +127,10 @@ def _start_profile(self, span):
126127
# unix timestamp that is on span.start_time
127128
# setting it to 0 means the profiler will internally measure time on start
128129
profile = Profile(sampled, 0)
129-
# TODO-neel-potel sampling context??
130-
profile._set_initial_sampling_decision(sampling_context={})
130+
sampling_context = create_sampling_context(
131+
span.name, span.attributes, span.parent, span.context.trace_id
132+
)
133+
profile._set_initial_sampling_decision(sampling_context)
131134
profile.__enter__()
132135
set_sentry_meta(span, "profile", profile)
133136

sentry_sdk/tracing_utils.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from typing import Generator
3434
from typing import Optional
3535
from typing import Union
36-
3736
from types import FrameType
3837

3938

@@ -731,6 +730,3 @@ def get_current_span(scope=None):
731730
LOW_QUALITY_TRANSACTION_SOURCES,
732731
SENTRY_TRACE_HEADER_NAME,
733732
)
734-
735-
if TYPE_CHECKING:
736-
from sentry_sdk.tracing import Span

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def clean_scopes():
7474
scope._isolation_scope.set(None)
7575
scope._current_scope.set(None)
7676

77-
potel_scope._setup_initial_scopes()
77+
potel_scope.setup_initial_scopes()
7878

7979

8080
@pytest.fixture(autouse=True)

tests/integrations/clickhouse_driver/test_clickhouse_driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Tests need a local clickhouse instance running, this can best be done using
33
```sh
4-
docker run -d -p 18123:8123 -p9000:9000 --name clickhouse-test --ulimit nofile=262144:262144 --rm clickhouse/clickhouse-server
4+
docker run -d -p 18123:8123 -p9000:9000 --name clickhouse-test --ulimit nofile=262144:262144 --rm clickhouse
55
```
66
"""
77

tests/integrations/stdlib/test_subprocess.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99

10-
from sentry_sdk import capture_exception, capture_message, start_transaction
10+
from sentry_sdk import capture_exception, capture_message, start_span
1111
from sentry_sdk.integrations.stdlib import StdlibIntegration
1212
from tests.conftest import ApproxDict
1313

@@ -59,7 +59,7 @@ def test_subprocess_basic(
5959
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
6060
events = capture_events()
6161

62-
with start_transaction(name="foo") as transaction:
62+
with start_span(name="foo") as span:
6363
args = [
6464
sys.executable,
6565
"-c",
@@ -110,7 +110,7 @@ def test_subprocess_basic(
110110

111111
assert os.environ == old_environ
112112

113-
assert transaction.trace_id in str(output)
113+
assert span.trace_id in str(output)
114114

115115
capture_message("hi")
116116

@@ -178,7 +178,7 @@ def test_subprocess_basic(
178178
def test_subprocess_empty_env(sentry_init, monkeypatch):
179179
monkeypatch.setenv("TEST_MARKER", "should_not_be_seen")
180180
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
181-
with start_transaction(name="foo"):
181+
with start_span(name="foo"):
182182
args = [
183183
sys.executable,
184184
"-c",
@@ -201,7 +201,7 @@ def test_subprocess_span_origin(sentry_init, capture_events):
201201
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
202202
events = capture_events()
203203

204-
with start_transaction(name="foo"):
204+
with start_span(name="foo"):
205205
args = [
206206
sys.executable,
207207
"-c",

tests/tracing/test_sampling.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,24 @@ def test_records_lost_event_only_if_traces_sampler_enabled(
307307

308308
# Use Counter because order of calls does not matter
309309
assert Counter(record_lost_event_calls) == Counter(expected_record_lost_event_calls)
310+
311+
312+
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
313+
def test_profiles_sampler_gets_sampling_context(sentry_init, parent_sampling_decision):
314+
def dummy_profiles_sampler(sampling_context):
315+
assert sampling_context["transaction_context"] == {
316+
"name": "dogpark",
317+
"op": "op",
318+
"source": "custom",
319+
}
320+
assert sampling_context["parent_sampled"] == parent_sampling_decision
321+
return 1.0
322+
323+
sentry_init(traces_sample_rate=1.0, profiles_sampler=dummy_profiles_sampler)
324+
325+
sentry_trace = "12312012123120121231201212312012-1121201211212012-{}".format(
326+
int(parent_sampling_decision)
327+
)
328+
with sentry_sdk.continue_trace({"sentry-trace": sentry_trace}):
329+
with sentry_sdk.start_span(name="dogpark", op="op"):
330+
pass

0 commit comments

Comments
 (0)