Skip to content

Commit 14113bb

Browse files
authored
Record faas.name on Lambda OTEL spans. (#1163)
1 parent 31be935 commit 14113bb

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

lambda-runtime/src/layers/otel.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ where
7777
"Lambda function invocation",
7878
"otel.name" = req.context.env_config.function_name,
7979
"otel.kind" = field::Empty,
80+
{ attribute::FAAS_NAME } = req.context.env_config.function_name,
8081
{ attribute::FAAS_TRIGGER } = &self.otel_attribute_trigger,
8182
{ attribute::FAAS_INVOCATION_ID } = req.context.request_id,
8283
{ attribute::FAAS_COLDSTART } = self.coldstart,
@@ -87,6 +88,7 @@ where
8788
"Lambda function invocation",
8889
"otel.name" = req.context.env_config.function_name,
8990
"otel.kind" = field::Empty,
91+
{ attribute::FAAS_NAME } = req.context.env_config.function_name,
9092
{ attribute::FAAS_TRIGGER } = &self.otel_attribute_trigger,
9193
{ attribute::FAAS_INVOCATION_ID } = req.context.request_id,
9294
{ attribute::FAAS_COLDSTART } = self.coldstart
@@ -171,3 +173,57 @@ impl Display for OpenTelemetryFaasTrigger {
171173
}
172174
}
173175
}
176+
177+
#[cfg(test)]
178+
mod tests {
179+
use super::*;
180+
use crate::{Config, Context};
181+
use lambda_runtime_api_client::BoxError;
182+
use std::sync::Arc;
183+
use tower::service_fn;
184+
use tracing_capture::{CaptureLayer, SharedStorage};
185+
use tracing_subscriber::layer::SubscriberExt;
186+
187+
fn invocation_with_function_name(function_name: &str) -> LambdaInvocation {
188+
let (parts, _) = http::Response::new(()).into_parts();
189+
LambdaInvocation {
190+
parts,
191+
body: bytes::Bytes::new(),
192+
context: Context {
193+
env_config: Arc::new(Config {
194+
function_name: function_name.to_owned(),
195+
..Default::default()
196+
}),
197+
..Default::default()
198+
},
199+
}
200+
}
201+
202+
#[tokio::test]
203+
async fn should_record_faas_name_from_the_function_configuration() {
204+
// given
205+
let storage = SharedStorage::default();
206+
let subscriber = tracing_subscriber::registry().with(CaptureLayer::new(&storage));
207+
let _guard = tracing::subscriber::set_default(subscriber);
208+
let inner = service_fn(|_req: LambdaInvocation| async { Ok::<(), BoxError>(()) });
209+
let mut service = OpenTelemetryLayer::new(|| {}).layer(inner);
210+
211+
// when
212+
service
213+
.call(invocation_with_function_name("my-function"))
214+
.await
215+
.expect("invocation should succeed");
216+
217+
// then
218+
let storage = storage.lock();
219+
let span = storage
220+
.all_spans()
221+
.find(|span| span.metadata().name() == "Lambda function invocation")
222+
.expect("otel span should be recorded");
223+
let faas_name = span.value("faas.name").expect("faas.name attribute should be recorded");
224+
assert_eq!(
225+
*faas_name, "my-function",
226+
"faas.name should match the function's configured name"
227+
);
228+
}
229+
}

0 commit comments

Comments
 (0)