13个版本 (7个破坏性)
0.19.0 | 2024年6月30日 |
---|---|
0.18.0 | 2024年3月9日 |
0.16.0 | 2023年12月30日 |
0.15.0 | 2023年11月25日 |
0.12.0 | 2023年7月2日 |
#561 在 调试
每月下载量 12,044
34KB
507 行
init-tracing-opentelemetry
一组初始化(以及更多)跟踪和 opentelemetry 的辅助工具(自己组合或使用预设的预设)
//...
use axum_tracing_opentelemetry::opentelemetry_tracing_layer;
#[tokio::main]
async fn main() -> Result<(), axum::BoxError> {
// very opinionated init of tracing, look as is source to compose your own
init_tracing_opentelemetry::tracing_subscriber_ext::init_subscribers()?;
...;
Ok(())
}
并且在应用程序关闭时调用 opentelemetry::global::shutdown_tracer_provider();
确保发送挂起的跟踪...
要配置 opentelemetry 跟踪器和跟踪,您可以使用 init_tracing_opentelemetry::tracing_subscriber_ext
中的函数,但它们非常具有意见(并且仍在进行改进以使其更可定制和友好),因此我们建议您自己进行组合,但查看代码(以避免一些问题)并分享您的反馈。
pub fn build_loglevel_filter_layer() -> tracing_subscriber::filter::EnvFilter {
// filter what is output on log (fmt)
// std::env::set_var("RUST_LOG", "warn,axum_tracing_opentelemetry=info,otel=debug");
std::env::set_var(
"RUST_LOG",
format!(
// `otel::tracing` should be a level trace to emit opentelemetry trace & span
// `otel::setup` set to debug to log detected resources, configuration read and infered
"{},otel::tracing=trace,otel=debug",
std::env::var("RUST_LOG")
.or_else(|_| std::env::var("OTEL_LOG_LEVEL"))
.unwrap_or_else(|_| "info".to_string())
),
);
EnvFilter::from_default_env()
}
pub fn build_otel_layer<S>() -> Result<OpenTelemetryLayer<S, Tracer>, BoxError>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
use crate::{
init_propagator, //stdio,
otlp,
resource::DetectResource,
};
let otel_rsrc = DetectResource::default()
//.with_fallback_service_name(env!("CARGO_PKG_NAME"))
//.with_fallback_service_version(env!("CARGO_PKG_VERSION"))
.build();
let otel_tracer = otlp::init_tracer(otel_rsrc, otlp::identity)?;
// to not send trace somewhere, but continue to create and propagate,...
// then send them to `axum_tracing_opentelemetry::stdio::WriteNoWhere::default()`
// or to `std::io::stdout()` to print
//
// let otel_tracer =
// stdio::init_tracer(otel_rsrc, stdio::identity, stdio::WriteNoWhere::default())?;
init_propagator()?;
Ok(tracing_opentelemetry::layer().with_tracer(otel_tracer))
}
要检索当前的 trace_id
(例如将其添加到错误消息中(作为标题或属性))
# use tracing_opentelemetry_instrumentation_sdk;
let trace_id = tracing_opentelemetry_instrumentation_sdk::find_current_trace_id();
//json!({ "error" : "xxxxxx", "trace_id": trace_id})
基于环境变量的配置
为了简化设置并符合 OpenTelemetry SDK 配置,配置可以使用以下环境变量完成(参见上面的示例 init_tracing()
)
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
回退到OTEL_EXPORTER_OTLP_ENDPOINT
用于导出器/收集器的 URLOTEL_EXPORTER_OTLP_TRACES_PROTOCOL
回退到OTEL_EXPORTER_OTLP_PROTOCOL
,回退到基于 ENDPOINT 端口的自检测OTEL_SERVICE_NAME
用于服务名称OTEL_PROPAGATORS
用于传播者配置OTEL_TRACES_SAMPLER
和OTEL_TRACES_SAMPLER_ARG
用于采样器配置
在 Kubernetes 的上下文中,上述环境变量可以通过 OpenTelemetry 操作员(通过 inject-sdk
)注入
apiVersion: apps/v1
kind: Deployment
spec:
template:
metadata:
annotations:
# to inject environment variables only by opentelemetry-operator
instrumentation.opentelemetry.io/inject-sdk: "opentelemetry-operator/instrumentation"
instrumentation.opentelemetry.io/container-names: "app"
containers:
- name: app
如果没有设置 inject-sdk
,可以手动设置环境变量,例如
apiVersion: apps/v1
kind: Deployment
spec:
template:
metadata:
containers:
- name: app
env:
- name: OTEL_SERVICE_NAME
value: "app"
- name: OTEL_EXPORTER_OTLP_PROTOCOL
value: "grpc"
# for otel collector in `deployment` mode, use the name of the service
# - name: OTEL_EXPORTER_OTLP_ENDPOINT
# value: "http://opentelemetry-collector.opentelemetry-collector:4317"
# for otel collector in sidecar mode (imply to deploy a sidecar CR per namespace)
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: "https://127.0.0.1:4317"
# for `daemonset` mode: need to use the local daemonset (value interpolated by k8s: `$(...)`)
# - name: OTEL_EXPORTER_OTLP_ENDPOINT
# value: "http://$(HOST_IP):4317"
# - name: HOST_IP
# valueFrom:
# fieldRef:
# fieldPath: status.hostIP
变更日志 - 历史
依赖项
~7–21MB
~287K SLoC