Skip to content

Commit 5996b7e

Browse files
committed
Pass excluded_urls to Flask auto-instrumentation
The Flask instrumentation can be applied on two levels: * The `module` level (auto-instrumentation), which patches the `Flask` class. * The `app` level, which patches a `Flask` instance. This commit applies a few missing `excluded_urls` keyword arguments to ensure the auto-instrumentation provides the same configurability as the app-level instrumentation.
1 parent b3aa6bd commit 5996b7e

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,19 @@ def __init__(self, *args, **kwargs):
205205
self._is_instrumented_by_opentelemetry = True
206206

207207
self.wsgi_app = _rewrapped_app(
208-
self.wsgi_app, _InstrumentedFlask._response_hook
208+
self.wsgi_app,
209+
_InstrumentedFlask._response_hook,
210+
excluded_urls=_InstrumentedFlask._excluded_urls,
209211
)
210212

211213
tracer = trace.get_tracer(
212214
__name__, __version__, _InstrumentedFlask._tracer_provider
213215
)
214216

215217
_before_request = _wrapped_before_request(
216-
_InstrumentedFlask._request_hook, tracer,
218+
_InstrumentedFlask._request_hook,
219+
tracer,
220+
excluded_urls=_InstrumentedFlask._excluded_urls,
217221
)
218222
self._before_request = _before_request
219223
self.before_request(_before_request)
@@ -273,7 +277,9 @@ def instrument_app(
273277
else _excluded_urls_from_env
274278
)
275279
app._original_wsgi_app = app.wsgi_app
276-
app.wsgi_app = _rewrapped_app(app.wsgi_app, response_hook)
280+
app.wsgi_app = _rewrapped_app(
281+
app.wsgi_app, response_hook, excluded_urls=excluded_urls
282+
)
277283

278284
tracer = trace.get_tracer(__name__, __version__, tracer_provider)
279285

Diff for: instrumentation/opentelemetry-instrumentation-flask/tests/test_automatic.py

+20
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,23 @@ def test_uninstrument(self):
5959
self.assertEqual([b"Hello: 123"], list(resp.response))
6060
span_list = self.memory_exporter.get_finished_spans()
6161
self.assertEqual(len(span_list), 1)
62+
63+
def test_exluded_urls_explicit(self):
64+
FlaskInstrumentor().uninstrument()
65+
FlaskInstrumentor().instrument(excluded_urls="/hello/456")
66+
67+
self.app = flask.Flask(__name__)
68+
self.app.route("/hello/<int:helloid>")(self._hello_endpoint)
69+
client = Client(self.app, BaseResponse)
70+
71+
resp = client.get("/hello/123")
72+
self.assertEqual(200, resp.status_code)
73+
self.assertEqual([b"Hello: 123"], list(resp.response))
74+
span_list = self.memory_exporter.get_finished_spans()
75+
self.assertEqual(len(span_list), 1)
76+
77+
resp = client.get("/hello/456")
78+
self.assertEqual(200, resp.status_code)
79+
self.assertEqual([b"Hello: 456"], list(resp.response))
80+
span_list = self.memory_exporter.get_finished_spans()
81+
self.assertEqual(len(span_list), 1)

0 commit comments

Comments
 (0)