Skip to content

Commit 9325013

Browse files
committed
use into_iter
1 parent f737e28 commit 9325013

File tree

5 files changed

+147
-34
lines changed

5 files changed

+147
-34
lines changed

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
use crate::collections::identifiable::Id;
55
use crate::internal::Function as InternalFunction;
66

7+
// For owned values - forward to reference version
8+
impl From<InternalFunction> for datadog_profiling_otel::Function {
9+
fn from(internal_function: InternalFunction) -> Self {
10+
Self::from(&internal_function)
11+
}
12+
}
13+
14+
// For references (existing implementation)
715
impl From<&InternalFunction> for datadog_profiling_otel::Function {
816
fn from(internal_function: &InternalFunction) -> Self {
917
Self {
@@ -41,20 +49,58 @@ mod tests {
4149

4250
#[test]
4351
fn test_from_internal_function_with_large_offsets() {
44-
// Create an internal function with larger offsets
52+
// Create an internal function with large offsets
53+
let internal_function = InternalFunction {
54+
name: StringId::from_offset(999999),
55+
system_name: StringId::from_offset(888888),
56+
filename: StringId::from_offset(777777),
57+
};
58+
59+
// Convert to OpenTelemetry Function
60+
let otel_function = datadog_profiling_otel::Function::from(&internal_function);
61+
62+
// Verify the conversion
63+
assert_eq!(otel_function.name_strindex, 999999);
64+
assert_eq!(otel_function.system_name_strindex, 888888);
65+
assert_eq!(otel_function.filename_strindex, 777777);
66+
assert_eq!(otel_function.start_line, 0);
67+
}
68+
69+
#[test]
70+
fn test_into_otel_function() {
71+
// Create an internal function
4572
let internal_function = InternalFunction {
4673
name: StringId::from_offset(100),
4774
system_name: StringId::from_offset(200),
4875
filename: StringId::from_offset(300),
4976
};
5077

51-
// Convert to OpenTelemetry Function
52-
let otel_function = datadog_profiling_otel::Function::from(&internal_function);
78+
// Convert using .into() method
79+
let otel_function: datadog_profiling_otel::Function = (&internal_function).into();
5380

54-
// Verify the conversion - note: StringId doesn't add 1, it's direct conversion
81+
// Verify the conversion
5582
assert_eq!(otel_function.name_strindex, 100);
5683
assert_eq!(otel_function.system_name_strindex, 200);
5784
assert_eq!(otel_function.filename_strindex, 300);
5885
assert_eq!(otel_function.start_line, 0);
5986
}
87+
88+
#[test]
89+
fn test_into_otel_function_owned() {
90+
// Create an internal function
91+
let internal_function = InternalFunction {
92+
name: StringId::from_offset(400),
93+
system_name: StringId::from_offset(500),
94+
filename: StringId::from_offset(600),
95+
};
96+
97+
// Convert using .into() method with owned value
98+
let otel_function: datadog_profiling_otel::Function = internal_function.into();
99+
100+
// Verify the conversion
101+
assert_eq!(otel_function.name_strindex, 400);
102+
assert_eq!(otel_function.system_name_strindex, 500);
103+
assert_eq!(otel_function.filename_strindex, 600);
104+
assert_eq!(otel_function.start_line, 0);
105+
}
60106
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
use crate::collections::identifiable::Id;
55
use crate::internal::Location as InternalLocation;
66

7+
// For owned values - forward to reference version
8+
impl From<InternalLocation> for datadog_profiling_otel::Location {
9+
fn from(internal_location: InternalLocation) -> Self {
10+
Self::from(&internal_location)
11+
}
12+
}
13+
14+
// For references (existing implementation)
715
impl From<&InternalLocation> for datadog_profiling_otel::Location {
816
fn from(internal_location: &InternalLocation) -> Self {
917
Self {
@@ -92,4 +100,24 @@ mod tests {
92100
assert_eq!(otel_location.line[0].function_index, 21);
93101
assert_eq!(otel_location.line[0].line, 200);
94102
}
103+
104+
#[test]
105+
fn test_into_otel_location_owned() {
106+
// Create an internal location
107+
let internal_location = InternalLocation {
108+
mapping_id: Some(MappingId::from_offset(30)),
109+
function_id: FunctionId::from_offset(40),
110+
address: 0x4000,
111+
line: 300,
112+
};
113+
114+
// Convert using .into() method with owned value
115+
let otel_location: datadog_profiling_otel::Location = internal_location.into();
116+
117+
// Verify the conversion - note: from_offset adds 1 to avoid zero values
118+
assert_eq!(otel_location.mapping_index, 31);
119+
assert_eq!(otel_location.address, 0x4000);
120+
assert_eq!(otel_location.line[0].function_index, 41);
121+
assert_eq!(otel_location.line[0].line, 300);
122+
}
95123
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
use crate::collections::identifiable::Id;
55
use crate::internal::Mapping as InternalMapping;
66

7+
// For owned values - forward to reference version
8+
impl From<InternalMapping> for datadog_profiling_otel::Mapping {
9+
fn from(internal_mapping: InternalMapping) -> Self {
10+
Self::from(&internal_mapping)
11+
}
12+
}
13+
14+
// For references (existing implementation)
715
impl From<&InternalMapping> for datadog_profiling_otel::Mapping {
816
fn from(internal_mapping: &InternalMapping) -> Self {
917
Self {
@@ -92,4 +100,25 @@ mod tests {
92100
assert_eq!(otel_mapping.file_offset, 0x200);
93101
assert_eq!(otel_mapping.filename_strindex, 555);
94102
}
103+
104+
#[test]
105+
fn test_into_otel_mapping_owned() {
106+
// Create an internal mapping
107+
let internal_mapping = InternalMapping {
108+
memory_start: 0x5000,
109+
memory_limit: 0x6000,
110+
file_offset: 0x300,
111+
filename: StringId::from_offset(777),
112+
build_id: StringId::from_offset(888),
113+
};
114+
115+
// Convert using .into() method with owned value
116+
let otel_mapping: datadog_profiling_otel::Mapping = internal_mapping.into();
117+
118+
// Verify the conversion
119+
assert_eq!(otel_mapping.memory_start, 0x5000);
120+
assert_eq!(otel_mapping.memory_limit, 0x6000);
121+
assert_eq!(otel_mapping.file_offset, 0x300);
122+
assert_eq!(otel_mapping.filename_strindex, 777);
123+
}
95124
}

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

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ impl InternalProfile {
6969
.period
7070
.map(|(period_value, _)| period_value)
7171
.unwrap_or(0),
72-
comment_strindices: vec![], // We don't have comments
73-
profile_id: vec![], // TODO: Implement when we handle profile IDs
72+
comment_strindices: vec![], // We don't have comments
73+
profile_id: vec![], // TODO: Implement when we handle profile IDs
7474
dropped_attributes_count: 0, // We don't drop attributes
7575
original_payload_format: String::new(), // There is no original payload
76-
original_payload: vec![], // There is no original payload
77-
attribute_indices: vec![], // There are currently no attributes at this level
76+
original_payload: vec![], // There is no original payload
77+
attribute_indices: vec![], // There are currently no attributes at this level
7878
};
7979

8080
profiles.push(profile);
@@ -149,30 +149,10 @@ impl InternalProfile {
149149

150150
// Convert the ProfilesDictionary components
151151
let dictionary = datadog_profiling_otel::ProfilesDictionary {
152-
// Convert mappings
153-
mapping_table: self.mappings.iter().map(|mapping| mapping.into()).collect(),
154-
155-
// Convert locations
156-
location_table: self
157-
.locations
158-
.iter()
159-
.map(|location| location.into())
160-
.collect(),
161-
162-
// Convert functions
163-
function_table: self
164-
.functions
165-
.iter()
166-
.map(|function| function.into())
167-
.collect(),
168-
169-
// Convert stack traces
170-
stack_table: self
171-
.stack_traces
172-
.iter()
173-
.map(|stack_trace| stack_trace.into())
174-
.collect(),
175-
152+
mapping_table: self.mappings.into_iter().map(From::from).collect(),
153+
location_table: self.locations.into_iter().map(From::from).collect(),
154+
function_table: self.functions.into_iter().map(From::from).collect(),
155+
stack_table: self.stack_traces.into_iter().map(From::from).collect(),
176156
string_table,
177157
attribute_table,
178158
attribute_units,

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
use crate::collections::identifiable::Id;
55
use crate::internal::StackTrace as InternalStackTrace;
66

7+
// For owned values - forward to reference version
8+
impl From<InternalStackTrace> for datadog_profiling_otel::Stack {
9+
fn from(internal_stack_trace: InternalStackTrace) -> Self {
10+
Self::from(&internal_stack_trace)
11+
}
12+
}
13+
14+
// For references (existing implementation)
715
impl From<&InternalStackTrace> for datadog_profiling_otel::Stack {
816
fn from(internal_stack_trace: &InternalStackTrace) -> Self {
917
Self {
@@ -55,13 +63,35 @@ mod tests {
5563
fn test_into_otel_stack() {
5664
// Create an internal stack trace
5765
let internal_stack_trace = InternalStackTrace {
58-
locations: vec![LocationId::from_offset(42), LocationId::from_offset(123)],
66+
locations: vec![
67+
LocationId::from_offset(10),
68+
LocationId::from_offset(20),
69+
LocationId::from_offset(30),
70+
],
5971
};
6072

6173
// Convert using .into() method
6274
let otel_stack: datadog_profiling_otel::Stack = (&internal_stack_trace).into();
6375

6476
// Verify the conversion - note: from_offset adds 1 to avoid zero values
65-
assert_eq!(otel_stack.location_indices, vec![43, 124]);
77+
assert_eq!(otel_stack.location_indices, vec![11, 21, 31]);
78+
}
79+
80+
#[test]
81+
fn test_into_otel_stack_owned() {
82+
// Create an internal stack trace
83+
let internal_stack_trace = InternalStackTrace {
84+
locations: vec![
85+
LocationId::from_offset(40),
86+
LocationId::from_offset(50),
87+
LocationId::from_offset(60),
88+
],
89+
};
90+
91+
// Convert using .into() method with owned value
92+
let otel_stack: datadog_profiling_otel::Stack = internal_stack_trace.into();
93+
94+
// Verify the conversion - note: from_offset adds 1 to avoid zero values
95+
assert_eq!(otel_stack.location_indices, vec![41, 51, 61]);
6696
}
6797
}

0 commit comments

Comments
 (0)