more telemetry
This commit is contained in:
		
							
								
								
									
										4
									
								
								Rocket.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								Rocket.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					[default]
 | 
				
			||||||
 | 
					address = "0.0.0.0"
 | 
				
			||||||
 | 
					port = 8000
 | 
				
			||||||
 | 
					cli_colors = false
 | 
				
			||||||
							
								
								
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -1,6 +1,6 @@
 | 
				
			|||||||
use opentelemetry::{global, metrics::Counter, KeyValue};
 | 
					use opentelemetry::{global, metrics::Counter, KeyValue};
 | 
				
			||||||
use prometheus::Registry;
 | 
					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::error;
 | 
				
			||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
 | 
					use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
 | 
				
			||||||
use std::process;
 | 
					use std::process;
 | 
				
			||||||
@@ -12,7 +12,7 @@ struct MetricsState {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[rocket::main]
 | 
					#[rocket::main]
 | 
				
			||||||
async fn main() -> Result<(), tracing_loki::Error> {
 | 
					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();
 | 
					    let (meter_provider, registry) = init_meter_provider();
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    // Create a meter from the above MeterProvider.
 | 
					    // 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 counter = meter.u64_counter("my_counter").init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let _ = rocket::build()
 | 
					    let _ = rocket::build()
 | 
				
			||||||
 | 
					        .mount("/", FileServer::from("www/"))
 | 
				
			||||||
        .mount("/", routes![metrics, hello_world])
 | 
					        .mount("/", routes![metrics, hello_world])
 | 
				
			||||||
        .manage(registry)
 | 
					        .manage(registry)
 | 
				
			||||||
        .attach(MetricsState { counter })
 | 
					        .attach(MetricsState { counter })
 | 
				
			||||||
@@ -32,7 +33,7 @@ async fn main() -> Result<(), tracing_loki::Error> {
 | 
				
			|||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[get("/")]
 | 
					#[get("/hello_world")]
 | 
				
			||||||
fn hello_world() -> &'static str {
 | 
					fn hello_world() -> &'static str {
 | 
				
			||||||
    "Hello world." 
 | 
					    "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 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 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 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(
 | 
					        self.counter.add(
 | 
				
			||||||
            1, &[
 | 
					            1, &[
 | 
				
			||||||
                KeyValue::new("Path", path),
 | 
					                KeyValue::new("path", path),
 | 
				
			||||||
                KeyValue::new("Method", method),
 | 
					                KeyValue::new("method", method),
 | 
				
			||||||
                KeyValue::new("Status", status),
 | 
					                KeyValue::new("status", status),
 | 
				
			||||||
                KeyValue::new("Query", query),
 | 
					                KeyValue::new("query", query),
 | 
				
			||||||
                KeyValue::new("IP", ip),
 | 
					                KeyValue::new("ip", ip),
 | 
				
			||||||
                KeyValue::new("User-Agent", agent),
 | 
					                KeyValue::new("user-agent", agent),
 | 
				
			||||||
 | 
					                KeyValue::new("referer", referer),
 | 
				
			||||||
            ], );
 | 
					            ], );
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user