Skip to content

Commit

Permalink
ch4: replace Logger with TracingLogger
Browse files Browse the repository at this point in the history
Signed-off-by: Jin Dong <jin.dong@databricks.com>
  • Loading branch information
djdongjin committed Mar 27, 2024
1 parent fcc7001 commit 0355150
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/audit.yaml
Expand Up @@ -17,7 +17,7 @@ jobs:
security_audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: taiki-e/install-action@cargo-deny
- name: Scan for vulnerabilities
run: cargo deny check advisories
20 changes: 20 additions & 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
Expand Up @@ -23,6 +23,7 @@ secrecy = { version = "0.8.0", features = ["serde"] }
serde = { version = "1.0.197", features = ["derive"] }
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
tracing = { version = "0.1.40", features = ["log"] }
tracing-actix-web = "0.7.10"
tracing-bunyan-formatter = "0.3.9"
tracing-log = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["registry", "env-filter"] }
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -28,3 +28,5 @@ To disable a clippy warning: `#[allow(clippy::lint_name)]`
### Ch4

- [ ] Understand all those tracing related crates (e.g., `tracing::instrument`).
- [ ] Wire tracing with open telemetry (`tracing-opentelemetry`).
- [ ] `tracing` v.s. `log`.
2 changes: 1 addition & 1 deletion src/main.rs
Expand Up @@ -14,7 +14,7 @@ async fn main() -> Result<(), std::io::Error> {
let address = format!("127.0.0.1:{}", configuration.application_port);
let listener = TcpListener::bind(address)?;
let connection_pool =
PgPool::connect(&configuration.database.connection_string().expose_secret())
PgPool::connect(configuration.database.connection_string().expose_secret())
.await
.expect("Failed to connect to Postgres.");

Expand Down
1 change: 0 additions & 1 deletion src/routes/subscriptions.rs
Expand Up @@ -13,7 +13,6 @@ pub struct FormData {
name = "Adding a new subscriber", // span message, fn name by default
skip(form, db_pool), // skip these two fields in the span
fields(
request_id = %Uuid::new_v4(),
subscriber_email = %form.email,
subscriber_name = %form.name
)
Expand Down
4 changes: 2 additions & 2 deletions src/startup.rs
@@ -1,18 +1,18 @@
use crate::routes::{health_check, subscribe};

use actix_web::dev::Server;
use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer};
use sqlx::PgPool;
use std::net::TcpListener;
use tracing_actix_web::TracingLogger;

// `async` is no longer needed as we don't have .await calls.
pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Error> {
// create a ARC pointer to the DB connection
let db_pool = web::Data::new(db_pool);
let server = HttpServer::new(move || {
App::new()
.wrap(Logger::default())
.wrap(TracingLogger::default())
// .route("/", web::get().to(greet))
// .route("/{name}", web::get().to(greet))
.route("/health_check", web::get().to(health_check))
Expand Down
4 changes: 2 additions & 2 deletions tests/health_check.rs
Expand Up @@ -52,7 +52,7 @@ async fn spawn_app() -> TestApp {
async fn configure_database(config: &DatabseSettings) -> PgPool {
// Create database
let mut connection =
PgConnection::connect(&config.connection_string_without_db().expose_secret())
PgConnection::connect(config.connection_string_without_db().expose_secret())
.await
.expect("Failed to connect to Postgres.");

Expand All @@ -62,7 +62,7 @@ async fn configure_database(config: &DatabseSettings) -> PgPool {
.await
.expect("Failed to create database.");

let connection_pool = PgPool::connect(&config.connection_string().expose_secret())
let connection_pool = PgPool::connect(config.connection_string().expose_secret())
.await
.expect("Failed to connect to Postgres.");

Expand Down

0 comments on commit 0355150

Please sign in to comment.