more telemetry

This commit is contained in:
oliver 2024-10-10 23:40:00 +00:00
parent 7de9830f66
commit e73b29c2a3
2 changed files with 16 additions and 9 deletions

4
Rocket.toml Normal file
View File

@ -0,0 +1,4 @@
[default]
address = "0.0.0.0"
port = 8000
cli_colors = false

View File

@ -1,6 +1,6 @@
use opentelemetry::{global, metrics::Counter, KeyValue};
use prometheus::Registry;
use rocket::{fairing::{Fairing, Info, Kind}, get, routes, Request, Response, State};
use rocket::{fairing::{Fairing, Info, Kind}, fs::FileServer, get, routes, Request, Response, State};
use tracing::error;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use std::process;
@ -12,7 +12,7 @@ struct MetricsState {
#[rocket::main]
async fn main() -> Result<(), tracing_loki::Error> {
let logs_provider = init_logs_provider("http://127.0.0.1:3100");
let logs_provider = init_logs_provider("http://10.0.0.100:3100");
let (meter_provider, registry) = init_meter_provider();
// Create a meter from the above MeterProvider.
@ -22,6 +22,7 @@ async fn main() -> Result<(), tracing_loki::Error> {
let counter = meter.u64_counter("my_counter").init();
let _ = rocket::build()
.mount("/", FileServer::from("www/"))
.mount("/", routes![metrics, hello_world])
.manage(registry)
.attach(MetricsState { counter })
@ -32,7 +33,7 @@ async fn main() -> Result<(), tracing_loki::Error> {
Ok(())
}
#[get("/")]
#[get("/hello_world")]
fn hello_world() -> &'static str {
"Hello world."
}
@ -102,15 +103,17 @@ impl Fairing for MetricsState {
let query = req.query_fields().map(|f| format!("{}={},", f.name, f.value)).collect::<String>();
let ip = req.client_ip().map_or_else(|| "N/A".to_string(), |ip| ip.to_string());
let agent = req.headers().get_one("USER-AGENT").unwrap_or("N/A").to_owned();
let referer = req.headers().get_one("Referer").unwrap_or("N/A").to_owned();
self.counter.add(
1, &[
KeyValue::new("Path", path),
KeyValue::new("Method", method),
KeyValue::new("Status", status),
KeyValue::new("Query", query),
KeyValue::new("IP", ip),
KeyValue::new("User-Agent", agent),
KeyValue::new("path", path),
KeyValue::new("method", method),
KeyValue::new("status", status),
KeyValue::new("query", query),
KeyValue::new("ip", ip),
KeyValue::new("user-agent", agent),
KeyValue::new("referer", referer),
], );
}
}