1

I'm pretty new to Actix/Rust (coming from Java+SpringBoot).

I am setting up a Rust microservice and need to integrate it into the rest of the tracing ecosystem at my company. We are using ECS format for our logs so that we can easily see all of the logs for a given request by filtering on the traceId

My goal is to include a traceId into a structured log event when something is logged from the log crate (info!() / warn!() / error!()). I would like to get this traceId from B3 propagation headers.

For example, I would like for the following code to create 3 log events that all have the traceId.

#[get("/")]
pub async fn hello() -> impl Responder {
    info!("1");
    info!("2");
    info!("3");
    HttpResponse::Ok().body("Hello world!")
}

I am using env_logger with a custom pipe to send the logs

env_logger::builder()
        .format(ecs_logger::format) // Configure ECS logger
        .target(env_logger::Target::Pipe(Box::new(kafka_pipe))) // Write to Kafka
        .init();

However, my issue is that I am struggling find a way to get the current traceId/spanId into the log record. In Java, the MDC class is how I would accomplish

// Middleware filter in SpringBoot
MDC.put("traceId", "<my-trace-id>");

I've tried using various crates like tracing/opentelemetry but I am getting a bit overwhelmed by all of the crates and how they interact with each other.

Ross Sullivan
  • 396
  • 1
  • 3
  • 13
  • Take a look at the [`tracing`](https://crates.io/crates/tracing) crate: it allows you to define spans and attach metadata to them. – Jmb Jun 26 '23 at 10:36
  • yes, however my question is more along the lines of how do all of the tracing packages in the Rust ecosystem fit together? I'm a little lost on the difference between the tracing and opentelemetry packages. – Ross Sullivan Jun 26 '23 at 13:34
  • 2
    `log` is just a facade. It defines the `debug!`, `info!`, etc. macros, but by themselves they do nothing. It requires a backend that defines how to handle them. `tracing` is another facade which offers more functionality (notably spans), but it too requires a backend to actually trace the messages. – Jmb Jun 26 '23 at 14:25
  • 2
    [`tracing-log`](https://crates.io/crates/tracing-log) is a `log` backend that forwards to `tracing` so that they can be used together. [`opentelemetry`](https://crates.io/crates/opentelemetry) provides backends [for `log`](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-appender-log/README.md) or [for `tracing`](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-appender-tracing/README.md) that enable them to send their traces to an Open Telemetry server. – Jmb Jun 26 '23 at 14:31
  • I see, that is very helpful. I did not realize that `tracing` is also just a facade. – Ross Sullivan Jun 29 '23 at 09:36

0 Answers0