Skip to content

Commit

Permalink
uvicorn has traceid
Browse files Browse the repository at this point in the history
  • Loading branch information
chaen committed Nov 16, 2023
1 parent acb1216 commit c723636
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ install_requires =
opentelemetry-exporter-otlp
opentelemetry-api
opentelemetry-sdk
opentelemetry-instrumentation-asgi



Expand Down
13 changes: 12 additions & 1 deletion src/diracx/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor
from opentelemetry.instrumentation.logging.constants import DEFAULT_LOGGING_FORMAT
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
ConsoleMetricExporter,
Expand Down Expand Up @@ -93,6 +94,13 @@ def instrument_otel(app: ASGIApp, app_name: str, log_correlation: bool = True) -
if log_correlation:
LoggingInstrumentor().instrument(set_logging_format=True)

# CHRIS HACK
# manually change the logging formater of uvicorn as it is
# not reached by the LoggingInstrumentor
logger = logging.getLogger("uvicorn.access")
for hl in logger.handlers:
hl.setFormatter(logging.Formatter(DEFAULT_LOGGING_FORMAT))

FastAPIInstrumentor.instrument_app(
app, tracer_provider=tracer, meter_provider=provider
)
Expand All @@ -113,6 +121,7 @@ def create_app_inner(
config_source: ConfigSource,
) -> DiracFastAPI:
app = DiracFastAPI()
instrument_otel(app, APP_NAME)

# Find which settings classes are available and add them to dependency_overrides
available_settings_classes: set[type[ServiceSettingsBase]] = set()
Expand Down Expand Up @@ -242,7 +251,9 @@ def create_app_inner(
allow_headers=["*"],
)

instrument_otel(app, APP_NAME)
print(f"CHRIS LOGS { sorted(logging.root.manager.loggerDict)}")

# instrument_otel(app, APP_NAME)

return app

Expand Down
4 changes: 4 additions & 0 deletions src/diracx/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import base64
import hashlib
import json
import logging
import os
import re
import secrets
Expand Down Expand Up @@ -52,6 +53,8 @@
)
from .fastapi_classes import DiracxRouter

logger = logging.getLogger(__name__)

oidc_scheme = OpenIdConnect(openIdConnectUrl="/.well-known/openid-configuration")


Expand Down Expand Up @@ -1017,6 +1020,7 @@ class UserInfoResponse(TypedDict):
async def userinfo(
user_info: Annotated[AuthorizedUserInfo, Depends(verify_dirac_access_token)]
) -> UserInfoResponse:
logger.info("CHRIS YEAH BABY")
return {
"sub": user_info.sub,
"vo": user_info.vo,
Expand Down

0 comments on commit c723636

Please sign in to comment.