|
| 1 | +use std::future::Future; |
| 2 | +use std::pin::Pin; |
| 3 | +use std::task; |
| 4 | + |
| 5 | +use lambda_runtime::LambdaInvocation; |
| 6 | +use opentelemetry_semantic_conventions::trace as traceconv; |
| 7 | +use pin_project::pin_project; |
| 8 | +use tower::{Layer, Service}; |
| 9 | +use tracing::instrument::Instrumented; |
| 10 | +use tracing::Instrument; |
| 11 | + |
| 12 | +/// Tower layer to add OpenTelemetry tracing to a Lambda function invocation. The layer accepts |
| 13 | +/// a function to flush OpenTelemetry after the end of the invocation. |
| 14 | +pub struct OpenTelemetryLayer<F> { |
| 15 | + flush_fn: F, |
| 16 | +} |
| 17 | + |
| 18 | +impl<F> OpenTelemetryLayer<F> |
| 19 | +where |
| 20 | + F: Fn() + Clone, |
| 21 | +{ |
| 22 | + pub fn new(flush_fn: F) -> Self { |
| 23 | + Self { flush_fn } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl<S, F> Layer<S> for OpenTelemetryLayer<F> |
| 28 | +where |
| 29 | + F: Fn() + Clone, |
| 30 | +{ |
| 31 | + type Service = OpenTelemetryService<S, F>; |
| 32 | + |
| 33 | + fn layer(&self, inner: S) -> Self::Service { |
| 34 | + OpenTelemetryService { |
| 35 | + inner, |
| 36 | + flush_fn: self.flush_fn.clone(), |
| 37 | + coldstart: true, |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Tower service created by [OpenTelemetryLayer]. |
| 43 | +pub struct OpenTelemetryService<S, F> { |
| 44 | + inner: S, |
| 45 | + flush_fn: F, |
| 46 | + coldstart: bool, |
| 47 | +} |
| 48 | + |
| 49 | +impl<S, F> Service<LambdaInvocation> for OpenTelemetryService<S, F> |
| 50 | +where |
| 51 | + S: Service<LambdaInvocation, Response = ()>, |
| 52 | + F: Fn() + Clone, |
| 53 | +{ |
| 54 | + type Error = S::Error; |
| 55 | + type Response = (); |
| 56 | + type Future = OpenTelemetryFuture<Instrumented<S::Future>, F>; |
| 57 | + |
| 58 | + fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> task::Poll<Result<(), Self::Error>> { |
| 59 | + self.inner.poll_ready(cx) |
| 60 | + } |
| 61 | + |
| 62 | + fn call(&mut self, req: LambdaInvocation) -> Self::Future { |
| 63 | + let span = tracing::info_span!( |
| 64 | + "Lambda function invocation", |
| 65 | + "otel.name" = req.context.env_config.function_name, |
| 66 | + { traceconv::FAAS_TRIGGER } = "http", |
| 67 | + { traceconv::FAAS_INVOCATION_ID } = req.context.request_id, |
| 68 | + { traceconv::FAAS_COLDSTART } = self.coldstart |
| 69 | + ); |
| 70 | + |
| 71 | + // After the first execution, we can set 'coldstart' to false |
| 72 | + self.coldstart = false; |
| 73 | + |
| 74 | + let fut = self.inner.call(req).instrument(span); |
| 75 | + OpenTelemetryFuture { |
| 76 | + future: Some(fut), |
| 77 | + flush_fn: self.flush_fn.clone(), |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Future created by [OpenTelemetryService]. |
| 83 | +#[pin_project] |
| 84 | +pub struct OpenTelemetryFuture<Fut, F> { |
| 85 | + #[pin] |
| 86 | + future: Option<Fut>, |
| 87 | + flush_fn: F, |
| 88 | +} |
| 89 | + |
| 90 | +impl<Fut, F> Future for OpenTelemetryFuture<Fut, F> |
| 91 | +where |
| 92 | + Fut: Future, |
| 93 | + F: Fn(), |
| 94 | +{ |
| 95 | + type Output = Fut::Output; |
| 96 | + |
| 97 | + fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> { |
| 98 | + // First, try to get the ready value of the future |
| 99 | + let ready = task::ready!(self |
| 100 | + .as_mut() |
| 101 | + .project() |
| 102 | + .future |
| 103 | + .as_pin_mut() |
| 104 | + .expect("future polled after completion") |
| 105 | + .poll(cx)); |
| 106 | + |
| 107 | + // If we got the ready value, we first drop the future: this ensures that the |
| 108 | + // OpenTelemetry span attached to it is closed and included in the subsequent flush. |
| 109 | + Pin::set(&mut self.as_mut().project().future, None); |
| 110 | + (self.project().flush_fn)(); |
| 111 | + task::Poll::Ready(ready) |
| 112 | + } |
| 113 | +} |
0 commit comments