Skip to content
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

Allow specifying excluded paths #56

Closed
Closed
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
5 changes: 5 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ Trace All Requests
opentracing_tracer = ## some OpenTracing tracer implementation
tracing = FlaskTracing(opentracing_tracer, True, app, [optional_args])

It is possible to exclude paths from tracing by adding them in the parameter `exluded_paths`:

.. code-block:: python
tracing = FlaskTracing(opentracing_tracer, True, app, [optional_args], exluded_paths=['/metrics', '/healthz'])

Trace Individual Requests
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
13 changes: 12 additions & 1 deletion flask_opentracing/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FlaskTracing(opentracing.Tracer):
@param tracer the OpenTracing tracer implementation to trace requests with
"""
def __init__(self, tracer=None, trace_all_requests=None, app=None,
traced_attributes=[], start_span_cb=None):
traced_attributes=[], start_span_cb=None, excluded_paths=None):

if start_span_cb is not None and not callable(start_span_cb):
raise ValueError('start_span_cb is not callable')
Expand All @@ -21,6 +21,12 @@ def __init__(self, tracer=None, trace_all_requests=None, app=None,
if trace_all_requests is None:
trace_all_requests = False if app is None else True

if (trace_all_requests is None or not trace_all_requests) and excluded_paths is not None:
raise ValueError('excluded_paths only makes sense when trace_all_requests is True')

if excluded_paths is None:
excluded_paths = [] if trace_all_requests is True else None

if not callable(tracer):
self.__tracer = tracer
self.__tracer_getter = None
Expand All @@ -31,6 +37,7 @@ def __init__(self, tracer=None, trace_all_requests=None, app=None,
self._trace_all_requests = trace_all_requests
self._start_span_cb = start_span_cb
self._current_scopes = {}
self._excluded_paths = excluded_paths

# tracing all requests requires that app != None
if self._trace_all_requests:
Expand Down Expand Up @@ -109,6 +116,8 @@ def get_span(self, request=None):

def _before_request_fn(self, attributes):
request = stack.top.request
if request.path in self._excluded_paths:
return
operation_name = request.endpoint
headers = {}
for k, v in request.headers:
Expand Down Expand Up @@ -141,6 +150,8 @@ def _before_request_fn(self, attributes):

def _after_request_fn(self, response=None, error=None):
request = stack.top.request
if request.path in self._excluded_paths:
return

# the pop call can fail if the request is interrupted by a
# `before_request` method so we need a default
Expand Down