Skip to content

Commit

Permalink
Merge pull request #76 from frigus02/exp-histo
Browse files Browse the repository at this point in the history
Support exporting ExponentialHistogram
  • Loading branch information
frigus02 authored May 15, 2024
2 parents daea8aa + a05783e commit c2fc74c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Set tags Cloud role and Cloud role instance also from resource attributes `k8s.{deployment,replicaset,statefulset,job,cronjob,daemonset,pod}.name`. This matches [the behavior of the JS exporter](https://github.com/Azure/azure-sdk-for-js/blob/c66cad23c4b803719db65cb48a453b0adc13307b/sdk/monitor/monitor-opentelemetry-exporter/src/utils/common.ts#L75-L138).
- Remove option to configure temporality seletor (`with_temporality_selector`). Application Insights supports delta temporality only as defined in the [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/58bfe48eabe887545198d66c43f44071b822373f/specification/metrics/sdk_exporters/otlp.md?plain=1#L46-L47).
- Add support for `ExponentialHistogram` export.

## [0.31.0] - 2024-05-09

Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,12 @@ async fn main() {
//! Metrics get reported to Application Insights as Metric Data. The [`Aggregation`] determines how
//! the data is represented.
//!
//! | Aggregator | Data representation |
//! | ---------- | -------------------------------------------------------------------- |
//! | Histogram | aggregation with sum, count, min, and max (buckets are not exported) |
//! | Gauge | one measurement |
//! | Sum | aggregation with only a value |
//! | Aggregator | Data representation |
//! | -------------------- | -------------------------------------------------------------------- |
//! | Histogram | aggregation with sum, count, min, and max (buckets are not exported) |
//! | ExponentialHistogram | aggregation with sum, count, min, and max (buckets are not exported) |
//! | Gauge | one measurement |
//! | Sum | aggregation with only a value |
//!
//! [`Aggregation`]: https://docs.rs/opentelemetry/0.20.0/opentelemetry/sdk/metrics/data/trait.Aggregation.html
#![doc(html_root_url = "https://docs.rs/opentelemetry-application-insights/0.31.0")]
Expand Down
34 changes: 33 additions & 1 deletion src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use opentelemetry::{
use opentelemetry_http::HttpClient;
use opentelemetry_sdk::{
metrics::{
data::{Gauge, Histogram, Metric, ResourceMetrics, Sum, Temporality},
data::{ExponentialHistogram, Gauge, Histogram, Metric, ResourceMetrics, Sum, Temporality},
exporter::PushMetricsExporter,
reader::{AggregationSelector, TemporalitySelector},
Aggregation, InstrumentKind,
Expand Down Expand Up @@ -152,6 +152,12 @@ fn map_metric(metric: &Metric) -> Vec<EnvelopeData> {
map_histogram(metric, histogram)
} else if let Some(histogram) = data.downcast_ref::<Histogram<f64>>() {
map_histogram(metric, histogram)
} else if let Some(exp_histogram) = data.downcast_ref::<ExponentialHistogram<i64>>() {
map_exponential_histogram(metric, exp_histogram)
} else if let Some(exp_histogram) = data.downcast_ref::<ExponentialHistogram<u64>>() {
map_exponential_histogram(metric, exp_histogram)
} else if let Some(exp_histogram) = data.downcast_ref::<ExponentialHistogram<f64>>() {
map_exponential_histogram(metric, exp_histogram)
} else if let Some(sum) = data.downcast_ref::<Sum<u64>>() {
map_sum(metric, sum)
} else if let Some(sum) = data.downcast_ref::<Sum<i64>>() {
Expand Down Expand Up @@ -208,6 +214,32 @@ fn map_histogram<T: ToF64Lossy>(metric: &Metric, histogram: &Histogram<T>) -> Ve
.collect()
}

fn map_exponential_histogram<T: ToF64Lossy>(
metric: &Metric,
exp_histogram: &ExponentialHistogram<T>,
) -> Vec<EnvelopeData> {
exp_histogram
.data_points
.iter()
.map(|data_point| {
let time = data_point.time;
let data = DataPoint {
ns: None,
name: metric.name.clone().into(),
kind: Some(DataPointType::Aggregation {
count: Some(data_point.count.try_into().unwrap_or_default()),
min: data_point.min.as_ref().map(ToF64Lossy::to_f64_lossy),
max: data_point.max.as_ref().map(ToF64Lossy::to_f64_lossy),
std_dev: None,
}),
value: data_point.sum.to_f64_lossy(),
};
let attrs = data_point.attributes.to_owned();
EnvelopeData { time, data, attrs }
})
.collect()
}

fn map_sum<T: ToF64Lossy>(metric: &Metric, sum: &Sum<T>) -> Vec<EnvelopeData> {
sum.data_points
.iter()
Expand Down

0 comments on commit c2fc74c

Please sign in to comment.