Skip to content

Commit 3154dbc

Browse files
committed
adjust
1 parent ab3b6eb commit 3154dbc

File tree

5 files changed

+58
-39
lines changed

5 files changed

+58
-39
lines changed

MIGRATION_GUIDE.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
2020
- Redis integration: In Redis pipeline spans there is no `span["data"]["redis.commands"]` that contains a dict `{"count": 3, "first_ten": ["cmd1", "cmd2", ...]}` but instead `span["data"]["redis.commands.count"]` (containing `3`) and `span["data"]["redis.commands.first_ten"]` (containing `["cmd1", "cmd2", ...]`).
2121
- clickhouse-driver integration: The query is now available under the `db.query.text` span attribute (only if `send_default_pii` is `True`).
2222
- `sentry_sdk.init` now returns `None` instead of a context manager.
23-
- The `sampling_context` argument of `traces_sampler` now additionally contains all span attributes known at span start.
24-
- If you're using the Celery integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `celery_job` dictionary anymore. Instead, the individual keys are now available as:
23+
- The `sampling_context` argument of `traces_sampler` and `profiles_sampler` now additionally contains all span attributes known at span start.
24+
- If you're using the Celery integration and a custom traces/profiles sampler, the `sampling_context` argument of `traces_sampler` and `profiles_sampler` doesn't contain the `celery_job` dictionary anymore. Instead, the individual keys are now available as:
2525

26-
| Dictionary keys | Sampling context key |
27-
| ---------------------- | -------------------- |
28-
| `celery_job["args"]` | `celery.job.args` |
29-
| `celery_job["kwargs"]` | `celery.job.kwargs` |
30-
| `celery_job["task"]` | `celery.job.task` |
26+
| Dictionary keys | Sampling context key | Example |
27+
| ---------------------- | --------------------------- | ------------------------------ |
28+
| `celery_job["args"]` | `celery.job.args.{index}` | `celery.job.args.0` |
29+
| `celery_job["kwargs"]` | `celery.job.kwargs.{kwarg}` | `celery.job.kwargs.kwarg_name` |
30+
| `celery_job["task"]` | `celery.job.task` | |
3131

3232
Note that all of these are serialized, i.e., not the original `args` and `kwargs` but rather OpenTelemetry-friendly span attributes.
3333

34-
- If you're using the AIOHTTP integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `aiohttp_request` object anymore. Instead, some of the individual properties of the request are accessible, if available, as follows:
34+
- If you're using the AIOHTTP integration and a custom traces/profiles sampler, the `sampling_context` argument of `traces_sampler` and `profiles_sampler` doesn't contain the `aiohttp_request` object anymore. Instead, some of the individual properties of the request are accessible, if available, as follows:
3535

3636
| Request property | Sampling context key(s) |
3737
| ---------------- | ------------------------------- |
@@ -42,7 +42,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
4242
| `scheme` | `url.scheme` |
4343
| full URL | `url.full` |
4444

45-
- If you're using the Tornado integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `tornado_request` object anymore. Instead, some of the individual properties of the request are accessible, if available, as follows:
45+
- If you're using the Tornado integration and a custom traces/profiles sampler, the `sampling_context` argument of `traces_sampler` and `profiles_sampler` doesn't contain the `tornado_request` object anymore. Instead, some of the individual properties of the request are accessible, if available, as follows:
4646

4747
| Request property | Sampling context key(s) |
4848
| ---------------- | --------------------------------------------------- |
@@ -54,7 +54,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
5454
| `version` | `network.protocol.name`, `network.protocol.version` |
5555
| full URL | `url.full` |
5656

57-
- If you're using the generic WSGI integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `wsgi_environ` object anymore. Instead, the individual properties of the environment are accessible, if available, as follows:
57+
- If you're using the generic WSGI integration and a custom traces/profiles sampler, the `sampling_context` argument of `traces_sampler` and `profiles_sampler` doesn't contain the `wsgi_environ` object anymore. Instead, the individual properties of the environment are accessible, if available, as follows:
5858

5959
| Env property | Sampling context key(s) |
6060
| ----------------- | ------------------------------------------------- |
@@ -67,7 +67,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
6767
| `wsgi.url_scheme` | `url.scheme` |
6868
| full URL | `url.full` |
6969

