Skip to content

Memory leak & CPU burn in sentry-opentelemetry integration #6267

@volodkindv

Description

@volodkindv

How do you use Sentry?

Self-hosted/on-premise

Version

2.42.1

Steps to Reproduce

Context: two services using sentry-sdk and opentelemetry. One is async, based on Faststream with Kafka. Second - backend on synchronous Django.

Both of them are degrading after several hours/minutes of work. Symptoms are slow memory leak and high CPU load.

Reason is in SentrySpanProcessor, made as singletone with decorator in init() method.
Minimal example to reproduce:

import types
import gc
import sentry_sdk
from sentry_sdk.integrations.opentelemetry.propagator import SentryPropagator
from sentry_sdk.integrations.opentelemetry.integration import OpenTelemetryIntegration
from opentelemetry import trace
import sentry_sdk.scope
from opentelemetry.trace.propagation import set_span_in_context


OpenTelemetryIntegration.setup_once()

tracer = trace.get_tracer(__name__)
propagator = SentryPropagator()


def get_global_processors_count():
    return len(sentry_sdk.scope.global_event_processors)


def emulate_sentry_propagator_inject():
    with tracer.start_as_current_span("bar") as new_span:
        context = set_span_in_context(new_span)
        propagator.inject("any_carrier", context)  # here created new global_processor


def main():
    sentry_sdk.init(dsn=None, traces_sample_rate=1.0, debug=False)

    for i in range(10):
        # for example, outgoing http request
        emulate_sentry_propagator_inject()

        gc.collect()
        funcs = sum(1 for obj in gc.get_objects() if isinstance(obj, types.FunctionType))
        cells = sum(1 for obj in gc.get_objects() if isinstance(obj, types.CellType))

        print(f"After {i}th call emulate_sentry_propagator_inject:")
        print(f"  global processors_count: {get_global_processors_count()}")
        print(f"  function objects: {funcs}, cells: {cells}\n")


if __name__ == "__main__":
    main()


Expected Result

Constant count of objects in GC and global_event_processors

Actual Result

Count of objects in GC and global_event_processors increased after every call of "propagator.inject()".

Metadata

Metadata

Labels

No fields configured for issues without a type.

Projects

Status

Waiting for: Product Owner

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions