Skip to content

Commit

Permalink
refactor: common metrics code
Browse files Browse the repository at this point in the history
  • Loading branch information
chesedo committed Nov 11, 2022
1 parent 16e1586 commit b777998
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 38 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.

5 changes: 4 additions & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ description = "Common library for the shuttle platform (https://www.shuttle.rs/)
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = { version = "0.1.52", optional = true }
axum = { version = "0.5.8", optional = true }
chrono = { version = "0.4.22", features = ["serde"] }
comfy-table = { version = "6.1.0", optional = true }
crossterm = { version = "0.25.0", optional = true }
Expand All @@ -22,5 +24,6 @@ uuid = { version = "1.1.1", features = ["v4", "serde"] }
[features]
default = ["models"]

models = ["display", "serde_json", "http"]
backend = ["async-trait", "axum"]
display = ["comfy-table", "crossterm"]
models = ["display", "serde_json", "http"]
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use axum::extract::{FromRequest, Path, RequestParts};
use tracing::Span;

/// Used to record a bunch of metrics info
/// The tracing layer on the server should record a `request.params.<param>` field for each parameter
/// that should be recorded
pub struct Metrics;

#[async_trait]
Expand All @@ -15,10 +17,10 @@ where
type Rejection = Infallible;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
// We expect some path parameters
// Get path parameters if they exist
let Path(path): Path<HashMap<String, String>> = match req.extract().await {
Ok(path) => path,
Err(_) => todo!(),
Err(_) => return Ok(Metrics),
};

let span = Span::current();
Expand Down
1 change: 1 addition & 0 deletions common/src/backends/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod metrics;
2 changes: 2 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "backend")]
pub mod backends;
pub mod database;
pub mod deployment;
pub mod log;
Expand Down
4 changes: 2 additions & 2 deletions deployer/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use fqdn::FQDN;
use futures::StreamExt;
use opentelemetry::global;
use opentelemetry_http::HeaderExtractor;
use shuttle_common::backends::metrics::Metrics;
use shuttle_common::models::secret;
use shuttle_common::project::ProjectName;
use shuttle_common::LogItem;
Expand All @@ -30,7 +31,6 @@ use std::time::Duration;

pub use {self::error::Error, self::error::Result};

mod metrics;
mod project;

pub fn make_router(
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn make_router(
.layer(RequireAuthorizationLayer::bearer(&admin_secret))
// This route should be below the auth bearer since it does not need authentication
.route("/projects/:project_name/status", get(get_status))
.route_layer(from_extractor::<metrics::Metrics>())
.route_layer(from_extractor::<Metrics>())
.layer(
TraceLayer::new_for_http()
.make_span_with(|request: &Request<Body>| {
Expand Down
1 change: 1 addition & 0 deletions gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ uuid = { version = "1.2.1", features = [ "v4" ] }
[dependencies.shuttle-common]
version = "0.7.2"
path = "../common"
features = ["backend"]

[dev-dependencies]
anyhow = "1"
Expand Down
3 changes: 2 additions & 1 deletion gateway/src/api/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use axum::routing::{any, get, post};
use axum::{Json as AxumJson, Router};
use http::StatusCode;
use serde::{Deserialize, Serialize};
use shuttle_common::backends::metrics::Metrics;
use shuttle_common::models::error::ErrorKind;
use shuttle_common::models::{project, user};
use tokio::sync::mpsc::Sender;
Expand Down Expand Up @@ -191,7 +192,7 @@ pub fn make_api(service: Arc<GatewayService>, sender: Sender<BoxedTask>) -> Rout
.route("/admin/revive", post(revive_projects))
.layer(Extension(service))
.layer(Extension(sender))
.route_layer(from_extractor::<crate::api::metrics::Metrics>())
.route_layer(from_extractor::<Metrics>())
.layer(
TraceLayer::new_for_http()
.make_span_with(|request: &Request<Body>| {
Expand Down
31 changes: 0 additions & 31 deletions gateway/src/api/metrics.rs

This file was deleted.

1 change: 0 additions & 1 deletion gateway/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod latest;
mod metrics;

pub use latest::make_api;

0 comments on commit b777998

Please sign in to comment.