Skip to content

WIP: Potel sampling #3457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 45 commits into from
Closed

WIP: Potel sampling #3457

wants to merge 45 commits into from

Conversation

antonpirker
Copy link
Member

@antonpirker antonpirker commented Aug 24, 2024

Sampling using Potel


Thank you for contributing to sentry-python! Please add tests to validate your changes, and lint your code using tox -e linters.

Running the test suite on your PR might require maintainer approval. The AWS Lambda tests additionally require a maintainer to add a special label, and they will fail until this label is added.

sl0thentr0py and others added 30 commits June 11, 2024 13:43
* create a new otel context `_SCOPES_KEY` that will hold a tuple of
  `(curent_scope, isolation_scope)`
* the `current_scope` will always be forked (like on every span creation/context update in practice)
  * note that this is on `attach`, so not on all copy-on-write context
    object creation but only on apis such as
    [`trace.use_span`](https://github.com/open-telemetry/opentelemetry-python/blob/ba22b165471bde2037620f2c850ab648a849fbc0/opentelemetry-api/src/opentelemetry/trace/__init__.py#L547)
    or [`tracer.start_as_current_span`](https://github.com/open-telemetry/opentelemetry-python/blob/ba22b165471bde2037620f2c850ab648a849fbc0/opentelemetry-api/src/opentelemetry/trace/__init__.py#L329)
  * basically every otel `context` fork corresponds to our `current_scope` fork
* the `isolation_scope` currently will not be forked
  * these will later be updated, for instance when we update our top
    level scope apis that fork isolation scope, that will also have a
    corresponding change in this `attach` function
* create a new otel context `_SCOPES_KEY` that will hold a tuple of
  `(curent_scope, isolation_scope)`
* the `current_scope` will always be forked (like on every span creation/context update in practice)
  * note that this is on `attach`, so not on all copy-on-write context
    object creation but only on apis such as
    [`trace.use_span`](https://github.com/open-telemetry/opentelemetry-python/blob/ba22b165471bde2037620f2c850ab648a849fbc0/opentelemetry-api/src/opentelemetry/trace/__init__.py#L547)
    or [`tracer.start_as_current_span`](https://github.com/open-telemetry/opentelemetry-python/blob/ba22b165471bde2037620f2c850ab648a849fbc0/opentelemetry-api/src/opentelemetry/trace/__init__.py#L329)
  * basically every otel `context` fork corresponds to our `current_scope` fork
* the `isolation_scope` currently will not be forked
  * these will later be updated, for instance when we update our top
    level scope apis that fork isolation scope, that will also have a
    corresponding change in this `attach` function
Add simple scope management whenever a context is attached

* create a new otel context `_SCOPES_KEY` that will hold a tuple of
  `(curent_scope, isolation_scope)`
* the `current_scope` will always be forked (like on every span creation/context update in practice)
  * note that this is on `attach`, so not on all copy-on-write context
    object creation but only on apis such as
    [`trace.use_span`](https://github.com/open-telemetry/opentelemetry-python/blob/ba22b165471bde2037620f2c850ab648a849fbc0/opentelemetry-api/src/opentelemetry/trace/__init__.py#L547)
    or [`tracer.start_as_current_span`](https://github.com/open-telemetry/opentelemetry-python/blob/ba22b165471bde2037620f2c850ab648a849fbc0/opentelemetry-api/src/opentelemetry/trace/__init__.py#L329)
  * basically every otel `context` fork corresponds to our `current_scope` fork
* the `isolation_scope` currently will not be forked
  * these will later be updated, for instance when we update our top
    level scope apis that fork isolation scope, that will also have a
    corresponding change in this `attach` function
* only acts on `on_end` instead of both `on_start/on_end` as before
* store children spans in a dict mapping `span_id -> children`
* new dict only stores otel span objects and no sentry transaction/span objects so we save a bit of useless memory allocation
* I'm not using our current `Transaction/Span` classes at all to build the event because when we add our APIs later, we'll need to rip these out and we also avoid having to deal with the `instrumenter` problem
* if we get a root span (without parent), we recursively walk the dict and find the children and package up the transaction event and send it 
  * I didn't do it like JS because I think this way is better
  *  they [group an array of `finished_spans`](https://github.com/getsentry/sentry-javascript/blob/7e298036a21a5658f3eb9ba184165178c48d7ef8/packages/opentelemetry/src/spanExporter.ts#L132) every time a root span ends and I think this uses more cpu than what I did
  * and the dict like I used it doesn't take more space than the array either
* if we get a span with a parent we just update the dict to find the span later
* moved the common `is_sentry_span` logic to utils
szokeasaurusrex and others added 15 commits August 2, 2024 19:12
With this change, we aim to simplify the backwards-compatibility code
for POTel tracing.

We do this as follows:
  - Remove `start_*` functions from `tracing`
  - Remove unused parameters from `tracing.POTelSpan.__init__`.
  - Make all parameters to `tracing.POTelSpan.__init__` kwarg-only.
  - Allow `tracing.POTelSpan.__init__` to accept arbitrary kwargs,
    which are all ignored, for compatibility with old `Span` interface.
  - Completely remove `start_inactive_span`, since inactive spans can
    be created by setting `active=False` when constructing a
    `POTelSpan`.
* New `PotelScope` inherits from scope and reads the scope from the otel context key `SENTRY_SCOPES_KEY`
* New `isolation_scope` and `new_scope` context managers just use the context manager forking and yield with the scopes living on the above context key
  * isolation scope forking is done with the `SENTRY_FORK_ISOLATION_SCOPE_KEY` boolean context key
Copy link

codecov bot commented Sep 2, 2024

❌ 95 Tests Failed:

Tests completed Failed Passed Skipped
110 95 15 0
View the top 3 failed tests by shortest run time
tests.integrations.aws_lambda.test_aws test_non_dict_event[python3.8-"Good dog!"-False-1]
Stack Traces | 0.166s run time
.../integrations/aws_lambda/test_aws.py:509: in test_non_dict_event
    (
E   ValueError: not enough values to unpack (expected 2, got 0)
tests.integrations.aws_lambda.test_aws test_non_dict_event[python3.8-true-False-1]
Stack Traces | 0.172s run time
.../integrations/aws_lambda/test_aws.py:509: in test_non_dict_event
    (
E   ValueError: not enough values to unpack (expected 2, got 0)
tests.integrations.aws_lambda.test_aws test_non_dict_event[python3.8-\n            [\n                {\n                    "headers": {\n                        "Host": "x1.io",\n                        "X-Forwarded-Proto": "https"\n                    },\n                    "httpMethod": "GET",\n                    "path": "/path1",\n                    "queryStringParameters": {\n                        "done": "false"\n                    },\n                    "dog": "Maisey"\n                },\n                {\n                    "headers": {\n                        "Host": "x2.io",\n                        "X-Forwarded-Proto": "http"\n                    },\n                    "httpMethod": "POST",\n                    "path": "/path2",\n                    "queryStringParameters": {\n                        "done": "true"\n                    },\n                    "dog": "Charlie"\n                }\n            ]\n            -True-2]
Stack Traces | 0.187s run time
.../integrations/aws_lambda/test_aws.py:509: in test_non_dict_event
    (
E   ValueError: not enough values to unpack (expected 2, got 0)

To view individual test run time comparison to the main branch, go to the Test Analytics Dashboard

@antonpirker antonpirker changed the base branch from master to potel-base September 5, 2024 11:44
@antonpirker
Copy link
Member Author

Closing in favor of: #3501

@antonpirker antonpirker closed this Sep 5, 2024
@antonpirker antonpirker deleted the antonpirker/potel/sampler branch September 5, 2024 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sampling in POtel
4 participants