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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Lambda resource attribute detection.
//!
//! This module provides an AWS Lambda resource detector that implements the
//! OpenTelemetry SDK's `ResourceDetector` trait. It detects Lambda runtime
//! environment attributes and builds properly typed `Resource` objects.
//!
//! The detector reads from standard Lambda environment variables and follows
//! OpenTelemetry semantic conventions for cloud and FaaS attributes.

use opentelemetry::{KeyValue, Value};
use opentelemetry_proto::tonic::resource::v1::Resource as ProtoResource;
use opentelemetry_sdk::resource::{Resource, ResourceDetector};
use opentelemetry_semantic_conventions::SCHEMA_URL;
use opentelemetry_semantic_conventions::attribute as semconv_attr;
use opentelemetry_semantic_conventions::resource as semconv_res;
use std::borrow::Cow;
use std::env;

/// Re-export semantic conventions for use by other modules.
pub mod semconv {
    pub use opentelemetry_semantic_conventions::attribute::{
        CLOUD_PLATFORM, CLOUD_PROVIDER, CLOUD_REGION, FAAS_COLDSTART, FAAS_INVOCATION_ID,
        FAAS_MAX_MEMORY, FAAS_NAME, FAAS_TRIGGER, FAAS_VERSION,
    };
    pub use opentelemetry_semantic_conventions::resource::{
        AWS_LOG_GROUP_NAMES, AWS_LOG_STREAM_NAMES, FAAS_INSTANCE, SERVICE_NAME,
        TELEMETRY_SDK_LANGUAGE, TELEMETRY_SDK_NAME, TELEMETRY_SDK_VERSION,
    };
}

/// Environment variable names for Lambda runtime.
const AWS_EXECUTION_ENV: &str = "AWS_EXECUTION_ENV";
const AWS_REGION: &str = "AWS_REGION";
const AWS_LAMBDA_FUNCTION_NAME: &str = "AWS_LAMBDA_FUNCTION_NAME";
const AWS_LAMBDA_FUNCTION_VERSION: &str = "AWS_LAMBDA_FUNCTION_VERSION";
const AWS_LAMBDA_FUNCTION_MEMORY_SIZE: &str = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE";
const AWS_LAMBDA_LOG_GROUP_NAME: &str = "AWS_LAMBDA_LOG_GROUP_NAME";
const AWS_LAMBDA_LOG_STREAM_NAME: &str = "AWS_LAMBDA_LOG_STREAM_NAME";

/// AWS Lambda resource detector.
///
/// Detects Lambda runtime environment attributes from environment variables
/// and returns an OpenTelemetry `Resource` following semantic conventions.
///
/// This detector checks for the `AWS_EXECUTION_ENV` variable to confirm it's
/// running in a Lambda environment before collecting attributes.
///
/// # Environment Variables
///
/// The detector reads the following Lambda environment variables:
/// - `AWS_REGION` - Cloud region
/// - `AWS_LAMBDA_FUNCTION_NAME` - Function name (faas.name)
/// - `AWS_LAMBDA_FUNCTION_VERSION` - Function version (faas.version)
/// - `AWS_LAMBDA_FUNCTION_MEMORY_SIZE` - Memory in MB (converted to bytes for faas.max_memory)
/// - `AWS_LAMBDA_LOG_GROUP_NAME` - CloudWatch log group (may not be available to extensions)
/// - `AWS_LAMBDA_LOG_STREAM_NAME` - CloudWatch log stream (faas.instance, may not be available)
#[derive(Debug, Default)]
pub struct AwsLambdaDetector;

impl AwsLambdaDetector {
    /// Creates a new AWS Lambda detector.
    pub fn new() -> Self {
        Self
    }
}

