|
1 | 1 | import re
|
2 | 2 | import contextlib
|
| 3 | +import json |
3 | 4 | import math
|
4 | 5 |
|
5 | 6 | from numbers import Real
|
@@ -160,6 +161,36 @@ def maybe_create_breadcrumbs_from_span(hub, span):
|
160 | 161 | )
|
161 | 162 |
|
162 | 163 |
|
| 164 | +def compute_new_tracestate(transaction): |
| 165 | + # type: (Transaction) -> str |
| 166 | + """ |
| 167 | + Computes a new tracestate value for the transaction. |
| 168 | + """ |
| 169 | + data = {} |
| 170 | + |
| 171 | + client = (transaction.hub or sentry_sdk.Hub.current).client |
| 172 | + |
| 173 | + # if there's no client and/or no DSN, we're not sending anything anywhere, |
| 174 | + # so it's fine to not have any tracestate data |
| 175 | + if client and client.options.get("dsn"): |
| 176 | + options = client.options |
| 177 | + data = { |
| 178 | + "trace_id": transaction.trace_id, |
| 179 | + "environment": options["environment"], |
| 180 | + "release": options.get("release"), |
| 181 | + "public_key": Dsn(options["dsn"]).public_key, |
| 182 | + } |
| 183 | + |
| 184 | + tracestate_json = json.dumps(data) |
| 185 | + |
| 186 | + # Base64-encoded strings always come out with a length which is a multiple |
| 187 | + # of 4. In order to achieve this, the end is padded with one or more `=` |
| 188 | + # signs. Because the tracestate standard calls for using `=` signs between |
| 189 | + # vendor name and value (`sentry=xxx,dogsaregreat=yyy`), to avoid confusion |
| 190 | + # we strip the `=` |
| 191 | + return to_base64(tracestate_json).rstrip("=") |
| 192 | + |
| 193 | + |
163 | 194 | def _format_sql(cursor, sql):
|
164 | 195 | # type: (Any, str) -> Optional[str]
|
165 | 196 |
|
|
0 commit comments