opencensus-python icon indicating copy to clipboard operation
opencensus-python copied to clipboard

tracer.add_attribute_to_current_span from wsgi Middleware

Open ctaggart opened this issue 4 years ago • 3 comments

Is it possible to tracer.add_attribute_to_current_span within a Flask middleware?

I set up a basic middleware:

from werkzeug.wrappers import Request
from opencensus.trace import execution_context

class IDMiddleware:
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        request = Request(environ)
        tracer = execution_context.get_opencensus_tracer()
        tracer.add_attribute_to_current_span("id", 551212)
        return self.app(environ, start_response)

I registered both the FlaskMiddleware and this middleware:

middleware = FlaskMiddleware(app, sampler=sampler, exporter=exporter)
app.wsgi_app = IDMiddleware(app.wsgi_app)

But it errors with:

  File "/home/cameron/.local/share/virtualenvs/sark-o0ke7yP6/lib/python3.7/site-packages/opencensus/trace/tracer.py", line 132, in add_attribute_to_current_span
    attribute_key, attribute_value)
  File "/home/cameron/.local/share/virtualenvs/sark-o0ke7yP6/lib/python3.7/site-packages/opencensus/trace/tracers/context_tracer.py", line 147, in add_attribute_to_current_span
    current_span.add_attribute(attribute_key, attribute_value)
AttributeError: 'NoneType' object has no attribute 'add_attribute'

I'm guessing it is because the FlaskMiddleware isn't wsgi middleware and instead registered with these three callbacks: https://github.com/census-instrumentation/opencensus-python/blob/22c1a285d8234d3ac5414c1e7be6901b772681aa/contrib/opencensus-ext-flask/opencensus/ext/flask/flask_middleware.py#L122-L125

I couldn't figure out how to to control the order of middleware by turning it into wsgi middleware or registering another set of callbacks. Looks like #368 is related.

ctaggart avatar Jan 16 '20 18:01 ctaggart

Hi. I'm having the same issue. Is there any solution workaround for this?

tomas223 avatar Jun 02 '21 22:06 tomas223

I think this package is dead, and you need to switch to open telemetry.

On Thu, Jun 3, 2021, at 7:41 AM, Tomas Hornak wrote:

Hi. I'm having the same issue. Is there any solution workaround for this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/census-instrumentation/opencensus-python/issues/846#issuecomment-853429190, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKYPCX4B2HBXPJCHZIKB4DTQ2XRLANCNFSM4KHYZXPA.

allanlw avatar Jun 03 '21 00:06 allanlw

@allanlw thank you for response. That is unfortunate, because Azure, where I'm using it still recommends Opencensus :/

tomas223 avatar Jun 03 '21 06:06 tomas223