Skip to content

Commit

Permalink
prometheus /metrics with GET enabled by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-johnson committed Mar 23, 2022
1 parent d7f45ad commit e4b7654
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

20 changes: 5 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "restapi"
description = "A secure-by-default rest api stack implemented with hyper, tokio, bb8 and postgres with optional prometheus monitoring. This project is focused on providing end-to-end encryption by default for 12-factor applications looking to customize functionality using environment variables as needed. Includes a working user management and authentication backend written in postgresql with async S3 uploading for POST-ed data files."
version = "1.1.1"
description = "A secure-by-default rest api stack implemented with hyper, tokio, bb8 and postgres with prometheus for monitoring. This project is focused on providing end-to-end encryption by default for 12-factor applications looking to customize functionality using environment variables as needed. Includes a working user management and authentication backend written in postgresql with async S3 uploading for POST-ed data files."
version = "1.1.2"
edition = "2021"
license = "MIT"
authors = [
Expand Down Expand Up @@ -44,15 +44,15 @@ chrono = { version = "^0.4" }
futures = { version = "^0.3.21" }
hyper = { version = "^0.14.18", features = [ "http1", "http2", "server", "stream", "runtime" ] }
jsonwebtoken = { version = "^7.2.0" }
lazy_static = { version = "^1.4", optional = true }
lazy_static = { version = "^1.4" }
log = { version = "^0.4.16" }
native-tls = { version = "^0.2.8" }
openssl = { version = "0.10.38", features = ["vendored"] }
postgres = { version = "^0.19.2", features = [ "with-geo-types-0_7", "array-impls", "with-chrono-0_4", "with-bit-vec-0_6", "with-serde_json-1", "with-eui48-1", "with-uuid-0_8", "with-time-0_3" ] }
postgres-native-tls = { version = "^0.5.0" }
pretty_env_logger = { version = "^0.4.0" }
prometheus = { version = "0.13.0", optional = true }
prometheus-static-metric = { version = "0.5.1", optional = true }
prometheus = { version = "0.13.0" }
prometheus-static-metric = { version = "0.5.1" }
rusoto_s3 = { version = "^0.47.0" }
rusoto_core = { version = "^0.47.0" }
rust-argon2 = { version = "^1.0.0" }
Expand All @@ -67,16 +67,6 @@ tokio-test = { version = "^0.4.2" }
url = { version = "2.2.2" }
uuid = { version = "^0.8.2", features = ["serde", "v4", "v5"] }

[features]
default = [
"monitoring"
]
monitoring = [
"lazy_static",
"prometheus",
"prometheus-static-metric",
]

[lib]
name = "restapi"
path = "src/lib.rs"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rust Rest API Stack with User Management and Prometheus for Monitoring

A secure-by-default rest api stack implemented with hyper, tokio, bb8 and postgres with optional prometheus monitoring. This project is focused on providing end-to-end encryption by default for 12-factor applications looking to customize functionality using environment variables as needed. Includes a working user management and authentication backend written in postgresql with async S3 uploading for POST-ed data files.
A secure-by-default rest api stack implemented with hyper, tokio, bb8 and postgres with prometheus for monitoring. This project is focused on providing end-to-end encryption by default for 12-factor applications looking to customize functionality using environment variables as needed. Includes a working user management and authentication backend written in postgresql with async S3 uploading for POST-ed data files.

## Overview

Expand Down Expand Up @@ -360,5 +360,5 @@ Please refer to the [Integration Tests Using curl Guide](./tests/integration-usi
## Build Docs

```bash
cargo doc --features monitoring --example server
cargo doc --example server
```
2 changes: 1 addition & 1 deletion base.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ADD ./examples /server/examples

RUN echo "starting build" \
&& cd /server \
&& cargo build --release --features monitoring --example server
&& cargo build --release --example server

EXPOSE 3000
ENTRYPOINT ["/server/target/release/examples/server"]
2 changes: 1 addition & 1 deletion charts/rust-restapi/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Rust Rest API with hyper, tokio, bb8 and prometheus for monitoring
# Rust Rest API with hyper, tokio, bb8 and Prometheus for Monitoring

## Install

Expand Down
2 changes: 1 addition & 1 deletion derived.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ADD ./examples /server/examples

RUN echo "starting build" \
&& cd /server \
&& cargo build --release --features monitoring --example server
&& cargo build --release --example server

EXPOSE 3000
ENTRYPOINT ["/server/target/release/examples/server"]
1 change: 0 additions & 1 deletion examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ extern crate serde;
extern crate serde_json;
extern crate chrono;
extern crate uuid;
#[cfg(feature = "monitoring")]
extern crate prometheus;

use restapi::core::core_config::CoreConfig;
Expand Down
14 changes: 2 additions & 12 deletions src/handle_request.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//! # Handle all Client Requests
//!
//! ## Features
//!
//! When including the ``monitoring`` feature, the
//! server hosts a metrics endpoint for prometheus at:
//! ``https://API_ENDPOINT/metrics`` with a ``GET``
//! method.
//! Prometheus scrapes metrics at the endpoint:
//! ``https://API_ENDPOINT/metrics`` with a ``GET`` method.

use std::convert::Infallible;

Expand All @@ -14,7 +10,6 @@ use hyper::Body;
use hyper::Method;
use hyper::Response;

#[cfg(feature = "monitoring")]
use crate::monitoring::metrics::record_monitoring_metrics_api_before;
use crate::monitoring::metrics::record_monitoring_metrics_api_after;
use crate::monitoring::metrics::handle_showing_metrics;
Expand Down Expand Up @@ -47,11 +42,6 @@ use crate::requests::user::verify_user::verify_user;
///
/// The url routing handler for all api requests.
///
/// When including the ``monitoring`` feature, the
/// server hosts a metrics endpoint for prometheus at:
/// ``https://API_ENDPOINT/metrics`` with a ``GET``
/// method.
///
/// # Arguments
///
/// * `data` - [`CoreTaskItem`](crate::core::server::core_task_item::CoreTaskItem)
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! # Rust Rest API Stack with User Management
//!
//! A secure-by-default rest api stack implemented with hyper, tokio, bb8 and postgres. This project is focused on providing end-to-end encryption by default for 12-factor applications looking to customize functionality using environment variables as needed. Includes a working user management and authentication backend written in postgresql with async S3 uploading for POST-ed data files.
//! A secure-by-default rest api stack implemented with hyper, tokio, bb8 and postgres with prometheus for monitoring. This project is focused on providing end-to-end encryption by default for 12-factor applications looking to customize functionality using environment variables as needed. Includes a working user management and authentication backend written in postgresql with async S3 uploading for POST-ed data files.
//!
//! # Examples
//!
Expand Down Expand Up @@ -634,7 +634,7 @@
//! ## Build Docs
//!
//! ```bash
//! cargo doc --features monitoring --example server
//! cargo doc --example server
//! ```

extern crate pretty_env_logger;
Expand Down
46 changes: 9 additions & 37 deletions src/monitoring/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! # Monitoring a Hyper Server with Prometheus
//!
//! This module requires the `monitoring` feature to be enabled
//! during compilation.
use std::convert::Infallible;

use prometheus::*;
Expand Down Expand Up @@ -203,8 +201,7 @@ lazy_static! {
///
/// Prometheus prefers to scrape metrics on a timed frequency. This function
/// hosts all the collected metrics under the uri=`/metrics` with
/// a `GET` method assuming the `monitoring` feature was compiled into
/// the build.
/// a `GET` method.
///
/// # Examples
///
Expand All @@ -215,11 +212,6 @@ lazy_static! {
pub fn handle_showing_metrics()
-> std::result::Result<Response<Body>, Infallible>
{
#[cfg(not(feature = "monitoring"))]
return Ok(
Response::new(
Body::from(format!("{{\"status\":\"metrics not enabled\"}}"))));

let encoder = TextEncoder::new();
let mut buffer = vec![];
encoder
Expand All @@ -237,22 +229,16 @@ pub fn handle_showing_metrics()

/// record_monitoring_metrics_api_before
///
/// This method requires building the library with the
/// `monitoring` feature enabled
///
/// If the `monitoring` feature is not enabled this method
/// immediately returns.
///
/// If the `monitoring` feature is enabled, this method records
/// tracked metrics using [Prometheus](https://docs.rs/prometheus/latest/prometheus/)
/// This method records tracked metrics using
/// [Prometheus](https://docs.rs/prometheus/latest/prometheus/)
/// before the internal service handlers start processing the
/// request.
///
/// # Arguments
///
/// * `uri` - `str&` - url sub path without the hosting fqdn address
/// * `resource` - `str&` - api resource (`user`, `data`, `auth`, etc.)
/// * `method` - `str&` - api resource (`user`, `data`, `auth`, etc.)
/// * `resource` - `str&` - HTTP resource (`user`, `data`, `auth`, etc.)
/// * `method` - `str&` - HTTP method used (`get`, `post`, `put`, `delete`, etc.)
///
/// # Returns
///
Expand All @@ -272,10 +258,6 @@ pub fn record_monitoring_metrics_api_before(
resource: &str,
method: &str)
{
// if not compiled with the monitoring feature - return
#[cfg(not(feature = "monitoring"))]
return;

debug!("\
metrics - before - uri={uri} \
resource={resource} \
Expand Down Expand Up @@ -366,23 +348,17 @@ pub fn record_monitoring_metrics_api_before(

/// record_monitoring_metrics_api_after
///
/// This method requires building the library with the
/// `monitoring` feature enabled
///
/// If the `monitoring` feature is not enabled this method
/// immediately returns the `processed_response` variable.
///
/// If the `monitoring` feature is enabled, this method records
/// tracked metrics using [Prometheus](https://docs.rs/prometheus/latest/prometheus/)
/// This method records tracked metrics using
/// [Prometheus](https://docs.rs/prometheus/latest/prometheus/)
/// after the internal service handlers processed the
/// request. This allows for tracking latency and status codes
/// for each resource and each method.
///
/// # Arguments
///
/// * `uri` - `str&` - url sub path without the hosting fqdn address
/// * `resource` - `str&` - api resource (`user`, `data`, `auth`, etc.)
/// * `method` - `str&` - api resource (`user`, `data`, `auth`, etc.)
/// * `resource` - `str&` - HTTP resource (`user`, `data`, `auth`, etc.)
/// * `method` - `str&` - HTTP method used (`get`, `post`, `put`, `delete`, etc.)
/// * `processed_response` - `str&` - existing [`Response`](hyper::Response)
/// from the internal service handler
///
Expand Down Expand Up @@ -414,10 +390,6 @@ pub fn record_monitoring_metrics_api_after(
processed_response: std::result::Result<Response<Body>, Infallible>)
-> std::result::Result<Response<Body>, Infallible>
{
// if not compiled with the monitoring feature - return
#[cfg(not(feature = "monitoring"))]
return processed_response;

if processed_response.is_ok() {
let cloned_result = processed_response.unwrap();
match (
Expand Down

0 comments on commit e4b7654

Please sign in to comment.