opentelemetry-lambda-extension 0.1.7

AWS Lambda extension for collecting and exporting OpenTelemetry signals
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! OTLP signal exporter with retry and fallback.
//!
//! This module provides the flusher for exporting OTLP signals to a remote endpoint.
//! It includes retry logic with exponential backoff and stdout JSON fallback on failure.

use crate::aggregator::BatchedSignal;
use crate::config::{Compression, ExporterConfig, Protocol};
use prost::Message;
use reqwest::Client;
use serde::Serialize;
use std::io::Write;
use std::time::Duration;

const MAX_RETRIES: u32 = 3;
const INITIAL_BACKOFF: Duration = Duration::from_millis(50);

/// Result of an export operation.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExportResult {
    /// Export succeeded.
    Success,
    /// Export failed after retries, data written to stdout.
    Fallback,
    /// Export failed and no data was sent (no endpoint configured).
    Skipped,
}

/// Error during export.
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum ExportError {
    /// HTTP request failed.
    #[error("HTTP request failed")]
    Http(#[from] reqwest::Error),

    /// Server returned an error status.
    #[error("server returned {status}: {body}")]
    Status {
        /// HTTP status code returned by server.
        status: u16,
        /// Response body from server.
        body: String,
    },

    /// Encoding failed.
    #[error("failed to encode request")]
    Encode(#[source] Box<dyn std::error::Error + Send + Sync>),

    /// No endpoint configured.
    #[error("no endpoint configured")]
    NoEndpoint,
}

impl ExportError {
    pub(crate) fn encode<E: std::error::Error + Send + Sync + 'static>(error: E) -> Self {
        Self::Encode(Box::new(error))
    }

    pub(crate) fn status(status: u16, body: impl Into<String>) -> Self {
        Self::Status {
            status,
            body: body.into(),
        }
    }
}

/// OTLP exporter for sending signals to a remote endpoint.
pub struct OtlpExporter {
    config: ExporterConfig,
    client: Client,
}

impl OtlpExporter {
    /// Creates a new OTLP exporter with the given configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP client cannot be created.
    pub fn new(config: ExporterConfig) -> Result<Self, ExportError> {
        let client = Client::builder()
            .timeout(config.timeout)
            .build()
            .map_err(ExportError::Http)?;

        Ok(Self { config, client })
    }

    /// Creates a new exporter with default configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP client cannot be created.
    pub fn with_defaults() -> Result<Self, ExportError> {
        Self::new(ExporterConfig::default())
    }

    /// Exports a batch of signals.
    ///
    /// This method handles retries and fallback to stdout on failure.
    pub async fn export(&self, batch: BatchedSignal) -> ExportResult {
        if self.config.endpoint.is_none() {
            tracing::debug!("No endpoint configured, skipping export");
            return ExportResult::Skipped;
        }

        let result = self.export_with_retry(&batch).await;

        match result {
            Ok(()) => ExportResult::Success,
            Err(e) => {
                tracing::warn!(error = %e, "Export failed after retries, falling back to stdout");
                self.emit_to_stdout(&batch);
                ExportResult::Fallback
            }
        }
    }

    async fn export_with_retry(&self, batch: &BatchedSignal) -> Result<(), ExportError> {
        let mut last_error = None;
        let mut backoff = INITIAL_BACKOFF;

        for attempt in 0..MAX_RETRIES {
            match self.try_export(batch).await {
                Ok(()) => return Ok(()),
                Err(ExportError::Status { status, ref body }) if !Self::is_retryable(status) => {
                    tracing::error!(status, "Received non-retryable status code, not retrying");
                    return Err(ExportError::status(status, body.clone()));
                }
                Err(e) => {
                    tracing::warn!(
                        attempt = attempt + 1,
                        max_retries = MAX_RETRIES,
                        error = %e,
                        "Export attempt failed"
                    );
                    last_error = Some(e);

                    if attempt + 1 < MAX_RETRIES {
                        tokio::time::sleep(backoff).await;
                        backoff *= 2;
                    }
                }
            }
        }

        Err(last_error.unwrap_or(ExportError::NoEndpoint))
    }

    /// Determines if a status code is retryable per OTLP specification.
    ///
    /// Retryable: 408 (Request Timeout), 429 (Too Many Requests), 5xx (Server Errors)
    /// Non-retryable: 400, 401, 403, 404, and other 4xx client errors
    fn is_retryable(status: u16) -> bool {
        matches!(status, 408 | 429) || (500..600).contains(&status)
    }

    async fn try_export(&self, batch: &BatchedSignal) -> Result<(), ExportError> {
        let endpoint = self
            .config
            .endpoint
            .as_ref()
            .ok_or(ExportError::NoEndpoint)?;

        let (path, body, content_type) = match batch {
            BatchedSignal::Traces(req) => {
                ("/v1/traces", self.encode_request(req)?, self.content_type())
            }
            BatchedSignal::Metrics(req) => (
                "/v1/metrics",
                self.encode_request(req)?,
                self.content_type(),
            ),
            BatchedSignal::Logs(req) => {
                ("/v1/logs", self.encode_request(req)?, self.content_type())
            }
        };

        let url = format!("{}{}", endpoint, path);

        let mut request = self
            .client
            .post(&url)
            .header("Content-Type", content_type)
            .body(body);

        for (key, value) in &self.config.headers {
            request = request.header(key, value);
        }

        if self.config.compression == Compression::Gzip {
            request = request.header("Content-Encoding", "gzip");
        }

        let response = request.send().await.map_err(ExportError::Http)?;

        let status = response.status();
        if status.is_success() {
            Ok(())
        } else {
            let body = response.text().await.unwrap_or_default();
            Err(ExportError::status(status.as_u16(), body))
        }
    }

    fn encode_request<T: Message>(&self, request: &T) -> Result<Vec<u8>, ExportError> {
        let mut buf = Vec::with_capacity(request.encoded_len());
        request.encode(&mut buf).map_err(ExportError::encode)?;

        if self.config.compression == Compression::Gzip {
            use flate2::Compression as GzCompression;
            use flate2::write::GzEncoder;

            let mut encoder = GzEncoder::new(Vec::new(), GzCompression::default());
            encoder.write_all(&buf).map_err(ExportError::encode)?;
            encoder.finish().map_err(ExportError::encode)
        } else {
            Ok(buf)
        }
    }

    fn content_type(&self) -> &'static str {
        match self.config.protocol {
            Protocol::Http => "application/x-protobuf",
            Protocol::Grpc => "application/grpc",
        }
    }

    fn emit_to_stdout(&self, batch: &BatchedSignal) {
        use std::io::Write as _;

        let fallback = match batch {
            BatchedSignal::Traces(req) => OtlpFallback {
                otlp_fallback: OtlpFallbackData {
                    signal_type: "traces",
                    request: serde_json::to_value(req).unwrap_or_default(),
                },
            },
            BatchedSignal::Metrics(req) => OtlpFallback {
                otlp_fallback: OtlpFallbackData {
                    signal_type: "metrics",
                    request: serde_json::to_value(req).unwrap_or_default(),
                },
            },
            BatchedSignal::Logs(req) => OtlpFallback {
                otlp_fallback: OtlpFallbackData {
                    signal_type: "logs",
                    request: serde_json::to_value(req).unwrap_or_default(),
                },
            },
        };

        if let Ok(json) = serde_json::to_string(&fallback) {
            // Use explicit I/O to handle broken pipes gracefully
            let mut stdout = std::io::stdout().lock();
            let _ = writeln!(stdout, "{}", json);
        }
    }

    /// Returns whether an endpoint is configured.
    pub fn has_endpoint(&self) -> bool {
        self.config.endpoint.is_some()
    }

    /// Returns the configured endpoint URL.
    pub fn endpoint(&self) -> Option<&str> {
        self.config.endpoint.as_deref()
    }
}

