|
17 | 17 | # py3 |
18 | 18 | from http.client import HTTPSConnection |
19 | 19 |
|
20 | | -from sentry_sdk import capture_message |
| 20 | +try: |
| 21 | + from unittest import mock # python 3.3 and above |
| 22 | +except ImportError: |
| 23 | + import mock # python < 3.3 |
| 24 | + |
| 25 | +from sentry_sdk import capture_message, start_transaction |
21 | 26 | from sentry_sdk.integrations.stdlib import StdlibIntegration |
22 | 27 |
|
23 | 28 |
|
@@ -110,3 +115,35 @@ def test_httplib_misuse(sentry_init, capture_events): |
110 | 115 | "status_code": 200, |
111 | 116 | "reason": "OK", |
112 | 117 | } |
| 118 | + |
| 119 | + |
| 120 | +def test_outgoing_trace_headers( |
| 121 | + sentry_init, monkeypatch, StringContaining # noqa: N803 |
| 122 | +): |
| 123 | + # HTTPSConnection.send is passed a string containing (among other things) |
| 124 | + # the headers on the request. Mock it so we can check the headers, and also |
| 125 | + # so it doesn't try to actually talk to the internet. |
| 126 | + mock_send = mock.Mock() |
| 127 | + monkeypatch.setattr(HTTPSConnection, "send", mock_send) |
| 128 | + |
| 129 | + sentry_init(traces_sample_rate=1.0) |
| 130 | + |
| 131 | + with start_transaction( |
| 132 | + name="/interactions/other-dogs/new-dog", |
| 133 | + op="greeting.sniff", |
| 134 | + trace_id="12312012123120121231201212312012", |
| 135 | + ) as transaction: |
| 136 | + |
| 137 | + HTTPSConnection("www.squirrelchasers.com").request("GET", "/top-chasers") |
| 138 | + |
| 139 | + request_span = transaction._span_recorder.spans[-1] |
| 140 | + |
| 141 | + expected_sentry_trace = ( |
| 142 | + "sentry-trace: {trace_id}-{parent_span_id}-{sampled}".format( |
| 143 | + trace_id=transaction.trace_id, |
| 144 | + parent_span_id=request_span.span_id, |
| 145 | + sampled=1, |
| 146 | + ) |
| 147 | + ) |
| 148 | + |
| 149 | + mock_send.assert_called_with(StringContaining(expected_sentry_trace)) |
0 commit comments