Created
January 15, 2020 17:30
-
-
Save joar/101cbe4a6baa32bf2feabc456255ba3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
from opencensus.ext.flask.flask_middleware import FlaskMiddleware | |
from opencensus.trace import utils, execution_context | |
from opencensus.trace.blank_span import BlankSpan | |
ATTR_REQUEST_BLACKLISTED = "request_blacklisted" | |
class FixedFlaskMiddleware(FlaskMiddleware): | |
def _before_request(self): | |
""" | |
Works around a grey area in opencensus' FlaskMiddleware where spans | |
created during requests that are blacklisted end up polluting the | |
previous span. | |
""" | |
# Do not trace if the url is blacklisted | |
if utils.disable_tracing_url(flask.request.url, self.blacklist_paths): | |
execution_context.set_current_span(BlankSpan()) | |
execution_context.set_opencensus_attr( | |
ATTR_REQUEST_BLACKLISTED, True | |
) | |
return | |
execution_context.set_opencensus_attr(ATTR_REQUEST_BLACKLISTED, False) | |
return super(FixedFlaskMiddleware, self)._before_request() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment