|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +from datetime import datetime, timezone |
| 4 | + |
| 5 | +from aiohttp.web import Application, Request, Response, run_app |
| 6 | +from opentelemetry import trace |
| 7 | +from opentelemetry.sdk.resources import Resource |
| 8 | +from opentelemetry.sdk.trace import TracerProvider |
| 9 | + |
| 10 | +from asgi_monitor.integrations.aiohttp import MetricsConfig, TracingConfig, setup_metrics, setup_tracing |
| 11 | +from asgi_monitor.logging import configure_logging |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +async def index(requests: Request) -> Response: |
| 17 | + logger.info("Start sleeping at %s", datetime.now(tz=timezone.utc)) |
| 18 | + await asyncio.sleep(1) |
| 19 | + logger.info("Stopped sleeping at %s", datetime.now(tz=timezone.utc)) |
| 20 | + return Response(text="OK") |
| 21 | + |
| 22 | + |
| 23 | +def create_app() -> Application: |
| 24 | + configure_logging(json_format=True, include_trace=False) |
| 25 | + |
| 26 | + app = Application() |
| 27 | + app.router.add_get("/", index) |
| 28 | + |
| 29 | + resource = Resource.create( |
| 30 | + attributes={ |
| 31 | + "service.name": "aiohttp", |
| 32 | + }, |
| 33 | + ) |
| 34 | + tracer_provider = TracerProvider(resource=resource) |
| 35 | + trace.set_tracer_provider(tracer_provider) |
| 36 | + |
| 37 | + trace_config = TracingConfig(tracer_provider=tracer_provider) |
| 38 | + metrics_config = MetricsConfig(app_name="aiohttp", include_trace_exemplar=True) |
| 39 | + |
| 40 | + setup_metrics(app=app, config=metrics_config) |
| 41 | + setup_tracing(app=app, config=trace_config) |
| 42 | + |
| 43 | + return app |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + app = create_app() |
| 48 | + run_app(app=app, host="127.0.0.1", port=8000) |
0 commit comments