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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
from unittest import mock
import pytest
from string import Template
from typing import Dict
import sentry_sdk
from sentry_sdk.integrations.rust_tracing import (
RustTracingIntegration,
RustTracingLayer,
RustTracingLevel,
EventTypeMapping,
)
from sentry_sdk import start_transaction, capture_message
def _test_event_type_mapping(metadata: Dict[str, object]) -> EventTypeMapping:
level = RustTracingLevel(metadata.get("level"))
if level == RustTracingLevel.Error:
return EventTypeMapping.Exc
elif level in (RustTracingLevel.Warn, RustTracingLevel.Info):
return EventTypeMapping.Breadcrumb
elif level == RustTracingLevel.Debug:
return EventTypeMapping.Event
elif level == RustTracingLevel.Trace:
return EventTypeMapping.Ignore
else:
return EventTypeMapping.Ignore
class FakeRustTracing:
# Parameters: `level`, `index`
span_template = Template(
"""{"index":$index,"is_root":false,"metadata":{"fields":["index","use_memoized","version"],"file":"src/lib.rs","is_event":false,"is_span":true,"level":"$level","line":40,"module_path":"_bindings","name":"fibonacci","target":"_bindings"},"parent":null,"use_memoized":true}"""
)
# Parameters: `level`, `index`
event_template = Template(
"""{"message":"Getting the ${index}th fibonacci number","metadata":{"fields":["message"],"file":"src/lib.rs","is_event":true,"is_span":false,"level":"$level","line":23,"module_path":"_bindings","name":"event src/lib.rs:23","target":"_bindings"}}"""
)
def __init__(self):
self.spans = {}
def set_layer_impl(self, layer: RustTracingLayer):
self.layer = layer
def new_span(self, level: RustTracingLevel, span_id: int, index_arg: int = 10):
span_attrs = self.span_template.substitute(level=level.value, index=index_arg)
state = self.layer.on_new_span(span_attrs, str(span_id))
self.spans[span_id] = state
def close_span(self, span_id: int):
state = self.spans.pop(span_id)
self.layer.on_close(str(span_id), state)
def event(self, level: RustTracingLevel, span_id: int, index_arg: int = 10):
event = self.event_template.substitute(level=level.value, index=index_arg)
state = self.spans[span_id]
self.layer.on_event(event, state)
def record(self, span_id: int):
state = self.spans[span_id]
self.layer.on_record(str(span_id), """{"version": "memoized"}""", state)
def test_on_new_span_on_close(sentry_init, capture_events):
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_on_new_span_on_close",
initializer=rust_tracing.set_layer_impl,
include_tracing_fields=True,
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
events = capture_events()
with start_transaction():
rust_tracing.new_span(RustTracingLevel.Info, 3)
sentry_first_rust_span = sentry_sdk.get_current_span()
_, rust_first_rust_span = rust_tracing.spans[3]
assert sentry_first_rust_span == rust_first_rust_span
rust_tracing.close_span(3)
assert sentry_sdk.get_current_span() != sentry_first_rust_span
(event,) = events
assert len(event["spans"]) == 1
# Ensure the span metadata is wired up
span = event["spans"][0]
assert span["op"] == "function"
assert span["origin"] == "auto.function.rust_tracing.test_on_new_span_on_close"
assert span["description"] == "_bindings::fibonacci"
# Ensure the span was opened/closed appropriately
assert span["start_timestamp"] is not None
assert span["timestamp"] is not None
# Ensure the extra data from Rust is hooked up
data = span["data"]
assert data["use_memoized"]
assert data["index"] == 10
assert data["version"] is None
def test_nested_on_new_span_on_close(sentry_init, capture_events):
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_nested_on_new_span_on_close",
initializer=rust_tracing.set_layer_impl,
include_tracing_fields=True,
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
events = capture_events()
with start_transaction():
original_sentry_span = sentry_sdk.get_current_span()
rust_tracing.new_span(RustTracingLevel.Info, 3, index_arg=10)
sentry_first_rust_span = sentry_sdk.get_current_span()
_, rust_first_rust_span = rust_tracing.spans[3]
# Use a different `index_arg` value for the inner span to help
# distinguish the two at the end of the test
rust_tracing.new_span(RustTracingLevel.Info, 5, index_arg=9)
sentry_second_rust_span = sentry_sdk.get_current_span()
rust_parent_span, rust_second_rust_span = rust_tracing.spans[5]
assert rust_second_rust_span == sentry_second_rust_span
assert rust_parent_span == sentry_first_rust_span
assert rust_parent_span == rust_first_rust_span
assert rust_parent_span != rust_second_rust_span
rust_tracing.close_span(5)
# Ensure the current sentry span was moved back to the parent
sentry_span_after_close = sentry_sdk.get_current_span()
assert sentry_span_after_close == sentry_first_rust_span
rust_tracing.close_span(3)
assert sentry_sdk.get_current_span() == original_sentry_span
(event,) = events
assert len(event["spans"]) == 2
# Ensure the span metadata is wired up for all spans
first_span, second_span = event["spans"]
assert first_span["op"] == "function"
assert (
first_span["origin"]
== "auto.function.rust_tracing.test_nested_on_new_span_on_close"
)
assert first_span["description"] == "_bindings::fibonacci"
assert second_span["op"] == "function"
assert (
second_span["origin"]
== "auto.function.rust_tracing.test_nested_on_new_span_on_close"
)
assert second_span["description"] == "_bindings::fibonacci"
# Ensure the spans were opened/closed appropriately
assert first_span["start_timestamp"] is not None
assert first_span["timestamp"] is not None
assert second_span["start_timestamp"] is not None
assert second_span["timestamp"] is not None
# Ensure the extra data from Rust is hooked up in both spans
first_span_data = first_span["data"]
assert first_span_data["use_memoized"]
assert first_span_data["index"] == 10
assert first_span_data["version"] is None
second_span_data = second_span["data"]
assert second_span_data["use_memoized"]
assert second_span_data["index"] == 9
assert second_span_data["version"] is None
def test_on_new_span_without_transaction(sentry_init):
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_on_new_span_without_transaction", rust_tracing.set_layer_impl
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
assert sentry_sdk.get_current_span() is None
# Should still create a span hierarchy, it just will not be under a txn
rust_tracing.new_span(RustTracingLevel.Info, 3)
current_span = sentry_sdk.get_current_span()
assert current_span is not None
assert current_span.containing_transaction is None
def test_on_event_exception(sentry_init, capture_events):
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_on_event_exception",
rust_tracing.set_layer_impl,
event_type_mapping=_test_event_type_mapping,
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
events = capture_events()
sentry_sdk.get_isolation_scope().clear_breadcrumbs()
with start_transaction():
rust_tracing.new_span(RustTracingLevel.Info, 3)
# Mapped to Exception
rust_tracing.event(RustTracingLevel.Error, 3)
rust_tracing.close_span(3)
assert len(events) == 2
exc, _tx = events
assert exc["level"] == "error"
assert exc["logger"] == "_bindings"
assert exc["message"] == "Getting the 10th fibonacci number"
assert exc["breadcrumbs"]["values"] == []
location_context = exc["contexts"]["rust_tracing_location"]
assert location_context["module_path"] == "_bindings"
assert location_context["file"] == "src/lib.rs"
assert location_context["line"] == 23
field_context = exc["contexts"]["rust_tracing_fields"]
assert field_context["message"] == "Getting the 10th fibonacci number"
def test_on_event_breadcrumb(sentry_init, capture_events):
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_on_event_breadcrumb",
rust_tracing.set_layer_impl,
event_type_mapping=_test_event_type_mapping,
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
events = capture_events()
sentry_sdk.get_isolation_scope().clear_breadcrumbs()
with start_transaction():
rust_tracing.new_span(RustTracingLevel.Info, 3)
# Mapped to Breadcrumb
rust_tracing.event(RustTracingLevel.Info, 3)
rust_tracing.close_span(3)
capture_message("test message")
assert len(events) == 2
message, _tx = events
breadcrumbs = message["breadcrumbs"]["values"]
assert len(breadcrumbs) == 1
assert breadcrumbs[0]["level"] == "info"
assert breadcrumbs[0]["message"] == "Getting the 10th fibonacci number"
assert breadcrumbs[0]["type"] == "default"
def test_on_event_event(sentry_init, capture_events):
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_on_event_event",
rust_tracing.set_layer_impl,
event_type_mapping=_test_event_type_mapping,
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
events = capture_events()
sentry_sdk.get_isolation_scope().clear_breadcrumbs()
with start_transaction():
rust_tracing.new_span(RustTracingLevel.Info, 3)
# Mapped to Event
rust_tracing.event(RustTracingLevel.Debug, 3)
rust_tracing.close_span(3)
assert len(events) == 2
event, _tx = events
assert event["logger"] == "_bindings"
assert event["level"] == "debug"
assert event["message"] == "Getting the 10th fibonacci number"
assert event["breadcrumbs"]["values"] == []
location_context = event["contexts"]["rust_tracing_location"]
assert location_context["module_path"] == "_bindings"
assert location_context["file"] == "src/lib.rs"
assert location_context["line"] == 23
field_context = event["contexts"]["rust_tracing_fields"]
assert field_context["message"] == "Getting the 10th fibonacci number"
def test_on_event_ignored(sentry_init, capture_events):
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_on_event_ignored",
rust_tracing.set_layer_impl,
event_type_mapping=_test_event_type_mapping,
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
events = capture_events()
sentry_sdk.get_isolation_scope().clear_breadcrumbs()
with start_transaction():
rust_tracing.new_span(RustTracingLevel.Info, 3)
# Ignored
rust_tracing.event(RustTracingLevel.Trace, 3)
rust_tracing.close_span(3)
assert len(events) == 1
(tx,) = events
assert tx["type"] == "transaction"
assert "message" not in tx
def test_span_filter(sentry_init, capture_events):
def span_filter(metadata: Dict[str, object]) -> bool:
return RustTracingLevel(metadata.get("level")) in (
RustTracingLevel.Error,
RustTracingLevel.Warn,
RustTracingLevel.Info,
RustTracingLevel.Debug,
)
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_span_filter",
initializer=rust_tracing.set_layer_impl,
span_filter=span_filter,
include_tracing_fields=True,
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
events = capture_events()
with start_transaction():
original_sentry_span = sentry_sdk.get_current_span()
# Span is not ignored
rust_tracing.new_span(RustTracingLevel.Info, 3, index_arg=10)
info_span = sentry_sdk.get_current_span()
# Span is ignored, current span should remain the same
rust_tracing.new_span(RustTracingLevel.Trace, 5, index_arg=9)
assert sentry_sdk.get_current_span() == info_span
# Closing the filtered span should leave the current span alone
rust_tracing.close_span(5)
assert sentry_sdk.get_current_span() == info_span
rust_tracing.close_span(3)
assert sentry_sdk.get_current_span() == original_sentry_span
(event,) = events
assert len(event["spans"]) == 1
# The ignored span has index == 9
assert event["spans"][0]["data"]["index"] == 10
def test_record(sentry_init):
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_record",
initializer=rust_tracing.set_layer_impl,
include_tracing_fields=True,
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
with start_transaction():
rust_tracing.new_span(RustTracingLevel.Info, 3)
span_before_record = sentry_sdk.get_current_span().to_json()
assert span_before_record["data"]["version"] is None
rust_tracing.record(3)
span_after_record = sentry_sdk.get_current_span().to_json()
assert span_after_record["data"]["version"] == "memoized"
def test_record_in_ignored_span(sentry_init):
def span_filter(metadata: Dict[str, object]) -> bool:
# Just ignore Trace
return RustTracingLevel(metadata.get("level")) != RustTracingLevel.Trace
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_record_in_ignored_span",
rust_tracing.set_layer_impl,
span_filter=span_filter,
include_tracing_fields=True,
)
sentry_init(integrations=[integration], traces_sample_rate=1.0)
with start_transaction():
rust_tracing.new_span(RustTracingLevel.Info, 3)
span_before_record = sentry_sdk.get_current_span().to_json()
assert span_before_record["data"]["version"] is None
rust_tracing.new_span(RustTracingLevel.Trace, 5)
rust_tracing.record(5)
# `on_record()` should not do anything to the current Sentry span if the associated Rust span was ignored
span_after_record = sentry_sdk.get_current_span().to_json()
assert span_after_record["data"]["version"] is None
@pytest.mark.parametrize(
"send_default_pii, include_tracing_fields, tracing_fields_expected",
[
(True, True, True),
(True, False, False),
(True, None, True),
(False, True, True),
(False, False, False),
(False, None, False),
],
)
def test_include_tracing_fields(
sentry_init, send_default_pii, include_tracing_fields, tracing_fields_expected
):
rust_tracing = FakeRustTracing()
integration = RustTracingIntegration(
"test_record",
initializer=rust_tracing.set_layer_impl,
include_tracing_fields=include_tracing_fields,
)
sentry_init(
integrations=[integration],
traces_sample_rate=1.0,
send_default_pii=send_default_pii,
)
with start_transaction():
rust_tracing.new_span(RustTracingLevel.Info, 3)
span_before_record = sentry_sdk.get_current_span().to_json()
if tracing_fields_expected:
assert span_before_record["data"]["version"] is None
else:
assert span_before_record["data"]["version"] == "[Filtered]"
rust_tracing.record(3)
span_after_record = sentry_sdk.get_current_span().to_json()
if tracing_fields_expected:
assert span_after_record["data"] == {
"thread.id": mock.ANY,
"thread.name": mock.ANY,
"use_memoized": True,
"version": "memoized",
"index": 10,
}
else:
assert span_after_record["data"] == {
"thread.id": mock.ANY,
"thread.name": mock.ANY,
"use_memoized": "[Filtered]",
"version": "[Filtered]",
"index": "[Filtered]",
}
|