Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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