TelemetryΒΆ
This module sets up OpenTelemetry tracing for a service.
ClassesΒΆ
TelemetryConfig ΒΆ
TelemetryConfig(service_name: str, jaeger_endpoint: str | None = None, jaeger_transport: str = 'grpc', local_logging_dir: str | None = None, file_export_name: str | None = None, sqlite_db_name: str | None = None, duckdb_name: str | None = None, duckdb_ttl_days: int | None = None, enable_jaeger: bool = True, enable_file: bool = True, enable_sql: bool = True, enable_duckdb: bool = True, enable_otlp: bool = True, otlp_protocol: str = 'grpc', otlp_endpoint: str = 'http://localhost:4317', batch_processor_options: dict | None = None)
This configuration class sets up OpenTelemetry tracing.
- Export spans via OTLP (gRPC or HTTP).
- Write spans to a file.
- Save spans in a SQLite database.
Only exporters with a non-None configuration will be activated.
:param service_name: Name of your service.
:param jaeger_endpoint: The Jaeger collector gRPC endpoint (e.g., "localhost:14250"). :param file_export_path: If provided, spans will be written to this file. :param sqlite_db_path: If provided, spans will be stored in this SQLite DB. :param duckdb_ttl_days: Delete traces older than this many days (default: None = keep forever). :param batch_processor_options: Dict of options for BatchSpanProcessor (e.g., {"max_export_batch_size": 10}).
Source code in src/flock/logging/telemetry.py
Methods:ΒΆ
setup_tracing ΒΆ
Set up OpenTelemetry tracing with the specified exporters.
Source code in src/flock/logging/telemetry.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
log_exception_to_otel ΒΆ
Log unhandled exceptions to OpenTelemetry.
Source code in src/flock/logging/telemetry.py
Functions:ΒΆ
otlp_from_env ΒΆ
OTLP export as the standard OpenTelemetry variables ask for it.
Returns (enabled, protocol, endpoint) with protocol "grpc" or "http". Export is on when OTEL_EXPORTER_OTLP_TRACES_ENDPOINT or OTEL_EXPORTER_OTLP_ENDPOINT is set. The protocol comes from OTEL_EXPORTER_OTLP_TRACES_PROTOCOL, then OTEL_EXPORTER_OTLP_PROTOCOL: grpc (the default) or http/protobuf. The traces endpoint is used as given; over HTTP the base endpoint gets /v1/traces, as the OpenTelemetry spec says. The exporters read their headers themselves (OTEL_EXPORTER_OTLP_TRACES_HEADERS, OTEL_EXPORTER_OTLP_HEADERS).
Source code in src/flock/logging/telemetry.py
flush_traces ΒΆ
Write the spans still waiting in a batch, waiting at most timeout_millis.
OpenTelemetry's force_flush can block longer than its timeout on a slow exporter, so it runs in a thread of its own that is left behind after the timeout. A later call waits for that flush and then flushes again, for the spans that ended meanwhile. Returns whether the flush finished in time and succeeded; a provider that was shut down has nothing left to write.