Telemetry

This commit is contained in:
Alphonse Paix
2025-08-22 08:14:59 +02:00
parent 709bd28a8c
commit f1290d0bc5
11 changed files with 239 additions and 46 deletions

View File

@@ -1,7 +1,20 @@
use once_cell::sync::Lazy;
use secrecy::ExposeSecret;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use tokio::net::TcpListener;
use uuid::Uuid;
use zero2prod::configuration::{DatabaseSettings, get_configuration};
use zero2prod::{
configuration::{DatabaseSettings, get_configuration},
telemetry::init_subscriber,
};
static TRACING: Lazy<()> = Lazy::new(|| {
if std::env::var("TEST_LOG").is_ok() {
init_subscriber(std::io::stdout);
} else {
init_subscriber(std::io::sink);
}
});
pub struct TestApp {
pub address: String,
@@ -28,7 +41,7 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
let app = spawn_app().await;
let configuration = get_configuration().expect("Failed to read configuration");
let connection_string = configuration.database.connection_string();
let mut connection = PgConnection::connect(&connection_string)
let mut connection = PgConnection::connect(connection_string.expose_secret())
.await
.expect("Failed to connect to Postgres");
let client = reqwest::Client::new();
@@ -82,6 +95,8 @@ async fn subscribe_returns_a_422_when_data_is_missing() {
}
async fn spawn_app() -> TestApp {
Lazy::force(&TRACING);
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap().to_string();
@@ -102,7 +117,7 @@ async fn spawn_app() -> TestApp {
}
async fn configure_database(config: &DatabaseSettings) -> PgPool {
let connection = PgPool::connect(&config.connection_string_without_db())
let connection = PgPool::connect(config.connection_string_without_db().expose_secret())
.await
.expect("Failed to connect to Postgres");
connection
@@ -110,7 +125,7 @@ async fn configure_database(config: &DatabaseSettings) -> PgPool {
.await
.expect("Failed to create the database");
let connection_pool = PgPool::connect(&config.connection_string())
let connection_pool = PgPool::connect(config.connection_string().expose_secret())
.await
.expect("Failed to connect to Postgres");
sqlx::migrate!("./migrations")