Skip to content

Commit 2e01644

Browse files
committed
Report believable metrics in metrics example
1 parent b9330e3 commit 2e01644

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ backtrace = "0.3.60"
4747
env_logger = "0.9.0"
4848
opentelemetry = { version = "0.16", features = ["rt-tokio"] }
4949
opentelemetry-application-insights = { path = ".", features = ["reqwest-client", "reqwest-blocking-client"] }
50+
rand = "0.8.4"
5051
surf = "2.2.0"
5152
test-case = "1.1.0"
5253
tokio = { version = "1.7.0", features = ["rt", "macros", "process", "time"] }

examples/metrics.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
use opentelemetry::{
2-
baggage::BaggageExt,
32
global,
4-
metrics::ObserverResult,
3+
metrics::{ObserverResult, Unit},
54
sdk::{self, metrics::controllers},
6-
Context, Key, KeyValue,
5+
KeyValue,
76
};
7+
use rand::{thread_rng, Rng};
88
use std::{env, time::Duration};
99

10-
fn common_attributes() -> Vec<KeyValue> {
11-
vec![
12-
KeyValue::new("A", "1"),
13-
KeyValue::new("B", "2"),
14-
KeyValue::new("C", "3"),
15-
]
16-
}
17-
1810
#[tokio::main]
1911
async fn main() {
2012
env_logger::init();
@@ -35,20 +27,40 @@ async fn main() {
3527

3628
global::set_meter_provider(controller.provider());
3729

38-
let meter = global::meter("ex.com/basic");
30+
let meter = global::meter("custom.instrumentation");
3931

40-
let one_metric_callback = |res: ObserverResult<f64>| res.observe(1.0, &common_attributes());
32+
// Observer
33+
let cpu_utilization_callback = |res: ObserverResult<f64>| {
34+
let mut rng = thread_rng();
35+
res.observe(
36+
rng.gen_range(0.1..0.2),
37+
&[KeyValue::new("state", "idle"), KeyValue::new("cpu", 0)],
38+
)
39+
};
4140
let _ = meter
42-
.f64_value_observer("ex.com.one", one_metric_callback)
43-
.with_description("A ValueObserver set to 1.0")
41+
.f64_value_observer("system.cpu.utilization", cpu_utilization_callback)
42+
.with_unit(Unit::new("1"))
4443
.init();
4544

46-
let value_recorder = meter.f64_value_recorder("ex.com.two").init();
47-
meter.record_batch_with_context(
48-
&Context::current_with_baggage(vec![Key::from("ex.com/another").string("xyz")]),
49-
&common_attributes(),
50-
vec![value_recorder.measurement(2.0)],
51-
);
45+
// Recorder
46+
let value_recorder = meter
47+
.i64_value_recorder("http.server.duration")
48+
.with_unit(Unit::new("milliseconds"))
49+
.init();
50+
let mut rng = thread_rng();
51+
for _ in 1..5 {
52+
value_recorder.record(
53+
rng.gen_range(200..300),
54+
&[
55+
KeyValue::new("http.method", "GET"),
56+
KeyValue::new("http.host", "10.1.2.4"),
57+
KeyValue::new("http.scheme", "http"),
58+
KeyValue::new("http.target", "/hello/world?name={}"),
59+
KeyValue::new("http.status_code", "200"),
60+
],
61+
);
62+
tokio::time::sleep(Duration::from_millis(500)).await;
63+
}
5264

53-
tokio::time::sleep(Duration::from_secs(5)).await;
65+
tokio::time::sleep(Duration::from_secs(3)).await;
5466
}

0 commit comments

Comments
 (0)