Ejemplo n.º 1
0
def app(elasticapm_client):
    class HelloHandler(tornado.web.RequestHandler):
        def get(self):
            with async_capture_span("test"):
                pass
            return self.write("Hello, world")

        post = get

    class RenderHandler(tornado.web.RequestHandler):
        def get(self):
            with async_capture_span("test"):
                pass
            items = ["Item 1", "Item 2", "Item 3"]
            return self.render("test.html",
                               title="Testing so hard",
                               items=items)

    class BoomHandler(tornado.web.RequestHandler):
        def get(self):
            raise tornado.web.HTTPError()

        post = get

    app = tornado.web.Application(
        [(r"/", HelloHandler), (r"/boom", BoomHandler),
         (r"/render", RenderHandler)],
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
    )
    apm = ElasticAPM(app, elasticapm_client)
    yield app
    elasticapm.uninstrument()
    def stop(self):
        if self.counter:
            self.counter.stop()

        if self.apm_client:
            elasticapm.uninstrument()
        super().stop()
Ejemplo n.º 3
0
def django_sending_elasticapm_client(request, validating_httpserver):
    validating_httpserver.serve_content(
        code=202, content="", headers={"Location": "http://example.com/foo"})
    client_config = getattr(request, "param", {})
    client_config.setdefault("server_url", validating_httpserver.url)
    client_config.setdefault("service_name", "app")
    client_config.setdefault("secret_token", "secret")
    client_config.setdefault("transport_class",
                             "elasticapm.transport.http.Transport")
    client_config.setdefault("span_frames_min_duration", -1)
    app = apps.get_app_config("elasticapm.contrib.django")
    old_client = app.client
    client = DjangoClient(**client_config)
    register_handlers(client)
    instrument(client)
    app.client = client
    client.httpserver = validating_httpserver
    yield client
    client.close()
    elasticapm.uninstrument()

    app.client = old_client

    if old_client:
        register_handlers(old_client)
        instrument(old_client)
Ejemplo n.º 4
0
def app_static_files_only(elasticapm_client):
    app = Starlette()
    app.add_middleware(ElasticAPM, client=elasticapm_client)
    app.mount("/tmp", StaticFiles(directory=file_path), name="static")

    yield app

    elasticapm.uninstrument()
Ejemplo n.º 5
0
def app(elasticapm_client):
    app = Starlette()
    sub = Starlette()
    subsub = Starlette()
    app.mount("/sub", sub)
    sub.mount("/subsub", subsub)

    @app.route("/", methods=["GET", "POST"])
    async def hi(request):
        await request.body()
        with async_capture_span("test"):
            pass
        return PlainTextResponse("ok")

    @app.route("/hi/{name}", methods=["GET"])
    async def hi_name(request):
        name = request.path_params["name"]
        return PlainTextResponse("Hi {}".format(name))

    @app.route("/hello", methods=["GET", "POST"])
    async def hello(request):
        with async_capture_span("test"):
            pass
        return PlainTextResponse("ok")

    @app.route("/raise-exception", methods=["GET", "POST"])
    async def raise_exception(request):
        await request.body()
        raise ValueError()

    @app.route("/hi/{name}/with/slash/", methods=["GET", "POST"])
    async def with_slash(request):
        return PlainTextResponse("Hi {}".format(request.path_params["name"]))

    @app.route("/hi/{name}/without/slash", methods=["GET", "POST"])
    async def without_slash(request):
        return PlainTextResponse("Hi {}".format(request.path_params["name"]))

    @sub.route("/hi")
    async def hi_from_sub(request):
        return PlainTextResponse("sub")

    @subsub.route("/hihi/{name}")
    async def hi_from_sub(request):
        return PlainTextResponse(request.path_params["name"])

    @app.websocket_route("/ws")
    async def ws(websocket):
        await websocket.accept()
        await websocket.send_text("Hello, world!")
        await websocket.close()

    app.add_middleware(ElasticAPM, client=elasticapm_client)

    yield app

    elasticapm.uninstrument()
Ejemplo n.º 6
0
def flask_apm_client(request, flask_app, elasticapm_client):
    client_config = getattr(request, "param", {})
    client_config.setdefault("app", flask_app)
    client_config.setdefault("client", elasticapm_client)
    client = ElasticAPM(**client_config)
    try:
        yield client
    finally:
        elasticapm.uninstrument()
        signals.request_started.disconnect(client.request_started)
        signals.request_finished.disconnect(client.request_finished)
        # remove logging handler if it was added
        logger = logging.getLogger()
        for handler in list(logger.handlers):
            if getattr(handler, "client", None) is client.client:
                logger.removeHandler(handler)
