Skip to content

Commit 0bd48b1

Browse files
committed
feat(otel): allow to configure the faas.trigger attribute of the span
1 parent f8cc32d commit 0bd48b1

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

lambda-runtime/src/layers/otel.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use tracing::{instrument::Instrumented, Instrument};
1010
/// a function to flush OpenTelemetry after the end of the invocation.
1111
pub struct OpenTelemetryLayer<F> {
1212
flush_fn: F,
13+
otel_attribute_trigger: Option<String>,
1314
}
1415

1516
impl<F> OpenTelemetryLayer<F>
@@ -18,7 +19,20 @@ where
1819
{
1920
/// Create a new [OpenTelemetryLayer] with the provided flush function.
2021
pub fn new(flush_fn: F) -> Self {
21-
Self { flush_fn }
22+
Self {
23+
flush_fn,
24+
otel_attribute_trigger: None,
25+
}
26+
}
27+
28+
/// Configure the `faas.trigger` attribute of the OpenTelemetry span.
29+
/// Defaults to `http` if not set.
30+
/// See https://opentelemetry.io/docs/specs/semconv/attributes-registry/faas/ for the list of possible triggers.
31+
pub fn with_trigger<T: Into<String>>(self, trigger: T) -> Self {
32+
Self {
33+
otel_attribute_trigger: Some(trigger.into()),
34+
..self
35+
}
2236
}
2337
}
2438

@@ -33,6 +47,10 @@ where
3347
inner,
3448
flush_fn: self.flush_fn.clone(),
3549
coldstart: true,
50+
otel_attribute_trigger: self
51+
.otel_attribute_trigger
52+
.clone()
53+
.unwrap_or_else(|| "http".to_string()),
3654
}
3755
}
3856
}
@@ -42,6 +60,7 @@ pub struct OpenTelemetryService<S, F> {
4260
inner: S,
4361
flush_fn: F,
4462
coldstart: bool,
63+
otel_attribute_trigger: String,
4564
}
4665

4766
impl<S, F> Service<LambdaInvocation> for OpenTelemetryService<S, F>
@@ -61,7 +80,7 @@ where
6180
let span = tracing::info_span!(
6281
"Lambda function invocation",
6382
"otel.name" = req.context.env_config.function_name,
64-
{ traceconv::FAAS_TRIGGER } = "http",
83+
{ traceconv::FAAS_TRIGGER } = &self.otel_attribute_trigger,
6584
{ traceconv::FAAS_INVOCATION_ID } = req.context.request_id,
6685
{ traceconv::FAAS_COLDSTART } = self.coldstart
6786
);

0 commit comments

Comments
 (0)