|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Dedicated OTLP exporter for relayed telemetry from supervisors. |
| 5 | +//! |
| 6 | +//! Uses a separate gRPC client to forward pre-enriched trace data to the |
| 7 | +//! configured OTLP collector, bypassing the gateway's own `SdkTracerProvider` |
| 8 | +//! which would overwrite resource attributes. |
| 9 | +
|
| 10 | +use std::sync::Arc; |
| 11 | + |
| 12 | +use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest; |
| 13 | +use prost::Message; |
| 14 | +use tonic::transport::Channel; |
| 15 | +use tracing::{debug, info}; |
| 16 | + |
| 17 | +use opentelemetry_proto::tonic::collector::trace::v1::trace_service_client::TraceServiceClient; |
| 18 | + |
| 19 | +/// Exporter that forwards raw protobuf-encoded trace data to an OTLP collector. |
| 20 | +#[derive(Debug, Clone)] |
| 21 | +pub struct OtelRelayExporter { |
| 22 | + client: TraceServiceClient<Channel>, |
| 23 | +} |
| 24 | + |
| 25 | +impl OtelRelayExporter { |
| 26 | + /// Connect to the OTLP collector at the given gRPC endpoint. |
| 27 | + pub async fn connect(endpoint: &str) -> Result<Self, ConnectError> { |
| 28 | + let channel = Channel::from_shared(endpoint.to_string()) |
| 29 | + .map_err(|e| ConnectError::InvalidUri(e.to_string()))? |
| 30 | + .connect() |
| 31 | + .await |
| 32 | + .map_err(ConnectError::Transport)?; |
| 33 | + Ok(Self { |
| 34 | + client: TraceServiceClient::new(channel), |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + /// Export raw protobuf-encoded `ExportTraceServiceRequest` bytes. |
| 39 | + pub async fn export_raw(&self, trace_data: Vec<u8>) -> Result<(), ExportError> { |
| 40 | + let request = ExportTraceServiceRequest::decode(trace_data.as_slice()) |
| 41 | + .map_err(ExportError::Decode)?; |
| 42 | + |
| 43 | + let mut client = self.client.clone(); |
| 44 | + client |
| 45 | + .export(tonic::Request::new(request)) |
| 46 | + .await |
| 47 | + .map_err(ExportError::Grpc)?; |
| 48 | + |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Debug, thiserror::Error)] |
| 54 | +pub enum ExportError { |
| 55 | + #[error("failed to decode trace data: {0}")] |
| 56 | + Decode(prost::DecodeError), |
| 57 | + #[error("gRPC export failed: {0}")] |
| 58 | + Grpc(tonic::Status), |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Debug, thiserror::Error)] |
| 62 | +pub enum ConnectError { |
| 63 | + #[error("invalid OTLP endpoint URI: {0}")] |
| 64 | + InvalidUri(String), |
| 65 | + #[error("transport error: {0}")] |
| 66 | + Transport(tonic::transport::Error), |
| 67 | +} |
| 68 | + |
| 69 | +/// Create a relay exporter from the gateway's OTLP config, if configured. |
| 70 | +pub async fn try_create_exporter( |
| 71 | + config_file: Option<&crate::config_file::ConfigFile>, |
| 72 | +) -> Option<Arc<OtelRelayExporter>> { |
| 73 | + let Some(cf) = config_file else { |
| 74 | + debug!("no config file; OTEL relay disabled"); |
| 75 | + return None; |
| 76 | + }; |
| 77 | + let Some(otlp) = cf.openshell.gateway.otlp.as_ref() else { |
| 78 | + debug!("no [openshell.gateway.otlp] section in config; OTEL relay disabled"); |
| 79 | + return None; |
| 80 | + }; |
| 81 | + match OtelRelayExporter::connect(&otlp.endpoint).await { |
| 82 | + Ok(exporter) => { |
| 83 | + info!(endpoint = %otlp.endpoint, "OTEL relay exporter connected"); |
| 84 | + Some(Arc::new(exporter)) |
| 85 | + } |
| 86 | + Err(e) => { |
| 87 | + tracing::warn!( |
| 88 | + endpoint = %otlp.endpoint, |
| 89 | + error = %e, |
| 90 | + "failed to connect OTEL relay exporter; relay disabled" |
| 91 | + ); |
| 92 | + None |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod tests { |
| 99 | + use super::*; |
| 100 | + |
| 101 | + #[tokio::test] |
| 102 | + async fn connect_rejects_invalid_endpoint_uri() { |
| 103 | + let err = OtelRelayExporter::connect("not a valid uri") |
| 104 | + .await |
| 105 | + .expect_err("an unparsable endpoint must not connect"); |
| 106 | + assert!(matches!(err, ConnectError::InvalidUri(_)), "{err}"); |
| 107 | + } |
| 108 | + |
| 109 | + #[tokio::test] |
| 110 | + async fn export_raw_rejects_undecodable_trace_bytes() { |
| 111 | + // A lazy channel performs no I/O, so the decode failure surfaces |
| 112 | + // before any RPC is attempted and no collector is needed. |
| 113 | + let channel = Channel::from_static("http://127.0.0.1:1").connect_lazy(); |
| 114 | + let exporter = OtelRelayExporter { |
| 115 | + client: TraceServiceClient::new(channel), |
| 116 | + }; |
| 117 | + let err = exporter |
| 118 | + .export_raw(vec![0xff, 0xff, 0xff]) |
| 119 | + .await |
| 120 | + .expect_err("garbage bytes must not decode as ExportTraceServiceRequest"); |
| 121 | + assert!(matches!(err, ExportError::Decode(_)), "{err}"); |
| 122 | + } |
| 123 | +} |
0 commit comments