Skip to content

Commit ab51110

Browse files
committed
Merge branch 'potel-base' into potel-base-run-all-tests
2 parents e894904 + 8b70a66 commit ab51110

File tree

13 files changed

+99
-69
lines changed

13 files changed

+99
-69
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/integrations/sqlalchemy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ def _after_cursor_execute(conn, cursor, statement, parameters, context, *args):
7676
context, "_sentry_sql_span_manager", None
7777
) # type: Optional[ContextManager[Any]]
7878

79-
if ctx_mgr is not None:
80-
context._sentry_sql_span_manager = None
81-
ctx_mgr.__exit__(None, None, None)
82-
8379
span = getattr(context, "_sentry_sql_span", None) # type: Optional[Span]
8480
if span is not None:
8581
with capture_internal_exceptions():
8682
add_query_source(span)
8783

84+
if ctx_mgr is not None:
85+
context._sentry_sql_span_manager = None
86+
ctx_mgr.__exit__(None, None, None)
87+
8888

8989
def _handle_error(context, *args):
9090
# type: (Any, *Any) -> None

sentry_sdk/tracing_utils.py

Lines changed: 1 addition & 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

@@ -148,6 +147,7 @@ def record_sql_queries(
148147
op=OP.DB,
149148
name=query,
150149
origin=span_origin,
150+
only_if_parent=True,
151151
) as span:
152152
for k, v in data.items():
153153
span.set_data(k, v)
@@ -731,6 +731,3 @@ def get_current_span(scope=None):
731731
LOW_QUALITY_TRANSACTION_SOURCES,
732732
SENTRY_TRACE_HEADER_NAME,
733733
)
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/pymongo/test_pymongo.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22

3-
from sentry_sdk import capture_message, start_transaction
3+
import sentry_sdk
44
from sentry_sdk.consts import SPANDATA
55
from sentry_sdk.integrations.pymongo import PyMongoIntegration, _strip_pii
66

@@ -37,7 +37,7 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii):
3737

3838
connection = MongoClient(mongo_server.uri)
3939

40-
with start_transaction():
40+
with sentry_sdk.start_span():
4141
list(
4242
connection["test_db"]["test_collection"].find({"foobar": 1})
4343
) # force query execution
@@ -119,13 +119,9 @@ def test_breadcrumbs(
119119
list(
120120
connection["test_db"]["test_collection"].find({"foobar": 1})
121121
) # force query execution
122-
capture_message("hi")
123-
124-
if traces_sample_rate:
125-
event = events[1]
126-
else:
127-
event = events[0]
122+
sentry_sdk.capture_message("hi")
128123

124+
(event,) = events
129125
(crumb,) = event["breadcrumbs"]["values"]
130126

131127
assert crumb["category"] == "query"
@@ -450,7 +446,7 @@ def test_span_origin(sentry_init, capture_events, mongo_server):
450446

451447
connection = MongoClient(mongo_server.uri)
452448

453-
with start_transaction():
449+
with sentry_sdk.start_span():
454450
list(
455451
connection["test_db"]["test_collection"].find({"foobar": 1})
456452
) # force query execution

0 commit comments

Comments
 (0)