feat(telemtry): Moved from logs to tracing
- Updated docs
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
pub mod configuration;
|
||||
pub mod routes;
|
||||
pub mod startup;
|
||||
pub mod telemetry;
|
||||
|
@@ -1,14 +1,15 @@
|
||||
use std::net::TcpListener;
|
||||
|
||||
use email_newsletter_api::telemetry::{get_subscriber, init_subscriber};
|
||||
use email_newsletter_api::{configuration::get_configuration, startup};
|
||||
use env_logger::Env;
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
let configuration = get_configuration().expect("Failed to read configuration");
|
||||
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||
let subscriber = get_subscriber("email_newsletter_api".into(), "info".into());
|
||||
init_subscriber(subscriber);
|
||||
|
||||
let db_conn = PgPool::connect(&configuration.database.connection_string())
|
||||
.await
|
||||
|
@@ -1,6 +1,7 @@
|
||||
use actix_web::{web, HttpResponse};
|
||||
use chrono::Utc;
|
||||
use sqlx::PgPool;
|
||||
use tracing::Instrument;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
@@ -15,13 +16,17 @@ pub async fn subscribe_route(
|
||||
) -> HttpResponse {
|
||||
let request_id = Uuid::new_v4();
|
||||
|
||||
log::info!(
|
||||
"request_id {} - Saving '{}' '{}' as a new subscriber in PostgreSQL",
|
||||
request_id,
|
||||
form.name,
|
||||
form.email
|
||||
let request_span = tracing::info_span!(
|
||||
"Adding new subscriber",
|
||||
%request_id,
|
||||
subscriber_email = %form.email,
|
||||
subscriber_name = %form.name
|
||||
);
|
||||
|
||||
let _request_span_guard = request_span.enter();
|
||||
|
||||
let query_span = tracing::info_span!("Adding new subscriber in PostgreSQL");
|
||||
|
||||
match sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO subscriptions (id, email, name, subscribed_at)
|
||||
@@ -33,17 +38,12 @@ pub async fn subscribe_route(
|
||||
Utc::now()
|
||||
)
|
||||
.execute(db_conn_pool.get_ref())
|
||||
.instrument(query_span)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
log::info!(
|
||||
"request_id {} - Saved new subscriber details in PostgreSQL",
|
||||
request_id
|
||||
);
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
Ok(_) => HttpResponse::Ok().finish(),
|
||||
Err(err) => {
|
||||
log::info!(
|
||||
tracing::error!(
|
||||
"request_id {} - Failed to execute query: {:?}",
|
||||
request_id,
|
||||
err
|
||||
|
21
src/telemetry.rs
Normal file
21
src/telemetry.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
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};
|
||||
|
||||
pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Send + Sync {
|
||||
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)
|
||||
}
|
||||
|
||||
// init_subscriber should only be called once
|
||||
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
|
||||
LogTracer::init().expect("Failed to set logger");
|
||||
set_global_default(subscriber).expect("Failed to set subscriber");
|
||||
}
|
Reference in New Issue
Block a user