#[derive(Serialize)]
struct OtlpFallback<'a> {
    otlp_fallback: OtlpFallbackData<'a>,
}

#[derive(Serialize)]
struct OtlpFallbackData<'a> {
    #[serde(rename = "type")]
    signal_type: &'a str,
    request: serde_json::Value,
}

#[cfg(test)]
mod tests {
    use super::*;
    use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest;
    use opentelemetry_proto::tonic::trace::v1::{ResourceSpans, ScopeSpans, Span};
    use std::error::Error;

    fn make_trace_batch() -> BatchedSignal {
        BatchedSignal::Traces(ExportTraceServiceRequest {
            resource_spans: vec![ResourceSpans {
                scope_spans: vec![ScopeSpans {
                    spans: vec![Span {
                        name: "test-span".to_string(),
                        trace_id: vec![1; 16],
                        span_id: vec![1; 8],
                        ..Default::default()
                    }],
                    ..Default::default()
                }],
                ..Default::default()
            }],
        })
    }

    #[tokio::test]
    async fn test_export_no_endpoint_skips() {
        let exporter = OtlpExporter::with_defaults().unwrap();
        let batch = make_trace_batch();

        let result = exporter.export(batch).await;
        assert_eq!(result, ExportResult::Skipped);
    }

    #[test]
    fn test_has_endpoint() {
        let exporter = OtlpExporter::with_defaults().unwrap();
        assert!(!exporter.has_endpoint());

        let config = ExporterConfig {
            endpoint: Some("http://localhost:4318".to_string()),
            ..Default::default()
        };
        let exporter = OtlpExporter::new(config).unwrap();
        assert!(exporter.has_endpoint());
    }

