|
| 1 | +/* |
| 2 | + * Parseable Server (C) 2022 - 2025 Parseable, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +use opentelemetry_otlp::SpanExporter; |
| 20 | +use opentelemetry_sdk::{ |
| 21 | + Resource, |
| 22 | + propagation::TraceContextPropagator, |
| 23 | + trace::{BatchSpanProcessor, SdkTracerProvider}, |
| 24 | +}; |
| 25 | + |
| 26 | +const OTEL_EXPORTER_OTLP_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_ENDPOINT"; |
| 27 | +const OTEL_EXPORTER_OTLP_PROTOCOL: &str = "OTEL_EXPORTER_OTLP_PROTOCOL"; |
| 28 | + |
| 29 | +/// Initialise an OTLP tracer provider. |
| 30 | +/// |
| 31 | +/// **Required env var:** |
| 32 | +/// - `OTEL_EXPORTER_OTLP_ENDPOINT` — collector address. |
| 33 | +/// For HTTP exporters the SDK appends the signal path automatically: |
| 34 | +/// e.g. `http://localhost:4318` → `http://localhost:4318/v1/traces`. |
| 35 | +/// Set a signal-specific var `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` to |
| 36 | +/// supply a full URL without any path suffix being added. |
| 37 | +/// |
| 38 | +/// **Optional env vars (all read by the SDK automatically):** |
| 39 | +/// - `OTEL_EXPORTER_OTLP_PROTOCOL` — transport + serialisation (default: `http/json`): |
| 40 | +/// - `grpc` → gRPC / tonic (Jaeger, Tempo, …) |
| 41 | +/// - `http/json` → HTTP + JSON (Parseable OSS ingest at `/v1/traces`) |
| 42 | +/// - `http/protobuf` → HTTP + protobuf |
| 43 | +/// - `OTEL_EXPORTER_OTLP_HEADERS` — comma-separated `key=value` pairs forwarded |
| 44 | +/// as gRPC metadata or HTTP headers, e.g. |
| 45 | +/// `authorization=Basic <token>,x-p-stream=my-stream,x-p-log-source=otel-traces` |
| 46 | +/// |
| 47 | +/// Returns `None` when `OTEL_EXPORTER_OTLP_ENDPOINT` is not set (OTEL disabled). |
| 48 | +/// The caller must call `provider.shutdown()` before process exit. |
| 49 | +pub fn init_tracing() -> Option<SdkTracerProvider> { |
| 50 | + // Only used to decide whether OTEL is enabled; the SDK reads it again |
| 51 | + // from env to build the exporter (which also appends /v1/traces for HTTP). |
| 52 | + std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT).ok()?; |
| 53 | + |
| 54 | + let protocol = |
| 55 | + std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL).unwrap_or_else(|_| "http/json".to_string()); |
| 56 | + |
| 57 | + // Build the exporter using the SDK's env-var-aware builders. |
| 58 | + // We intentionally do NOT call .with_endpoint() / .with_headers() / |
| 59 | + // .with_metadata() here — the SDK reads OTEL_EXPORTER_OTLP_ENDPOINT and |
| 60 | + // OTEL_EXPORTER_OTLP_HEADERS from the environment automatically, which |
| 61 | + // preserves correct path-appending behaviour for HTTP exporters. |
| 62 | + // |
| 63 | + // The HTTP builder reads OTEL_EXPORTER_OTLP_PROTOCOL to select between |
| 64 | + // http/json (default) and http/protobuf automatically. |
| 65 | + let exporter = match protocol.as_str() { |
| 66 | + // ── gRPC ───────────────────────────────────────────────────────────── |
| 67 | + "grpc" => SpanExporter::builder().with_tonic().build(), |
| 68 | + // ── HTTP/JSON (default) or HTTP/Protobuf ───────────────────────────── |
| 69 | + // The SDK reads OTEL_EXPORTER_OTLP_PROTOCOL from the environment |
| 70 | + // to select between http/json and http/protobuf automatically. |
| 71 | + // Default when OTEL_EXPORTER_OTLP_PROTOCOL is unset is http/json, |
| 72 | + // which is required for Parseable OSS — it only accepts application/json. |
| 73 | + _ => SpanExporter::builder().with_http().build(), |
| 74 | + }; |
| 75 | + |
| 76 | + let exporter = exporter |
| 77 | + .map_err(|e| tracing::warn!("Failed to build OTEL span exporter: {}", e)) |
| 78 | + .ok()?; |
| 79 | + |
| 80 | + let resource = Resource::builder_empty() |
| 81 | + .with_service_name("parseable") |
| 82 | + .build(); |
| 83 | + |
| 84 | + let processor = BatchSpanProcessor::builder(exporter).build(); |
| 85 | + |
| 86 | + let provider = SdkTracerProvider::builder() |
| 87 | + .with_span_processor(processor) |
| 88 | + .with_resource(resource) |
| 89 | + .build(); |
| 90 | + |
| 91 | + opentelemetry::global::set_tracer_provider(provider.clone()); |
| 92 | + |
| 93 | + // Register the W3C TraceContext propagator globally. |
| 94 | + // This is REQUIRED for: |
| 95 | + // - Incoming HTTP header extraction (traceparent/tracestate) |
| 96 | + // - Cross-thread channel propagation via inject/extract |
| 97 | + // Without this, propagator.extract() returns an empty context. |
| 98 | + opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new()); |
| 99 | + |
| 100 | + Some(provider) |
| 101 | +} |
0 commit comments