70-
- If you're using the generic ASGI integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `asgi_scope` object anymore. Instead, the individual properties of the scope, if available, are accessible as follows:
70+
- If you're using the generic ASGI integration and a custom traces/profiles sampler, the `sampling_context` argument of `traces_sampler` and `profiles_sampler` doesn't contain the `asgi_scope` object anymore. Instead, the individual properties of the scope, if available, are accessible as follows:
7171

7272
| Scope property | Sampling context key(s) |
7373
| -------------- | ------------------------------- |
@@ -81,19 +81,19 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
8181
| `client` | `client.address`, `client.port` |
8282
| full URL | `url.full` |
8383

84-
- If you're using the RQ integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `rq_job` object anymore. Instead, the individual properties of the job and the queue, if available, are accessible as follows:
84+
- If you're using the RQ integration and a custom traces/profiles sampler, the `sampling_context` argument of `traces_sampler` and `profiles_sampler` doesn't contain the `rq_job` object anymore. Instead, the individual properties of the job and the queue, if available, are accessible as follows:
8585

86-
| RQ property | Sampling context key(s) |
87-
| --------------- | ---------------------------- |
88-
| `rq_job.args` | `rq.job.args` |
89-
| `rq_job.kwargs` | `rq.job.kwargs` |
90-
| `rq_job.func` | `rq.job.func` |
91-
| `queue.name` | `messaging.destination.name` |
92-
| `rq_job.id` | `messaging.message.id` |
86+
| RQ property | Sampling context key | Example |
87+
| --------------- | ---------------------------- | ---------------------- |
88+
| `rq_job.args` | `rq.job.args.{index}` | `rq.job.args.0` |
89+
| `rq_job.kwargs` | `rq.job.kwargs.{kwarg}` | `rq.job.args.my_kwarg` |
90+
| `rq_job.func` | `rq.job.func` | |
91+
| `queue.name` | `messaging.destination.name` | |
92+
| `rq_job.id` | `messaging.message.id` | |
9393

9494
Note that `rq.job.args`, `rq.job.kwargs`, and `rq.job.func` are serialized and not the actual objects on the job.
9595

96-
- If you're using the AWS Lambda integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `aws_event` and `aws_context` objects anymore. Instead, the following, if available, is accessible:
96+
- If you're using the AWS Lambda integration and a custom traces/profiles sampler, the `sampling_context` argument of `traces_sampler` and `profiles_sampler` doesn't contain the `aws_event` and `aws_context` objects anymore. Instead, the following, if available, is accessible:
9797

9898
| AWS property | Sampling context key(s) |
9999
| ------------------------------------------- | ----------------------- |
@@ -105,7 +105,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
105105
| `aws_event["headers"]["Host"]` | `server.address` |
106106
| `aws_context["function_name"]` | `faas.name` |
107107

108-
- If you're using the GCP integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `gcp_env` and `gcp_event` keys anymore. Instead, the following, if available, is accessible:
108+
- If you're using the GCP integration and a custom traces/profiles sampler, the `sampling_context` argument of `traces_sampler` and `profiles_sampler` doesn't contain the `gcp_env` and `gcp_event` keys anymore. Instead, the following, if available, is accessible:
109109

110110
| Old sampling context key | New sampling context key |
111111
| --------------------------------- | -------------------------- |

sentry_sdk/integrations/celery/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
ensure_integration_enabled,
2121
event_from_exception,
2222
reraise,
23-
_serialize_span_attribute,
2423
)
2524

2625
from typing import TYPE_CHECKING
@@ -517,7 +516,14 @@ def _prepopulate_attributes(task, args, kwargs):
517516
# type: (Any, *Any, **Any) -> dict[str, str]
518517
attributes = {
519518
"celery.job.task": task.name,
520-
"celery.job.args": _serialize_span_attribute(args),
521-
"celery.job.kwargs": _serialize_span_attribute(kwargs),
522519
}
520+
521+
for i, arg in enumerate(args):
522+
with capture_internal_exceptions():
523+
attributes[f"celery.job.args.{i}"] = str(arg)
524+
525+
for kwarg, value in kwargs.items():
526+
with capture_internal_exceptions():
527+
attributes[f"celery.job.kwargs.{kwarg}"] = str(value)
528+
523529
return attributes

