Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metric SDK] - Avoid exposing AttributeSet to exporters - Part1 #1792

Merged
merged 7 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ members = [
"examples/*",
"stress",
]
# Any deleted crates with remaining README
exclude = []
# Crates temporarily excluded from the workspace
exclude = ["opentelemetry-prometheus"]
resolver = "2"

[profile.bench]
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-sdk/src/metrics/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Sum<T> {
pub struct DataPoint<T> {
/// Attributes is the set of key value pairs that uniquely identify the
/// time series.
pub attributes: AttributeSet,
pub attributes: Vec<KeyValue>,
Copy link
Contributor

@utpilla utpilla May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a boxed slice instead of a Vec? It would save 8 bytes (usize length) per instance since unlike Vec we wouldn't need an additional pointer to track the capacity. That could save a considerable amount of memory if we are going to have a lot of these DataPoints.

Suggested change
pub attributes: Vec<KeyValue>,
pub attributes: Box<[KeyValue]>,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the PR desc, will revisit the public APIs. This PR is to unblock the unwanted exposure of AttributeSet outside of core sdk.

/// The time when the time series was started.
pub start_time: Option<SystemTime>,
/// The time when the time series was recorded.
Expand Down Expand Up @@ -143,7 +143,7 @@ impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Histogram<T> {
#[derive(Debug)]
pub struct HistogramDataPoint<T> {
/// The set of key value pairs that uniquely identify the time series.
pub attributes: AttributeSet,
pub attributes: Vec<KeyValue>,
/// The time when the time series was started.
pub start_time: SystemTime,
/// The time when the time series was recorded.
Expand Down
32 changes: 10 additions & 22 deletions opentelemetry-sdk/src/metrics/internal/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ mod tests {
let (measure, agg) = AggregateBuilder::<u64>::new(None, None).last_value();
let mut a = Gauge {
data_points: vec![DataPoint {
attributes: AttributeSet::from(&[KeyValue::new("a", 1)][..]),
attributes: vec![KeyValue::new("a", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 1u64,
Expand All @@ -241,10 +241,7 @@ mod tests {
assert_eq!(count, 1);
assert!(new_agg.is_none());
assert_eq!(a.data_points.len(), 1);
assert_eq!(
a.data_points[0].attributes,
AttributeSet::from(&new_attributes[..])
);
assert_eq!(a.data_points[0].attributes, new_attributes.to_vec());
assert_eq!(a.data_points[0].value, 2);
}

Expand All @@ -256,14 +253,14 @@ mod tests {
let mut a = Sum {
data_points: vec![
DataPoint {
attributes: AttributeSet::from(&[KeyValue::new("a1", 1)][..]),
attributes: vec![KeyValue::new("a1", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 1u64,
exemplars: vec![],
},
DataPoint {
attributes: AttributeSet::from(&[KeyValue::new("a2", 2)][..]),
attributes: vec![KeyValue::new("a2", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 2u64,
Expand All @@ -287,10 +284,7 @@ mod tests {
assert_eq!(a.temporality, temporality);
assert!(a.is_monotonic);
assert_eq!(a.data_points.len(), 1);
assert_eq!(
a.data_points[0].attributes,
AttributeSet::from(&new_attributes[..])
);
assert_eq!(a.data_points[0].attributes, new_attributes.to_vec());
assert_eq!(a.data_points[0].value, 3);
}
}
Expand All @@ -302,14 +296,14 @@ mod tests {
let mut a = Sum {
data_points: vec![
DataPoint {
attributes: AttributeSet::from(&[KeyValue::new("a1", 1)][..]),
attributes: vec![KeyValue::new("a1", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 1u64,
exemplars: vec![],
},
DataPoint {
attributes: AttributeSet::from(&[KeyValue::new("a2", 2)][..]),
attributes: vec![KeyValue::new("a2", 1)],
start_time: Some(SystemTime::now()),
time: Some(SystemTime::now()),
value: 2u64,
Expand All @@ -333,10 +327,7 @@ mod tests {
assert_eq!(a.temporality, temporality);
assert!(a.is_monotonic);
assert_eq!(a.data_points.len(), 1);
assert_eq!(
a.data_points[0].attributes,
AttributeSet::from(&new_attributes[..])
);
assert_eq!(a.data_points[0].attributes, new_attributes.to_vec());
assert_eq!(a.data_points[0].value, 3);
}
}
Expand All @@ -348,7 +339,7 @@ mod tests {
.explicit_bucket_histogram(vec![1.0], true, true);
let mut a = Histogram {
data_points: vec![HistogramDataPoint {
attributes: AttributeSet::from(&[KeyValue::new("a2", 2)][..]),
attributes: vec![KeyValue::new("a1", 1)],
start_time: SystemTime::now(),
time: SystemTime::now(),
count: 2,
Expand All @@ -374,10 +365,7 @@ mod tests {
assert!(new_agg.is_none());
assert_eq!(a.temporality, temporality);
assert_eq!(a.data_points.len(), 1);
assert_eq!(
a.data_points[0].attributes,
AttributeSet::from(&new_attributes[..])
);
assert_eq!(a.data_points[0].attributes, new_attributes.to_vec());
assert_eq!(a.data_points[0].count, 1);
assert_eq!(a.data_points[0].bounds, vec![1.0]);
assert_eq!(a.data_points[0].bucket_counts, vec![0, 1]);
Expand Down
11 changes: 9 additions & 2 deletions opentelemetry-sdk/src/metrics/internal/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{collections::HashMap, sync::Mutex, time::SystemTime};

use crate::metrics::data::{self, Aggregation, Temporality};
use crate::{attributes::AttributeSet, metrics::data::HistogramDataPoint};
use opentelemetry::KeyValue;
use opentelemetry::{global, metrics::MetricsError};

use super::{
Expand Down Expand Up @@ -165,7 +166,10 @@ impl<T: Number<T>> Histogram<T> {

for (a, b) in values.drain() {
h.data_points.push(HistogramDataPoint {
attributes: a,
attributes: a
.iter()
.map(|(k, v)| KeyValue::new(k.clone(), v.clone()))
.collect(),
start_time: start,
time: t,
count: b.count,
Expand Down Expand Up @@ -236,7 +240,10 @@ impl<T: Number<T>> Histogram<T> {
// overload the system.
for (a, b) in values.iter() {
h.data_points.push(HistogramDataPoint {
attributes: a.clone(),
attributes: a
.iter()
.map(|(k, v)| KeyValue::new(k.clone(), v.clone()))
.collect(),
start_time: start,
time: t,
count: b.count,
Expand Down
7 changes: 5 additions & 2 deletions opentelemetry-sdk/src/metrics/internal/last_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use crate::{attributes::AttributeSet, metrics::data::DataPoint};
use opentelemetry::{global, metrics::MetricsError};
use opentelemetry::{global, metrics::MetricsError, KeyValue};

use super::{
aggregate::{is_under_cardinality_limit, STREAM_OVERFLOW_ATTRIBUTE_SET},
Expand Down Expand Up @@ -66,7 +66,10 @@ impl<T: Number<T>> LastValue<T> {

for (attrs, value) in values.drain() {
dest.push(DataPoint {
attributes: attrs,
attributes: attrs
.iter()
.map(|(k, v)| KeyValue::new(k.clone(), v.clone()))
.collect(),
time: Some(value.timestamp),
value: value.value,
start_time: None,
Expand Down
30 changes: 22 additions & 8 deletions opentelemetry-sdk/src/metrics/internal/sum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::atomic::{AtomicBool, Ordering};
use std::vec;
use std::{
collections::{hash_map::Entry, HashMap},
sync::Mutex,
Expand All @@ -7,6 +8,7 @@

use crate::attributes::AttributeSet;
use crate::metrics::data::{self, Aggregation, DataPoint, Temporality};
use opentelemetry::KeyValue;
use opentelemetry::{global, metrics::MetricsError};

use super::{
Expand Down Expand Up @@ -131,7 +133,7 @@
.swap(false, Ordering::AcqRel)
{
s_data.data_points.push(DataPoint {
attributes: AttributeSet::default(),
attributes: vec![],
start_time: Some(prev_start),
time: Some(t),
value: self.value_map.no_attribute_value.get_and_reset_value(),
Expand All @@ -141,7 +143,10 @@

for (attrs, value) in values.drain() {
s_data.data_points.push(DataPoint {
attributes: attrs,
attributes: attrs
.iter()
.map(|(k, v)| KeyValue::new(k.clone(), v.clone()))
.collect(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not worried about the extra cost here, as this is in collect() path only, to focus on fixing the hot path first. The collect() path can be refactored afterwards.

Copy link
Member

@lalitb lalitb May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can implement From trait, and use it throughout? And also, no need of cloning as we are already draining.

start_time: Some(prev_start),
time: Some(t),
value,
Expand Down Expand Up @@ -201,7 +206,7 @@
.load(Ordering::Acquire)
{
s_data.data_points.push(DataPoint {
attributes: AttributeSet::default(),
attributes: vec![],
start_time: Some(prev_start),
time: Some(t),
value: self.value_map.no_attribute_value.get_value(),
Expand All @@ -215,7 +220,10 @@
// overload the system.
for (attrs, value) in values.iter() {
s_data.data_points.push(DataPoint {
attributes: attrs.clone(),
attributes: attrs
.iter()
.map(|(k, v)| KeyValue::new(k.clone(), v.clone()))
.collect(),
start_time: Some(prev_start),
time: Some(t),
value: *value,
Expand Down Expand Up @@ -297,7 +305,7 @@
.swap(false, Ordering::AcqRel)
{
s_data.data_points.push(DataPoint {
attributes: AttributeSet::default(),
attributes: vec![],

Check warning on line 308 in opentelemetry-sdk/src/metrics/internal/sum.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/internal/sum.rs#L308

Added line #L308 was not covered by tests
start_time: Some(prev_start),
time: Some(t),
value: self.value_map.no_attribute_value.get_and_reset_value(),
Expand All @@ -312,7 +320,10 @@
new_reported.insert(attrs.clone(), value);
}
s_data.data_points.push(DataPoint {
attributes: attrs.clone(),
attributes: attrs
.iter()
.map(|(k, v)| KeyValue::new(k.clone(), v.clone()))
.collect(),
start_time: Some(prev_start),
time: Some(t),
value: delta,
Expand Down Expand Up @@ -379,7 +390,7 @@
.load(Ordering::Acquire)
{
s_data.data_points.push(DataPoint {
attributes: AttributeSet::default(),
attributes: vec![],
start_time: Some(prev_start),
time: Some(t),
value: self.value_map.no_attribute_value.get_value(),
Expand All @@ -394,7 +405,10 @@
new_reported.insert(attrs.clone(), *value);
}
s_data.data_points.push(DataPoint {
attributes: attrs.clone(),
attributes: attrs
.iter()
.map(|(k, v)| KeyValue::new(k.clone(), v.clone()))
.collect(),
start_time: Some(prev_start),
time: Some(t),
value: delta,
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-sdk/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ mod tests {
if datapoint
.attributes
.iter()
.any(|(k, v)| k.as_str() == "key1" && v.as_str() == "value1")
.any(|kv| kv.key.as_str() == "key1" && kv.value.as_str() == "value1")
{
data_point1 = Some(datapoint);
}
Expand All @@ -184,7 +184,7 @@ mod tests {
if datapoint
.attributes
.iter()
.any(|(k, v)| k.as_str() == "key1" && v.as_str() == "value2")
.any(|kv| kv.key.as_str() == "key1" && kv.value.as_str() == "value2")
{
data_point1 = Some(datapoint);
}
Expand Down Expand Up @@ -1000,7 +1000,7 @@ mod tests {
datapoint
.attributes
.iter()
.any(|(k, v)| k.as_str() == key && v.as_str() == value)
.any(|kv| kv.key.as_str() == key && kv.value.as_str() == value)
})
}

Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-stdout/src/metrics/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct DataPoint {
attributes: AttributeSet,
attributes: Vec<KeyValue>,
#[serde(serialize_with = "as_opt_human_readable")]
start_time: Option<SystemTime>,
#[serde(serialize_with = "as_opt_human_readable")]
Expand All @@ -253,7 +253,7 @@
impl<T: Into<DataValue> + Copy> From<&data::DataPoint<T>> for DataPoint {
fn from(value: &data::DataPoint<T>) -> Self {
DataPoint {
attributes: AttributeSet::from(&value.attributes),
attributes: value.attributes.iter().map(Into::into).collect(),

Check warning on line 256 in opentelemetry-stdout/src/metrics/transform.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/transform.rs#L256

Added line #L256 was not covered by tests
start_time_unix_nano: value.start_time,
time_unix_nano: value.time,
start_time: value.start_time,
Expand Down Expand Up @@ -284,7 +284,7 @@
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct HistogramDataPoint {
attributes: AttributeSet,
attributes: Vec<KeyValue>,
#[serde(serialize_with = "as_unix_nano")]
start_time_unix_nano: SystemTime,
#[serde(serialize_with = "as_unix_nano")]
Expand All @@ -306,7 +306,7 @@
impl<T: Into<DataValue> + Copy> From<&data::HistogramDataPoint<T>> for HistogramDataPoint {
fn from(value: &data::HistogramDataPoint<T>) -> Self {
HistogramDataPoint {
attributes: AttributeSet::from(&value.attributes),
attributes: value.attributes.iter().map(Into::into).collect(),

Check warning on line 309 in opentelemetry-stdout/src/metrics/transform.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/transform.rs#L309

Added line #L309 was not covered by tests
start_time_unix_nano: value.start_time,
time_unix_nano: value.time,
start_time: value.start_time,
Expand Down
2 changes: 1 addition & 1 deletion scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if rustup component add clippy; then
"opentelemetry-appender-log"
"opentelemetry-appender-tracing"
"opentelemetry-otlp"
"opentelemetry-prometheus"
# "opentelemetry-prometheus" - temporarily exlude Prometheus from CI.
"opentelemetry-proto"
"opentelemetry-sdk"
"opentelemetry-semantic-conventions"
Expand Down
Loading