zero-to-production/chapter05/src/telemetry.rs
Luca Palmieri 0a4addc6bb
Chapter 5 (#13)
* Add chapter 5

* Basic deployment stuff

* Fix Dockerfile.

* Add an explicit connection timeout.

* Align port with configuration.

* Use debug for faster feedback loops.

* Add address configuration.

* Provision database.

* Use structured options.

* Add configuration

* Fix typo.

* Customise deserialization logic.

* Change to Require.

* Fix spec.

* Add a few more things to the dockerignore file.

* Update to match chapter.

* Add base configuration.

* Amend configuratiohn.

Co-authored-by: LukeMathWalker <rust@lpalmieri.com>
2020-11-01 21:25:11 +00:00

30 lines
1.2 KiB
Rust

use tracing::subscriber::set_global_default;
use tracing::Subscriber;
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_log::LogTracer;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
/// Compose multiple layers into a `tracing`'s subscriber.
///
/// # Implementation Notes
///
/// We are using `impl Subscriber` as return type to avoid having to spell out the actual
/// type of the returned subscriber, which is indeed quite complex.
pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Sync + Send {
let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter));
let formatting_layer = BunyanFormattingLayer::new(name, std::io::stdout);
Registry::default()
.with(env_filter)
.with(JsonStorageLayer)
.with(formatting_layer)
}
/// Register a subscriber as global default to process span data.
///
/// It should only be called once!
pub fn init_subscriber(subscriber: impl Subscriber + Sync + Send) {
LogTracer::init().expect("Failed to set logger");
set_global_default(subscriber).expect("Failed to set subscriber");
}