Skip to content

Commit 4507e16

Browse files
authored
Merge branch 'main' into logrecord-getters
2 parents 29474f4 + 465fcc2 commit 4507e16

File tree

6 files changed

+41
-67
lines changed

6 files changed

+41
-67
lines changed

opentelemetry-proto/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## vNext
44

5+
- Update proto definitions to v1.4.0 [#2315](https://github.com/open-telemetry/opentelemetry-rust/pull/2315)
6+
7+
58
## 0.27.0
69

710
Released 2024-Nov-11

opentelemetry-proto/src/proto/tonic/opentelemetry.proto.metrics.v1.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33
/// storage, OR can be embedded by other protocols that transfer OTLP metrics
44
/// data but do not implement the OTLP protocol.
55
///
6+
/// MetricsData
7+
/// └─── ResourceMetrics
8+
/// ├── Resource
9+
/// ├── SchemaURL
10+
/// └── ScopeMetrics
11+
/// ├── Scope
12+
/// ├── SchemaURL
13+
/// └── Metric
14+
/// ├── Name
15+
/// ├── Description
16+
/// ├── Unit
17+
/// └── data
18+
/// ├── Gauge
19+
/// ├── Sum
20+
/// ├── Histogram
21+
/// ├── ExponentialHistogram
22+
/// └── Summary
23+
///
624
/// The main difference between this message and collector protocol is that
725
/// in this message there will not be any "control" or "metadata" specific to
826
/// OTLP protocol.
@@ -71,7 +89,6 @@ pub struct ScopeMetrics {
7189
///
7290
/// <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md>
7391
///
74-
///
7592
/// The data model and relation between entities is shown in the
7693
/// diagram below. Here, "DataPoint" is the term used to refer to any
7794
/// one of the specific data point value types, and "points" is the term used
@@ -83,7 +100,7 @@ pub struct ScopeMetrics {
83100
/// - DataPoint contains timestamps, attributes, and one of the possible value type
84101
/// fields.
85102
///
86-
/// Metric
103+
/// Metric
87104
/// +------------+
88105
/// |name |
89106
/// |description |
@@ -277,6 +294,9 @@ pub struct ExponentialHistogram {
277294
/// data type. These data points cannot always be merged in a meaningful way.
278295
/// While they can be useful in some applications, histogram data points are
279296
/// recommended for new applications.
297+
/// Summary metrics do not have an aggregation temporality field. This is
298+
/// because the count and sum fields of a SummaryDataPoint are assumed to be
299+
/// cumulative values.
280300
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
281301
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
282302
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
@@ -587,7 +607,8 @@ pub mod exponential_histogram_data_point {
587607
}
588608
}
589609
/// SummaryDataPoint is a single data point in a timeseries that describes the
590-
/// time-varying values of a Summary metric.
610+
/// time-varying values of a Summary metric. The count and sum fields represent
611+
/// cumulative values.
591612
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
592613
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
593614
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]

opentelemetry-sdk/benches/metrics_counter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
|--------------------------------|-------------|
1010
| Counter_Add_Sorted | 172 ns |
1111
| Counter_Add_Unsorted | 183 ns |
12-
| Counter_Overflow | 898 ns |
12+
| Counter_Overflow | 562 ns |
1313
| ThreadLocal_Random_Generator_5 | 37 ns |
1414
*/
1515

opentelemetry-sdk/src/metrics/internal/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ pub(crate) use exponential_histogram::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};
1818
use once_cell::sync::Lazy;
1919
use opentelemetry::{otel_warn, KeyValue};
2020

21-
use crate::metrics::AttributeSet;
22-
2321
pub(crate) static STREAM_OVERFLOW_ATTRIBUTES: Lazy<Vec<KeyValue>> =
2422
Lazy::new(|| vec![KeyValue::new("otel.metric.overflow", "true")]);
2523

@@ -95,7 +93,7 @@ where
9593
}
9694

9795
// Try to retrieve and update the tracker with the attributes sorted.
98-
let sorted_attrs = AttributeSet::from(attributes).into_vec();
96+
let sorted_attrs = sort_and_dedup(attributes);
9997
if let Some(tracker) = trackers.get(sorted_attrs.as_slice()) {
10098
tracker.update(value);
10199
return;
@@ -198,6 +196,16 @@ fn prepare_data<T>(data: &mut Vec<T>, list_len: usize) {
198196
}
199197
}
200198

199+
fn sort_and_dedup(attributes: &[KeyValue]) -> Vec<KeyValue> {
200+
// Use newly allocated vec here as incoming attributes are immutable so
201+
// cannot sort/de-dup in-place. TODO: This allocation can be avoided by
202+
// leveraging a ThreadLocal vec.
203+
let mut sorted = attributes.to_vec();
204+
sorted.sort_unstable_by(|a, b| a.key.cmp(&b.key));
205+
sorted.dedup_by(|a, b| a.key == b.key);
206+
sorted
207+
}
208+
201209
/// Marks a type that can have a value added and retrieved atomically. Required since
202210
/// different types have different backing atomic mechanisms
203211
pub(crate) trait AtomicTracker<T>: Sync + Send + 'static {

opentelemetry-sdk/src/metrics/mod.rs

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ pub use view::*;
7777
// #[cfg(not(feature = "spec_unstable_metrics_views"))]
7878
// pub(crate) use view::*;
7979

80-
use std::collections::hash_map::DefaultHasher;
81-
use std::collections::HashSet;
82-
use std::hash::{Hash, Hasher};
83-
84-
use opentelemetry::KeyValue;
80+
use std::hash::Hash;
8581

8682
/// Defines the window that an aggregation was calculated over.
8783
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash)]
@@ -106,60 +102,6 @@ pub enum Temporality {
106102
LowMemory,
107103
}
108104

109-
/// A unique set of attributes that can be used as instrument identifiers.
110-
///
111-
/// This must implement [Hash], [PartialEq], and [Eq] so it may be used as
112-
/// HashMap keys and other de-duplication methods.
113-
#[derive(Clone, Default, Debug, PartialEq, Eq)]
114-
pub(crate) struct AttributeSet(Vec<KeyValue>, u64);
115-
116-
impl From<&[KeyValue]> for AttributeSet {
117-
fn from(values: &[KeyValue]) -> Self {
118-
let mut seen_keys = HashSet::with_capacity(values.len());
119-
let vec = values
120-
.iter()
121-
.rev()
122-
.filter_map(|kv| {
123-
if seen_keys.insert(kv.key.clone()) {
124-
Some(kv.clone())
125-
} else {
126-
None
127-
}
128-
})
129-
.collect::<Vec<_>>();
130-
131-
AttributeSet::new(vec)
132-
}
133-
}
134-
135-
fn calculate_hash(values: &[KeyValue]) -> u64 {
136-
let mut hasher = DefaultHasher::new();
137-
values.iter().fold(&mut hasher, |mut hasher, item| {
138-
item.hash(&mut hasher);
139-
hasher
140-
});
141-
hasher.finish()
142-
}
143-
144-
impl AttributeSet {
145-
fn new(mut values: Vec<KeyValue>) -> Self {
146-
values.sort_unstable_by(|a, b| a.key.cmp(&b.key));
147-
let hash = calculate_hash(&values);
148-
AttributeSet(values, hash)
149-
}
150-
151-
/// Returns the underlying Vec of KeyValue pairs
152-
pub(crate) fn into_vec(self) -> Vec<KeyValue> {
153-
self.0
154-
}
155-
}
156-
157-
impl Hash for AttributeSet {
158-
fn hash<H: Hasher>(&self, state: &mut H) {
159-
state.write_u64(self.1)
160-
}
161-
}
162-
163105
#[cfg(all(test, feature = "testing"))]
164106
mod tests {
165107
use self::data::{DataPoint, HistogramDataPoint, ScopeMetrics};

0 commit comments

Comments
 (0)