|  | 
|  | 1 | +import asyncio | 
|  | 2 | + | 
|  | 3 | +import tornado.web | 
|  | 4 | + | 
|  | 5 | +from . import tracing | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +class AsyncScopeHandler(tornado.web.RequestHandler): | 
|  | 9 | +    async def do_something(self): | 
|  | 10 | +        tracing = self.settings.get('opentracing_tracing') | 
|  | 11 | +        with tracing.tracer.start_active_span('Child'): | 
|  | 12 | +            tracing.tracer.active_span.set_tag('start', 0) | 
|  | 13 | +            await asyncio.sleep(0) | 
|  | 14 | +            tracing.tracer.active_span.set_tag('end', 1) | 
|  | 15 | + | 
|  | 16 | +    async def get(self): | 
|  | 17 | +        tracing = self.settings.get('opentracing_tracing') | 
|  | 18 | +        span = tracing.get_span(self.request) | 
|  | 19 | +        assert span is not None | 
|  | 20 | +        assert tracing.tracer.active_span is span | 
|  | 21 | + | 
|  | 22 | +        await self.do_something() | 
|  | 23 | + | 
|  | 24 | +        assert tracing.tracer.active_span is span | 
|  | 25 | +        self.write('{}') | 
|  | 26 | + | 
|  | 27 | + | 
|  | 28 | +class DecoratedAsyncHandler(tornado.web.RequestHandler): | 
|  | 29 | +    @tracing.trace('protocol', 'doesntexist') | 
|  | 30 | +    async def get(self): | 
|  | 31 | +        await asyncio.sleep(0) | 
|  | 32 | +        self.set_status(201) | 
|  | 33 | +        self.write('{}') | 
|  | 34 | + | 
|  | 35 | + | 
|  | 36 | +class DecoratedAsyncErrorHandler(tornado.web.RequestHandler): | 
|  | 37 | +    @tracing.trace() | 
|  | 38 | +    async def get(self): | 
|  | 39 | +        await asyncio.sleep(0) | 
|  | 40 | +        raise ValueError('invalid value') | 
|  | 41 | + | 
|  | 42 | + | 
|  | 43 | +class DecoratedAsyncScopeHandler(tornado.web.RequestHandler): | 
|  | 44 | +    async def do_something(self): | 
|  | 45 | +        with tracing.tracer.start_active_span('Child'): | 
|  | 46 | +            tracing.tracer.active_span.set_tag('start', 0) | 
|  | 47 | +            await asyncio.sleep(0) | 
|  | 48 | +            tracing.tracer.active_span.set_tag('end', 1) | 
|  | 49 | + | 
|  | 50 | +    @tracing.trace() | 
|  | 51 | +    async def get(self): | 
|  | 52 | +        span = tracing.get_span(self.request) | 
|  | 53 | +        assert span is not None | 
|  | 54 | +        assert tracing.tracer.active_span is span | 
|  | 55 | + | 
|  | 56 | +        await self.do_something() | 
|  | 57 | + | 
|  | 58 | +        assert tracing.tracer.active_span is span | 
|  | 59 | +        self.set_status(201) | 
|  | 60 | +        self.write('{}') | 
0 commit comments