Skip to content

Commit 1c34d78

Browse files
committed
better signature
1 parent 302cb49 commit 1c34d78

File tree

1 file changed

+38
-38
lines changed
  • datadog-profiling/src/internal/profile/otel_emitter

1 file changed

+38
-38
lines changed

datadog-profiling/src/internal/profile/otel_emitter/profile.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ use crate::iter::{IntoLendingIterator, LendingIterator};
88
use anyhow::{Context, Result};
99
use std::collections::HashMap;
1010

11-
impl TryFrom<InternalProfile> for datadog_profiling_otel::ProfilesData {
12-
type Error = anyhow::Error;
13-
14-
fn try_from(mut internal_profile: InternalProfile) -> Result<Self> {
11+
impl InternalProfile {
12+
/// Serializes the profile into OpenTelemetry format
13+
///
14+
/// * `end_time` - Optional end time of the profile. Passing None will use the current time.
15+
/// * `duration` - Optional duration of the profile. Passing None will try to calculate the
16+
/// duration based on the end time minus the start time, but under anomalous conditions this
17+
/// may fail as system clocks can be adjusted. The programmer may also accidentally pass an
18+
/// earlier time. The duration will be set to zero these cases.
19+
pub fn serialize_into_otel(
20+
mut self,
21+
_end_time: Option<std::time::SystemTime>,
22+
_duration: Option<std::time::Duration>,
23+
) -> anyhow::Result<datadog_profiling_otel::ProfilesData> {
1524
// Create individual OpenTelemetry Profiles for each ValueType
16-
let mut profiles = Vec::with_capacity(internal_profile.sample_types.len());
25+
let mut profiles = Vec::with_capacity(self.sample_types.len());
1726

18-
for sample_type in internal_profile.sample_types.iter() {
27+
for sample_type in self.sample_types.iter() {
1928
// Convert the ValueType to OpenTelemetry format
2029
let otel_sample_type = datadog_profiling_otel::ValueType {
2130
type_strindex: sample_type.r#type.value.to_raw_id() as i32,
@@ -28,7 +37,7 @@ impl TryFrom<InternalProfile> for datadog_profiling_otel::ProfilesData {
2837
let profile = datadog_profiling_otel::Profile {
2938
sample_type: Some(otel_sample_type),
3039
sample: vec![], // TODO: Implement sample conversion
31-
time_nanos: internal_profile
40+
time_nanos: self
3241
.start_time
3342
.duration_since(std::time::UNIX_EPOCH)
3443
.unwrap_or_default()
@@ -47,22 +56,18 @@ impl TryFrom<InternalProfile> for datadog_profiling_otel::ProfilesData {
4756
profiles.push(profile);
4857
}
4958

50-
for (sample, timestamp, mut values) in
51-
std::mem::take(&mut internal_profile.observations).into_iter()
52-
{
59+
for (sample, timestamp, mut values) in std::mem::take(&mut self.observations).into_iter() {
5360
let stack_index = sample.stacktrace.to_raw_id() as i32;
54-
let label_set = internal_profile.get_label_set(sample.labels)?;
61+
let label_set = self.get_label_set(sample.labels)?;
5562
let attribute_indicies: Vec<_> =
5663
label_set.iter().map(|x| x.to_raw_id() as i32).collect();
5764
let labels = label_set
5865
.iter()
59-
.map(|l| internal_profile.get_label(*l).copied())
66+
.map(|l| self.get_label(*l).copied())
6067
.collect::<Result<Vec<_>>>()?;
6168
let link_index = 0; // TODO, handle links properly
6269
let timestamps_unix_nano = timestamp.map_or(vec![], |ts| vec![ts.get() as u64]);
63-
internal_profile
64-
.upscaling_rules
65-
.upscale_values(&mut values, &labels)?;
70+
self.upscaling_rules.upscale_values(&mut values, &labels)?;
6671

6772
for (idx, value) in values.iter().enumerate() {
6873
if *value != 0 {
@@ -84,8 +89,8 @@ impl TryFrom<InternalProfile> for datadog_profiling_otel::ProfilesData {
8489
// with lifetimes tied to the iterator itself, so we need to manually iterate and
8590
// convert each string reference to an owned String.
8691
let string_table = {
87-
let mut strings = Vec::with_capacity(internal_profile.strings.len());
88-
let mut iter = internal_profile.strings.into_lending_iter();
92+
let mut strings = Vec::with_capacity(self.strings.len());
93+
let mut iter = self.strings.into_lending_iter();
8994
while let Some(s) = iter.next() {
9095
strings.push(s.to_string());
9196
}
@@ -94,9 +99,9 @@ impl TryFrom<InternalProfile> for datadog_profiling_otel::ProfilesData {
9499

95100
// Convert labels to KeyValues for the attribute table
96101
let mut key_to_unit_map = HashMap::new();
97-
let mut attribute_table = Vec::with_capacity(internal_profile.labels.len());
102+
let mut attribute_table = Vec::with_capacity(self.labels.len());
98103

99-
for label in internal_profile.labels.iter() {
104+
for label in self.labels.iter() {
100105
let key_value = convert_label_to_key_value(label, &string_table, &mut key_to_unit_map)
101106
.with_context(|| {
102107
format!(
@@ -121,28 +126,24 @@ impl TryFrom<InternalProfile> for datadog_profiling_otel::ProfilesData {
121126
// Convert the ProfilesDictionary components
122127
let dictionary = datadog_profiling_otel::ProfilesDictionary {
123128
// Convert mappings
124-
mapping_table: internal_profile
125-
.mappings
126-
.iter()
127-
.map(|mapping| mapping.into())
128-
.collect(),
129+
mapping_table: self.mappings.iter().map(|mapping| mapping.into()).collect(),
129130

130131
// Convert locations
131-
location_table: internal_profile
132+
location_table: self
132133
.locations
133134
.iter()
134135
.map(|location| location.into())
135136
.collect(),
136137

137138
// Convert functions
138-
function_table: internal_profile
139+
function_table: self
139140
.functions
140141
.iter()
141142
.map(|function| function.into())
142143
.collect(),
143144

144145
// Convert stack traces
145-
stack_table: internal_profile
146+
stack_table: self
146147
.stack_traces
147148
.iter()
148149
.map(|stack_trace| stack_trace.into())
@@ -168,7 +169,7 @@ impl TryFrom<InternalProfile> for datadog_profiling_otel::ProfilesData {
168169
schema_url: String::new(), // TODO: Implement when we handle schema URLs
169170
}];
170171

171-
Ok(Self {
172+
Ok(datadog_profiling_otel::ProfilesData {
172173
resource_profiles,
173174
dictionary: Some(dictionary),
174175
})
@@ -178,15 +179,14 @@ impl TryFrom<InternalProfile> for datadog_profiling_otel::ProfilesData {
178179
#[cfg(test)]
179180
mod tests {
180181
use crate::internal::profile::Profile as InternalProfile;
181-
use datadog_profiling_otel::ProfilesData;
182182

183183
#[test]
184184
fn test_from_internal_profile_empty() {
185185
// Create an empty internal profile
186186
let internal_profile = InternalProfile::new(&[], None);
187187

188188
// Convert to OpenTelemetry ProfilesData
189-
let otel_profiles_data = ProfilesData::try_from(internal_profile).unwrap();
189+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
190190

191191
// Verify the conversion
192192
assert!(otel_profiles_data.dictionary.is_some());
@@ -225,7 +225,7 @@ mod tests {
225225
let _function2_id = internal_profile.add_function(&function2);
226226

227227
// Convert to OpenTelemetry ProfilesData
228-
let otel_profiles_data = ProfilesData::try_from(internal_profile).unwrap();
228+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
229229

230230
// Verify the conversion
231231
assert!(otel_profiles_data.dictionary.is_some());
@@ -276,7 +276,7 @@ mod tests {
276276
let _ = internal_profile.add_sample(sample, None);
277277

278278
// Convert to OpenTelemetry ProfilesData
279-
let otel_profiles_data = ProfilesData::try_from(internal_profile).unwrap();
279+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
280280

281281
// Verify the conversion
282282
assert!(otel_profiles_data.dictionary.is_some());
@@ -326,7 +326,7 @@ mod tests {
326326
let internal_profile = InternalProfile::new(&sample_types, None);
327327

328328
// Convert to OpenTelemetry ProfilesData
329-
let otel_profiles_data = ProfilesData::try_from(internal_profile).unwrap();
329+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
330330

331331
// Verify that individual profiles are created for each sample type
332332
assert_eq!(otel_profiles_data.resource_profiles.len(), 1);
@@ -400,7 +400,7 @@ mod tests {
400400
let _ = internal_profile.add_sample(sample, None);
401401

402402
// Convert to OpenTelemetry ProfilesData
403-
let otel_profiles_data = ProfilesData::try_from(internal_profile).unwrap();
403+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
404404

405405
// Verify the conversion
406406
assert!(otel_profiles_data.dictionary.is_some());
@@ -487,7 +487,7 @@ mod tests {
487487
let _ = internal_profile.add_sample(sample, None);
488488

489489
// Convert to OpenTelemetry ProfilesData
490-
let otel_profiles_data = ProfilesData::try_from(internal_profile).unwrap();
490+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
491491

492492
// Verify the conversion
493493
let _dictionary = otel_profiles_data.dictionary.unwrap();
@@ -556,7 +556,7 @@ mod tests {
556556
let _ = internal_profile.add_sample(sample, Some(timestamp));
557557

558558
// Convert to OpenTelemetry ProfilesData
559-
let otel_profiles_data = ProfilesData::try_from(internal_profile).unwrap();
559+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
560560

561561
// Verify the conversion
562562
let profile = &otel_profiles_data.resource_profiles[0].scope_profiles[0].profiles[0];
@@ -616,7 +616,7 @@ mod tests {
616616
let _ = internal_profile.add_sample(sample, None);
617617

618618
// Convert to OpenTelemetry ProfilesData
619-
let otel_profiles_data = ProfilesData::try_from(internal_profile).unwrap();
619+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
620620

621621
// Verify the conversion
622622
let profile0 = &otel_profiles_data.resource_profiles[0].scope_profiles[0].profiles[0];
@@ -687,7 +687,7 @@ mod tests {
687687
let _ = internal_profile.add_sample(sample3, None);
688688

689689
// Convert to OpenTelemetry ProfilesData
690-
let otel_profiles_data = ProfilesData::try_from(internal_profile).unwrap();
690+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
691691

692692
// Verify the conversion
693693
let profile = &otel_profiles_data.resource_profiles[0].scope_profiles[0].profiles[0];

0 commit comments

Comments
 (0)