Ejemplo n.º 7
0
def aioeapm(elasticapm_client):
    async def hello(request):
        with async_capture_span("test"):
            pass
        return aiohttp.web.Response(body=b"Hello, world")

    async def boom(request):
        raise aiohttp.web.HTTPInternalServerError(headers={"boom": "boom"})

    app = aiohttp.web.Application()
    app.router.add_route("GET", "/", hello)
    app.router.add_route("GET", "/hello", hello)
    app.router.add_route("GET", "/boom", boom)
    apm = ElasticAPM(app, elasticapm_client)
    yield apm

    elasticapm.uninstrument()
Ejemplo n.º 8
0
def django_elasticapm_client(request):
    client_config = getattr(request, "param", {})
    client_config.setdefault("service_name", "app")
    client_config.setdefault("secret_token", "secret")
    client_config.setdefault("span_frames_min_duration", -1)
    app = apps.get_app_config("elasticapm")
    old_client = app.client
    client = TempStoreClient(**client_config)
    register_handlers(client)
    instrument(client)
    app.client = client
    yield client
    client.close()
    elasticapm.uninstrument()

    app.client = old_client

    if old_client:
        register_handlers(old_client)
        instrument(old_client)
Ejemplo n.º 9
0
def sanic_app(elasticapm_client):
    app = Sanic(name="elastic-apm")
    apm = ElasticAPM(app=app, client=elasticapm_client)
    TestManager(app=app)

    bp = Blueprint(name="test", url_prefix="/apm", version="v1")

    @app.exception(Exception)
    def handler(request, exception):
        return json({"response": str(exception)}, 500)

    @bp.post("/unhandled-exception")
    async def raise_something(request):
        raise CustomException("Unhandled")

    @app.route("/", methods=["GET", "POST"])
    def default_route(request: Request):
        with async_capture_span("test"):
            pass
        return json({"response": "ok"})

    @app.get("/greet/<name:str>")
    async def greet_person(request: Request, name: str):
        return json({"response": f"Hello {name}"})

    @app.get("/capture-exception")
    async def capture_exception(request):
        try:
            1 / 0
        except ZeroDivisionError:
            await apm.capture_exception()
        return json({"response": "invalid"}, 500)

    app.blueprint(blueprint=bp)

    try:
        yield app
    finally:
        elasticapm.uninstrument()
Ejemplo n.º 10
0
def app(elasticapm_client):
    app = Starlette()

    @app.route("/", methods=["GET", "POST"])
    async def hi(request):
        with async_capture_span("test"):
            pass
        return PlainTextResponse("ok")

    @app.route("/hi/{name}", methods=["GET"])
    async def hi_name(request):
        name = request.path_params["name"]
        return PlainTextResponse("Hi {}".format(name))

    @app.route("/hello", methods=["GET", "POST"])
    async def hello(request):
        with async_capture_span("test"):
            pass
        return PlainTextResponse("ok")

    @app.route("/raise-exception", methods=["GET", "POST"])
    async def raise_exception(request):
        raise ValueError()

    @app.route("/hi/{name}/with/slash/", methods=["GET", "POST"])
    async def with_slash(request):
        return PlainTextResponse("Hi {}".format(request.path_params["name"]))

    @app.route("/hi/{name}/without/slash", methods=["GET", "POST"])
    async def without_slash(request):
        return PlainTextResponse("Hi {}".format(request.path_params["name"]))

    app.add_middleware(ElasticAPM, client=elasticapm_client)

    yield app

    elasticapm.uninstrument()
Ejemplo n.º 11
0
def instrument():
    elasticapm.instrument()
    yield
    elasticapm.uninstrument()
Ejemplo n.º 12
0
 def close(self):
     if self.apm_client:
         elasticapm.uninstrument()
Ejemplo n.º 13
0
def tracer(elasticapm_client):
    yield Tracer(client_instance=elasticapm_client)
    elasticapm.uninstrument()
Ejemplo n.º 14
0
    def close(self):
        if self.counter:
            self.counter.stop()

        if self.apm_client:
            elasticapm.uninstrument()
Ejemplo n.º 15
0
 def stop(self):
     super().stop()
     if self.apm_client:
         elasticapm.uninstrument()
     self.submit_client.stop()