Skip to content

Commit 4fa2bc0

Browse files
authored
Print some instructions and information (#2)
These instructions include what the local address is, which endpoints there are, and how to use this with am. It is also possible to change the listen address by exporting the LISTEN_ADDRESS environment variable, which defaults to 127.0.0.1:3000.
1 parent d295e2c commit 4fa2bc0

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ cargo run
3737

3838
The application will start on a port 3000 by default and expose a metrics endpoint.
3939

40+
NOTE: You can override the listen address by exporting the `LISTEN_ADDRESS`
41+
environment variable. Note that you have to use this same address in the next
42+
step.
43+
4044
4. Start the Autometrics CLI and Explorer
4145

4246
Start the Autometrics CLI and point it to the endpoint it can scrape metrics from.

src/main.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use axum::http::StatusCode;
22
use axum::response::IntoResponse;
33
use axum::routing::get;
44
use axum::Router;
5+
use std::env;
56
use std::time::Duration;
67
use tokio::time::sleep;
78

@@ -20,7 +21,7 @@ const API_SLO: Objective = Objective::new("api")
2021
.latency(ObjectiveLatency::Ms250, ObjectivePercentile::P90);
2122

2223
#[tokio::main]
23-
async fn main() {
24+
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
2425
// Set up the exporter to collect metrics
2526
prometheus_exporter::init();
2627

@@ -33,11 +34,29 @@ async fn main() {
3334
get(|| async { prometheus_exporter::encode_http_response() }),
3435
);
3536

36-
let addr = "[::]:3000".parse().unwrap();
37-
axum::Server::bind(&addr)
38-
.serve(app.into_make_service())
39-
.await
40-
.unwrap();
37+
let addr = env::var("LISTEN_ADDRESS")
38+
.unwrap_or_else(|_| "127.0.0.1:3000".to_string())
39+
.parse()?;
40+
41+
let server = axum::Server::try_bind(&addr)?.serve(app.into_make_service());
42+
43+
let local_addr = server.local_addr();
44+
45+
eprintln!("Listening on: {local_addr}",);
46+
eprintln!("");
47+
eprintln!("The following endpoints are available: ");
48+
eprintln!("");
49+
eprintln!("- http://{local_addr}/ | static 200 response",);
50+
eprintln!("- http://{local_addr}/slow | same but it is delayed with 1 second",);
51+
eprintln!("- http://{local_addr}/error | static 500 response",);
52+
eprintln!("- http://{local_addr}/metrics | Prometheus endpoint containing the metrics",);
53+
eprintln!("");
54+
eprintln!("To see the metrics in Explorer run: `am start {local_addr}`");
55+
56+
// Start accepting and handling requests
57+
server.await?;
58+
59+
Ok(())
4160
}
4261

4362
// our main handler function that is fine

0 commit comments

Comments
 (0)