Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ lazy_static! {
pub static ref INCOMING_REQUESTS: IntCounterVec = IntCounterVec::new(
Opts::new("incoming_requests", "The number of HTTP requests received"),
&["http_method", "path"]
).expect("Prometheus metric initialization failed");
).expect("Prometheus metric options should be valid");
// Request counter by status code
pub static ref RESPONSE_CODE_COLLECTOR: IntCounterVec = IntCounterVec::new(
Opts::new("outgoing_response", "The number of responses sent"),
&["status_code", "http_method", "path"]
).expect("Prometheus metric initialization failed");
).expect("Prometheus metric options should be valid");
// Response histogram by response time
pub static ref RESPONSE_TIME_COLLECTOR: HistogramVec = HistogramVec::new(
HistogramOpts{
common_opts: Opts::new("response_time", "The time taken to respond to each request"),
buckets: prometheus::DEFAULT_BUCKETS.to_vec(), // Change buckets here if desired
},
&["status_code", "http_method", "path"],
).expect("Prometheus metric initialization failed");
).expect("Prometheus metric options should be valid");
}

/// Registers various prometheus metrics with the global registry
pub fn register_metrics() {
let registry = prometheus::default_registry();
registry
.register(Box::new(INCOMING_REQUESTS.clone()))
.expect("registering prometheus metrics during initialization failed");
.expect("Prometheus metrics registration should not fail during initialization");
registry
.register(Box::new(RESPONSE_CODE_COLLECTOR.clone()))
.expect("registering prometheus metrics during initialization failed");
.expect("Prometheus metrics registration should not fail during initialization");
registry
.register(Box::new(RESPONSE_TIME_COLLECTOR.clone()))
.expect("registering prometheus metrics during initialization failed");
.expect("Prometheus metrics registration should not fail during initialization");
}

/// Returns currently gathered prometheus metrics
Expand Down
8 changes: 4 additions & 4 deletions src/validated_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod tests {

#[tokio::test]
async fn ok() {
let body = Body::from("{\"foo\": \"abc\", \"bar\": 123}");
let body = Body::from(r#"{"foo": "abc", "bar": 123}"#);
let response = request(body).await;

assert_eq!(response.status(), StatusCode::OK);
Expand All @@ -113,7 +113,7 @@ mod tests {

#[tokio::test]
async fn invalid_foo_type() {
let body = Body::from("{\"foo\": 123}");
let body = Body::from(r#"{"foo": 123}"#);
let response = request(body).await;

assert_eq!(response.status(), StatusCode::BAD_REQUEST);
Expand All @@ -125,7 +125,7 @@ mod tests {

#[tokio::test]
async fn invalid_foo_too_short() {
let body = Body::from("{\"foo\": \"\"}");
let body = Body::from(r#"{"foo": ""}"#);
let response = request(body).await;

assert_eq!(response.status(), StatusCode::BAD_REQUEST);
Expand All @@ -139,7 +139,7 @@ mod tests {

#[tokio::test]
async fn invalid_foo_too_long() {
let body = Body::from("{\"foo\": \"abcd\"}");
let body = Body::from(r#"{"foo": "abcd"}"#);
let response = request(body).await;

assert_eq!(response.status(), StatusCode::BAD_REQUEST);
Expand Down
Loading