Skip to content

Commit 6962621

Browse files
authored
Start adding metrics advanced (view) examples (#1170)
1 parent 9f28319 commit 6962621

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ members = [
2727
"opentelemetry-otlp/examples/basic-otlp-http",
2828
"opentelemetry-otlp/examples/external-otlp-grpcio-async-std",
2929
"examples/metrics-basic",
30+
"examples/metrics-advanced",
3031
"examples/logs-basic",
3132
"examples/traceresponse",
3233
"examples/tracing-grpc",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "metrics-advanced"
3+
version = "0.1.0"
4+
edition = "2021"
5+
license = "Apache-2.0"
6+
publish = false
7+
8+
[dependencies]
9+
opentelemetry_api = { path = "../../opentelemetry-api", features = ["metrics"] }
10+
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["metrics", "rt-tokio"] }
11+
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["metrics"]}
12+
tokio = { version = "1.0", features = ["full"] }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Metric SDK Advanced Configuration Example
2+
3+
This example shows how to customize the OpenTelemetry Rust Metric SDK. This
4+
shows how to change temporality, how to customize the aggregation using the
5+
concept of "Views" etc. The examples write output to stdout, but could be
6+
replaced with other exporters.
7+
8+
## Usage
9+
10+
Run the following, and the Metrics will be written out to stdout.
11+
12+
```shell
13+
$ cargo run
14+
```
15+
16+
17+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use opentelemetry_api::metrics::Unit;
2+
use opentelemetry_api::Key;
3+
use opentelemetry_api::{metrics::MeterProvider as _, KeyValue};
4+
use opentelemetry_sdk::metrics::{Instrument, MeterProvider, PeriodicReader, Stream};
5+
use opentelemetry_sdk::{runtime, Resource};
6+
use std::error::Error;
7+
8+
fn init_meter_provider() -> MeterProvider {
9+
// for example 1
10+
let my_view_rename_and_unit = |i: &Instrument| {
11+
if i.name == "my_histogram" {
12+
Some(
13+
Stream::new()
14+
.name("my_histogram_renamed")
15+
.unit(Unit::new("milliseconds")),
16+
)
17+
} else {
18+
None
19+
}
20+
};
21+
22+
// for example 2
23+
let my_view_drop_attributes = |i: &Instrument| {
24+
if i.name == "my_counter" {
25+
Some(Stream::new().allowed_attribute_keys(vec![Key::from("mykey1")]))
26+
} else {
27+
None
28+
}
29+
};
30+
31+
let exporter = opentelemetry_stdout::MetricsExporter::default();
32+
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
33+
MeterProvider::builder()
34+
.with_reader(reader)
35+
.with_resource(Resource::new(vec![KeyValue::new(
36+
"service.name",
37+
"metrics-advanced-example",
38+
)]))
39+
.with_view(my_view_rename_and_unit)
40+
.with_view(my_view_drop_attributes)
41+
.build()
42+
}
43+
44+
#[tokio::main]
45+
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
46+
let meter_provider = init_meter_provider();
47+
let meter = meter_provider.meter("mylibraryname");
48+
49+
// Example 1 - Rename metric using View.
50+
// This instrument will be renamed to "my_histogram_renamed",
51+
// and its unit changed to "milliseconds"
52+
// using view.
53+
let histogram = meter
54+
.f64_histogram("my_histogram")
55+
.with_unit(Unit::new("ms"))
56+
.with_description("My histogram example description")
57+
.init();
58+
59+
// Record measurements using the histogram instrument.
60+
histogram.record(
61+
10.5,
62+
[
63+
KeyValue::new("mykey1", "myvalue1"),
64+
KeyValue::new("mykey2", "myvalue2"),
65+
KeyValue::new("mykey3", "myvalue3"),
66+
KeyValue::new("mykey4", "myvalue4"),
67+
]
68+
.as_ref(),
69+
);
70+
71+
// Example 2 - Drop unwanted attributes using view.
72+
let counter = meter.u64_counter("my_counter").init();
73+
74+
// Record measurements using the Counter instrument.
75+
// Though we are passing 4 attributes here, only 1 will be used
76+
// for aggregation as view is configured to use only "mykey1"
77+
// attribute.
78+
counter.add(
79+
10,
80+
[
81+
KeyValue::new("mykey1", "myvalue1"),
82+
KeyValue::new("mykey2", "myvalue2"),
83+
KeyValue::new("mykey3", "myvalue3"),
84+
KeyValue::new("mykey4", "myvalue4"),
85+
]
86+
.as_ref(),
87+
);
88+
89+
// Metrics are exported by default every 30 seconds when using stdout exporter,
90+
// however shutting down the MeterProvider here instantly flushes
91+
// the metrics, instead of waiting for the 30 sec interval.
92+
meter_provider.shutdown()?;
93+
Ok(())
94+
}

examples/metrics-basic/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
104104
.as_ref(),
105105
);
106106

107-
// Note that there is no ObservableHistogram instruments.
107+
// Note that there is no ObservableHistogram instrument.
108108

109109
// Create a ObservableGauge instrument and register a callback that reports the measurement.
110110
let gauge = meter
@@ -128,7 +128,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
128128

129129
// Note that Gauge only has a Observable version.
130130

131-
// Metrics are exported by default every 30 seconds,
131+
// Metrics are exported by default every 30 seconds when using stdout exporter,
132132
// however shutting down the MeterProvider here instantly flushes
133133
// the metrics, instead of waiting for the 30 sec interval.
134134
meter_provider.shutdown()?;

0 commit comments

Comments
 (0)