impl ResourceDetector for AwsLambdaDetector {
    fn detect(&self) -> Resource {
        let execution_env = env::var(AWS_EXECUTION_ENV).ok();
        if !execution_env
            .as_ref()
            .map(|e| e.starts_with("AWS_Lambda_"))
            .unwrap_or(false)
        {
            return Resource::builder_empty().build();
        }

        let mut attributes = vec![
            KeyValue::new(semconv_attr::CLOUD_PROVIDER, "aws"),
            KeyValue::new(semconv_attr::CLOUD_PLATFORM, "aws_lambda"),
        ];

        if let Ok(region) = env::var(AWS_REGION) {
            attributes.push(KeyValue::new(semconv_attr::CLOUD_REGION, region));
        }

        if let Ok(name) = env::var(AWS_LAMBDA_FUNCTION_NAME) {
            attributes.push(KeyValue::new(semconv_attr::FAAS_NAME, name));
        }

        if let Ok(version) = env::var(AWS_LAMBDA_FUNCTION_VERSION) {
            attributes.push(KeyValue::new(semconv_attr::FAAS_VERSION, version));
        }

        if let Ok(memory) = env::var(AWS_LAMBDA_FUNCTION_MEMORY_SIZE)
            && let Ok(mb) = memory.parse::<i64>()
        {
            attributes.push(KeyValue::new(
                semconv_attr::FAAS_MAX_MEMORY,
                mb * 1024 * 1024,
            ));
        }

        if let Ok(log_group) = env::var(AWS_LAMBDA_LOG_GROUP_NAME) {
            use opentelemetry::StringValue;
            attributes.push(KeyValue::new(
                semconv_res::AWS_LOG_GROUP_NAMES,
                Value::Array(vec![StringValue::from(log_group)].into()),
            ));
        }

        if let Ok(log_stream) = env::var(AWS_LAMBDA_LOG_STREAM_NAME) {
            attributes.push(KeyValue::new(semconv_res::FAAS_INSTANCE, log_stream));
        }

        Resource::builder_empty()
            .with_schema_url(attributes.iter().cloned(), Cow::Borrowed(SCHEMA_URL))
            .build()
    }
}

/// Extension-specific resource detector.
///
/// Adds extension-specific attributes that distinguish the extension's
/// telemetry from the Lambda function's telemetry.
#[derive(Debug, Default)]
pub struct ExtensionDetector;

impl ExtensionDetector {
    /// Creates a new extension detector.
    pub fn new() -> Self {
        Self
    }
}

impl ResourceDetector for ExtensionDetector {
    fn detect(&self) -> Resource {
        let attributes = vec![
            KeyValue::new(semconv_res::SERVICE_NAME, "opentelemetry-lambda-extension"),
            KeyValue::new(
                semconv_res::TELEMETRY_SDK_NAME,
                "opentelemetry-lambda-extension",
            ),
            KeyValue::new(semconv_res::TELEMETRY_SDK_LANGUAGE, "rust"),
            KeyValue::new(
                semconv_res::TELEMETRY_SDK_VERSION,
                env!("CARGO_PKG_VERSION"),
            ),
        ];

        Resource::builder_empty()
            .with_attributes(attributes)
            .build()
    }
}

/// Detects Lambda environment and returns a configured Resource.
///
/// This convenience function creates a Resource by running the Lambda
/// detector and extension detector, merging their results.
pub fn detect_resource() -> Resource {
    let lambda_detector = AwsLambdaDetector::new();
    let extension_detector = ExtensionDetector::new();

    Resource::builder_empty()
        .with_detector(Box::new(lambda_detector))
        .with_detector(Box::new(extension_detector))
        .build()
}

/// Converts an SDK Resource to the protobuf Resource type for OTLP export.
pub fn to_proto_resource(resource: &Resource) -> ProtoResource {
    resource.into()
}

/// Builder for customising Lambda resource attributes.
///
/// This builder wraps the SDK's ResourceBuilder and provides a convenient
/// API for adding custom attributes alongside detected ones.
#[derive(Debug)]
#[must_use = "builders do nothing unless .build() is called"]
pub struct ResourceBuilder {
    inner: opentelemetry_sdk::resource::ResourceBuilder,
}

