Skip to content

Commit

Permalink
Allow specifying excluded paths
Browse files Browse the repository at this point in the history
  • Loading branch information
toelke committed Apr 20, 2023
1 parent c7270ca commit 27e5831
Showing 1 changed file with 12 additions and 1 deletion.
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

0 comments on commit 27e5831

Please sign in to comment.