Skip to content

Commit

Permalink
Include other metric types in user-events metrics exporter (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
lzchen and cijothomas authored Jan 31, 2024
1 parent 65eccba commit c56e309
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 131 deletions.
3 changes: 3 additions & 0 deletions opentelemetry-user-events-metrics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Fixed a bug which caused Histogram, Gauge metrics to be dropped.
[#30](https://github.com/open-telemetry/opentelemetry-rust-contrib/pull/30).

## v0.2.1

- Update eventheader version to 0.3.4.
Expand Down
111 changes: 100 additions & 11 deletions opentelemetry-user-events-metrics/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use opentelemetry_sdk::{
runtime, Resource,
};
use opentelemetry_user_events_metrics::MetricsExporter;
use std::thread;
use std::time::Duration;

fn init_metrics(exporter: MetricsExporter) -> SdkMeterProvider {
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
Expand All @@ -32,22 +34,109 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Some("test_url"),
Some(vec![KeyValue::new("key", "value")]),
);
let c = meter
// Create a Counter Instrument.
let counter = meter
.f64_counter("counter_test")
.with_description("test_decription")
.with_unit(Unit::new("test_unit"))
.init();
// Create a UpCounter Instrument.
let updown_counter = meter.i64_up_down_counter("up_down_counter_test").init();

c.add(
1.0,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
);
// Create a Histogram Instrument.
let histogram = meter
.f64_histogram("histogram_test")
.with_description("test_description")
.init();

// Create a ObservableGauge instrument and register a callback that reports the measurement.
let gauge = meter
.f64_observable_gauge("gauge_test")
.with_unit(Unit::new("test_unit"))
.with_description("test_descriptionn")
.init();

meter.register_callback(&[gauge.as_any()], move |observer| {
observer.observe_f64(
&gauge,
1.0,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
)
})?;

// Create a ObservableCounter instrument and register a callback that reports the measurement.
let observable_counter = meter
.u64_observable_counter("obs_counter_test")
.with_description("test_description")
.with_unit(Unit::new("tesT_unit"))
.init();

meter.register_callback(&[observable_counter.as_any()], move |observer| {
observer.observe_u64(
&observable_counter,
100,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
)
})?;

// Create a Observable UpDownCounter instrument and register a callback that reports the measurement.
let observable_up_down_counter = meter
.i64_observable_up_down_counter("obs_up_down_counter_test")
.with_description("test_description")
.with_unit(Unit::new("test_unit"))
.init();

meter.register_callback(&[observable_up_down_counter.as_any()], move |observer| {
observer.observe_i64(
&observable_up_down_counter,
100,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
)
})?;

loop {
// Record measurements using the Counter instrument.
counter.add(
1.0,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
);

meter_provider.shutdown()?;
// Record measurements using the UpCounter instrument.
updown_counter.add(
-10,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
);

Ok(())
// Record measurements using the histogram instrument.
histogram.record(
10.5,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
);
// Sleep for 1 second
thread::sleep(Duration::from_secs(1));
}
}
4 changes: 2 additions & 2 deletions opentelemetry-user-events-metrics/src/exporter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::transform::transform_resource_metrics;
use async_trait::async_trait;
use opentelemetry::metrics::{MetricsError, Result};
use opentelemetry_proto::tonic::collector::metrics::v1::ExportMetricsServiceRequest;
use opentelemetry_sdk::metrics::{
data::{ResourceMetrics, Temporality},
exporter::PushMetricsExporter,
Expand Down Expand Up @@ -69,7 +69,7 @@ impl Debug for MetricsExporter {
impl PushMetricsExporter for MetricsExporter {
async fn export(&self, metrics: &mut ResourceMetrics) -> Result<()> {
if self.trace_point.enabled() {
let proto_message = transform_resource_metrics(metrics);
let proto_message: ExportMetricsServiceRequest = (&*metrics).into();

let mut byte_array = Vec::new();
let _encode_result = proto_message
Expand Down
1 change: 0 additions & 1 deletion opentelemetry-user-events-metrics/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod exporter;
mod tracepoint;
mod transform;

pub use exporter::MetricsExporter;
117 changes: 0 additions & 117 deletions opentelemetry-user-events-metrics/src/transform/mod.rs

This file was deleted.

0 comments on commit c56e309

Please sign in to comment.