1919from opencensus .trace import execution_context
2020from opencensus .trace .propagation import google_cloud_format
2121from opencensus .trace .exporters import print_exporter
22+ from opencensus .trace .ext import utils
2223from opencensus .trace .samplers import always_on
2324from opencensus .trace import request_tracer
2425
@@ -37,6 +38,9 @@ class FlaskMiddleware(object):
3738 :type app: :class: `~flask.Flask`
3839 :param app: A flask application.
3940
41+ :type blacklist_paths: list
42+ :param blacklist_paths: Paths that do not trace.
43+
4044 :type sampler: :class: `type`
4145 :param sampler: Class for creating new Sampler objects. It should extend
4246 from the base :class:`.Sampler` type and implement
@@ -48,8 +52,16 @@ class FlaskMiddleware(object):
4852 :param exporter: Class for creating new exporter objects. Default to
4953 :class:`.PrintExporter`. The rest option is
5054 :class:`.FileExporter`.
55+
56+ :type propagator: :class: 'type'
57+ :param propagator: Class for creating new propagator objects. Default to
58+ :class:`.GoogleCloudFormatPropagator`. The rest option
59+ are :class:`.BinaryFormatPropagator`,
60+ :class:`.TextFormatPropagator` and
61+ :class:`.TraceContextPropagator`.
5162 """
52- def __init__ (self , app , sampler = None , exporter = None , propagator = None ):
63+ def __init__ (self , app , blacklist_paths = None , sampler = None , exporter = None ,
64+ propagator = None ):
5365 if sampler is None :
5466 sampler = always_on .AlwaysOnSampler ()
5567
@@ -60,6 +72,7 @@ def __init__(self, app, sampler=None, exporter=None, propagator=None):
6072 propagator = google_cloud_format .GoogleCloudFormatPropagator ()
6173
6274 self .app = app
75+ self .blacklist_paths = blacklist_paths
6376 self .sampler = sampler
6477 self .exporter = exporter
6578 self .propagator = propagator
@@ -74,6 +87,10 @@ def _before_request(self):
7487
7588 See: http://flask.pocoo.org/docs/0.12/api/#flask.Flask.before_request
7689 """
90+ # Do not trace if the url is blacklisted
91+ if utils .disable_tracing_url (flask .request .url , self .blacklist_paths ):
92+ return
93+
7794 try :
7895 header = get_flask_header ()
7996 span_context = self .propagator .from_header (header )
@@ -100,6 +117,10 @@ def _after_request(self, response):
100117
101118 See: http://flask.pocoo.org/docs/0.12/api/#flask.Flask.after_request
102119 """
120+ # Do not trace if the url is blacklisted
121+ if utils .disable_tracing_url (flask .request .url , self .blacklist_paths ):
122+ return response
123+
103124 try :
104125 tracer = execution_context .get_opencensus_tracer ()
105126 tracer .add_label_to_current_span (
0 commit comments