Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Add basic telemetry - tracing (#15)
Browse files Browse the repository at this point in the history
* Add basic telemetry - tracing

* Update to tracing with requests id
  • Loading branch information
adrianEffe authored Aug 6, 2023
1 parent 95b68ff commit 731187a
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
95 changes: 95 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ serde = { version = "1.0.171", features = ["derive"] }
config = "0.13.3"
uuid = { version = "1.4.1", features = ["v4", "serde"] }
chrono = { version = "0.4.21", features = ["serde"] }
tracing = "0.1.37"
tracing-subscriber = "0.3.17"
tower-http = { version = "0.4.3", features = ["trace"] }

[dependencies.sqlx]
version = "0.7.1"
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
pub mod configuration;
pub mod model;
pub mod request_tracing;
pub mod routes;
pub mod schema;

use axum::routing::{get, post};
use axum::Router;
use request_tracing::RequestSpan;
use routes::{full_url, health_check};
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use std::net::TcpListener;
use std::sync::Arc;
use tower_http::trace::TraceLayer;

pub async fn run(listener: TcpListener, db_connection: &str) {
let pool = connect_to_database(db_connection)
Expand All @@ -32,6 +35,7 @@ pub fn app(app_state: Arc<AppState>) -> Router {
Router::new()
.route("/health_check", get(health_check))
.route("/full_url", post(full_url))
.layer(TraceLayer::new_for_http().make_span_with(RequestSpan))
.with_state(app_state)
}

Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use briefly::{configuration::get_configuration, run};
use std::net::TcpListener;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.init();

let configuration = get_configuration().expect("Failed to read configuration");
let connection_string = configuration.database.connection_string();
let address = format!("127.0.0.1:{}", configuration.application_port);
Expand Down
17 changes: 17 additions & 0 deletions src/request_tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use axum::http;
use uuid::Uuid;

#[derive(Clone)]
pub struct RequestSpan;

impl<B> tower_http::trace::MakeSpan<B> for RequestSpan {
fn make_span(&mut self, request: &http::Request<B>) -> tracing::Span {
tracing::error_span!(
"rq",
id = %Uuid::new_v4().to_string(),
method = %request.method(),
uri = %request.uri(),
version = ?request.version(),
)
}
}

0 comments on commit 731187a

Please sign in to comment.