-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement RFC for layering of runtime (#845)
* feat: Implement RFC for layering of runtime * Add example * Fix compilation errors * Remove Send and 'static * Fix ci * Reduce diff * Implement review comments
- Loading branch information
Showing
20 changed files
with
1,160 additions
and
435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[package] | ||
name = "opentelemetry-tracing" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
# Library dependencies | ||
lambda_runtime = { path = "../../lambda-runtime" } | ||
pin-project = "1" | ||
opentelemetry-semantic-conventions = "0.14" | ||
tower = "0.4" | ||
tracing = "0.1" | ||
|
||
# Binary dependencies | ||
opentelemetry = { version = "0.22", optional = true } | ||
opentelemetry_sdk = { version = "0.22", features = ["rt-tokio"], optional = true } | ||
opentelemetry-stdout = { version = "0.3", features = ["trace"], optional = true } | ||
serde_json = { version = "1.0", optional = true } | ||
tokio = { version = "1", optional = true } | ||
tracing-opentelemetry = { version = "0.23", optional = true } | ||
tracing-subscriber = { version = "0.3", optional = true } | ||
|
||
[features] | ||
build-binary = [ | ||
"opentelemetry", | ||
"opentelemetry_sdk", | ||
"opentelemetry-stdout", | ||
"serde_json", | ||
"tokio", | ||
"tracing-opentelemetry", | ||
"tracing-subscriber", | ||
] | ||
|
||
[[bin]] | ||
name = "opentelemetry-tracing" | ||
required-features = ["build-binary"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task; | ||
|
||
use lambda_runtime::LambdaInvocation; | ||
use opentelemetry_semantic_conventions::trace as traceconv; | ||
use pin_project::pin_project; | ||
use tower::{Layer, Service}; | ||
use tracing::instrument::Instrumented; | ||
use tracing::Instrument; | ||
|
||
/// Tower layer to add OpenTelemetry tracing to a Lambda function invocation. The layer accepts | ||
/// a function to flush OpenTelemetry after the end of the invocation. | ||
pub struct OpenTelemetryLayer<F> { | ||
flush_fn: F, | ||
} | ||
|
||
impl<F> OpenTelemetryLayer<F> | ||
where | ||
F: Fn() + Clone, | ||
{ | ||
pub fn new(flush_fn: F) -> Self { | ||
Self { flush_fn } | ||
} | ||
} | ||
|
||
impl<S, F> Layer<S> for OpenTelemetryLayer<F> | ||
where | ||
F: Fn() + Clone, | ||
{ | ||
type Service = OpenTelemetryService<S, F>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
OpenTelemetryService { | ||
inner, | ||
flush_fn: self.flush_fn.clone(), | ||
coldstart: true, | ||
} | ||
} | ||
} | ||
|
||
/// Tower service created by [OpenTelemetryLayer]. | ||
pub struct OpenTelemetryService<S, F> { | ||
inner: S, | ||
flush_fn: F, | ||
coldstart: bool, | ||
} | ||
|
||
impl<S, F> Service<LambdaInvocation> for OpenTelemetryService<S, F> | ||
where | ||
S: Service<LambdaInvocation, Response = ()>, | ||
F: Fn() + Clone, | ||
{ | ||
type Error = S::Error; | ||
type Response = (); | ||
type Future = OpenTelemetryFuture<Instrumented<S::Future>, F>; | ||
|
||
fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> task::Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx) | ||
} | ||
|
||
fn call(&mut self, req: LambdaInvocation) -> Self::Future { | ||
let span = tracing::info_span!( | ||
"Lambda function invocation", | ||
"otel.name" = req.context.env_config.function_name, | ||
{ traceconv::FAAS_TRIGGER } = "http", | ||
{ traceconv::FAAS_INVOCATION_ID } = req.context.request_id, | ||
{ traceconv::FAAS_COLDSTART } = self.coldstart | ||
); | ||
|
||
// After the first execution, we can set 'coldstart' to false | ||
self.coldstart = false; | ||
|
||
let fut = self.inner.call(req).instrument(span); | ||
OpenTelemetryFuture { | ||
future: Some(fut), | ||
flush_fn: self.flush_fn.clone(), | ||
} | ||
} | ||
} | ||
|
||
/// Future created by [OpenTelemetryService]. | ||
#[pin_project] | ||
pub struct OpenTelemetryFuture<Fut, F> { | ||
#[pin] | ||
future: Option<Fut>, | ||
flush_fn: F, | ||
} | ||
|
||
impl<Fut, F> Future for OpenTelemetryFuture<Fut, F> | ||
where | ||
Fut: Future, | ||
F: Fn(), | ||
{ | ||
type Output = Fut::Output; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> { | ||
// First, try to get the ready value of the future | ||
let ready = task::ready!(self | ||
.as_mut() | ||
.project() | ||
.future | ||
.as_pin_mut() | ||
.expect("future polled after completion") | ||
.poll(cx)); | ||
|
||
// If we got the ready value, we first drop the future: this ensures that the | ||
// OpenTelemetry span attached to it is closed and included in the subsequent flush. | ||
Pin::set(&mut self.as_mut().project().future, None); | ||
(self.project().flush_fn)(); | ||
task::Poll::Ready(ready) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use lambda_runtime::{LambdaEvent, Runtime}; | ||
use opentelemetry::trace::TracerProvider; | ||
use opentelemetry_sdk::{runtime, trace}; | ||
use opentelemetry_tracing::OpenTelemetryLayer; | ||
use tower::{service_fn, BoxError}; | ||
use tracing_subscriber::prelude::*; | ||
|
||
async fn echo(event: LambdaEvent<serde_json::Value>) -> Result<serde_json::Value, &'static str> { | ||
Ok(event.payload) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), BoxError> { | ||
// Set up OpenTelemetry tracer provider that writes spans to stdout for debugging purposes | ||
let exporter = opentelemetry_stdout::SpanExporter::default(); | ||
let tracer_provider = trace::TracerProvider::builder() | ||
.with_batch_exporter(exporter, runtime::Tokio) | ||
.build(); | ||
|
||
// Set up link between OpenTelemetry and tracing crate | ||
tracing_subscriber::registry() | ||
.with(tracing_opentelemetry::OpenTelemetryLayer::new( | ||
tracer_provider.tracer("my-app"), | ||
)) | ||
.init(); | ||
|
||
// Initialize the Lambda runtime and add OpenTelemetry tracing | ||
let runtime = Runtime::new(service_fn(echo)).layer(OpenTelemetryLayer::new(|| { | ||
// Make sure that the trace is exported before the Lambda runtime is frozen | ||
tracer_provider.force_flush(); | ||
})); | ||
runtime.run().await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.