Skip to content

Commit 9fa29e5

Browse files
feat(clickhouse_driver): Set breadcrumbs in the streaming trace lifecycle
1 parent 58823cc commit 9fa29e5

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

sentry_sdk/integrations/clickhouse_driver.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ def _inner(*args: "P.args", **kwargs: "P.kwargs") -> "T":
9595
SPANDATA.DB_QUERY_TEXT: str(query),
9696
},
9797
)
98+
99+
sentry_sdk.get_isolation_scope().add_breadcrumb(
100+
message=query,
101+
category="query",
102+
data={
103+
SPANDATA.DB_SYSTEM: "clickhouse",
104+
SPANDATA.DB_NAME: connection.database,
105+
SPANDATA.DB_DRIVER_NAME: "clickhouse-driver",
106+
SPANDATA.SERVER_ADDRESS: connection.host,
107+
SPANDATA.SERVER_PORT: connection.port,
108+
SPANDATA.DB_USER: connection.user,
109+
},
110+
)
98111
else:
99112
span = sentry_sdk.start_span(
100113
op=OP.DB,

tests/integrations/clickhouse_driver/test_clickhouse_driver.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
EXPECT_PARAMS_IN_SELECT = False
2222

2323

24-
def test_clickhouse_client_breadcrumbs(sentry_init, capture_events) -> None:
24+
@pytest.mark.parametrize("span_streaming", [True, False])
25+
def test_clickhouse_client_breadcrumbs(
26+
sentry_init, capture_events, span_streaming
27+
) -> None:
2528
sentry_init(
2629
integrations=[ClickhouseDriverIntegration()],
30+
trace_lifecycle="stream" if span_streaming else "static",
2731
_experiments={"record_sql_params": True},
2832
)
2933
events = capture_events()
@@ -127,10 +131,14 @@ def test_clickhouse_client_breadcrumbs(sentry_init, capture_events) -> None:
127131
assert actual_query_breadcrumbs == expected_breadcrumbs
128132

129133

130-
def test_clickhouse_client_breadcrumbs_with_pii(sentry_init, capture_events) -> None:
134+
@pytest.mark.parametrize("span_streaming", [True, False])
135+
def test_clickhouse_client_breadcrumbs_with_pii(
136+
sentry_init, capture_events, span_streaming
137+
) -> None:
131138
sentry_init(
132139
integrations=[ClickhouseDriverIntegration()],
133140
send_default_pii=True,
141+
trace_lifecycle="stream" if span_streaming else "static",
134142
_experiments={"record_sql_params": True},
135143
)
136144
events = capture_events()
@@ -229,11 +237,13 @@ def test_clickhouse_client_breadcrumbs_with_pii(sentry_init, capture_events) ->
229237
assert event["breadcrumbs"]["values"] == expected_breadcrumbs
230238

231239

240+
@pytest.mark.parametrize("span_streaming", [True, False])
232241
def test_clickhouse_client_breadcrumbs_with_data_collection(
233-
sentry_init, capture_events
242+
sentry_init, capture_events, span_streaming
234243
) -> None:
235244
sentry_init(
236245
integrations=[ClickhouseDriverIntegration()],
246+
trace_lifecycle="stream" if span_streaming else "static",
237247
_experiments={"data_collection": {"database_query_data": True}},
238248
)
239249
events = capture_events()
@@ -332,11 +342,13 @@ def test_clickhouse_client_breadcrumbs_with_data_collection(
332342
assert event["breadcrumbs"]["values"] == expected_breadcrumbs
333343

334344

345+
@pytest.mark.parametrize("span_streaming", [True, False])
335346
def test_clickhouse_client_breadcrumbs_with_data_collection_disabled(
336-
sentry_init, capture_events
347+
sentry_init, capture_events, span_streaming
337348
) -> None:
338349
sentry_init(
339350
integrations=[ClickhouseDriverIntegration()],
351+
trace_lifecycle="stream" if span_streaming else "static",
340352
_experiments={"data_collection": {"database_query_data": False}},
341353
)
342354
events = capture_events()
@@ -431,12 +443,14 @@ def test_clickhouse_client_breadcrumbs_with_data_collection_disabled(
431443
assert "db.result" not in crumb["data"]
432444

433445

446+
@pytest.mark.parametrize("span_streaming", [True, False])
434447
def test_clickhouse_client_breadcrumbs_data_collection_overrides_pii(
435-
sentry_init, capture_events
448+
sentry_init, capture_events, span_streaming
436449
) -> None:
437450
sentry_init(
438451
integrations=[ClickhouseDriverIntegration()],
439452
send_default_pii=True,
453+
trace_lifecycle="stream" if span_streaming else "static",
440454
_experiments={"data_collection": {"database_query_data": False}},
441455
)
442456
events = capture_events()
@@ -531,11 +545,13 @@ def test_clickhouse_client_breadcrumbs_data_collection_overrides_pii(
531545
assert "db.result" not in crumb["data"]
532546

533547

548+
@pytest.mark.parametrize("span_streaming", [True, False])
534549
def test_clickhouse_client_breadcrumbs_with_data_collection_default(
535-
sentry_init, capture_events
550+
sentry_init, capture_events, span_streaming
536551
) -> None:
537552
sentry_init(
538553
integrations=[ClickhouseDriverIntegration()],
554+
trace_lifecycle="stream" if span_streaming else "static",
539555
_experiments={"data_collection": {}},
540556
)
541557
events = capture_events()

0 commit comments

Comments
 (0)