sentry_sdk/integrations/rq.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from sentry_sdk.integrations.logging import ignore_logger
77
from sentry_sdk.tracing import TRANSACTION_SOURCE_TASK
88
from sentry_sdk.utils import (
9-
_serialize_span_attribute,
109
capture_internal_exceptions,
1110
ensure_integration_enabled,
1211
event_from_exception,
@@ -183,6 +182,7 @@ def _prepopulate_attributes(job, queue):
183182
# type: (Job, Queue) -> dict[str, Any]
184183
attributes = {
185184
"messaging.system": "rq",
185+
"rq.job.id": job.id,
186186
}
187187

188188
for prop, attr in JOB_PROPERTY_TO_ATTRIBUTE.items():
@@ -193,14 +193,20 @@ def _prepopulate_attributes(job, queue):
193193
if getattr(queue, prop, None) is not None:
194194
attributes[attr] = getattr(queue, prop)
195195

196-
for key in ("args", "kwargs"):
197-
if getattr(job, key, None):
198-
attributes[f"rq.job.{key}"] = _serialize_span_attribute(getattr(job, key))
196+
if getattr(job, "args", None):
197+
for i, arg in enumerate(job.args):
198+
with capture_internal_exceptions():
199+
attributes[f"rq.job.args.{i}"] = str(arg)
200+
201+
if getattr(job, "kwargs", None):
202+
for kwarg, value in job.kwargs.items():
203+
with capture_internal_exceptions():
204+
attributes[f"rq.job.kwargs.{kwarg}"] = str(value)
199205

200206
func = job.func
201207
if callable(func):
202208
func = func.__name__
203209

204-
attributes["rq.job.func"] = _serialize_span_attribute(func)
210+
attributes["rq.job.func"] = str(func)
205211

206212
return attributes

tests/integrations/celery/test_celery.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
_wrap_task_run,
1414
)
1515
from sentry_sdk.integrations.celery.beat import _get_headers
16-
from sentry_sdk.utils import _serialize_span_attribute
1716
from tests.conftest import ApproxDict
1817

1918

@@ -448,12 +447,10 @@ def walk_dogs(x, y):
448447

449448
sampling_context = traces_sampler.call_args_list[1][0][0]
450449
assert sampling_context["celery.job.task"] == "dog_walk"
451-
assert sampling_context["celery.job.args"] == _serialize_span_attribute(
452-
args_kwargs["args"]
453-
)
454-
assert sampling_context["celery.job.kwargs"] == _serialize_span_attribute(
455-
args_kwargs["kwargs"]
456-
)
450+
for i, arg in enumerate(args_kwargs["args"]):
451+
assert sampling_context[f"celery.job.args.{i}"] == str(arg)
452+
for kwarg, value in args_kwargs["kwargs"].items():
453+
assert sampling_context[f"celery.job.kwargs.{kwarg}"] == str(value)
457454

458455

459456
def test_abstract_task(capture_events, celery, celery_invocation):

tests/integrations/rq/test_rq.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,23 @@ def test_traces_sampler_gets_correct_values_in_sampling_context(sentry_init):
227227
queue = rq.Queue(connection=FakeStrictRedis())
228228
worker = rq.SimpleWorker([queue], connection=queue.connection)
229229

230-
queue.enqueue(do_trick, "Bodhi", trick="roll over")
230+
queue.enqueue(
231+
do_trick,
232+
"Bodhi",
233+
{"age": 5},
234+
trick="roll over",
235+
times=2,
236+
followup=["fetch", "give paw"],
237+
)
231238
worker.work(burst=True)
232239

233240
sampling_context = traces_sampler.call_args_list[0][0][0]
234241
assert sampling_context["messaging.system"] == "rq"
235-
assert sampling_context["rq.job.args"] == ["Bodhi"]
236-
assert sampling_context["rq.job.kwargs"] == '{"trick": "roll over"}'
242+
assert sampling_context["rq.job.args.0"] == "Bodhi"
243+
assert sampling_context["rq.job.args.1"] == "{'age': 5}"
244+
assert sampling_context["rq.job.kwargs.trick"] == "roll over"
245+
assert sampling_context["rq.job.kwargs.times"] == "2"
246+
assert sampling_context["rq.job.kwargs.followup"] == "['fetch', 'give paw']"
237247
assert sampling_context["rq.job.func"] == "do_trick"
238248
assert sampling_context["messaging.message.id"]
239249
assert sampling_context["messaging.destination.name"] == "default"

0 commit comments

Comments
 (0)