Skip to content

Dual POTel setup improvements #4401

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

Draft
wants to merge 6 commits into
base: potel-base
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions sentry_sdk/opentelemetry/span_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def on_start(self, span, parent_context=None):
return

self._add_root_span(span, get_current_span(parent_context))
self._add_origin(span)
self._start_profile(span)

def on_end(self, span):
Expand All @@ -98,6 +99,13 @@ def force_flush(self, timeout_millis=30000):
# type: (int) -> bool
return True

def _add_origin(self, span):
# type: (Span) -> None
if span.instrumentation_scope and span.instrumentation_scope.name:
span.set_attribute(
SentrySpanAttribute.ORIGIN, span.instrumentation_scope.name
)

def _add_root_span(self, span, parent_span):
# type: (Span, AbstractSpan) -> None
"""
Expand Down
20 changes: 17 additions & 3 deletions sentry_sdk/opentelemetry/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SentrySampler,
SentrySpanProcessor,
)
from sentry_sdk.utils import logger


def patch_readable_span():
Expand All @@ -28,8 +29,21 @@ def sentry_patched_readable_span(self):

def setup_sentry_tracing():
# type: () -> None
provider = TracerProvider(sampler=SentrySampler())
provider.add_span_processor(SentrySpanProcessor())
trace.set_tracer_provider(provider)

# TracerProvider can only be set once. If we're the first ones setting it,
# there's no issue. If it already exists, we need to patch it.
from opentelemetry.trace import _TRACER_PROVIDER_SET_ONCE

if _TRACER_PROVIDER_SET_ONCE._done:
logger.debug("[Tracing] Detected an existing TracerProvider, patching")
tracer_provider = trace.get_tracer_provider()
tracer_provider.add_span_processor(SentrySpanProcessor())
tracer_provider.sampler = SentrySampler()

else:
logger.debug("[Tracing] No TracerProvider set, creating a new one")
tracer_provider = TracerProvider(sampler=SentrySampler())
tracer_provider.add_span_processor(SentrySpanProcessor())
trace.set_tracer_provider(tracer_provider)

set_global_textmap(SentryPropagator())
2 changes: 1 addition & 1 deletion sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def __init__(
If otel_span is passed explicitly, just acts as a proxy.

If span is passed explicitly, use it. The only purpose of this param
if backwards compatibility with start_transaction(transaction=...).
is backwards compatibility with start_transaction(transaction=...).

If only_if_parent is True, just return an INVALID_SPAN
and avoid instrumentation if there's no active parent span.
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import socket
import warnings
from opentelemetry import trace as otel_trace
from opentelemetry.util._once import Once
from threading import Thread
from http.server import BaseHTTPRequestHandler, HTTPServer

Expand Down Expand Up @@ -73,6 +75,13 @@ def clean_scopes():
setup_initial_scopes()


@pytest.fixture(autouse=True)
def clean_tracer():
"""Reset TracerProvider so that we can set it up from scratch."""
otel_trace._TRACER_PROVIDER_SET_ONCE = Once()
otel_trace._TRACER_PROVIDER = None


@pytest.fixture(autouse=True)
def internal_exceptions(request):
errors = []
Expand Down
Loading