Skip to content

Commit

Permalink
fix: add x request (#161)
Browse files Browse the repository at this point in the history
* fix: add timing traces

* fix: add x-request-id

* chore: don't span with state
  • Loading branch information
chris13524 authored Dec 12, 2023
1 parent 62a2455 commit 0065b04
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ axum = "0.6.1"
axum-macros = "0.3.0"
tokio = { version = "1.0", features = ["full"] }
tower = { version = "0.4", features = ["util", "timeout", "load-shed", "limit"] }
tower-http = { version = "0.3.0", features = ["add-extension", "auth", "compression-full", "trace", "cors"] }
tower-http = { version = "0.3.0", features = ["add-extension", "auth", "compression-full", "trace", "cors", "request-id", "util"] }
hyper = "0.14"
http = "0.2.8"

Expand Down
24 changes: 17 additions & 7 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ clean:
@echo '==> Cleaning project target/*'
cargo clean

# Reformat code
fmt:
@echo '==> Reformatting code'
cargo +nightly fmt
fmt-imports:
#!/bin/bash
set -euo pipefail

if command -v cargo-fmt >/dev/null; then
echo '==> Running rustfmt'
cargo +nightly fmt -- --config group_imports=StdExternalCrate,imports_granularity=One
else
echo '==> rustfmt not found in PATH, skipping'
fi

fmt-imports:
#!/bin/bash
Expand Down Expand Up @@ -96,7 +102,11 @@ restart-keyserver-docker:
docker-compose -f ./ops/docker-compose.keyserver.yml -f ./ops/docker-compose.storage.yml up -d --build --force-recreate --no-deps keyserver

# Lint the project for any quality issues
lint: rs-check-fmt check clippy commit-check
lint: clippy fmt commit-check

unit: lint test test-all test-doc tf-lint

devloop: unit fmt-imports

# Run project linter
clippy:
Expand All @@ -111,13 +121,13 @@ clippy:
fi

# Run code formatting check
rs-check-fmt:
fmt:
#!/bin/bash
set -euo pipefail

if command -v cargo-fmt >/dev/null; then
echo '==> Running rustfmt'
cargo fmt -- --check
cargo fmt
else
echo '==> rustfmt not found in PATH, skipping'
fi
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/identity/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl From<ResolveIdentityResponse> for Value {
}
}

#[instrument(name = "resolve_handler", skip(state))]
#[instrument(name = "resolve_handler", skip_all)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(params): Query<ResolveIdentityPayload>,
Expand Down
29 changes: 19 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use {
tower::ServiceBuilder,
tower_http::{
cors::CorsLayer,
request_id::MakeRequestUuid,
trace::{DefaultMakeSpan, DefaultOnRequest, DefaultOnResponse, TraceLayer},
ServiceBuilderExt,
},
tracing::Level,
wc::geoip::{
Expand Down Expand Up @@ -63,16 +65,23 @@ pub async fn bootstrap(

let state_arc = Arc::new(state);

let global_middleware = ServiceBuilder::new().layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::new().include_headers(true))
.on_request(DefaultOnRequest::new().level(Level::INFO))
.on_response(
DefaultOnResponse::new()
.level(Level::INFO)
.include_headers(true),
),
);
let global_middleware = ServiceBuilder::new()
.set_x_request_id(MakeRequestUuid)
.layer(
TraceLayer::new_for_http()
.make_span_with(
DefaultMakeSpan::new()
.level(Level::INFO)
.include_headers(true),
)
.on_request(DefaultOnRequest::new().level(Level::INFO))
.on_response(
DefaultOnResponse::new()
.level(Level::INFO)
.include_headers(true),
),
)
.propagate_x_request_id();

let cors_layer = CorsLayer::new()
.allow_headers([http::header::CONTENT_TYPE])
Expand Down

0 comments on commit 0065b04

Please sign in to comment.