Skip to content

Instantly share code, notes, and snippets.

@joar
Created January 15, 2020 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joar/101cbe4a6baa32bf2feabc456255ba3a to your computer and use it in GitHub Desktop.
Save joar/101cbe4a6baa32bf2feabc456255ba3a to your computer and use it in GitHub Desktop.
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