pub struct OtelTracingLayer<T> { /* private fields */ }Expand description
Tower middleware to create an OpenTelemetry tracing span for Lambda invocations.
This layer wraps a Lambda service to automatically create and configure OpenTelemetry spans for each invocation. It supports:
- Automatic span creation with configurable names
- Automatic attribute extraction from supported event types
- Custom attribute extraction through closures
- Context propagation from HTTP headers
- Response status tracking
§Example
use lambda_otel_lite::{init_telemetry, OtelTracingLayer, TelemetryConfig, SpanAttributes};
use lambda_runtime::{service_fn, Error, LambdaEvent, Runtime};
use aws_lambda_events::event::apigw::ApiGatewayV2httpRequest;
use tower::ServiceBuilder;
async fn handler(event: LambdaEvent<ApiGatewayV2httpRequest>) -> Result<serde_json::Value, Error> {
Ok(serde_json::json!({ "statusCode": 200 }))
}
let completion_handler = init_telemetry(TelemetryConfig::default()).await?;
// Create a layer with custom name and attribute extraction
let layer = OtelTracingLayer::new(completion_handler)
.with_name("api-handler")
.with_extractor_fn(|event| {
let mut attributes = std::collections::HashMap::new();
attributes.insert("custom.field".to_string(), "value".to_string());
SpanAttributes {
attributes,
..SpanAttributes::default()
}
});
// Apply the layer to your handler
let service = ServiceBuilder::new()
.layer(layer)
.service_fn(handler);
Runtime::new(service).run().awaitImplementations§
Source§impl<T> OtelTracingLayer<T>
impl<T> OtelTracingLayer<T>
Sourcepub fn new(completion_handler: TelemetryCompletionHandler) -> Self
pub fn new(completion_handler: TelemetryCompletionHandler) -> Self
Create a new OpenTelemetry tracing layer with the required completion handler.
The completion handler is used to signal when spans should be exported. It’s typically
obtained from init_telemetry.
§Arguments
completion_handler- Handler for managing span export timing
Sourcepub fn with_name(self, name: impl Into<String>) -> Self
pub fn with_name(self, name: impl Into<String>) -> Self
Set the span name.
This name will be used for all spans created by this layer. It should describe the purpose of the Lambda function (e.g., “process-order”, “api-handler”).
§Arguments
name- The name to use for spans
Sourcepub fn with_extractor_fn(
self,
f: impl Fn(&LambdaEvent<T>) -> SpanAttributes + Send + Sync + 'static,
) -> Self
pub fn with_extractor_fn( self, f: impl Fn(&LambdaEvent<T>) -> SpanAttributes + Send + Sync + 'static, ) -> Self
Set the event extractor function from a closure.
This function will be called for each invocation to extract custom attributes for the span. It’s useful when you need to:
- Extract attributes from unsupported event types
- Add custom attributes beyond what’s provided by
SpanAttributesExtractor - Override the default attribute extraction
§Arguments
f- Closure that takes a reference to the Lambda event and returns span attributes
§Example
let layer = OtelTracingLayer::new(completion_handler)
.with_name("my-handler")
.with_extractor_fn(|event: &LambdaEvent<Value>| {
let mut attributes = std::collections::HashMap::new();
// Extract custom fields from the event
if let Ok(payload) = serde_json::to_value(&event.payload) {
if let Some(user_id) = payload.get("userId").and_then(|v| v.as_str()) {
attributes.insert("user.id".to_string(), user_id.to_string());
}
}
SpanAttributes {
attributes,
..SpanAttributes::default()
}
});Trait Implementations§
Source§impl<T: Clone> Clone for OtelTracingLayer<T>
impl<T: Clone> Clone for OtelTracingLayer<T>
Source§fn clone(&self) -> OtelTracingLayer<T>
fn clone(&self) -> OtelTracingLayer<T>
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<T> Freeze for OtelTracingLayer<T>
impl<T> !RefUnwindSafe for OtelTracingLayer<T>
impl<T> Send for OtelTracingLayer<T>
impl<T> Sync for OtelTracingLayer<T>
impl<T> Unpin for OtelTracingLayer<T>
impl<T> !UnwindSafe for OtelTracingLayer<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request