Skip to content

Commit

Permalink
Modify opentelemetry_sdk to be no_std compatible (part 2)
Browse files Browse the repository at this point in the history
- Remove timestamps from OTEL metric instruments

We don't really need time fields here since they're ignored by the BulkExporter anyway which injects its own time and only looks at the values

Change-Id: I4f94cdeab73760880b88ff0a8b5e642e0763fe95
  • Loading branch information
msilezin committed Aug 2, 2024
1 parent 1307898 commit d5f9a4c
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ edition = "2021"
rust-version = "1.65"

[dependencies]
oak_core = { path = "../../../../oak_core" }
opentelemetry_rk = { version = "0.23", path = "../opentelemetry/" }
async-std = { version = "1.10", features = ["unstable"], optional = true }
hashbrown = "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate alloc;

use alloc::borrow::Cow;
use core::{any, fmt};
use std::time::SystemTime;

use opentelemetry_rk::KeyValue;

Expand Down Expand Up @@ -101,10 +100,6 @@ pub struct DataPoint<T> {
/// Attributes is the set of key value pairs that uniquely identify the
/// time series.
pub attributes: Vec<KeyValue>,
/// The time when the time series was started.
pub start_time: Option<SystemTime>,
/// The time when the time series was recorded.
pub time: Option<SystemTime>,
/// The value of this data point.
pub value: T,
/// The sampled [Exemplar]s collected during the time series.
Expand All @@ -115,8 +110,6 @@ impl<T: Copy> Clone for DataPoint<T> {
fn clone(&self) -> Self {
Self {
attributes: self.attributes.clone(),
start_time: self.start_time,
time: self.time,
value: self.value,
exemplars: self.exemplars.clone(),
}
Expand Down Expand Up @@ -147,10 +140,6 @@ impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Histogram<T> {
pub struct HistogramDataPoint<T> {
/// The set of key value pairs that uniquely identify the time series.
pub attributes: Vec<KeyValue>,
/// The time when the time series was started.
pub start_time: SystemTime,
/// The time when the time series was recorded.
pub time: SystemTime,

/// The number of updates this histogram has been calculated with.
pub count: u64,
Expand All @@ -176,8 +165,6 @@ impl<T: Copy> Clone for HistogramDataPoint<T> {
fn clone(&self) -> Self {
Self {
attributes: self.attributes.clone(),
start_time: self.start_time,
time: self.time,
count: self.count,
bounds: self.bounds.clone(),
bucket_counts: self.bucket_counts.clone(),
Expand Down Expand Up @@ -214,10 +201,6 @@ impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for ExponentialHistogram
pub struct ExponentialHistogramDataPoint<T> {
/// The set of key value pairs that uniquely identify the time series.
pub attributes: Vec<KeyValue>,
/// When the time series was started.
pub start_time: SystemTime,
/// The time when the time series was recorded.
pub time: SystemTime,

/// The number of updates this histogram has been calculated with.
pub count: usize,
Expand Down Expand Up @@ -277,8 +260,6 @@ pub struct Exemplar<T> {
/// The attributes recorded with the measurement but filtered out of the
/// time series' aggregated data.
pub filtered_attributes: Vec<KeyValue>,
/// The time when the measurement was recorded.
pub time: SystemTime,
/// The measured value.
pub value: T,
/// The ID of the span that was active during the measurement.
Expand All @@ -295,7 +276,6 @@ impl<T: Copy> Clone for Exemplar<T> {
fn clone(&self) -> Self {
Self {
filtered_attributes: self.filtered_attributes.clone(),
time: self.time,
value: self.value,
span_id: self.span_id,
trace_id: self.trace_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ mod tests {
DataPoint, ExponentialBucket, ExponentialHistogram, ExponentialHistogramDataPoint,
Histogram, HistogramDataPoint, Sum,
};
use std::{time::SystemTime, vec};

use std::vec;

use super::*;

Expand All @@ -230,8 +231,6 @@ mod tests {
let mut a = Gauge {
data_points: vec![DataPoint {
attributes: vec![KeyValue::new("a", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 1u64,
exemplars: vec![],
}],
Expand All @@ -257,15 +256,11 @@ mod tests {
data_points: vec![
DataPoint {
attributes: vec![KeyValue::new("a1", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 1u64,
exemplars: vec![],
},
DataPoint {
attributes: vec![KeyValue::new("a2", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 2u64,
exemplars: vec![],
},
Expand Down Expand Up @@ -300,15 +295,11 @@ mod tests {
data_points: vec![
DataPoint {
attributes: vec![KeyValue::new("a1", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 1u64,
exemplars: vec![],
},
DataPoint {
attributes: vec![KeyValue::new("a2", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 2u64,
exemplars: vec![],
},
Expand Down Expand Up @@ -343,8 +334,6 @@ mod tests {
let mut a = Histogram {
data_points: vec![HistogramDataPoint {
attributes: vec![KeyValue::new("a1", 1)],
start_time: SystemTime::now(),
time: SystemTime::now(),
count: 2,
bounds: vec![1.0, 2.0],
bucket_counts: vec![0, 1, 1],
Expand Down Expand Up @@ -386,8 +375,6 @@ mod tests {
let mut a = ExponentialHistogram {
data_points: vec![ExponentialHistogramDataPoint {
attributes: vec![KeyValue::new("a1", 1)],
start_time: SystemTime::now(),
time: SystemTime::now(),
count: 2,
min: None,
max: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, f64::consts::LOG2_E, sync::Mutex, time::SystemTime};
use std::{collections::HashMap, f64::consts::LOG2_E, sync::Mutex};

use once_cell::sync::Lazy;
use opentelemetry_rk::{metrics::MetricsError, KeyValue};
Expand Down Expand Up @@ -318,8 +318,6 @@ pub(crate) struct ExpoHistogram<T> {
max_scale: i8,

values: Mutex<HashMap<AttributeSet, ExpoHistogramDataPoint<T>>>,

start: Mutex<SystemTime>,
}

impl<T: Number<T>> ExpoHistogram<T> {
Expand All @@ -336,7 +334,6 @@ impl<T: Number<T>> ExpoHistogram<T> {
max_size: max_size as i32,
max_scale,
values: Mutex::new(HashMap::default()),
start: Mutex::new(SystemTime::now()),
}
}

Expand Down Expand Up @@ -364,12 +361,6 @@ impl<T: Number<T>> ExpoHistogram<T> {
&self,
dest: Option<&mut dyn Aggregation>,
) -> (usize, Option<Box<dyn Aggregation>>) {
let t = SystemTime::now();
let start = self
.start
.lock()
.map(|s| *s)
.unwrap_or_else(|_| SystemTime::now());

let h = dest.and_then(|d| d.as_mut().downcast_mut::<data::ExponentialHistogram<T>>());
let mut new_agg = if h.is_none() {
Expand Down Expand Up @@ -400,8 +391,6 @@ impl<T: Number<T>> ExpoHistogram<T> {
.iter()
.map(|(k, v)| KeyValue::new(k.clone(), v.clone()))
.collect(),
start_time: start,
time: t,
count: b.count,
min: if self.record_min_max {
Some(b.min)
Expand All @@ -428,25 +417,13 @@ impl<T: Number<T>> ExpoHistogram<T> {
exemplars: vec![],
});
}

// The delta collection cycle resets.
if let Ok(mut start) = self.start.lock() {
*start = t;
}

(n, new_agg.map(|a| Box::new(a) as Box<_>))
}

pub(crate) fn cumulative(
&self,
dest: Option<&mut dyn Aggregation>,
) -> (usize, Option<Box<dyn Aggregation>>) {
let t = SystemTime::now();
let start = self
.start
.lock()
.map(|s| *s)
.unwrap_or_else(|_| SystemTime::now());

let h = dest.and_then(|d| d.as_mut().downcast_mut::<data::ExponentialHistogram<T>>());
let mut new_agg = if h.is_none() {
Expand Down Expand Up @@ -481,8 +458,6 @@ impl<T: Number<T>> ExpoHistogram<T> {
.iter()
.map(|(k, v)| KeyValue::new(k.clone(), v.clone()))
.collect(),
start_time: start,
time: t,
count: b.count,
min: if self.record_min_max {
Some(b.min)
Expand Down Expand Up @@ -1271,8 +1246,6 @@ mod tests {
min: Some(1.into()),
max: Some(16.into()),
sum: 31.into(),
start_time: SystemTime::now(),
time: SystemTime::now(),
scale: -1,
positive_bucket: data::ExponentialBucket {
offset: -1,
Expand Down Expand Up @@ -1319,8 +1292,6 @@ mod tests {
offset: -1,
counts: vec![1, 4, 1],
},
start_time: SystemTime::now(),
time: SystemTime::now(),
negative_bucket: data::ExponentialBucket {
offset: 0,
counts: vec![],
Expand Down Expand Up @@ -1365,8 +1336,6 @@ mod tests {
offset: -1,
counts: vec![1, 4, 1],
},
start_time: SystemTime::now(),
time: SystemTime::now(),
negative_bucket: data::ExponentialBucket {
offset: 0,
counts: vec![],
Expand Down Expand Up @@ -1411,8 +1380,6 @@ mod tests {
counts: vec![1, 6, 2],
},
attributes: vec![],
start_time: SystemTime::now(),
time: SystemTime::now(),
negative_bucket: data::ExponentialBucket {
offset: 0,
counts: vec![],
Expand Down Expand Up @@ -1441,15 +1408,14 @@ mod tests {
count = out_fn.call(Some(got.as_mut())).0
}

assert_aggregation_eq::<T>(Box::new(test.want), got, true, test.name);
assert_aggregation_eq::<T>(Box::new(test.want), got, test.name);
assert_eq!(test.want_count, count, "{}", test.name);
}
}

fn assert_aggregation_eq<T: Number<T> + PartialEq>(
a: Box<dyn Aggregation>,
b: Box<dyn Aggregation>,
ignore_timestamp: bool,
test_name: &'static str,
) {
assert_eq!(
Expand All @@ -1471,7 +1437,6 @@ mod tests {
assert_data_points_eq(
a,
b,
ignore_timestamp,
"mismatching gauge data points",
test_name,
);
Expand All @@ -1498,7 +1463,6 @@ mod tests {
assert_data_points_eq(
a,
b,
ignore_timestamp,
"mismatching sum data points",
test_name,
);
Expand All @@ -1520,7 +1484,6 @@ mod tests {
assert_hist_data_points_eq(
a,
b,
ignore_timestamp,
"mismatching hist data points",
test_name,
);
Expand All @@ -1545,7 +1508,6 @@ mod tests {
assert_exponential_hist_data_points_eq(
a,
b,
ignore_timestamp,
"mismatching hist data points",
test_name,
);
Expand All @@ -1558,7 +1520,6 @@ mod tests {
fn assert_data_points_eq<T: Number<T>>(
a: &data::DataPoint<T>,
b: &data::DataPoint<T>,
ignore_timestamp: bool,
message: &'static str,
test_name: &'static str,
) {
Expand All @@ -1568,21 +1529,11 @@ mod tests {
test_name, message
);
assert_eq!(a.value, b.value, "{}: {} value", test_name, message);

if !ignore_timestamp {
assert_eq!(
a.start_time, b.start_time,
"{}: {} start time",
test_name, message
);
assert_eq!(a.time, b.time, "{}: {} time", test_name, message);
}
}

fn assert_hist_data_points_eq<T: Number<T>>(
a: &data::HistogramDataPoint<T>,
b: &data::HistogramDataPoint<T>,
ignore_timestamp: bool,
message: &'static str,
test_name: &'static str,
) {
Expand All @@ -1601,21 +1552,11 @@ mod tests {
assert_eq!(a.min, b.min, "{}: {} min", test_name, message);
assert_eq!(a.max, b.max, "{}: {} max", test_name, message);
assert_eq!(a.sum, b.sum, "{}: {} sum", test_name, message);

if !ignore_timestamp {
assert_eq!(
a.start_time, b.start_time,
"{}: {} start time",
test_name, message
);
assert_eq!(a.time, b.time, "{}: {} time", test_name, message);
}
}

fn assert_exponential_hist_data_points_eq<T: Number<T>>(
a: &data::ExponentialHistogramDataPoint<T>,
b: &data::ExponentialHistogramDataPoint<T>,
ignore_timestamp: bool,
message: &'static str,
test_name: &'static str,
) {
Expand Down Expand Up @@ -1646,14 +1587,5 @@ mod tests {
"{}: {} neg",
test_name, message
);

if !ignore_timestamp {
assert_eq!(
a.start_time, b.start_time,
"{}: {} start time",
test_name, message
);
assert_eq!(a.time, b.time, "{}: {} time", test_name, message);
}
}
}
Loading

0 comments on commit d5f9a4c

Please sign in to comment.