Skip to content

Commit

Permalink
feat: test telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Yag000 committed Aug 29, 2023
1 parent fa5fab0 commit d13dee8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
tracing-bunyan-formatter = "0.3"
tracing-log = "0.1"
once_cell = "1"

[dev-dependencies]
reqwest = { version = "0.11", features = ["json"] }
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async fn main() -> Result<(), std::io::Error> {
// Logger setup
LogTracer::init().expect("Failed to set logger");
// Info or above will be logged if the RUST_LOG environment variable is not set
let subscriber = get_subscriber("zero2prod".into(), "info".into());
let subscriber = get_subscriber("zero2prod".into(), "info".into(), std::io::stdout);
set_global_default(subscriber).expect("Failed to set subscriber");

// We want to panic if we cannot read the configuration
Expand Down
13 changes: 10 additions & 3 deletions src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tracing::{subscriber::set_global_default, Subscriber};
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_log::LogTracer;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
use tracing_subscriber::{fmt::MakeWriter, layer::SubscriberExt, EnvFilter, Registry};

/// Compose multiple layers into a `tracing`'s subscriber.
///
Expand All @@ -13,10 +13,17 @@ use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
/// We need to explicitly call out that the returned subscriber is
/// `Send` and `Sync` to make it possible to pass it to `init_subscriber`
/// later on.
pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Send + Sync {
pub fn get_subscriber<Sink>(
name: String,
env_filter: String,
sink: Sink,
) -> impl Subscriber + Sync + Send
where
Sink: for<'a> MakeWriter<'a> + Send + Sync + 'static,
{
let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter));
let formatting_layer = BunyanFormattingLayer::new(name.into(), std::io::stdout);
let formatting_layer = BunyanFormattingLayer::new(name.into(), sink);
Registry::default()
.with(env_filter)
.with(JsonStorageLayer)
Expand Down
22 changes: 21 additions & 1 deletion tests/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
use once_cell::sync::Lazy;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use std::net::TcpListener;
use uuid::Uuid;
use zero2prod::configurations::get_configuration;
use zero2prod::{
configurations::get_configuration,
telemetry::{get_subscriber, inti_subscriber},
};

pub struct TestApp {
pub address: String,
pub db_pool: sqlx::PgPool,
}

static TRACING: Lazy<()> = Lazy::new(|| {
let default_filter_level = "info".into();
let subscriber_name = "test".into();

if std::env::var("TEST_LOG").is_ok() {
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::stdout);
inti_subscriber(subscriber);
} else {
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::sink);
inti_subscriber(subscriber);
}
});

async fn spawn_app() -> TestApp {
// This is only executed once thanks to `Lazy`
Lazy::force(&TRACING);

let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let address = format!("http://127.0.0.1:{port}");
Expand Down

0 comments on commit d13dee8

Please sign in to comment.