Skip to content

Commit e4b7654

Browse files
committed
prometheus /metrics with GET enabled by default.
1 parent d7f45ad commit e4b7654

File tree

10 files changed

+24
-73
lines changed

10 files changed

+24
-73
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "restapi"
3-
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."
4-
version = "1.1.1"
3+
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."
4+
version = "1.1.2"
55
edition = "2021"
66
license = "MIT"
77
authors = [
@@ -44,15 +44,15 @@ chrono = { version = "^0.4" }
4444
futures = { version = "^0.3.21" }
4545
hyper = { version = "^0.14.18", features = [ "http1", "http2", "server", "stream", "runtime" ] }
4646
jsonwebtoken = { version = "^7.2.0" }
47-
lazy_static = { version = "^1.4", optional = true }
47+
lazy_static = { version = "^1.4" }
4848
log = { version = "^0.4.16" }
4949
native-tls = { version = "^0.2.8" }
5050
openssl = { version = "0.10.38", features = ["vendored"] }
5151
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" ] }
5252
postgres-native-tls = { version = "^0.5.0" }
5353
pretty_env_logger = { version = "^0.4.0" }
54-
prometheus = { version = "0.13.0", optional = true }
55-
prometheus-static-metric = { version = "0.5.1", optional = true }
54+
prometheus = { version = "0.13.0" }
55+
prometheus-static-metric = { version = "0.5.1" }
5656
rusoto_s3 = { version = "^0.47.0" }
5757
rusoto_core = { version = "^0.47.0" }
5858
rust-argon2 = { version = "^1.0.0" }
@@ -67,16 +67,6 @@ tokio-test = { version = "^0.4.2" }
6767
url = { version = "2.2.2" }
6868
uuid = { version = "^0.8.2", features = ["serde", "v4", "v5"] }
6969

70-
[features]
71-
default = [
72-
"monitoring"
73-
]
74-
monitoring = [
75-
"lazy_static",
76-
"prometheus",
77-
"prometheus-static-metric",
78-
]
79-
8070
[lib]
8171
name = "restapi"
8272
path = "src/lib.rs"

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Rust Rest API Stack with User Management and Prometheus for Monitoring
22

3-
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.
3+
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.
44

55
## Overview
66

@@ -360,5 +360,5 @@ Please refer to the [Integration Tests Using curl Guide](./tests/integration-usi
360360
## Build Docs
361361
362362
```bash
363-
cargo doc --features monitoring --example server
363+
cargo doc --example server
364364
```

base.Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ADD ./examples /server/examples
2525

2626
RUN echo "starting build" \
2727
&& cd /server \
28-
&& cargo build --release --features monitoring --example server
28+
&& cargo build --release --example server
2929

3030
EXPOSE 3000
3131
ENTRYPOINT ["/server/target/release/examples/server"]

charts/rust-restapi/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Rust Rest API with hyper, tokio, bb8 and prometheus for monitoring
1+
# Rust Rest API with hyper, tokio, bb8 and Prometheus for Monitoring
22

33
## Install
44

derived.Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ADD ./examples /server/examples
2121

2222
RUN echo "starting build" \
2323
&& cd /server \
24-
&& cargo build --release --features monitoring --example server
24+
&& cargo build --release --example server
2525

2626
EXPOSE 3000
2727
ENTRYPOINT ["/server/target/release/examples/server"]

examples/server.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ extern crate serde;
44
extern crate serde_json;
55
extern crate chrono;
66
extern crate uuid;
7-
#[cfg(feature = "monitoring")]
87
extern crate prometheus;
98

109
use restapi::core::core_config::CoreConfig;

src/handle_request.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
//! # Handle all Client Requests
22
//!
3-
//! ## Features
4-
//!
5-
//! When including the ``monitoring`` feature, the
6-
//! server hosts a metrics endpoint for prometheus at:
7-
//! ``https://API_ENDPOINT/metrics`` with a ``GET``
8-
//! method.
3+
//! Prometheus scrapes metrics at the endpoint:
4+
//! ``https://API_ENDPOINT/metrics`` with a ``GET`` method.
95
106
use std::convert::Infallible;
117

@@ -14,7 +10,6 @@ use hyper::Body;
1410
use hyper::Method;
1511
use hyper::Response;
1612

17-
#[cfg(feature = "monitoring")]
1813
use crate::monitoring::metrics::record_monitoring_metrics_api_before;
1914
use crate::monitoring::metrics::record_monitoring_metrics_api_after;
2015
use crate::monitoring::metrics::handle_showing_metrics;
@@ -47,11 +42,6 @@ use crate::requests::user::verify_user::verify_user;
4742
///
4843
/// The url routing handler for all api requests.
4944
///
50-
/// When including the ``monitoring`` feature, the
51-
/// server hosts a metrics endpoint for prometheus at:
52-
/// ``https://API_ENDPOINT/metrics`` with a ``GET``
53-
/// method.
54-
///
5545
/// # Arguments
5646
///
5747
/// * `data` - [`CoreTaskItem`](crate::core::server::core_task_item::CoreTaskItem)

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! # Rust Rest API Stack with User Management
22
//!
3-
//! 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.
3+
//! 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.
44
//!
55
//! # Examples
66
//!
@@ -634,7 +634,7 @@
634634
//! ## Build Docs
635635
//!
636636
//! ```bash
637-
//! cargo doc --features monitoring --example server
637+
//! cargo doc --example server
638638
//! ```
639639
640640
extern crate pretty_env_logger;

src/monitoring/metrics.rs

+9-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! # Monitoring a Hyper Server with Prometheus
22
//!
3-
//! This module requires the `monitoring` feature to be enabled
4-
//! during compilation.
53
use std::convert::Infallible;
64

75
use prometheus::*;
@@ -203,8 +201,7 @@ lazy_static! {
203201
///
204202
/// Prometheus prefers to scrape metrics on a timed frequency. This function
205203
/// hosts all the collected metrics under the uri=`/metrics` with
206-
/// a `GET` method assuming the `monitoring` feature was compiled into
207-
/// the build.
204+
/// a `GET` method.
208205
///
209206
/// # Examples
210207
///
@@ -215,11 +212,6 @@ lazy_static! {
215212
pub fn handle_showing_metrics()
216213
-> std::result::Result<Response<Body>, Infallible>
217214
{
218-
#[cfg(not(feature = "monitoring"))]
219-
return Ok(
220-
Response::new(
221-
Body::from(format!("{{\"status\":\"metrics not enabled\"}}"))));
222-
223215
let encoder = TextEncoder::new();
224216
let mut buffer = vec![];
225217
encoder
@@ -237,22 +229,16 @@ pub fn handle_showing_metrics()
237229

238230
/// record_monitoring_metrics_api_before
239231
///
240-
/// This method requires building the library with the
241-
/// `monitoring` feature enabled
242-
///
243-
/// If the `monitoring` feature is not enabled this method
244-
/// immediately returns.
245-
///
246-
/// If the `monitoring` feature is enabled, this method records
247-
/// tracked metrics using [Prometheus](https://docs.rs/prometheus/latest/prometheus/)
232+
/// This method records tracked metrics using
233+
/// [Prometheus](https://docs.rs/prometheus/latest/prometheus/)
248234
/// before the internal service handlers start processing the
249235
/// request.
250236
///
251237
/// # Arguments
252238
///
253239
/// * `uri` - `str&` - url sub path without the hosting fqdn address
254-
/// * `resource` - `str&` - api resource (`user`, `data`, `auth`, etc.)
255-
/// * `method` - `str&` - api resource (`user`, `data`, `auth`, etc.)
240+
/// * `resource` - `str&` - HTTP resource (`user`, `data`, `auth`, etc.)
241+
/// * `method` - `str&` - HTTP method used (`get`, `post`, `put`, `delete`, etc.)
256242
///
257243
/// # Returns
258244
///
@@ -272,10 +258,6 @@ pub fn record_monitoring_metrics_api_before(
272258
resource: &str,
273259
method: &str)
274260
{
275-
// if not compiled with the monitoring feature - return
276-
#[cfg(not(feature = "monitoring"))]
277-
return;
278-
279261
debug!("\
280262
metrics - before - uri={uri} \
281263
resource={resource} \
@@ -366,23 +348,17 @@ pub fn record_monitoring_metrics_api_before(
366348

367349
/// record_monitoring_metrics_api_after
368350
///
369-
/// This method requires building the library with the
370-
/// `monitoring` feature enabled
371-
///
372-
/// If the `monitoring` feature is not enabled this method
373-
/// immediately returns the `processed_response` variable.
374-
///
375-
/// If the `monitoring` feature is enabled, this method records
376-
/// tracked metrics using [Prometheus](https://docs.rs/prometheus/latest/prometheus/)
351+
/// This method records tracked metrics using
352+
/// [Prometheus](https://docs.rs/prometheus/latest/prometheus/)
377353
/// after the internal service handlers processed the
378354
/// request. This allows for tracking latency and status codes
379355
/// for each resource and each method.
380356
///
381357
/// # Arguments
382358
///
383359
/// * `uri` - `str&` - url sub path without the hosting fqdn address
384-
/// * `resource` - `str&` - api resource (`user`, `data`, `auth`, etc.)
385-
/// * `method` - `str&` - api resource (`user`, `data`, `auth`, etc.)
360+
/// * `resource` - `str&` - HTTP resource (`user`, `data`, `auth`, etc.)
361+
/// * `method` - `str&` - HTTP method used (`get`, `post`, `put`, `delete`, etc.)
386362
/// * `processed_response` - `str&` - existing [`Response`](hyper::Response)
387363
/// from the internal service handler
388364
///
@@ -414,10 +390,6 @@ pub fn record_monitoring_metrics_api_after(
414390
processed_response: std::result::Result<Response<Body>, Infallible>)
415391
-> std::result::Result<Response<Body>, Infallible>
416392
{
417-
// if not compiled with the monitoring feature - return
418-
#[cfg(not(feature = "monitoring"))]
419-
return processed_response;
420-
421393
if processed_response.is_ok() {
422394
let cloned_result = processed_response.unwrap();
423395
match (

0 commit comments

Comments
 (0)