    #[test]
    fn test_encode_request() {
        let config = ExporterConfig {
            compression: Compression::None,
            ..Default::default()
        };
        let exporter = OtlpExporter::new(config).unwrap();

        let request = ExportTraceServiceRequest::default();
        let encoded = exporter.encode_request(&request);
        assert!(encoded.is_ok());
    }

    #[test]
    fn test_encode_request_with_gzip() {
        let config = ExporterConfig {
            compression: Compression::Gzip,
            ..Default::default()
        };
        let exporter = OtlpExporter::new(config).unwrap();

        let request = ExportTraceServiceRequest::default();
        let encoded = exporter.encode_request(&request);
        assert!(encoded.is_ok());
    }

    #[test]
    fn test_content_type() {
        let config = ExporterConfig {
            protocol: Protocol::Http,
            ..Default::default()
        };
        let exporter = OtlpExporter::new(config).unwrap();
        assert_eq!(exporter.content_type(), "application/x-protobuf");

        let config = ExporterConfig {
            protocol: Protocol::Grpc,
            ..Default::default()
        };
        let exporter = OtlpExporter::new(config).unwrap();
        assert_eq!(exporter.content_type(), "application/grpc");
    }

    #[test]
    fn test_export_error_display() {
        let err = ExportError::NoEndpoint;
        assert_eq!(format!("{}", err), "no endpoint configured");

        let err = ExportError::status(500, "Internal Server Error");
        assert!(format!("{}", err).contains("500"));
        assert!(matches!(err, ExportError::Status { status: 500, .. }));
    }

    #[test]
    fn test_export_error_chain() {
        let io_err = std::io::Error::other("test error");
        let err = ExportError::encode(io_err);

        assert!(err.source().is_some());
        assert!(format!("{}", err).contains("encode"));
    }

    #[test]
    fn test_is_retryable() {
        assert!(OtlpExporter::is_retryable(408));
        assert!(OtlpExporter::is_retryable(429));
        assert!(OtlpExporter::is_retryable(500));
        assert!(OtlpExporter::is_retryable(502));
        assert!(OtlpExporter::is_retryable(503));
        assert!(OtlpExporter::is_retryable(504));

        assert!(!OtlpExporter::is_retryable(400));
        assert!(!OtlpExporter::is_retryable(401));
        assert!(!OtlpExporter::is_retryable(403));
        assert!(!OtlpExporter::is_retryable(404));
        assert!(!OtlpExporter::is_retryable(405));
    }
}