diff --git a/diracx-routers/src/diracx/routers/__init__.py b/diracx-routers/src/diracx/routers/__init__.py index 26fdaf46..e822ebf4 100644 --- a/diracx-routers/src/diracx/routers/__init__.py +++ b/diracx-routers/src/diracx/routers/__init__.py @@ -14,7 +14,11 @@ 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, @@ -22,6 +26,10 @@ 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, @@ -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) @@ -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 @@ -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) diff --git a/diracx-routers/src/diracx/routers/auth/well_known.py b/diracx-routers/src/diracx/routers/auth/well_known.py index 99772aec..727bb6c9 100644 --- a/diracx-routers/src/diracx/routers/auth/well_known.py +++ b/diracx-routers/src/diracx/routers/auth/well_known.py @@ -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}")