Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reduced metrics push interval + used `autometrics_example_util::sleep…
…_random_duration` to simulate random work
  • Loading branch information
egtwp committed Jun 16, 2023
commit dcfad4b1778aff86f1320c459a64fa4f96e10904
1 change: 1 addition & 0 deletions examples/opentelemetry-push/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"

[dependencies]
autometrics = { path = "../../autometrics", features = ["opentelemetry"] }
autometrics-example-util = { path = "../util" }
# Note that the version of the opentelemetry crate MUST match
# the version used by autometrics
opentelemetry = { version = "0.19", features = ["metrics", "rt-tokio"] }
Expand Down
14 changes: 9 additions & 5 deletions examples/opentelemetry-push/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use autometrics::autometrics;
use autometrics_example_util::sleep_random_duration;
use opentelemetry::{runtime, Context};
use opentelemetry::sdk::export::metrics::aggregation::cumulative_temporality_selector;
use opentelemetry::sdk::metrics::controllers::BasicController;
Expand All @@ -14,6 +15,7 @@ fn init_metrics() -> metrics::Result<BasicController> {
endpoint: "http://localhost:4317".to_string(),
..ExportConfig::default()
};
let push_interval = Duration::from_secs(1);
opentelemetry_otlp::new_pipeline()
.metrics(
selectors::simple::inexpensive(),
Expand All @@ -25,25 +27,27 @@ fn init_metrics() -> metrics::Result<BasicController> {
.tonic()
.with_export_config(export_config),
)
.with_period(push_interval)
.build()
}

#[autometrics]
fn do_stuff() {
async fn do_stuff() {
println!("Doing stuff...");
sleep_random_duration().await;
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let meter_provider = init_metrics()?;
let cx = Context::current();

for _ in 0..10 {
do_stuff();
for _ in 0..100 {
do_stuff().await;
}

println!("Waiting so that we could see metrics being pushed via OTLP every 10 seconds...");
sleep(Duration::from_secs(60)).await;
println!("Waiting so that we could see metrics going down...");
sleep(Duration::from_secs(10)).await;
meter_provider.stop(&cx)?;

Ok(())
Expand Down