Skip to content

Commit c710521

Browse files
committed
perf(sampling): change api to avoid collecting meta and metrics
1 parent 30f75df commit c710521

3 files changed

Lines changed: 49 additions & 49 deletions

File tree

libdd-sampling/src/datadog_sampler.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,10 @@ mod tests {
463463
}
464464

465465
impl<'a> SpanProperties for TestSpan<'a> {
466-
type Attribute = TestAttribute;
466+
type Attribute<'b>
467+
= &'b TestAttribute
468+
where
469+
Self: 'b;
467470

468471
fn operation_name(&self) -> Cow<'_, str> {
469472
self.get_operation_name()
@@ -505,10 +508,7 @@ mod tests {
505508
})
506509
}
507510

508-
fn attributes<'b>(&'b self) -> impl Iterator<Item = &'b Self::Attribute>
509-
where
510-
Self: 'b,
511-
{
511+
fn attributes(&self) -> impl Iterator<Item = &TestAttribute> + '_ {
512512
self.attributes.iter()
513513
}
514514

libdd-sampling/src/types.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ pub trait AttributeLike {
4747
fn value(&self) -> &Self::Value;
4848
}
4949

50+
impl<T: AttributeLike> AttributeLike for &T {
51+
type Value = T::Value;
52+
53+
fn key(&self) -> &str {
54+
(**self).key()
55+
}
56+
57+
fn value(&self) -> &Self::Value {
58+
(**self).value()
59+
}
60+
}
61+
5062
/// A trait for extracting typed values from attribute values.
5163
///
5264
/// Provides methods for converting attribute values to common types used in sampling logic.
@@ -86,7 +98,9 @@ pub trait AttributeFactory {
8698
/// resource name, and status codes used by sampling rules.
8799
pub trait SpanProperties {
88100
/// The type of attribute that implements `AttributeLike`.
89-
type Attribute: AttributeLike;
101+
type Attribute<'a>: AttributeLike
102+
where
103+
Self: 'a;
90104

91105
/// Returns the operation name for the span.
92106
///
@@ -115,9 +129,7 @@ pub trait SpanProperties {
115129
fn status_code(&self) -> Option<u32>;
116130

117131
/// Returns an iterator over span attributes.
118-
fn attributes<'a>(&'a self) -> impl Iterator<Item = &'a Self::Attribute>
119-
where
120-
Self: 'a;
132+
fn attributes(&self) -> impl Iterator<Item = Self::Attribute<'_>> + '_;
121133

122134
/// Returns an alternate key for the given attribute key.
123135
///

libdd-sampling/src/v04_span.rs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,16 @@ impl<'a> AttributeLike for SpanAttribute<'a> {
101101
}
102102
}
103103

104-
/// Span properties extracted from a v04 `Span<T>`, with attributes pre-collected.
105-
///
106-
/// Pre-collection is required because [`SpanProperties::attributes`] must return
107-
/// `impl Iterator<Item = &Self::Attribute>` — items need to exist in memory to be referenced.
108-
pub struct V04SpanProperties<'a> {
109-
name: &'a str,
110-
service: &'a str,
111-
resource: &'a str,
104+
/// Span properties borrowing from a v04 `Span<T>`.
105+
pub struct V04SpanProperties<'a, T: TraceData> {
106+
span: &'a Span<T>,
112107
env: Option<&'a str>,
113108
status_code: Option<u32>,
114-
attributes: Vec<SpanAttribute<'a>>,
115109
}
116110

