1
- use std:: { future:: Future , pin:: Pin , task} ;
1
+ use std:: { fmt :: Display , future:: Future , pin:: Pin , task} ;
2
2
3
3
use crate :: LambdaInvocation ;
4
4
use opentelemetry_semantic_conventions:: trace as traceconv;
@@ -10,6 +10,7 @@ use tracing::{instrument::Instrumented, Instrument};
10
10
/// a function to flush OpenTelemetry after the end of the invocation.
11
11
pub struct OpenTelemetryLayer < F > {
12
12
flush_fn : F ,
13
+ otel_attribute_trigger : OpenTelemetryFaasTrigger ,
13
14
}
14
15
15
16
impl < F > OpenTelemetryLayer < F >
18
19
{
19
20
/// Create a new [OpenTelemetryLayer] with the provided flush function.
20
21
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
+ }
22
34
}
23
35
}
24
36
33
45
inner,
34
46
flush_fn : self . flush_fn . clone ( ) ,
35
47
coldstart : true ,
48
+ otel_attribute_trigger : self . otel_attribute_trigger . to_string ( ) ,
36
49
}
37
50
}
38
51
}
@@ -42,6 +55,7 @@ pub struct OpenTelemetryService<S, F> {
42
55
inner : S ,
43
56
flush_fn : F ,
44
57
coldstart : bool ,
58
+ otel_attribute_trigger : String ,
45
59
}
46
60
47
61
impl < S , F > Service < LambdaInvocation > for OpenTelemetryService < S , F >
61
75
let span = tracing:: info_span!(
62
76
"Lambda function invocation" ,
63
77
"otel.name" = req. context. env_config. function_name,
64
- { traceconv:: FAAS_TRIGGER } = "http" ,
78
+ { traceconv:: FAAS_TRIGGER } = & self . otel_attribute_trigger ,
65
79
{ traceconv:: FAAS_INVOCATION_ID } = req. context. request_id,
66
80
{ traceconv:: FAAS_COLDSTART } = self . coldstart
67
81
) ;
@@ -114,3 +128,33 @@ where
114
128
task:: Poll :: Ready ( ready)
115
129
}
116
130
}
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