-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working example of lambda_http with Opentelemetry and shared client #947
Comments
Hi, just chiming in... I think it may depends on how Starting from this example something like this should work: use lambda_runtime::{
layers::{OpenTelemetryFaasTrigger, OpenTelemetryLayer as OtelLayer},
LambdaEvent, Runtime,
};
use opentelemetry::trace::TracerProvider;
use opentelemetry_sdk::{runtime, trace};
use tower::{service_fn, BoxError};
use tracing_subscriber::prelude::*;
use aws_sdk_dynamodb::Client as DynamoDbClient;
async fn echo(
event: LambdaEvent<serde_json::Value>,
client: DynamoDbClient,
) -> Result<serde_json::Value, &'static str> {
// use the client here
// ...
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();
let config = aws_config::load_from_env().await;
let dynamodb_client = DynamoDbClient::new(&config);
let func = service_fn(move |event| {
let client = dynamodb_client.clone();
echo(event, client)
});
// Initialize the Lambda runtime and add OpenTelemetry tracing
let runtime = Runtime::new(func).layer(
// Create a tracing span for each Lambda invocation
OtelLayer::new(|| {
// Make sure that the trace is exported before the Lambda runtime is frozen
tracer_provider.force_flush();
})
// Set the "faas.trigger" attribute of the span to "pubsub"
.with_trigger(OpenTelemetryFaasTrigger::PubSub),
);
runtime.run().await?;
Ok(())
} |
I've managed to make something work (the use aws_config::BehaviorVersion;
use lambda_http::lambda_runtime::layers::{OpenTelemetryFaasTrigger, OpenTelemetryLayer};
use lambda_http::{lambda_runtime, service_fn, Adapter, Request};
use lambdas::api::routes::users::get::api_user_list;
use lambdas::utils::telemetry::init_tracer;
#[tokio::main]
async fn main() -> Result<(), lambda_http::Error> {
let config = aws_config::load_defaults(BehaviorVersion::latest()).await;
let db_client = aws_sdk_dynamodb::Client::new(&config);
let _guard = init_tracer();
let runtime = lambda_runtime::Runtime::new(Adapter::from(service_fn(|event: Request| async {
api_user_list(event, &db_client).await
})))
.layer(
OpenTelemetryLayer::new(|| {
_guard.tracer_provider.force_flush();
})
.with_trigger(OpenTelemetryFaasTrigger::Http),
);
runtime.run().await?;
Ok(())
} Thanks for the help ! |
Thanks! I'll close the issue as it seems that you find a working solution, let us know if you want to re-open it. use aws_config::BehaviorVersion;
use aws_sdk_dynamodb::Client as DynamoDbClient;
use lambda_http::lambda_runtime::layers::{OpenTelemetryFaasTrigger, OpenTelemetryLayer};
use lambda_http::lambda_runtime::Runtime;
use lambda_http::Adapter;
use lambda_http::{service_fn, Request};
use opentelemetry_sdk::{runtime, trace};
async fn echo(
_event: Request,
_client: &DynamoDbClient,
) -> Result<serde_json::Value, &'static str> {
Ok(serde_json::json!({
"status": 200,
"message": "hello world",
}))
}
#[tokio::main]
async fn main() -> Result<(), lambda_http::Error> {
let config = aws_config::load_defaults(BehaviorVersion::latest()).await;
let db_client = aws_sdk_dynamodb::Client::new(&config);
let exporter = opentelemetry_stdout::SpanExporter::default();
let tracer_provider = trace::TracerProvider::builder()
.with_batch_exporter(exporter, runtime::Tokio)
.build();
let handler_func_closure = |event: Request| async { echo(event, &db_client).await };
let runtime = Runtime::new(Adapter::from(service_fn(handler_func_closure))).layer(
OpenTelemetryLayer::new(|| {
tracer_provider.force_flush();
})
.with_trigger(OpenTelemetryFaasTrigger::Http),
);
runtime.run().await?;
Ok(())
} |
This issue is now closed. Comments on closed issues are hard for our team to see. |
Thanks guys, I'll keep this solution for now. Cheers, |
Hi,
I'm trying to use the new opentelemtry feature with a shared dynamodb client :
And I'm getting the following error :
I can't find a working example of this setup, is it the right way to do it ?
Thanks
The text was updated successfully, but these errors were encountered: