|
| 1 | + |
| 2 | +## Observability with OpenTelemetry |
| 3 | + |
| 4 | +`TiTiler` provides built-in observability through OpenTelemetry, automatically creating traces for all API endpoints. These traces include detailed spans for key internal operations like data access and image processing, enabling fine-grained performance analysis and debugging. |
| 5 | + |
| 6 | +This instrumentation works seamlessly with other OpenTelemetry libraries, such as FastAPIInstrumentor, to provide a complete, end-to-end view of your application's performance, from incoming request to final response. |
| 7 | + |
| 8 | +### Installation |
| 9 | + |
| 10 | +To enable telemetry, you must install titiler.core with the [telemetry] extra. This ensures all necessary OpenTelemetry packages are installed. |
| 11 | + |
| 12 | +```bash |
| 13 | +python -m pip install -U pip |
| 14 | + |
| 15 | +# From Pypi |
| 16 | +python -m pip install titiler.core[telemetry] |
| 17 | + |
| 18 | +# Or from sources |
| 19 | +git clone https://github.com/developmentseed/titiler.git |
| 20 | +cd titiler && python -m pip install -e src/titiler/core[telemetry] |
| 21 | +``` |
| 22 | + |
| 23 | +### Configuration |
| 24 | + |
| 25 | +To export traces, you need to configure your application to send them to an observability platform (like Jaeger or Datadog) using an OTLP Exporter. |
| 26 | + |
| 27 | +The following example demonstrates how to set up a tracer provider that exports data via the OTLP protocol over HTTP. This setup is typically done once when your application starts. |
| 28 | + |
| 29 | +```python |
| 30 | +# In your main application file, e.g., main.py |
| 31 | + |
| 32 | +import os |
| 33 | +from fastapi import FastAPI |
| 34 | +from opentelemetry import trace |
| 35 | +from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter |
| 36 | +from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor |
| 37 | +from opentelemetry.sdk.resources import SERVICE_NAME, SERVICE_VERSION, Resource |
| 38 | +from opentelemetry.sdk.trace import TracerProvider |
| 39 | +from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 40 | + |
| 41 | +from titiler.core.factory import TilerFactory |
| 42 | + |
| 43 | +# --- OpenTelemetry Configuration --- |
| 44 | + |
| 45 | +# Define a "Resource" for your application. |
| 46 | +# This adds metadata to your traces, like the service name and version. |
| 47 | +resource = Resource.create( |
| 48 | + { |
| 49 | + SERVICE_NAME: os.getenv("OTEL_SERVICE_NAME", "titiler"), |
| 50 | + SERVICE_VERSION: "0.1", |
| 51 | + } |
| 52 | +) |
| 53 | + |
| 54 | +# Create a "TracerProvider" with the defined resource. |
| 55 | +# The provider manages the creation of tracers. |
| 56 | +provider = TracerProvider(resource=resource) |
| 57 | + |
| 58 | +# Configure an "Exporter" to send telemetry data. |
| 59 | +# The OTLPSpanExporter sends data to an OTLP-compatible endpoint. |
| 60 | +# By default, it reads the endpoint from the OTEL_EXPORTER_OTLP_ENDPOINT |
| 61 | +# environment variable. The default for HTTP is http://localhost:4318. |
| 62 | +exporter = OTLPSpanExporter() |
| 63 | + |
| 64 | +# Use a "BatchSpanProcessor" to send spans in the background. |
| 65 | +# This is the recommended processor for production. |
| 66 | +processor = BatchSpanProcessor(exporter) |
| 67 | +provider.add_span_processor(processor) |
| 68 | + |
| 69 | +# Set the configured provider as the global tracer provider. |
| 70 | +trace.set_tracer_provider(provider) |
| 71 | + |
| 72 | +# --- FastAPI Application Setup --- |
| 73 | +app = FastAPI(title="My TiTiler App") |
| 74 | + |
| 75 | +# Instrument the FastAPI application. |
| 76 | +# This adds middleware to trace requests, responses, and exceptions, |
| 77 | +# complementing TiTiler's internal endpoint tracing. |
| 78 | +FastAPIInstrumentor.instrument_app(app) |
| 79 | + |
| 80 | +# Add your TiTiler endpoints |
| 81 | +cog = TilerFactory() |
| 82 | +app.include_router(cog.router) |
| 83 | +``` |
0 commit comments