Skip to content

Commit

Permalink
nearly there
Browse files Browse the repository at this point in the history
  • Loading branch information
chaen committed May 17, 2024
1 parent 18b7dfc commit 608ed35
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
49 changes: 41 additions & 8 deletions diracx-routers/src/diracx/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, Response
from fastapi.routing import APIRoute
from opentelemetry import metrics, trace

# https://opentelemetry.io/blog/2023/logs-collection/
# https://github.com/mhausenblas/ref.otel.help/blob/main/how-to/logs-collection/yoda/main.py
from opentelemetry import _logs, metrics, trace
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
OTLPSpanExporter as OTLPSpanExporterGRPC,
)
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor
from opentelemetry.instrumentation.logging.constants import DEFAULT_LOGGING_FORMAT

# from opentelemetry.sdk._logs.export import ConsoleLogExporter
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
PeriodicExportingMetricReader,
Expand Down Expand Up @@ -62,13 +70,18 @@
OTEL_GRPC_ENDPOINT = os.environ.get(
"OTEL_GRPC_ENDPOINT", "diracx-demo-opentelemetry-collector:4317"
)
# OTEL_GRPC_ENDPOINT = "172.18.0.2:4317"
OTEL_GRPC_ENDPOINT = "diracx-demo-opentelemetry-collector:4317"


def instrument_otel(app: ASGIApp, app_name: str, log_correlation: bool = True) -> None:
# Setting jaeger
# set the service name to show in traces
resource = Resource.create(attributes={"service.name": app_name})
resource = Resource.create(
attributes={
"service.name": app_name,
"service.instance.id": os.uname().nodename,
}
)

# set the tracer provider
tracer_provider = TracerProvider(resource=resource)
Expand Down Expand Up @@ -96,14 +109,31 @@ def instrument_otel(app: ASGIApp, app_name: str, log_correlation: bool = True) -

# # override logger format which with trace id and span id
if log_correlation:
LoggingInstrumentor().instrument(set_logging_format=True)

LoggingInstrumentor().instrument(set_logging_format=False)
logger_provider = LoggerProvider(resource=resource)
_logs.set_logger_provider(logger_provider)

otlp_exporter = OTLPLogExporter(endpoint=OTEL_GRPC_ENDPOINT, insecure=True)
logger_provider.add_log_record_processor(BatchLogRecordProcessor(otlp_exporter))

# console_exporter = ConsoleLogExporter()
# logger_provider.add_log_record_processor(BatchLogRecordProcessor(console_exporter))

handler = LoggingHandler(level=logging.DEBUG, logger_provider=logger_provider)
# Add the handler to all diracx and uvicorn logger
# uvicorn.access
for logger_name in logging.root.manager.loggerDict:
if "diracx" in logger_name or logger_name == "uvicorn.access":
print(f"CHRIS SETTING FOR {logger_name}")
logging.getLogger(logger_name).addHandler(handler)
# logging.getLogger().addHandler(handler)
pass
# 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))
# logger = logging.getLogger("uvicorn.access")
# for hl in logger.handlers:
# hl.setFormatter(logging.Formatter(DEFAULT_LOGGING_FORMAT))

# FastAPIInstrumentor.instrument_app(
# app, tracer_provider=tracer_provider, meter_provider=meter_provider
Expand Down Expand Up @@ -266,6 +296,9 @@ def create_app_inner(
)

print(f"CHRIS LOGS { sorted(logging.root.manager.loggerDict)}")
import time

time.sleep(2)

instrument_otel(app, APP_NAME)

Expand Down
5 changes: 4 additions & 1 deletion diracx-routers/src/diracx/routers/auth/well_known.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ async def openid_configuration(
settings: AuthSettings,
):
"""OpenID Connect discovery endpoint."""
logger.info("CHRIS ICI")
print(
f"MEEEERDE{logger.name} {logger.handlers=} {logging.getLogger().handlers=} {logging.getLogger('uvicorn.access').handlers=}"
)
logger.info(f"CHRIS ICI {logger.name}")
scopes_supported = []
for vo in config.Registry:
scopes_supported.append(f"vo:{vo}")
Expand Down

0 comments on commit 608ed35

Please sign in to comment.