-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathtest_otlp.py
More file actions
224 lines (169 loc) · 7.18 KB
/
test_otlp.py
File metadata and controls
224 lines (169 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import pytest
import responses
from opentelemetry import trace
from opentelemetry.trace import (
get_tracer_provider,
set_tracer_provider,
ProxyTracerProvider,
format_span_id,
format_trace_id,
get_current_span,
)
from opentelemetry.context import attach, detach
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.util._once import Once
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from sentry_sdk.integrations.otlp import OTLPIntegration, SentryOTLPPropagator
from sentry_sdk.scope import get_external_propagation_context
original_propagator = get_global_textmap()
@pytest.fixture(autouse=True)
def reset_otlp(uninstall_integration):
trace._TRACER_PROVIDER_SET_ONCE = Once()
trace._TRACER_PROVIDER = None
set_global_textmap(original_propagator)
uninstall_integration("otlp")
def test_sets_new_tracer_provider_with_otlp_exporter(sentry_init):
existing_tracer_provider = get_tracer_provider()
assert isinstance(existing_tracer_provider, ProxyTracerProvider)
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
integrations=[OTLPIntegration()],
)
tracer_provider = get_tracer_provider()
assert tracer_provider is not existing_tracer_provider
assert isinstance(tracer_provider, TracerProvider)
(span_processor,) = tracer_provider._active_span_processor._span_processors
assert isinstance(span_processor, BatchSpanProcessor)
exporter = span_processor.span_exporter
assert isinstance(exporter, OTLPSpanExporter)
assert (
exporter._endpoint
== "https://bla.ingest.sentry.io/api/12312012/integration/otlp/v1/traces/"
)
assert "X-Sentry-Auth" in exporter._headers
assert (
"Sentry sentry_key=mysecret, sentry_version=7, sentry_client=sentry.python/"
in exporter._headers["X-Sentry-Auth"]
)
def test_uses_existing_tracer_provider_with_otlp_exporter(sentry_init):
existing_tracer_provider = TracerProvider()
set_tracer_provider(existing_tracer_provider)
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
integrations=[OTLPIntegration()],
)
tracer_provider = get_tracer_provider()
assert tracer_provider == existing_tracer_provider
assert isinstance(tracer_provider, TracerProvider)
(span_processor,) = tracer_provider._active_span_processor._span_processors
assert isinstance(span_processor, BatchSpanProcessor)
exporter = span_processor.span_exporter
assert isinstance(exporter, OTLPSpanExporter)
assert (
exporter._endpoint
== "https://bla.ingest.sentry.io/api/12312012/integration/otlp/v1/traces/"
)
assert "X-Sentry-Auth" in exporter._headers
assert (
"Sentry sentry_key=mysecret, sentry_version=7, sentry_client=sentry.python/"
in exporter._headers["X-Sentry-Auth"]
)
def test_does_not_setup_exporter_when_disabled(sentry_init):
existing_tracer_provider = get_tracer_provider()
assert isinstance(existing_tracer_provider, ProxyTracerProvider)
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
integrations=[OTLPIntegration(setup_otlp_traces_exporter=False)],
)
tracer_provider = get_tracer_provider()
assert tracer_provider is existing_tracer_provider
def test_sets_propagator(sentry_init):
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
integrations=[OTLPIntegration()],
)
propagator = get_global_textmap()
assert isinstance(get_global_textmap(), SentryOTLPPropagator)
assert propagator is not original_propagator
def test_does_not_set_propagator_if_disabled(sentry_init):
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
integrations=[OTLPIntegration(setup_propagator=False)],
)
propagator = get_global_textmap()
assert not isinstance(propagator, SentryOTLPPropagator)
assert propagator is original_propagator
@responses.activate
def test_otel_propagation_context(sentry_init):
responses.add(
responses.POST,
url="https://bla.ingest.sentry.io/api/12312012/integration/otlp/v1/traces/",
status=200,
)
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
integrations=[OTLPIntegration()],
)
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("foo") as root_span:
with tracer.start_as_current_span("bar") as span:
external_propagation_context = get_external_propagation_context()
# Force flush to ensure spans are exported while mock is active
get_tracer_provider().force_flush()
assert external_propagation_context is not None
(trace_id, span_id) = external_propagation_context
assert trace_id == format_trace_id(root_span.get_span_context().trace_id)
assert trace_id == format_trace_id(span.get_span_context().trace_id)
assert span_id == format_span_id(span.get_span_context().span_id)
def test_propagator_inject_head_of_trace(sentry_init):
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
integrations=[OTLPIntegration()],
)
tracer = trace.get_tracer(__name__)
propagator = get_global_textmap()
carrier = {}
with tracer.start_as_current_span("foo") as span:
propagator.inject(carrier)
span_context = span.get_span_context()
trace_id = format_trace_id(span_context.trace_id)
span_id = format_span_id(span_context.span_id)
assert "sentry-trace" in carrier
assert carrier["sentry-trace"] == f"{trace_id}-{span_id}-1"
#! we cannot populate baggage in otlp as head SDK yet
assert "baggage" not in carrier
def test_propagator_inject_continue_trace(sentry_init):
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
integrations=[OTLPIntegration()],
)
tracer = trace.get_tracer(__name__)
propagator = get_global_textmap()
carrier = {}
incoming_headers = {
"sentry-trace": "771a43a4192642f0b136d5159a501700-1234567890abcdef-1",
"baggage": (
"sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sampled=true"
),
}
ctx = propagator.extract(incoming_headers)
token = attach(ctx)
parent_span_context = get_current_span().get_span_context()
assert (
format_trace_id(parent_span_context.trace_id)
== "771a43a4192642f0b136d5159a501700"
)
assert format_span_id(parent_span_context.span_id) == "1234567890abcdef"
with tracer.start_as_current_span("foo") as span:
propagator.inject(carrier)
span_context = span.get_span_context()
trace_id = format_trace_id(span_context.trace_id)
span_id = format_span_id(span_context.span_id)
assert trace_id == "771a43a4192642f0b136d5159a501700"
assert "sentry-trace" in carrier
assert carrier["sentry-trace"] == f"{trace_id}-{span_id}-1"
assert "baggage" in carrier
assert carrier["baggage"] == incoming_headers["baggage"]
detach(token)