Skip to content

Commit 4c9a0e5

Browse files
committed
add docs
1 parent d9101a6 commit 4c9a0e5

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

docs/src/advanced/telemetry.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
```

src/titiler/application/titiler/application/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,16 +450,16 @@ def conformance(
450450
from opentelemetry import trace
451451
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
452452
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
453-
from opentelemetry.sdk.resources import Resource
453+
from opentelemetry.sdk.resources import SERVICE_NAME, SERVICE_VERSION, Resource
454454
from opentelemetry.sdk.trace import TracerProvider
455455
from opentelemetry.sdk.trace.export import BatchSpanProcessor
456456

457457
FastAPIInstrumentor.instrument_app(app)
458458

459459
resource = Resource.create(
460460
{
461-
"service.name": "titiler",
462-
"service.version": titiler_version,
461+
SERVICE_NAME: "titiler",
462+
SERVICE_VERSION: titiler_version,
463463
}
464464
)
465465

src/titiler/core/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ titiler/
5959
├── middleware.py - Starlette middlewares
6060
├── factory.py - Dynamic tiler endpoints factories
6161
├── routing.py - Custom APIRoute class
62+
├── telemetry.py - OpenTelemetry tracing functions
6263
└── utils.py - Titiler utility functions
6364
```

0 commit comments

Comments
 (0)