impl ResourceBuilder {
    /// Creates a new resource builder with Lambda detection enabled.
    pub fn new() -> Self {
        Self {
            inner: Resource::builder_empty()
                .with_detector(Box::new(AwsLambdaDetector::new()))
                .with_detector(Box::new(ExtensionDetector::new())),
        }
    }

    /// Detects Lambda environment attributes automatically.
    ///
    /// This is included by default in `new()`, but can be called again
    /// if you've created an empty builder.
    pub fn detect_lambda_environment(self) -> Self {
        Self {
            inner: self
                .inner
                .with_detector(Box::new(AwsLambdaDetector::new()))
                .with_detector(Box::new(ExtensionDetector::new())),
        }
    }

    /// Adds a custom string attribute.
    pub fn add_string(self, key: impl Into<Cow<'static, str>>, value: impl Into<String>) -> Self {
        Self {
            inner: self
                .inner
                .with_attribute(KeyValue::new(key.into(), value.into())),
        }
    }

    /// Adds a custom integer attribute.
    pub fn add_int(self, key: impl Into<Cow<'static, str>>, value: i64) -> Self {
        Self {
            inner: self.inner.with_attribute(KeyValue::new(key.into(), value)),
        }
    }

    /// Adds a custom boolean attribute.
    pub fn add_bool(self, key: impl Into<Cow<'static, str>>, value: bool) -> Self {
        Self {
            inner: self.inner.with_attribute(KeyValue::new(key.into(), value)),
        }
    }

    /// Builds the SDK Resource.
    pub fn build(self) -> Resource {
        self.inner.build()
    }

    /// Builds and converts to protobuf Resource.
    pub fn build_proto(self) -> ProtoResource {
        to_proto_resource(&self.build())
    }
}

