|  | 
| 86 | 86 |     ENABLE_PROMETHEUS, | 
| 87 | 87 |     LDAP_ADMIN_USERNAME, | 
| 88 | 88 |     OAUTH2_APPLICATION_CLIENT_ID, | 
|  | 89 | +    OTEL_EXPORTER_OTLP_ENDPOINT, | 
| 89 | 90 |     REDIS_URL, | 
| 90 | 91 |     REQUESTS_BASE_PATH, | 
| 91 | 92 |     REQUIRE_USER_AUTHENTICATION, | 
| @@ -344,6 +345,43 @@ def make_app(template_dir: str = None, update_routes: bool = False): | 
| 344 | 345 |         (r'/version-control', MainPageHandler), | 
| 345 | 346 |     ] | 
| 346 | 347 | 
 | 
|  | 348 | +    if ENABLE_PROMETHEUS or OTEL_EXPORTER_OTLP_ENDPOINT: | 
|  | 349 | +        from opentelemetry.instrumentation.tornado import TornadoInstrumentor | 
|  | 350 | +        TornadoInstrumentor().instrument() | 
|  | 351 | +        logger.info('OpenTelemetry instrumentation enabled.') | 
|  | 352 | + | 
|  | 353 | +    if OTEL_EXPORTER_OTLP_ENDPOINT: | 
|  | 354 | +        logger.info(f'OTEL_EXPORTER_OTLP_ENDPOINT: {OTEL_EXPORTER_OTLP_ENDPOINT}') | 
|  | 355 | + | 
|  | 356 | +        from opentelemetry import trace | 
|  | 357 | +        from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | 
|  | 358 | +            OTLPSpanExporter, | 
|  | 359 | +        ) | 
|  | 360 | +        from opentelemetry.sdk.resources import Resource | 
|  | 361 | +        from opentelemetry.sdk.trace import TracerProvider | 
|  | 362 | +        from opentelemetry.sdk.trace.export import BatchSpanProcessor | 
|  | 363 | + | 
|  | 364 | +        service_name = "mage-ai-server" | 
|  | 365 | +        resource = Resource(attributes={ | 
|  | 366 | +            "service.name": service_name, | 
|  | 367 | +        }) | 
|  | 368 | + | 
|  | 369 | +        # Set up a TracerProvider and attach an OTLP exporter to it | 
|  | 370 | +        trace.set_tracer_provider(TracerProvider(resource=resource)) | 
|  | 371 | +        tracer_provider = trace.get_tracer_provider() | 
|  | 372 | + | 
|  | 373 | +        # Configure OTLP exporter | 
|  | 374 | +        otlp_exporter = OTLPSpanExporter( | 
|  | 375 | +            # Endpoint of your OpenTelemetry Collector | 
|  | 376 | +            endpoint=OTEL_EXPORTER_OTLP_ENDPOINT, | 
|  | 377 | +            # Use insecure channel if your collector does not support TLS | 
|  | 378 | +            insecure=True | 
|  | 379 | +        ) | 
|  | 380 | + | 
|  | 381 | +        # Attach the OTLP exporter to the TracerProvider | 
|  | 382 | +        span_processor = BatchSpanProcessor(otlp_exporter) | 
|  | 383 | +        tracer_provider.add_span_processor(span_processor) | 
|  | 384 | + | 
| 347 | 385 |     if ENABLE_PROMETHEUS: | 
| 348 | 386 |         from opentelemetry import metrics | 
| 349 | 387 |         from opentelemetry.exporter.prometheus import PrometheusMetricReader | 
|  | 
0 commit comments