Skip to content

Commit e4fdcae

Browse files
committed
Call OTel instrument_app in Flask integration
1 parent ef1b2af commit e4fdcae

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

sentry_sdk/integrations/flask.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
RequestExtractor,
88
)
99
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
10+
from sentry_sdk.opentelemetry.tracing import SENTRY_TRACER_PROVIDER
1011
from sentry_sdk.scope import should_send_default_pii
1112
from sentry_sdk.utils import (
1213
capture_internal_exceptions,
1314
ensure_integration_enabled,
1415
event_from_exception,
1516
package_version,
1617
)
18+
from opentelemetry.instrumentation.flask import FlaskInstrumentor
1719

1820
from typing import TYPE_CHECKING
1921

@@ -91,6 +93,7 @@ def setup_once() -> None:
9193
got_request_exception.connect(_capture_exception)
9294

9395
old_app = Flask.__call__
96+
old_app_init = Flask.__init__
9497

9598
def sentry_patched_wsgi_app(
9699
self: Any, environ: Dict[str, str], start_response: Callable[..., Any]
@@ -109,9 +112,17 @@ def sentry_patched_wsgi_app(
109112
else DEFAULT_HTTP_METHODS_TO_CAPTURE
110113
),
111114
)
115+
112116
return middleware(environ, start_response)
113117

118+
def sentry_patched_flask_init(self, *args, **kwargs) -> Any:
119+
old_app_init(self, *args, **kwargs)
120+
FlaskInstrumentor.instrument_app(
121+
self, tracer_provider=SENTRY_TRACER_PROVIDER
122+
)
123+
114124
Flask.__call__ = sentry_patched_wsgi_app
125+
Flask.__init__ = sentry_patched_flask_init
115126

116127

117128
def _add_sentry_trace(

sentry_sdk/opentelemetry/tracing.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020

2121
READABLE_SPAN_PATCHED = False
2222

23+
SENTRY_TRACER_PROVIDER = TracerProvider(
24+
sampler=SentrySampler(),
25+
resource=Resource.create(
26+
{
27+
RESOURCE_SERVICE_NAME: "sentry-python",
28+
RESOURCE_SERVICE_VERSION: VERSION,
29+
RESOURCE_SERVICE_NAMESPACE: "sentry",
30+
}
31+
),
32+
)
33+
2334

2435
def patch_readable_span() -> None:
2536
"""
@@ -51,16 +62,7 @@ def setup_sentry_tracing() -> None:
5162

5263
else:
5364
logger.debug("[Tracing] No TracerProvider set, creating a new one")
54-
tracer_provider = TracerProvider(
55-
sampler=SentrySampler(),
56-
resource=Resource.create(
57-
{
58-
RESOURCE_SERVICE_NAME: "sentry-python",
59-
RESOURCE_SERVICE_VERSION: VERSION,
60-
RESOURCE_SERVICE_NAMESPACE: "sentry",
61-
}
62-
),
63-
)
65+
tracer_provider = SENTRY_TRACER_PROVIDER
6466
trace.set_tracer_provider(tracer_provider)
6567

6668
try:

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def get_file_text(file_name):
4242
"urllib3>=1.26.11",
4343
"certifi",
4444
"opentelemetry-sdk>=1.4.0",
45+
"opentelemetry-instrumentation-flask",
4546
],
4647
extras_require={
4748
"aiohttp": ["aiohttp>=3.5"],

0 commit comments

Comments
 (0)