Skip to content

Commit

Permalink
Start adding metrics advanced (view) examples (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed Jul 29, 2023
1 parent 9f28319 commit 6962621
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [
"opentelemetry-otlp/examples/basic-otlp-http",
"opentelemetry-otlp/examples/external-otlp-grpcio-async-std",
"examples/metrics-basic",
"examples/metrics-advanced",
"examples/logs-basic",
"examples/traceresponse",
"examples/tracing-grpc",
Expand Down
12 changes: 12 additions & 0 deletions examples/metrics-advanced/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "metrics-advanced"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
publish = false

[dependencies]
opentelemetry_api = { path = "../../opentelemetry-api", features = ["metrics"] }
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["metrics", "rt-tokio"] }
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["metrics"]}
tokio = { version = "1.0", features = ["full"] }
17 changes: 17 additions & 0 deletions examples/metrics-advanced/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Metric SDK Advanced Configuration Example

This example shows how to customize the OpenTelemetry Rust Metric SDK. This
shows how to change temporality, how to customize the aggregation using the
concept of "Views" etc. The examples write output to stdout, but could be
replaced with other exporters.

## Usage

Run the following, and the Metrics will be written out to stdout.

```shell
$ cargo run
```



94 changes: 94 additions & 0 deletions examples/metrics-advanced/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use opentelemetry_api::metrics::Unit;
use opentelemetry_api::Key;
use opentelemetry_api::{metrics::MeterProvider as _, KeyValue};
use opentelemetry_sdk::metrics::{Instrument, MeterProvider, PeriodicReader, Stream};
use opentelemetry_sdk::{runtime, Resource};
use std::error::Error;

fn init_meter_provider() -> MeterProvider {
// for example 1
let my_view_rename_and_unit = |i: &Instrument| {
if i.name == "my_histogram" {
Some(
Stream::new()
.name("my_histogram_renamed")
.unit(Unit::new("milliseconds")),
)
} else {
None
}
};

// for example 2
let my_view_drop_attributes = |i: &Instrument| {
if i.name == "my_counter" {
Some(Stream::new().allowed_attribute_keys(vec![Key::from("mykey1")]))
} else {
None
}
};

let exporter = opentelemetry_stdout::MetricsExporter::default();
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
MeterProvider::builder()
.with_reader(reader)
.with_resource(Resource::new(vec![KeyValue::new(
"service.name",
"metrics-advanced-example",
)]))
.with_view(my_view_rename_and_unit)
.with_view(my_view_drop_attributes)
.build()
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let meter_provider = init_meter_provider();
let meter = meter_provider.meter("mylibraryname");

// Example 1 - Rename metric using View.
// This instrument will be renamed to "my_histogram_renamed",
// and its unit changed to "milliseconds"
// using view.
let histogram = meter
.f64_histogram("my_histogram")
.with_unit(Unit::new("ms"))
.with_description("My histogram example description")
.init();

// Record measurements using the histogram instrument.
histogram.record(
10.5,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
KeyValue::new("mykey3", "myvalue3"),
KeyValue::new("mykey4", "myvalue4"),
]
.as_ref(),
);

// Example 2 - Drop unwanted attributes using view.
let counter = meter.u64_counter("my_counter").init();

// Record measurements using the Counter instrument.
// Though we are passing 4 attributes here, only 1 will be used
// for aggregation as view is configured to use only "mykey1"
// attribute.
counter.add(
10,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
KeyValue::new("mykey3", "myvalue3"),
KeyValue::new("mykey4", "myvalue4"),
]
.as_ref(),
);

// Metrics are exported by default every 30 seconds when using stdout exporter,
// however shutting down the MeterProvider here instantly flushes
// the metrics, instead of waiting for the 30 sec interval.
meter_provider.shutdown()?;
Ok(())
}
4 changes: 2 additions & 2 deletions examples/metrics-basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
.as_ref(),
);

// Note that there is no ObservableHistogram instruments.
// Note that there is no ObservableHistogram instrument.

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

// Note that Gauge only has a Observable version.

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

0 comments on commit 6962621

Please sign in to comment.