1 个不稳定版本
0.0.9 | 2024 年 3 月 30 日 |
---|
#261 in 调试
29 每月下载量
用于 xray-lite-aws-sdk
71KB
1.5K SLoC
xray-lite
AWS X-Ray 守护进程客户端,用于 AWS Lambda 上的 Rust 应用程序
安装 xray-lite
将以下内容添加到你的 Cargo.toml
文件中
[dependencies]
xray-lite = "0.0.9"
用法
AWS 服务操作的子段
建议使用 xray-lite-aws-sdk
扩展,通过 AWS SDK for Rust 跟踪操作。
以下是在 AWS X-Ray 修饰的 Lambda 函数调用中记录 AWS 服务操作子段的示例
use xray_lite::{AwsNamespace, Context, DaemonClient, SubsegmentContext};
fn main() {
// reads AWS_XRAY_DAEMON_ADDRESS
let client = DaemonClient::from_lambda_env().unwrap();
// reads _X_AMZN_TRACE_ID
let context = SubsegmentContext::from_lambda_env(client).unwrap();
do_s3_get_object(&context);
}
fn do_s3_get_object(context: &impl Context) {
// subsegment will have the name "S3" and `aws.operation` "GetObject"
let subsegment = context.enter_subsegment(AwsNamespace::new("S3", "GetObject"));
// call S3 GetObject ...
// if you are using `aws-sdk-s3` crate, you can update the subsegment
// with the request ID. suppose `out` is the output of the `GetObject`
// operation:
//
// subsegment
// .namespace_mut()
// .zip(out.request_id())
// .map(|(ns, id)| ns.request_id(id));
// the subsegment will be ended and reported when it is dropped
}
远程服务调用的子段
以下是在 AWS X-Ray 修饰的 Lambda 函数调用中记录远程服务调用子段的示例
use xray_lite::{Context, DaemonClient, RemoteNamespace, SubsegmentContext};
fn main() {
// reads AWS_XRAY_DAEMON_ADDRESS
let client = DaemonClient::from_lambda_env().unwrap();
// reads _X_AMZN_TRACE_ID
let context = SubsegmentContext::from_lambda_env(client).unwrap();
do_some_request(&context);
}
fn do_some_request(context: &impl Context) {
// subsegment will have the name "readme example",
// `http.request.method` "POST", and `http.request.url` "https://codemonger.io/"
let subsegment = context.enter_subsegment(RemoteNamespace::new(
"readme example",
"GET",
"https://codemonger.io/",
));
// do some request ...
// the subsegment will be ended and reported when it is dropped
}
自定义子段
以下是在 AWS X-Ray 修饰的 Lambda 函数调用中记录自定义子段的示例
use xray_lite::{Context, DaemonClient, CustomNamespace, SubsegmentContext};
fn main() {
// reads AWS_XRAY_DAEMON_ADDRESS
let client = DaemonClient::from_lambda_env().unwrap();
// reads _X_AMZN_TRACE_ID
let context = SubsegmentContext::from_lambda_env(client).unwrap()
.with_name_prefix("readme_example.");
do_something(&context);
}
fn do_something(context: &impl Context) {
// subsegment will have the name "readme_example.do_something"
let subsegment = context.enter_subsegment(CustomNamespace::new("do_something"));
// do some thing ...
// the subsegment will be ended and reported when it is dropped
}
不可变客户端和上下文
由于 X-Ray 跟踪可能是你的 Lambda 函数的辅助功能,你可能想在初始化客户端和上下文时忽略任何可能发生的错误。通过使用辅助特质 IntoInfallibleClient
和 IntoInfallibleContext
,你可以忽略这些错误而不会影响你的代码的其余部分
use xray_lite::{
AwsNamespace,
Context,
DaemonClient,
IntoInfallibleClient as _,
IntoInfallibleContext as _,
SubsegmentContext,
};
fn main() {
// Client creation error is ignored; e.g., AWS_XRAY_DAEMON_ADDRESS is not set
let client = DaemonClient::from_lambda_env().into_infallible();
// Context creation error is ignored; e.g., _X_AMZN_TRACE_ID is not set
let context = SubsegmentContext::from_lambda_env(client).into_infallible();
do_s3_get_object(&context);
}
fn do_s3_get_object(context: &impl Context) {
let subsegment = context.enter_subsegment(AwsNamespace::new("S3", "GetObject"));
// call S3 GetObject ...
}
扩展
API 文档
致谢
本项目建立在 杰出工作 的基础上,该工作由 Doug Tangren(softprops) 完成。
依赖项
约1-2MB
约40K SLoC