def test_trace_all_requests(self): app = Sanic("test_app") tracing = SanicTracing(tracer=opentracing.tracer, app=app, trace_all_requests=True) assert tracing._trace_all_requests is True tracing = SanicTracing(app=app, trace_all_requests=False) assert tracing._trace_all_requests is False
def test_error(self): def start_span_cb(span, request): raise RuntimeError("Should not happen") tracing = SanicTracing(tracer=MockTracer(), app=app, trace_all_requests=True, start_span_cb=start_span_cb) request, response = test_app.get("/test") assert "OK" in str(response.status_code) spans = tracing.tracer.finished_spans() assert len(spans) == 1 assert spans[0].tags.get(tags.ERROR, None) is None
def test_simple(self): def start_span_cb(span, request): span.set_tag("component", "not-sanic") span.set_tag("utag", "uvalue") tracing = SanicTracing(tracer=MockTracer(), app=app, trace_all_requests=True, start_span_cb=start_span_cb) request, response = test_app.get("/test") assert "OK" in str(response.status_code) spans = tracing.tracer.finished_spans() assert len(spans) == 1 assert spans[0].tags.get(tags.COMPONENT, None) == "not-sanic" assert spans[0].tags.get("utag", None) == "uvalue"
from sanic import Sanic from sanic import response from sanic_opentracing import SanicTracing from jaeger_tracer import jaeger_tracer app = Sanic(__name__) _tracing = SanicTracing(tracer=jaeger_tracer, trace_all_requests=True, app=app) @app.route("/example1") async def example1(request): return response.json("example1") @app.route("/example2") async def example2(request): return response.json("example2") @app.route("/log") @_tracing.trace() def log(request): # Extract the span information for request object. with jaeger_tracer.start_active_span( "python webserver internal span of log method") as scope: a = 1 b = 2 c = a + b scope.span.log_kv({"event": "my computer knows math!", "result": c}) return response.json("log")
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 from sanic import Sanic, request, response import opentracing from opentracing.ext import tags from opentracing.mocktracer import MockTracer from sanic_opentracing import SanicTracing import aiohttp app = Sanic(__name__) test_app = app.test_client tracing = SanicTracing(tracer=MockTracer(), app=app, trace_all_requests=True, exceptions=[ RuntimeError, ]) @app.listener('before_server_start') async def init(app, loop): app.aiohttp_session = aiohttp.ClientSession(loop=loop) @app.listener('after_server_stop') def finish(app, loop): loop.run_until_complete(app.aiohttp_session.close()) loop.close()
def test_tracer(self): tracer = opentracing.Tracer() tracing = SanicTracing(tracer=tracer) assert tracing.tracer is tracer assert tracing._trace_all_requests is False
def test_start_span_invalid(self): # start_span_cb need to be callable with pytest.raises(ValueError): SanicTracing(start_span_cb=0)
def test_trace_all_requests_no_app(self): # when trace_all_requests is True, an app object is *required* with pytest.raises(ValueError): SanicTracing(trace_all_requests=True)
from config.tracer import init_tracer from config.service import ServiceManager, service_watcher from sanic_opentracing import SanicTracing config = load_config() appid = config.get("APP_ID", __name__) trace_all = config.get("TRACE_ALL", False) jaeger_host = config.get("JAEGER_HOST", "localhost") app = Sanic(appid, error_handler=CustomHandler()) app.blueprint(swagger_blueprint) app.config = config jaeger_tracer = init_tracer(appid, jaeger_host) tracing = SanicTracing( tracer=jaeger_tracer, trace_all_requests=trace_all, app=app, exceptions=[RuntimeError], ) @app.listener("before_server_start") async def before_server_start(app, loop): config = app.config["DB_CONFIG"] # Config for Mysql Connection config["db"] = config["database"] del config["database"] app.add_task(service_watcher(app, loop)) app.db = await ConnectionPool(loop=loop, trace=True).init(app.config["DB_CONFIG"])