Skip to content

Commit

Permalink
update all examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mellowagain committed Dec 6, 2023
1 parent daf3095 commit 4a939da
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 51 deletions.
2 changes: 1 addition & 1 deletion autometrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ tracing-opentelemetry-0-22 = { package = "tracing-opentelemetry", version = "0.2

[dev-dependencies]
async-trait = "0.1.74"
axum = { version = "0.6", features = ["tokio"] }
axum = { version = "0.7.2", features = ["tokio"] }
criterion = "0.5"
http = "0.2"
opentelemetry = "0.21"
Expand Down
28 changes: 16 additions & 12 deletions autometrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,32 @@ See [autometrics.dev](https://docs.autometrics.dev/) for more details on the ide
Autometrics isn't tied to any web framework, but this shows how you can use the library in an [Axum](https://github.com/tokio-rs/axum) server.

```rust
use std::error::Error;
use autometrics::{autometrics, prometheus_exporter};
use axum::{routing::*, Router, Server};

// Instrument your functions with metrics
#[autometrics]
pub async fn create_user() -> Result<(), ()> {
Ok(())
Ok(())
}

// Export the metrics to Prometheus
#[tokio::main]
pub async fn main() {
prometheus_exporter::init();

let app = Router::new()
.route("/users", post(create_user))
.route(
"/metrics",
get(|| async { prometheus_exporter::encode_http_response() }),
);
Server::bind(&([127, 0, 0, 1], 0).into())
.serve(app.into_make_service());
pub async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
prometheus_exporter::init();

let app = Router::new()
.route("/users", post(create_user))
.route(
"/metrics",
get(|| async { prometheus_exporter::encode_http_response() }),
);


let listener = TcpListener::bind((Ipv4Addr::from([127, 0, 0, 1]), 0)).await?;
axum::serve(listener, app).await?;
Ok(())
}
```

Expand Down
24 changes: 13 additions & 11 deletions autometrics/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ pub async fn create_user() -> Result<(), ()> {

// Export the metrics to Prometheus
#[tokio::main]
pub async fn main() {
prometheus_exporter::init();

let app = Router::new()
.route("/users", post(create_user))
.route(
"/metrics",
get(|| async { prometheus_exporter::encode_http_response() }),
);
Server::bind(&([127, 0, 0, 1], 0).into())
.serve(app.into_make_service());
pub async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
prometheus_exporter::init();

let app = Router::new()
.route("/users", post(create_user))
.route(
"/metrics",
get(|| async { prometheus_exporter::encode_http_response() }),
);

let listener = TcpListener::bind((Ipv4Addr::from([127, 0, 0, 1]), 0)).await?;
axum::serve(listener, app).await?;
Ok(())
}
```

Expand Down
2 changes: 1 addition & 1 deletion examples/exemplars-tracing-opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ autometrics = { path = "../../autometrics", features = [
"exemplars-tracing-opentelemetry-0_22",
] }
autometrics-example-util = { path = "../util" }
axum = { version = "0.6", features = ["json"] }
axum = { version = "0.7.2", features = ["json"] }
opentelemetry = "0.20"
opentelemetry-stdout = { version = "0.1", features = ["trace"] }
reqwest = { version = "0.11", features = ["json"] }
Expand Down
20 changes: 12 additions & 8 deletions examples/exemplars-tracing-opentelemetry/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use autometrics::{autometrics, prometheus_exporter};
use autometrics_example_util::run_prometheus;
use axum::{routing::get, Router, Server};
use axum::{routing::get, Router, Server, ServiceExt};
use opentelemetry::sdk::trace::TracerProvider;
use opentelemetry::trace::TracerProvider as _;
use opentelemetry_stdout::SpanExporter;
use std::error::Error;
use std::net::Ipv4Addr;
use std::{io, net::SocketAddr, time::Duration};
use tokio::net::TcpListener;
use tracing::{instrument, trace};
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::{layer::SubscriberExt, prelude::*, Registry};
Expand All @@ -28,7 +31,7 @@ fn inner_function() {
}

#[tokio::main]
async fn main() {
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// Run Prometheus with flag --enable-feature=exemplars-storage
let _prometheus = run_prometheus(true);
tokio::spawn(generate_random_traffic());
Expand All @@ -54,17 +57,18 @@ async fn main() {
get(|| async { prometheus_exporter::encode_http_response() }),
);

let server = Server::bind(&([127, 0, 0, 1], 3000).into());

println!("\nVisit the following URL to see one of the charts along with the exemplars:");
println!("http://localhost:9090/graph?g0.expr=%23%20Rate%20of%20calls%20to%20the%20%60outer_function%60%20function%20per%20second%2C%20averaged%20over%205%20minute%20windows%0A%0Asum%20by%20(function%2C%20module%2C%20commit%2C%20version)%20(rate(%7B__name__%3D~%22function_calls(_count)%3F(_total)%3F%22%2Cfunction%3D%22outer_function%22%7D%5B5m%5D)%20*%20on%20(instance%2C%20job)%20group_left(version%2C%20commit)%20last_over_time(build_info%5B1s%5D))&g0.tab=0&g0.stacked=0&g0.show_exemplars=1&g0.range_input=1h");

server
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
.await
.expect("Error starting example API server");
let listener = TcpListener::bind((Ipv4Addr::from([127, 0, 0, 1]), 3000)).await?;
axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await?;

opentelemetry::global::shutdown_tracer_provider();
Ok(())
}

pub async fn generate_random_traffic() {
Expand Down
2 changes: 1 addition & 1 deletion examples/exemplars-tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ autometrics = { path = "../../autometrics", features = [
"exemplars-tracing"
] }
autometrics-example-util = { path = "../util" }
axum = { version = "0.6", features = ["json"] }
axum = { version = "0.7.2", features = ["json"] }
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
Expand Down
15 changes: 7 additions & 8 deletions examples/exemplars-tracing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use autometrics::{
};
use autometrics_example_util::run_prometheus;
use axum::{http::header::CONTENT_TYPE, response::Response, routing::get, Router};
use std::error::Error;
use std::net::Ipv4Addr;
use std::{net::SocketAddr, time::Duration};
use tokio::net::TcpListener;
use tracing::{instrument, trace};
use tracing_subscriber::{prelude::*, EnvFilter};
use uuid::Uuid;
Expand All @@ -27,7 +30,7 @@ fn inner_function(param: &str) {
}

#[tokio::main]
async fn main() {
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// Run Prometheus with flag --enable-feature=exemplars-storage
let _prometheus = run_prometheus(true);
tokio::spawn(generate_random_traffic());
Expand All @@ -46,16 +49,12 @@ async fn main() {
get(|| async { prometheus_exporter::encode_http_response() }),
);

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let server = axum::Server::bind(&addr);

println!("\nVisit the following URL to see one of the charts along with the exemplars:");
println!("http://localhost:9090/graph?g0.expr=%23%20Rate%20of%20calls%20to%20the%20%60outer_function%60%20function%20per%20second%2C%20averaged%20over%205%20minute%20windows%0A%0Asum%20by%20(function%2C%20module%2C%20commit%2C%20version)%20(rate(%7B__name__%3D~%22function_calls(_count)%3F(_total)%3F%22%2Cfunction%3D%22outer_function%22%7D%5B5m%5D)%20*%20on%20(instance%2C%20job)%20group_left(version%2C%20commit)%20last_over_time(build_info%5B1s%5D))&g0.tab=0&g0.stacked=0&g0.show_exemplars=1&g0.range_input=1h");

server
.serve(app.into_make_service())
.await
.expect("Error starting example API server");
let listener = TcpListener::bind((Ipv4Addr::from([127, 0, 0, 1]), 3000)).await?;
axum::serve(listener, app).await?;
Ok(())
}

pub async fn generate_random_traffic() {
Expand Down
2 changes: 1 addition & 1 deletion examples/full-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
autometrics = { path = "../../autometrics", features = ["prometheus-exporter"] }
autometrics-example-util = { path = "../util" }
axum = { version = "0.6", features = ["json"] }
axum = { version = "0.7.2", features = ["json"] }
rand = "0.8"
reqwest = { version = "0.11", features = ["json"] }
serde = { version = "1", features = ["derive"] }
Expand Down
16 changes: 8 additions & 8 deletions examples/full-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use autometrics::prometheus_exporter;
use autometrics_example_util::run_prometheus;
use axum::routing::{get, post};
use axum::Router;
use std::net::SocketAddr;
use std::error::Error;
use std::net::{Ipv4Addr, SocketAddr};
use tokio::net::TcpListener;

mod database;
mod error;
Expand All @@ -13,7 +15,7 @@ mod util;

/// Run the API server as well as Prometheus and a traffic generator
#[tokio::main]
async fn main() {
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// Run Prometheus and generate random traffic for the app
// (You would not actually do this in production, but it makes it easier to see the example in action)
let _prometheus = run_prometheus(false);
Expand All @@ -33,8 +35,8 @@ async fn main() {
)
.with_state(Database::new());

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let server = axum::Server::bind(&addr);
let listener = TcpListener::bind((Ipv4Addr::from([127, 0, 0, 1]), 3000)).await?;
let addr = listener.local_addr()?;

println!(
"The example API server is now running on: {addr}
Expand All @@ -45,8 +47,6 @@ Then, hover over one of the HTTP handler functions (in your editor) to bring up
Click on one of the Autometrics links to see the graph for that handler's metrics in Prometheus."
);

server
.serve(app.into_make_service())
.await
.expect("Error starting example API server");
axum::serve(listener, app).await?;
Ok(())
}

0 comments on commit 4a939da

Please sign in to comment.