#tracing-subscriber #datadog #layer #tracing #open-telemetry

datadog-formatting-layer

一个提供格式化事件以便Datadog可以解析的tracing-subscriber层的crate。

7个稳定版本

2.2.1 2024年5月30日
2.1.0 2024年3月6日
2.0.0 2024年3月5日
1.1.0 2023年9月18日
1.0.1 2023年6月21日

#448 in 调试

Download history 511/week @ 2024-04-29 341/week @ 2024-05-06 181/week @ 2024-05-13 92/week @ 2024-05-20 303/week @ 2024-05-27 189/week @ 2024-06-03 142/week @ 2024-06-10 219/week @ 2024-06-17 205/week @ 2024-06-24 79/week @ 2024-07-01 66/week @ 2024-07-08 127/week @ 2024-07-15 136/week @ 2024-07-22 119/week @ 2024-07-29 26/week @ 2024-08-05 46/week @ 2024-08-12

333 每月下载量

Apache-2.0

28KB
555

Datadog Formatting Layer

一个提供格式化事件以便Datadog可以解析的tracing-subscriber层的crate。

Release Test License Crates.io

特性

  • 提供tracing-subscriber层
  • 为datadog生成可解析的"日志"并将其打印到stdout
  • 启用span和"日志"之间的日志关联(见 datadog文档

为什么不直接使用 tracing_subscriber::fmt().json() 呢?

问题是,datadog期望"日志"以特定(大部分未文档化)的json格式。

这个crate尝试模仿这种格式。

使用方法

简单

use datadog_formatting_layer::DatadogFormattingLayer;
use tracing::info;
use tracing_subscriber::prelude::*;

tracing_subscriber::registry()
    .with(DatadogFormattingLayer::default())
    .init();

info!(user = "Jack", "Hello World!");

运行此代码将在stdout上产生以下输出

{
  "timestamp": "2023-06-21T10:36:50.364874878+00:00",
  "level": "INFO",
  "fields.user": "Jack",
  "message": "Hello World user=Jack",
  "target": "simple"
}

与Opentelemetry一起使用

use datadog_formatting_layer::DatadogFormattingLayer;
use opentelemetry::global;
use opentelemetry_datadog::ApiVersion;
use opentelemetry_sdk::{
    propagation::TraceContextPropagator,
    trace::{config, RandomIdGenerator, Sampler},
};
use tracing::{debug, error, info, instrument, warn};
use tracing_subscriber::{prelude::*, util::SubscriberInitExt};

// Just some otel boilerplate
global::set_text_map_propagator(TraceContextPropagator::new());

let tracer = opentelemetry_datadog::new_pipeline()
    .with_service_name("my-service")
    .with_trace_config(
        config()
            .with_sampler(Sampler::AlwaysOn)
            .with_id_generator(RandomIdGenerator::default()),
    )
    .with_api_version(ApiVersion::Version05)
    .with_env("rls")
    .with_version("420")
    .install_simple()
    .unwrap();

// Use both the tracer and the formatting layer
tracing_subscriber::registry()
    .with(DatadogFormattingLayer::default())
    .with(tracing_opentelemetry::layer().with_tracer(tracer))
    .init();

// Here no span exists
info!(user = "Jack", "Hello World!");
some_test("fasel");

// This will create a span and a trace id which is attached to the "logs"
#[instrument(fields(hello = "world"))]
fn some_test(value: &str) {
    // Here some span exists
    info!(ola = "salve", value, "Bla {value}");
}

当与此代码一起安装datadog代理时,日志将被发送到datadog并在那里解析。

否则,以下输出将被打印到stdout(为了可读性省略了字段)

{"timestamp":"2023-06-21T10:36:50.363224217+00:00","level":"INFO","message":"Hello World! user=Jack","target":"otel"}
{"timestamp":"2023-06-21T10:36:50.363384118+00:00","level":"INFO","message":"Bla fasel user=Jack ola=salve value=Fasel hello=world","target":"otel","dd.trace_id":0,"dd.span_id":10201226522570980512}

支持的Opentelemetry版本

Opentelemetry DatadogFormattingLayer
0.22.* 2.1.*, 2.2.*
0.20.* 2.0.*
0.20.* 1.1.*
0.19.* 1.0.*

依赖项

~7–9.5MB
~161K SLoC