1- use std:: { future:: Future , pin:: Pin , task} ;
1+ use std:: { fmt :: Display , future:: Future , pin:: Pin , task} ;
22
33use crate :: LambdaInvocation ;
44use opentelemetry_semantic_conventions:: trace as traceconv;
@@ -10,6 +10,7 @@ use tracing::{instrument::Instrumented, Instrument};
1010/// a function to flush OpenTelemetry after the end of the invocation.
1111pub struct OpenTelemetryLayer < F > {
1212 flush_fn : F ,
13+ otel_attribute_trigger : OpenTelemetryFaasTrigger ,
1314}
1415
1516impl < F > OpenTelemetryLayer < F >
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 : Default :: default ( ) ,
25+ }
26+ }
27+
28+ /// Configure the `faas.trigger` attribute of the OpenTelemetry span.
29+ pub fn with_trigger ( self , trigger : OpenTelemetryFaasTrigger ) -> Self {
30+ Self {
31+ otel_attribute_trigger : trigger,
32+ ..self
33+ }
2234 }
2335}
2436
3345 inner,
3446 flush_fn : self . flush_fn . clone ( ) ,
3547 coldstart : true ,
48+ otel_attribute_trigger : self . otel_attribute_trigger . to_string ( ) ,
3649 }
3750 }
3851}
@@ -42,6 +55,7 @@ pub struct OpenTelemetryService<S, F> {
4255 inner : S ,
4356 flush_fn : F ,
4457 coldstart : bool ,
58+ otel_attribute_trigger : String ,
4559}
4660
4761impl < S , F > Service < LambdaInvocation > for OpenTelemetryService < S , F >
6175 let span = tracing:: info_span!(
6276 "Lambda function invocation" ,
6377 "otel.name" = req. context. env_config. function_name,
64- { traceconv:: FAAS_TRIGGER } = "http" ,
78+ { traceconv:: FAAS_TRIGGER } = & self . otel_attribute_trigger ,
6579 { traceconv:: FAAS_INVOCATION_ID } = req. context. request_id,
6680 { traceconv:: FAAS_COLDSTART } = self . coldstart
6781 ) ;
@@ -114,3 +128,33 @@ where
114128 task:: Poll :: Ready ( ready)
115129 }
116130}
131+
132+ /// Represent the possible values for the OpenTelemetry `faas.trigger` attribute.
133+ /// See https://opentelemetry.io/docs/specs/semconv/attributes-registry/faas/ for more details.
134+ #[ derive( Default , Clone , Copy ) ]
135+ #[ non_exhaustive]
136+ pub enum OpenTelemetryFaasTrigger {
137+ /// A response to some data source operation such as a database or filesystem read/write
138+ #[ default]
139+ Datasource ,
140+ /// To provide an answer to an inbound HTTP request
141+ Http ,
142+ /// A function is set to be executed when messages are sent to a messaging system
143+ PubSub ,
144+ /// A function is scheduled to be executed regularly
145+ Timer ,
146+ /// If none of the others apply
147+ Other ,
148+ }
149+
150+ impl Display for OpenTelemetryFaasTrigger {
151+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
152+ match self {
153+ OpenTelemetryFaasTrigger :: Datasource => write ! ( f, "datasource" ) ,
154+ OpenTelemetryFaasTrigger :: Http => write ! ( f, "http" ) ,
155+ OpenTelemetryFaasTrigger :: PubSub => write ! ( f, "pubsub" ) ,
156+ OpenTelemetryFaasTrigger :: Timer => write ! ( f, "timer" ) ,
157+ OpenTelemetryFaasTrigger :: Other => write ! ( f, "other" ) ,
158+ }
159+ }
160+ }
0 commit comments