sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

Tracing: expose HTTP response in `sampling_context`

Open guidoiaquinti opened this issue 3 years ago • 4 comments

Problem Statement

Afaik there's currently no way to filter out transaction events of HTTP requests by status code (e.g. HTTP 404) using the traces_sampler configuration option. sampling_context currently exposes the request but not the response context.

Solution Brainstorm

There's a way to filter out transaction events by HTTP status code.

guidoiaquinti avatar Sep 14 '22 20:09 guidoiaquinti

You can manually call start_transaction and set a custom_sampling_context. (Like in our AWS Lambda integration: https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/aws_lambda.py#L147-L153)

The custom_sampling_context is available to the traces_sampler. So if you put your status code (or the whole response) in the custom_sampling_context you can filter by it in traces_sampler.

Does this solve your issue?

antonpirker avatar Sep 15 '22 08:09 antonpirker

Hey @antonpirker thank you for the additional context! 🙌

I can definitely make it work but I was wondering if this is something other users might benefit as well. If yes, we could probably make this context available as default instead of having to manually do the override.

What do you think?

guidoiaquinti avatar Sep 15 '22 18:09 guidoiaquinti

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

github-actions[bot] avatar Oct 12 '22 00:10 github-actions[bot]

hi @guidoiaquinti this is not really possible in a normal WSGI/ASGI integration due to lifecycle issues. We start and sample a transaction at the beginning of the request flow and the response is available at the end after the sampling is already done.

sl0thentr0py avatar Oct 12 '22 11:10 sl0thentr0py

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

github-actions[bot] avatar Nov 04 '22 00:11 github-actions[bot]