117-
impl<'a> V04SpanProperties<'a> {
111+
impl<'a, T: TraceData> V04SpanProperties<'a, T> {
118112
/// Builds span properties by borrowing from `span` for lifetime `'a`.
119-
pub fn from_span<T: TraceData>(span: &'a Span<T>) -> Self {
113+
pub fn from_span(span: &'a Span<T>) -> Self {
120114
let env = span.meta.get("env").map(|v| v.borrow());
121115

122116
let status_code = span
@@ -132,58 +126,52 @@ impl<'a> V04SpanProperties<'a> {
132126
.and_then(|s| s.borrow().parse().ok())
133127
});
134128

135-
let attributes = span
136-
.meta
137-
.iter()
138-
.map(|(k, v)| SpanAttribute {
139-
key: k.borrow(),
140-
value: SpanAttributeValue::Meta(v.borrow()),
141-
})
142-
.chain(span.metrics.iter().map(|(k, v)| SpanAttribute {
143-
key: k.borrow(),
144-
value: SpanAttributeValue::Metric(*v),
145-
}))
146-
.collect();
147-
148129
V04SpanProperties {
149-
name: span.name.borrow(),
150-
service: span.service.borrow(),
151-
resource: span.resource.borrow(),
130+
span,
152131
env,
153132
status_code,
154-
attributes,
155133
}
156134
}
157135
}
158136

159-
impl<'a> SpanProperties for V04SpanProperties<'a> {
160-
type Attribute = SpanAttribute<'a>;
137+
impl<'a, T: TraceData> SpanProperties for V04SpanProperties<'a, T> {
138+
type Attribute<'b>
139+
= SpanAttribute<'b>
140+
where
141+
Self: 'b;
161142

162143
fn operation_name(&self) -> Cow<'_, str> {
163-
Cow::Borrowed(self.name)
144+
Cow::Borrowed(self.span.name.borrow())
164145
}
165146

166147
fn service(&self) -> Cow<'_, str> {
167-
Cow::Borrowed(self.service)
148+
Cow::Borrowed(self.span.service.borrow())
168149
}
169150

170151
fn env(&self) -> Cow<'_, str> {
171152
self.env.map_or(Cow::Borrowed(""), Cow::Borrowed)
172153
}
173154

174155
fn resource(&self) -> Cow<'_, str> {
175-
Cow::Borrowed(self.resource)
156+
Cow::Borrowed(self.span.resource.borrow())
176157
}
177158

178159
fn status_code(&self) -> Option<u32> {
179160
self.status_code
180161
}
181162

182-
fn attributes<'b>(&'b self) -> impl Iterator<Item = &'b Self::Attribute>
183-
where
184-
Self: 'b,
185-
{
186-
self.attributes.iter()
163+
fn attributes(&self) -> impl Iterator<Item = SpanAttribute<'_>> + '_ {
164+
self.span
165+
.meta
166+
.iter()
167+
.map(|(k, v)| SpanAttribute {
168+
key: k.borrow(),
169+
value: SpanAttributeValue::Meta(v.borrow()),
170+
})
171+
.chain(self.span.metrics.iter().map(|(k, v)| SpanAttribute {
172+
key: k.borrow(),
173+
value: SpanAttributeValue::Metric(*v),
174+
}))
187175
}
188176

189177
fn get_alternate_key<'b>(&self, _key: &'b str) -> Option<Cow<'b, str>> {
@@ -204,7 +192,7 @@ pub struct V04SamplingData<'a, T: TraceData> {
204192
impl<'a, T: TraceData> SamplingData for V04SamplingData<'a, T> {
205193
type TraceId = u128;
206194
type Properties<'b>
207-
= V04SpanProperties<'b>
195+
= V04SpanProperties<'b, T>
208196
where
209197
Self: 'b;
210198

@@ -218,7 +206,7 @@ impl<'a, T: TraceData> SamplingData for V04SamplingData<'a, T> {
218206

219207
fn with_span_properties<S, R, F>(&self, s: &S, f: F) -> R
220208
where
221-
F: for<'b> Fn(&S, &V04SpanProperties<'b>) -> R,
209+
F: for<'b> Fn(&S, &V04SpanProperties<'b, T>) -> R,
222210
{
223211
let props = V04SpanProperties::from_span(self.span);
224212
f(s, &props)

0 commit comments

Comments
 (0)