Skip to content
Snippets Groups Projects
Verified Commit 12fa2692 authored by Leonidas Spyropoulos's avatar Leonidas Spyropoulos
Browse files

feat: optionally enable opentelemetry based on config


Local spawning of the service fails with OpenTelemetry enabled, only enable if config is set

Relates: b730f644

Signed-off-by: default avatarLeonidas Spyropoulos <artafinde@archlinux.org>
parent 935d4920
No related branches found
No related tags found
No related merge requests found
Pipeline #122534 failed
...@@ -14,12 +14,6 @@ from fastapi import FastAPI, HTTPException, Request, Response ...@@ -14,12 +14,6 @@ from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.responses import RedirectResponse from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from jinja2 import TemplateNotFound from jinja2 import TemplateNotFound
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from sqlalchemy import and_ from sqlalchemy import and_
from starlette.exceptions import HTTPException as StarletteHTTPException from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.middleware.authentication import AuthenticationMiddleware from starlette.middleware.authentication import AuthenticationMiddleware
...@@ -51,7 +45,6 @@ async def lifespan(app: FastAPI): ...@@ -51,7 +45,6 @@ async def lifespan(app: FastAPI):
# Setup the FastAPI app. # Setup the FastAPI app.
app = FastAPI(lifespan=lifespan) app = FastAPI(lifespan=lifespan)
# Instrument routes with the prometheus-fastapi-instrumentator # Instrument routes with the prometheus-fastapi-instrumentator
# library with custom collectors and expose /metrics. # library with custom collectors and expose /metrics.
instrumentator().add(prometheus.http_api_requests_total()) instrumentator().add(prometheus.http_api_requests_total())
...@@ -59,15 +52,23 @@ instrumentator().add(prometheus.http_requests_total()) ...@@ -59,15 +52,23 @@ instrumentator().add(prometheus.http_requests_total())
instrumentator().instrument(app) instrumentator().instrument(app)
# Instrument FastAPI for tracing if aurweb.config.get("tracing", "otlp_endpoint"):
FastAPIInstrumentor.instrument_app(app) from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
# Instrument FastAPI for tracing
FastAPIInstrumentor.instrument_app(app)
resource = Resource(attributes={"service.name": "aurweb"}) resource = Resource(attributes={"service.name": "aurweb"})
otlp_endpoint = aurweb.config.get("tracing", "otlp_endpoint") otlp_endpoint = aurweb.config.get("tracing", "otlp_endpoint")
otlp_exporter = OTLPSpanExporter(endpoint=otlp_endpoint) otlp_exporter = OTLPSpanExporter(endpoint=otlp_endpoint)
span_processor = BatchSpanProcessor(otlp_exporter) span_processor = BatchSpanProcessor(otlp_exporter)
trace.set_tracer_provider(TracerProvider(resource=resource)) trace.set_tracer_provider(TracerProvider(resource=resource))
trace.get_tracer_provider().add_span_processor(span_processor) trace.get_tracer_provider().add_span_processor(span_processor)
async def app_startup(): async def app_startup():
......
import fakeredis import fakeredis
from opentelemetry.instrumentation.redis import RedisInstrumentor
from redis import ConnectionPool, Redis from redis import ConnectionPool, Redis
import aurweb.config import aurweb.config
...@@ -8,7 +7,10 @@ from aurweb import aur_logging ...@@ -8,7 +7,10 @@ from aurweb import aur_logging
logger = aur_logging.get_logger(__name__) logger = aur_logging.get_logger(__name__)
pool = None pool = None
RedisInstrumentor().instrument() if aurweb.config.get("tracing", "otlp_endpoint"):
from opentelemetry.instrumentation.redis import RedisInstrumentor
RedisInstrumentor().instrument()
class FakeConnectionPool: class FakeConnectionPool:
......
...@@ -298,12 +298,16 @@ def get_engine(dbname: str = None, echo: bool = False): ...@@ -298,12 +298,16 @@ def get_engine(dbname: str = None, echo: bool = False):
connect_args["check_same_thread"] = False connect_args["check_same_thread"] = False
kwargs = {"echo": echo, "connect_args": connect_args} kwargs = {"echo": echo, "connect_args": connect_args}
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
from sqlalchemy import create_engine from sqlalchemy import create_engine
engine = create_engine(get_sqlalchemy_url(), **kwargs) if aurweb.config.get("tracing", "otlp_endpoint"):
SQLAlchemyInstrumentor().instrument(engine=engine) from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
_engines[dbname] = engine
engine = create_engine(get_sqlalchemy_url(), **kwargs)
SQLAlchemyInstrumentor().instrument(engine=engine)
_engines[dbname] = engine
else:
_engines[dbname] = create_engine(get_sqlalchemy_url(), **kwargs)
if is_sqlite: # pragma: no cover if is_sqlite: # pragma: no cover
setup_sqlite(_engines.get(dbname)) setup_sqlite(_engines.get(dbname))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment