Skip to content

Commit 407d294

Browse files
committed
get rid of anonymous class factory, do init in __init__() instead
1 parent e1cde03 commit 407d294

File tree

2 files changed

+67
-50
lines changed

2 files changed

+67
-50
lines changed

sentry_sdk/integrations/rust_tracing.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -226,38 +226,34 @@ def on_record(self, span_id: str, values: str, span_state: TraceState) -> None:
226226
sentry_span.set_data(key, value)
227227

228228

229-
def _create_integration(
230-
identifier: str,
231-
initializer: Callable[[RustTracingLayer], None],
232-
event_type_mapping: Callable[
233-
[Dict[str, Any]], EventTypeMapping
234-
] = default_event_type_mapping,
235-
span_filter: Callable[[Dict[str, Any]], bool] = default_span_filter,
236-
) -> object:
229+
class RustTracingIntegration(Integration):
237230
"""
238-
Each native extension used by a project requires its own integration, but
239-
`sentry_sdk` does not expect multiple instances of the same integration. To
240-
work around that, invoking `RustTracingIntegration()` actually calls this
241-
factory function which creates a unique anonymous class and returns an
242-
instance of it.
231+
Ingests tracing data from a Rust native extension's `tracing` instrumentation.
232+
233+
If a project uses more than one Rust native extension, each one will need
234+
its own instance of `RustTracingIntegration` with an initializer function
235+
specific to that extension.
236+
237+
Since all of the setup for this integration requires instance-specific state
238+
which is not available in `setup_once()`, setup instead happens in `__init__()`.
243239
"""
244-
origin = f"auto.native_extension.{identifier}"
245-
tracing_layer = RustTracingLayer(origin, event_type_mapping, span_filter)
246240

247-
def setup_once() -> None:
248-
initializer(tracing_layer)
249-
250-
anonymous_class = type(
251-
"",
252-
(Integration,),
253-
{
254-
"identifier": identifier,
255-
"setup_once": setup_once,
256-
"tracing_layer": tracing_layer,
257-
},
258-
)
259-
anonymous_class_instance = anonymous_class()
260-
return anonymous_class_instance
241+
def __init__(
242+
self,
243+
identifier: str,
244+
initializer: Callable[[RustTracingLayer], None],
245+
event_type_mapping: Callable[
246+
[Dict[str, Any]], EventTypeMapping
247+
] = default_event_type_mapping,
248+
span_filter: Callable[[Dict[str, Any]], bool] = default_span_filter,
249+
):
250+
self.identifier = identifier
251+
252+
origin = f"auto.native_extension.{identifier}"
253+
self.tracing_layer = RustTracingLayer(origin, event_type_mapping, span_filter)
261254

255+
initializer(self.tracing_layer)
262256

263-
RustTracingIntegration = _create_integration
257+
@staticmethod
258+
def setup_once() -> None:
259+
pass

tests/integrations/rust_tracing/test_rust_tracing.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ def record(self, span_id: int):
6161
self.layer.on_record(str(span_id), """{"version": "memoized"}""", state)
6262

6363

64-
def test_on_new_span_on_close(sentry_init, reset_integrations, capture_events):
64+
def test_on_new_span_on_close(sentry_init, capture_events):
6565
rust_tracing = FakeRustTracing()
66-
integration = RustTracingIntegration("test", rust_tracing.set_layer_impl)
66+
integration = RustTracingIntegration(
67+
"test_on_new_span_on_close", rust_tracing.set_layer_impl
68+
)
6769
sentry_init(integrations=[integration], traces_sample_rate=1.0)
6870

6971
events = capture_events()
@@ -84,7 +86,7 @@ def test_on_new_span_on_close(sentry_init, reset_integrations, capture_events):
8486
# Ensure the span metadata is wired up
8587
span = event["spans"][0]
8688
assert span["op"] == "native_extension"
87-
assert span["origin"] == "auto.native_extension.test"
89+
assert span["origin"] == "auto.native_extension.test_on_new_span_on_close"
8890
assert span["description"] == "_bindings::fibonacci"
8991

9092
# Ensure the span was opened/closed appropriately
@@ -98,9 +100,11 @@ def test_on_new_span_on_close(sentry_init, reset_integrations, capture_events):
98100
assert data["version"] is None
99101

100102

101-
def test_nested_on_new_span_on_close(sentry_init, reset_integrations, capture_events):
103+
def test_nested_on_new_span_on_close(sentry_init, capture_events):
102104
rust_tracing = FakeRustTracing()
103-
integration = RustTracingIntegration("test", rust_tracing.set_layer_impl)
105+
integration = RustTracingIntegration(
106+
"test_nested_on_new_span_on_close", rust_tracing.set_layer_impl
107+
)
104108
sentry_init(integrations=[integration], traces_sample_rate=1.0)
105109

