Skip to content

Commit

Permalink
Pass excluded_urls to Flask auto-instrumentation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mattoberle committed Jul 26, 2021
1 parent b3aa6bd commit 5996b7e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,19 @@ def __init__(self, *args, **kwargs):
self._is_instrumented_by_opentelemetry = True

self.wsgi_app = _rewrapped_app(
self.wsgi_app, _InstrumentedFlask._response_hook
self.wsgi_app,
_InstrumentedFlask._response_hook,
excluded_urls=_InstrumentedFlask._excluded_urls,
)

tracer = trace.get_tracer(
__name__, __version__, _InstrumentedFlask._tracer_provider
)

_before_request = _wrapped_before_request(
_InstrumentedFlask._request_hook, tracer,
_InstrumentedFlask._request_hook,
tracer,
excluded_urls=_InstrumentedFlask._excluded_urls,
)
self._before_request = _before_request
self.before_request(_before_request)
Expand Down Expand Up @@ -273,7 +277,9 @@ def instrument_app(
else _excluded_urls_from_env
)
app._original_wsgi_app = app.wsgi_app
app.wsgi_app = _rewrapped_app(app.wsgi_app, response_hook)
app.wsgi_app = _rewrapped_app(
app.wsgi_app, response_hook, excluded_urls=excluded_urls
)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,23 @@ def test_uninstrument(self):
self.assertEqual([b"Hello: 123"], list(resp.response))
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

def test_exluded_urls_explicit(self):
FlaskInstrumentor().uninstrument()
FlaskInstrumentor().instrument(excluded_urls="/hello/456")

self.app = flask.Flask(__name__)
self.app.route("/hello/<int:helloid>")(self._hello_endpoint)
client = Client(self.app, BaseResponse)

resp = client.get("/hello/123")
self.assertEqual(200, resp.status_code)
self.assertEqual([b"Hello: 123"], list(resp.response))
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

resp = client.get("/hello/456")
self.assertEqual(200, resp.status_code)
self.assertEqual([b"Hello: 456"], list(resp.response))
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

0 comments on commit 5996b7e

Please sign in to comment.