-
Notifications
You must be signed in to change notification settings - Fork 0
Add prometheus metrics endpoint #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
02980a8
Add basic prometheus metrics
b130fa3
Error handling + comments and formatting
091866e
Bump release version
e9ee571
Merge branch 'main' into feature/prometheus
14419ad
Re-add register_metrics call
8750e0a
Add prometheus deps
4f97972
Add more metric labels
7b93cc5
Gather system metrics too
65c6030
Remove unused import
b773d7a
Move TraceLayer to capture all requests
25bbac8
Remove commented out code
2dd0ac7
Switch to axum::middleware::from_fn
76bb58c
Merge branch 'main' into feature/prometheus
964ed6f
Ignore .vscode
3233c69
Add prometheus helpers
aa3e2ce
Remove unnecessary buffer clone/clear
sd109 ce7ab77
Merge branch 'main' into feature/prometheus
sd109 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# NOTE: This should only be used for explicitly testing the prometheus metrics | ||
# since it builds the Rust project from scratch within the container each time | ||
# so is very slow and inefficient for regular development work. | ||
services: | ||
active-storage-proxy: | ||
build: ../. | ||
ports: | ||
- "8080:8080" | ||
minio: | ||
image: minio/minio | ||
command: ["server", "data", "--console-address", ":9001"] | ||
ports: | ||
- "9000:9000" | ||
- "9001:9001" | ||
prometheus: | ||
image: prom/prometheus | ||
volumes: | ||
- type: bind | ||
source: ./prometheus.yml | ||
target: /etc/prometheus/prometheus.yml | ||
ports: | ||
- "9090:9090" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
global: | ||
scrape_interval: 5s | ||
scrape_configs: | ||
- job_name: active-storage-proxy | ||
static_configs: | ||
- targets: | ||
- "active-storage-proxy:8080" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::time::Instant; | ||
|
||
use axum::{http::Request, middleware::Next, response::IntoResponse}; | ||
use lazy_static::lazy_static; | ||
use prometheus::{self, Encoder, HistogramOpts, HistogramVec, IntCounterVec, Opts}; | ||
|
||
lazy_static! { | ||
// Simple request counter | ||
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"); | ||
// 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"); | ||
// 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"); | ||
} | ||
|
||
/// 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"); | ||
registry | ||
.register(Box::new(RESPONSE_CODE_COLLECTOR.clone())) | ||
.expect("registering prometheus metrics during initialization failed"); | ||
registry | ||
.register(Box::new(RESPONSE_TIME_COLLECTOR.clone())) | ||
.expect("registering prometheus metrics during initialization failed"); | ||
} | ||
|
||
/// Returns currently gathered prometheus metrics | ||
pub async fn metrics_handler() -> String { | ||
let encoder = prometheus::TextEncoder::new(); | ||
let mut buffer = Vec::new(); | ||
|
||
encoder | ||
.encode(&prometheus::gather(), &mut buffer) | ||
.expect("could not encode gathered metrics into temporary buffer"); | ||
|
||
String::from_utf8(buffer).expect("could not convert metrics buffer into string") | ||
} | ||
|
||
pub async fn track_metrics<B>(request: Request<B>, next: Next<B>) -> impl IntoResponse { | ||
// Extract some useful quantities | ||
let timer = Instant::now(); | ||
let http_method = &request.method().to_string().to_ascii_uppercase(); | ||
let request_path = request.uri().path().to_string(); | ||
|
||
// Increment request counter | ||
INCOMING_REQUESTS | ||
.with_label_values(&[http_method, &request_path]) | ||
.inc(); | ||
sd109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Pass request onto next layer | ||
let response = next.run(request).await; | ||
let status_code = response.status(); | ||
// Due to 'concentric shell model' for axum layers, | ||
// latency is time taken to traverse all inner | ||
// layers (including primary reduction operation) | ||
// and then back up the layer stack. | ||
let latency = timer.elapsed().as_secs_f64(); | ||
|
||
// Record response metrics | ||
RESPONSE_CODE_COLLECTOR | ||
.with_label_values(&[status_code.as_str(), http_method, &request_path]) | ||
.inc(); | ||
RESPONSE_TIME_COLLECTOR | ||
.with_label_values(&[status_code.as_str(), http_method, &request_path]) | ||
.observe(latency); | ||
|
||
response | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.