106110
events = capture_events()
@@ -138,10 +142,15 @@ def test_nested_on_new_span_on_close(sentry_init, reset_integrations, capture_ev
138142
# Ensure the span metadata is wired up for all spans
139143
first_span, second_span = event["spans"]
140144
assert first_span["op"] == "native_extension"
141-
assert first_span["origin"] == "auto.native_extension.test"
145+
assert (
146+
first_span["origin"] == "auto.native_extension.test_nested_on_new_span_on_close"
147+
)
142148
assert first_span["description"] == "_bindings::fibonacci"
143149
assert second_span["op"] == "native_extension"
144-
assert second_span["origin"] == "auto.native_extension.test"
150+
assert (
151+
second_span["origin"]
152+
== "auto.native_extension.test_nested_on_new_span_on_close"
153+
)
145154
assert second_span["description"] == "_bindings::fibonacci"
146155

147156
# Ensure the spans were opened/closed appropriately
@@ -164,7 +173,9 @@ def test_nested_on_new_span_on_close(sentry_init, reset_integrations, capture_ev
164173

165174
def test_on_new_span_without_transaction(sentry_init, reset_integrations):
166175
rust_tracing = FakeRustTracing()
167-
integration = RustTracingIntegration("test", rust_tracing.set_layer_impl)
176+
integration = RustTracingIntegration(
177+
"test_on_new_span_without_transaction", rust_tracing.set_layer_impl
178+
)
168179
sentry_init(integrations=[integration], traces_sample_rate=1.0)
169180

170181
assert sentry_sdk.get_current_span() is None
@@ -176,10 +187,12 @@ def test_on_new_span_without_transaction(sentry_init, reset_integrations):
176187
assert current_span.containing_transaction is None
177188

178189

179-
def test_on_event_exception(sentry_init, reset_integrations, capture_events):
190+
def test_on_event_exception(sentry_init, capture_events):
180191
rust_tracing = FakeRustTracing()
181192
integration = RustTracingIntegration(
182-
"test", rust_tracing.set_layer_impl, event_type_mapping=_test_event_type_mapping
193+
"test_on_event_exception",
194+
rust_tracing.set_layer_impl,
195+
event_type_mapping=_test_event_type_mapping,
183196
)
184197
sentry_init(integrations=[integration], traces_sample_rate=1.0)
185198

@@ -210,10 +223,12 @@ def test_on_event_exception(sentry_init, reset_integrations, capture_events):
210223
assert field_context["message"] == "Getting the 10th fibonacci number"
211224

212225

213-
def test_on_event_breadcrumb(sentry_init, reset_integrations, capture_events):
226+
def test_on_event_breadcrumb(sentry_init, capture_events):
214227
rust_tracing = FakeRustTracing()
215228
integration = RustTracingIntegration(
216-
"test", rust_tracing.set_layer_impl, event_type_mapping=_test_event_type_mapping
229+
"test_on_event_breadcrumb",
230+
rust_tracing.set_layer_impl,
231+
event_type_mapping=_test_event_type_mapping,
217232
)
218233
sentry_init(integrations=[integration], traces_sample_rate=1.0)
219234

@@ -239,10 +254,12 @@ def test_on_event_breadcrumb(sentry_init, reset_integrations, capture_events):
239254
assert breadcrumbs[0]["type"] == "default"
240255

241256

242-
def test_on_event_event(sentry_init, reset_integrations, capture_events):
257+
def test_on_event_event(sentry_init, capture_events):
243258
rust_tracing = FakeRustTracing()
244259
integration = RustTracingIntegration(
245-
"test", rust_tracing.set_layer_impl, event_type_mapping=_test_event_type_mapping
260+
"test_on_event_event",
261+
rust_tracing.set_layer_impl,
262+
event_type_mapping=_test_event_type_mapping,
246263
)
247264
sentry_init(integrations=[integration], traces_sample_rate=1.0)
248265

@@ -274,10 +291,12 @@ def test_on_event_event(sentry_init, reset_integrations, capture_events):
274291
assert field_context["message"] == "Getting the 10th fibonacci number"
275292

276293

277-
def test_on_event_ignored(sentry_init, reset_integrations, capture_events):
294+
def test_on_event_ignored(sentry_init, capture_events):
278295
rust_tracing = FakeRustTracing()
279296
integration = RustTracingIntegration(
280-
"test", rust_tracing.set_layer_impl, event_type_mapping=_test_event_type_mapping
297+
"test_on_event_ignored",
298+
rust_tracing.set_layer_impl,
299+
event_type_mapping=_test_event_type_mapping,
281300
)
282301
sentry_init(integrations=[integration], traces_sample_rate=1.0)
283302

@@ -298,7 +317,7 @@ def test_on_event_ignored(sentry_init, reset_integrations, capture_events):
298317
assert "message" not in tx
299318

300319

301-
def test_span_filter(sentry_init, reset_integrations, capture_events):
320+
def test_span_filter(sentry_init, capture_events):
302321
def span_filter(metadata: Dict[str, object]) -> bool:
303322
return RustTracingLevel(metadata.get("level")) in (
304323
RustTracingLevel.Error,
@@ -309,7 +328,7 @@ def span_filter(metadata: Dict[str, object]) -> bool:
309328

310329
rust_tracing = FakeRustTracing()
311330
integration = RustTracingIntegration(
312-
"test", rust_tracing.set_layer_impl, span_filter=span_filter
331+
"test_span_filter", rust_tracing.set_layer_impl, span_filter=span_filter
313332
)
314333
sentry_init(integrations=[integration], traces_sample_rate=1.0)
315334

@@ -340,7 +359,7 @@ def span_filter(metadata: Dict[str, object]) -> bool:
340359

341360
def test_record(sentry_init, reset_integrations):
342361
rust_tracing = FakeRustTracing()
343-
integration = RustTracingIntegration("test", rust_tracing.set_layer_impl)
362+
integration = RustTracingIntegration("test_record", rust_tracing.set_layer_impl)
344363
sentry_init(integrations=[integration], traces_sample_rate=1.0)
345364

346365
with start_transaction():
@@ -362,7 +381,9 @@ def span_filter(metadata: Dict[str, object]) -> bool:
362381

363382
rust_tracing = FakeRustTracing()
364383
integration = RustTracingIntegration(
365-
"test", rust_tracing.set_layer_impl, span_filter=span_filter
384+
"test_record_in_ignored_span",
385+
rust_tracing.set_layer_impl,
386+
span_filter=span_filter,
366387
)
367388
sentry_init(integrations=[integration], traces_sample_rate=1.0)
368389

0 commit comments

Comments
 (0)