Skip to content

Commit 3d16e60

Browse files
committed
more types
1 parent d682fac commit 3d16e60

File tree

5 files changed

+268
-23
lines changed

5 files changed

+268
-23
lines changed

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ mod tests {
2929
filename: StringId::from_offset(2),
3030
};
3131

32-
// Convert to OpenTelemetry Function using From trait
32+
// Convert to OpenTelemetry Function
3333
let otel_function = datadog_profiling_otel::Function::from(&internal_function);
3434

35-
// Verify the conversion
35+
// Verify the conversion - note: StringId doesn't add 1, it's direct conversion
3636
assert_eq!(otel_function.name_strindex, 0);
3737
assert_eq!(otel_function.system_name_strindex, 1);
3838
assert_eq!(otel_function.filename_strindex, 2);
@@ -48,32 +48,13 @@ mod tests {
4848
filename: StringId::from_offset(300),
4949
};
5050

51-
// Convert to OpenTelemetry Function using From trait
51+
// Convert to OpenTelemetry Function
5252
let otel_function = datadog_profiling_otel::Function::from(&internal_function);
5353

54-
// Verify the conversion
54+
// Verify the conversion - note: StringId doesn't add 1, it's direct conversion
5555
assert_eq!(otel_function.name_strindex, 100);
5656
assert_eq!(otel_function.system_name_strindex, 200);
5757
assert_eq!(otel_function.filename_strindex, 300);
5858
assert_eq!(otel_function.start_line, 0);
5959
}
60-
61-
#[test]
62-
fn test_into_otel_function() {
63-
// Create an internal function
64-
let internal_function = InternalFunction {
65-
name: StringId::from_offset(42),
66-
system_name: StringId::from_offset(123),
67-
filename: StringId::from_offset(456),
68-
};
69-
70-
// Convert using .into() method
71-
let otel_function: datadog_profiling_otel::Function = (&internal_function).into();
72-
73-
// Verify the conversion
74-
assert_eq!(otel_function.name_strindex, 42);
75-
assert_eq!(otel_function.system_name_strindex, 123);
76-
assert_eq!(otel_function.filename_strindex, 456);
77-
assert_eq!(otel_function.start_line, 0);
78-
}
7960
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::collections::identifiable::Id;
5+
use crate::internal::Location as InternalLocation;
6+
7+
impl From<&InternalLocation> for datadog_profiling_otel::Location {
8+
fn from(internal_location: &InternalLocation) -> Self {
9+
Self {
10+
mapping_index: internal_location.mapping_id
11+
.map(|id| id.to_raw_id() as i32)
12+
.unwrap_or(0), // 0 represents no mapping
13+
address: internal_location.address,
14+
line: vec![datadog_profiling_otel::Line {
15+
function_index: internal_location.function_id.to_raw_id() as i32,
16+
line: internal_location.line,
17+
column: 0, // Not available in internal Location
18+
}],
19+
is_folded: false, // Not available in internal Location
20+
attribute_indices: vec![], // Not available in internal Location
21+
}
22+
}
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
use crate::internal::{FunctionId, MappingId};
29+
30+
#[test]
31+
fn test_from_internal_location() {
32+
// Create an internal location
33+
let internal_location = InternalLocation {
34+
mapping_id: Some(MappingId::from_offset(1)),
35+
function_id: FunctionId::from_offset(2),
36+
address: 0x1000,
37+
line: 42,
38+
};
39+
40+
// Convert to OpenTelemetry Location
41+
let otel_location = datadog_profiling_otel::Location::from(&internal_location);
42+
43+
// Verify the conversion - note: from_offset adds 1 to avoid zero values
44+
assert_eq!(otel_location.mapping_index, 2);
45+
assert_eq!(otel_location.address, 0x1000);
46+
assert_eq!(otel_location.line.len(), 1);
47+
assert_eq!(otel_location.line[0].function_index, 3);
48+
assert_eq!(otel_location.line[0].line, 42);
49+
assert_eq!(otel_location.line[0].column, 0);
50+
assert_eq!(otel_location.is_folded, false);
51+
assert_eq!(otel_location.attribute_indices, vec![] as Vec<i32>);
52+
}
53+
54+
#[test]
55+
fn test_from_internal_location_no_mapping() {
56+
// Create an internal location without mapping
57+
let internal_location = InternalLocation {
58+
mapping_id: None,
59+
function_id: FunctionId::from_offset(5),
60+
address: 0x2000,
61+
line: 100,
62+
};
63+
64+
// Convert to OpenTelemetry Location
65+
let otel_location = datadog_profiling_otel::Location::from(&internal_location);
66+
67+
// Verify the conversion
68+
assert_eq!(otel_location.mapping_index, 0); // 0 represents no mapping
69+
assert_eq!(otel_location.address, 0x2000);
70+
assert_eq!(otel_location.line.len(), 1);
71+
assert_eq!(otel_location.line[0].function_index, 6);
72+
assert_eq!(otel_location.line[0].line, 100);
73+
}
74+
75+
#[test]
76+
fn test_into_otel_location() {
77+
// Create an internal location
78+
let internal_location = InternalLocation {
79+
mapping_id: Some(MappingId::from_offset(10)),
80+
function_id: FunctionId::from_offset(20),
81+
address: 0x3000,
82+
line: 200,
83+
};
84+
85+
// Convert using .into() method
86+
let otel_location: datadog_profiling_otel::Location = (&internal_location).into();
87+
88+
// Verify the conversion - note: from_offset adds 1 to avoid zero values
89+
assert_eq!(otel_location.mapping_index, 11);
90+
assert_eq!(otel_location.address, 0x3000);
91+
assert_eq!(otel_location.line[0].function_index, 21);
92+
assert_eq!(otel_location.line[0].line, 200);
93+
}
94+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::collections::identifiable::Id;
5+
use crate::internal::Mapping as InternalMapping;
6+
7+
impl From<&InternalMapping> for datadog_profiling_otel::Mapping {
8+
fn from(internal_mapping: &InternalMapping) -> Self {
9+
Self {
10+
memory_start: internal_mapping.memory_start,
11+
memory_limit: internal_mapping.memory_limit,
12+
file_offset: internal_mapping.file_offset,
13+
filename_strindex: internal_mapping.filename.to_raw_id() as i32,
14+
attribute_indices: vec![], // Not available in internal Mapping
15+
has_functions: true, // Assume true since we have function information
16+
has_filenames: true, // Assume true since we have filename
17+
has_line_numbers: true, // Assume true since we have line information
18+
has_inline_frames: false, // Not available in internal Mapping
19+
}
20+
}
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
use crate::collections::identifiable::StringId;
27+
28+
#[test]
29+
fn test_from_internal_mapping() {
30+
// Create an internal mapping
31+
let internal_mapping = InternalMapping {
32+
memory_start: 0x1000,
33+
memory_limit: 0x2000,
34+
file_offset: 0x100,
35+
filename: StringId::from_offset(42),
36+
build_id: StringId::from_offset(123),
37+
};
38+
39+
// Convert to OpenTelemetry Mapping
40+
let otel_mapping = datadog_profiling_otel::Mapping::from(&internal_mapping);
41+
42+
// Verify the conversion
43+
assert_eq!(otel_mapping.memory_start, 0x1000);
44+
assert_eq!(otel_mapping.memory_limit, 0x2000);
45+
assert_eq!(otel_mapping.file_offset, 0x100);
46+
assert_eq!(otel_mapping.filename_strindex, 42);
47+
assert_eq!(otel_mapping.attribute_indices, vec![] as Vec<i32>);
48+
assert_eq!(otel_mapping.has_functions, true);
49+
assert_eq!(otel_mapping.has_filenames, true);
50+
assert_eq!(otel_mapping.has_line_numbers, true);
51+
assert_eq!(otel_mapping.has_inline_frames, false);
52+
}
53+
54+
#[test]
55+
fn test_from_internal_mapping_large_values() {
56+
// Create an internal mapping with large values
57+
let internal_mapping = InternalMapping {
58+
memory_start: 0x1000000000000000,
59+
memory_limit: 0x2000000000000000,
60+
file_offset: 0x1000000000000000,
61+
filename: StringId::from_offset(999),
62+
build_id: StringId::from_offset(888),
63+
};
64+
65+
// Convert to OpenTelemetry Mapping
66+
let otel_mapping = datadog_profiling_otel::Mapping::from(&internal_mapping);
67+
68+
// Verify the conversion
69+
assert_eq!(otel_mapping.memory_start, 0x1000000000000000);
70+
assert_eq!(otel_mapping.memory_limit, 0x2000000000000000);
71+
assert_eq!(otel_mapping.file_offset, 0x1000000000000000);
72+
assert_eq!(otel_mapping.filename_strindex, 999);
73+
}
74+
75+
#[test]
76+
fn test_into_otel_mapping() {
77+
// Create an internal mapping
78+
let internal_mapping = InternalMapping {
79+
memory_start: 0x3000,
80+
memory_limit: 0x4000,
81+
file_offset: 0x200,
82+
filename: StringId::from_offset(555),
83+
build_id: StringId::from_offset(666),
84+
};
85+
86+
// Convert using .into() method
87+
let otel_mapping: datadog_profiling_otel::Mapping = (&internal_mapping).into();
88+
89+
// Verify the conversion
90+
assert_eq!(otel_mapping.memory_start, 0x3000);
91+
assert_eq!(otel_mapping.memory_limit, 0x4000);
92+
assert_eq!(otel_mapping.file_offset, 0x200);
93+
assert_eq!(otel_mapping.filename_strindex, 555);
94+
}
95+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
//! OpenTelemetry protobuf equivalents.
88
99
pub mod function;
10+
pub mod stack_trace;
11+
pub mod location;
12+
pub mod mapping;
1013

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::collections::identifiable::Id;
5+
use crate::internal::StackTrace as InternalStackTrace;
6+
7+
impl From<&InternalStackTrace> for datadog_profiling_otel::Stack {
8+
fn from(internal_stack_trace: &InternalStackTrace) -> Self {
9+
Self {
10+
location_indices: internal_stack_trace
11+
.locations
12+
.iter()
13+
.map(|location_id| location_id.to_raw_id() as i32)
14+
.collect(),
15+
}
16+
}
17+
}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use super::*;
22+
use crate::internal::LocationId;
23+
24+
#[test]
25+
fn test_from_internal_stack_trace() {
26+
// Create an internal stack trace
27+
let internal_stack_trace = InternalStackTrace {
28+
locations: vec![
29+
LocationId::from_offset(0),
30+
LocationId::from_offset(1),
31+
LocationId::from_offset(2),
32+
],
33+
};
34+
35+
// Convert to OpenTelemetry Stack
36+
let otel_stack = datadog_profiling_otel::Stack::from(&internal_stack_trace);
37+
38+
// Verify the conversion - note: from_offset adds 1 to avoid zero values
39+
assert_eq!(otel_stack.location_indices, vec![1, 2, 3]);
40+
}
41+
42+
#[test]
43+
fn test_from_empty_stack_trace() {
44+
// Create an internal stack trace with no locations
45+
let internal_stack_trace = InternalStackTrace {
46+
locations: vec![],
47+
};
48+
49+
// Convert to OpenTelemetry Stack
50+
let otel_stack = datadog_profiling_otel::Stack::from(&internal_stack_trace);
51+
52+
// Verify the conversion
53+
assert_eq!(otel_stack.location_indices, vec![] as Vec<i32>);
54+
}
55+
56+
#[test]
57+
fn test_into_otel_stack() {
58+
// Create an internal stack trace
59+
let internal_stack_trace = InternalStackTrace {
60+
locations: vec![
61+
LocationId::from_offset(42),
62+
LocationId::from_offset(123),
63+
],
64+
};
65+
66+
// Convert using .into() method
67+
let otel_stack: datadog_profiling_otel::Stack = (&internal_stack_trace).into();
68+
69+
// Verify the conversion - note: from_offset adds 1 to avoid zero values
70+
assert_eq!(otel_stack.location_indices, vec![43, 124]);
71+
}
72+
}

0 commit comments

Comments
 (0)