impl Default for ResourceBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use opentelemetry_proto::tonic::common::v1::any_value;

    fn get_string_value(resource: &Resource, key: &str) -> Option<String> {
        use opentelemetry::Key;
        let owned_key = Key::from(key.to_owned());
        resource.get(&owned_key).and_then(|v| match v {
            Value::String(s) => Some(s.to_string()),
            _ => None,
        })
    }

    fn get_int_value(resource: &Resource, key: &str) -> Option<i64> {
        use opentelemetry::Key;
        let owned_key = Key::from(key.to_owned());
        resource.get(&owned_key).and_then(|v| match v {
            Value::I64(i) => Some(i),
            _ => None,
        })
    }

    #[test]
    fn test_lambda_detector_outside_lambda() {
        // Without AWS_EXECUTION_ENV, should return empty resource
        temp_env::with_vars([(AWS_EXECUTION_ENV, None::<&str>)], || {
            let detector = AwsLambdaDetector::new();
            let resource = detector.detect();
            assert!(resource.is_empty());
        });
    }

    #[test]
    fn test_lambda_detector_in_lambda() {
        temp_env::with_vars(
            [
                (AWS_EXECUTION_ENV, Some("AWS_Lambda_nodejs18.x")),
                (AWS_REGION, Some("us-east-1")),
                (AWS_LAMBDA_FUNCTION_NAME, Some("test-function")),
                (AWS_LAMBDA_FUNCTION_VERSION, Some("$LATEST")),
                (AWS_LAMBDA_FUNCTION_MEMORY_SIZE, Some("128")),
            ],
            || {
                let detector = AwsLambdaDetector::new();
                let resource = detector.detect();

                assert_eq!(
                    get_string_value(&resource, semconv::CLOUD_PROVIDER),
                    Some("aws".to_string())
                );
                assert_eq!(
                    get_string_value(&resource, semconv::CLOUD_PLATFORM),
                    Some("aws_lambda".to_string())
                );
                assert_eq!(
                    get_string_value(&resource, semconv::CLOUD_REGION),
                    Some("us-east-1".to_string())
                );
                assert_eq!(
                    get_string_value(&resource, semconv::FAAS_NAME),
                    Some("test-function".to_string())
                );
                assert_eq!(
                    get_string_value(&resource, semconv::FAAS_VERSION),
                    Some("$LATEST".to_string())
                );
                assert_eq!(
                    get_int_value(&resource, semconv::FAAS_MAX_MEMORY),
                    Some(128 * 1024 * 1024)
                );
            },
        );
    }

    #[test]
    fn test_extension_detector() {
        let detector = ExtensionDetector::new();
        let resource = detector.detect();

        assert_eq!(
            get_string_value(&resource, semconv::SERVICE_NAME),
            Some("opentelemetry-lambda-extension".to_string())
        );
        assert_eq!(
            get_string_value(&resource, semconv::TELEMETRY_SDK_NAME),
            Some("opentelemetry-lambda-extension".to_string())
        );
        assert_eq!(
            get_string_value(&resource, semconv::TELEMETRY_SDK_LANGUAGE),
            Some("rust".to_string())
        );
        assert!(get_string_value(&resource, semconv::TELEMETRY_SDK_VERSION).is_some());
    }

    #[test]
    fn test_resource_builder_custom_attributes() {
        temp_env::with_vars([(AWS_EXECUTION_ENV, None::<&str>)], || {
            let resource = ResourceBuilder::new()
                .add_string("custom.string", "value")
                .add_int("custom.int", 42)
                .add_bool("custom.bool", true)
                .build();

            assert_eq!(
                get_string_value(&resource, "custom.string"),
                Some("value".to_string())
            );
            assert_eq!(get_int_value(&resource, "custom.int"), Some(42));
        });
    }

    #[test]
    fn test_detect_resource() {
        temp_env::with_vars([(AWS_EXECUTION_ENV, None::<&str>)], || {
            let resource = detect_resource();
            // Should at least have extension detector attributes
            assert!(get_string_value(&resource, semconv::SERVICE_NAME).is_some());
        });
    }

    #[test]
    fn test_to_proto_resource() {
        let resource = Resource::builder_empty()
            .with_attribute(KeyValue::new("test.key", "test.value"))
            .build();

        let proto = to_proto_resource(&resource);

        assert!(!proto.attributes.is_empty());
        let attr = &proto.attributes[0];
        assert_eq!(attr.key, "test.key");
        match &attr.value {
            Some(v) => match &v.value {
                Some(any_value::Value::StringValue(s)) => assert_eq!(s, "test.value"),
                _ => panic!("Expected string value"),
            },
            None => panic!("Expected value"),
        }
    }

    #[test]
    fn test_service_name_is_extension_not_function() {
        temp_env::with_vars(
            [
                (AWS_EXECUTION_ENV, Some("AWS_Lambda_nodejs18.x")),
                (AWS_LAMBDA_FUNCTION_NAME, Some("my-lambda-function")),
            ],
            || {
                let resource = detect_resource();

                let service_name = get_string_value(&resource, semconv::SERVICE_NAME);
                let faas_name = get_string_value(&resource, semconv::FAAS_NAME);

                assert_eq!(
                    service_name,
                    Some("opentelemetry-lambda-extension".to_string()),
                    "service.name should be the extension name, not the function name"
                );

                assert_eq!(
                    faas_name,
                    Some("my-lambda-function".to_string()),
                    "faas.name should be the Lambda function name"
                );
            },
        );
    }

    #[test]
    fn test_faas_instance_from_log_stream() {
        temp_env::with_vars(
            [
                (AWS_EXECUTION_ENV, Some("AWS_Lambda_nodejs18.x")),
                (
                    AWS_LAMBDA_LOG_STREAM_NAME,
                    Some("2024/01/01/[$LATEST]abc123"),
                ),
            ],
            || {
                let detector = AwsLambdaDetector::new();
                let resource = detector.detect();

                assert_eq!(
                    get_string_value(&resource, semconv::FAAS_INSTANCE),
                    Some("2024/01/01/[$LATEST]abc123".to_string())
                );
            },
        );
    }
}