Skip to content

Commit f737e28

Browse files
committed
handle periods
1 parent de2371a commit f737e28

File tree

1 file changed

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

1 file changed

+74
-13
lines changed

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

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,22 @@ impl InternalProfile {
5656
.duration_since(std::time::UNIX_EPOCH)
5757
.unwrap_or_default()
5858
.as_nanos() as i64,
59-
duration_nanos, // Use calculated duration
60-
period_type: None, // TODO: Implement when we handle period types
61-
period: 0, // TODO: Implement when we handle periods
62-
comment_strindices: vec![], // TODO: Implement when we handle comments
59+
duration_nanos, // Use calculated duration
60+
period_type: self.period.as_ref().map(|(_, period_type)| {
61+
datadog_profiling_otel::ValueType {
62+
type_strindex: period_type.r#type.value.to_raw_id() as i32,
63+
unit_strindex: period_type.unit.value.to_raw_id() as i32,
64+
aggregation_temporality:
65+
datadog_profiling_otel::AggregationTemporality::Delta.into(),
66+
}
67+
}),
68+
period: self
69+
.period
70+
.map(|(period_value, _)| period_value)
71+
.unwrap_or(0),
72+
comment_strindices: vec![], // We don't have comments
6373
profile_id: vec![], // TODO: Implement when we handle profile IDs
64-
dropped_attributes_count: 0, // TODO: Implement when we handle dropped attributes
74+
dropped_attributes_count: 0, // We don't drop attributes
6575
original_payload_format: String::new(), // There is no original payload
6676
original_payload: vec![], // There is no original payload
6777
attribute_indices: vec![], // There are currently no attributes at this level
@@ -222,7 +232,8 @@ mod tests {
222232
if !scope_profile.profiles.is_empty() {
223233
let profile = &scope_profile.profiles[0];
224234
// When no duration is provided, it should calculate from current time - start time
225-
// Since we're testing with None, None, the duration should be > 0 (current time - start time)
235+
// Since we're testing with None, None, the duration should be > 0 (current time - start
236+
// time)
226237
assert!(profile.duration_nanos > 0);
227238
}
228239
}
@@ -782,8 +793,10 @@ mod tests {
782793

783794
// Test with explicit duration
784795
let explicit_duration = std::time::Duration::from_secs(5);
785-
let otel_profiles_data = internal_profile.serialize_into_otel(None, Some(explicit_duration)).unwrap();
786-
796+
let otel_profiles_data = internal_profile
797+
.serialize_into_otel(None, Some(explicit_duration))
798+
.unwrap();
799+
787800
let profile = &otel_profiles_data.resource_profiles[0].scope_profiles[0].profiles[0];
788801
// Should use the explicit duration (5 seconds = 5_000_000_000 nanoseconds)
789802
assert_eq!(profile.duration_nanos, 5_000_000_000);
@@ -792,21 +805,69 @@ mod tests {
792805
let internal_profile2 = InternalProfile::new(&sample_types, None);
793806
let start_time = internal_profile2.start_time;
794807
let end_time = start_time + std::time::Duration::from_secs(3);
795-
let otel_profiles_data2 = internal_profile2.serialize_into_otel(Some(end_time), None).unwrap();
796-
808+
let otel_profiles_data2 = internal_profile2
809+
.serialize_into_otel(Some(end_time), None)
810+
.unwrap();
811+
797812
let profile2 = &otel_profiles_data2.resource_profiles[0].scope_profiles[0].profiles[0];
798-
// Should calculate duration from end_time - start_time (3 seconds = 3_000_000_000 nanoseconds)
813+
// Should calculate duration from end_time - start_time (3 seconds = 3_000_000_000
814+
// nanoseconds)
799815
assert_eq!(profile2.duration_nanos, 3_000_000_000);
800816

801817
// Test with both end_time and duration (duration should take precedence)
802818
let internal_profile3 = InternalProfile::new(&sample_types, None);
803819
let start_time3 = internal_profile3.start_time;
804820
let end_time3 = start_time3 + std::time::Duration::from_secs(10);
805821
let duration3 = std::time::Duration::from_secs(7);
806-
let otel_profiles_data3 = internal_profile3.serialize_into_otel(Some(end_time3), Some(duration3)).unwrap();
807-
822+
let otel_profiles_data3 = internal_profile3
823+
.serialize_into_otel(Some(end_time3), Some(duration3))
824+
.unwrap();
825+
808826
let profile3 = &otel_profiles_data3.resource_profiles[0].scope_profiles[0].profiles[0];
809827
// Should use the explicit duration (7 seconds = 7_000_000_000 nanoseconds)
810828
assert_eq!(profile3.duration_nanos, 7_000_000_000);
811829
}
830+
831+
#[test]
832+
fn test_period_conversion() {
833+
// Create an internal profile with sample types and period
834+
let sample_types = [crate::api::ValueType::new("cpu", "nanoseconds")];
835+
let period = crate::api::Period {
836+
r#type: crate::api::ValueType::new("cpu", "cycles"),
837+
value: 1000,
838+
};
839+
let internal_profile = InternalProfile::new(&sample_types, Some(period));
840+
841+
// Convert to OpenTelemetry ProfilesData
842+
let otel_profiles_data = internal_profile.serialize_into_otel(None, None).unwrap();
843+
844+
let profile = &otel_profiles_data.resource_profiles[0].scope_profiles[0].profiles[0];
845+
846+
// Should have period type information
847+
assert!(profile.period_type.is_some());
848+
let period_type = profile.period_type.as_ref().unwrap();
849+
850+
// The period type should be converted from the internal profile's period
851+
// Note: The exact string indices depend on the string table, but we can verify they're
852+
// valid
853+
assert!(period_type.type_strindex >= 0);
854+
assert!(period_type.unit_strindex >= 0);
855+
856+
// Should have the correct period value
857+
assert_eq!(profile.period, 1000);
858+
859+
// Test without period
860+
let internal_profile_no_period = InternalProfile::new(&sample_types, None);
861+
let otel_profiles_data_no_period = internal_profile_no_period
862+
.serialize_into_otel(None, None)
863+
.unwrap();
864+
865+
let profile_no_period =
866+
&otel_profiles_data_no_period.resource_profiles[0].scope_profiles[0].profiles[0];
867+
868+
// Should have no period type when no period is set
869+
assert!(profile_no_period.period_type.is_none());
870+
// Should have period value of 0 when no period is set
871+
assert_eq!(profile_no_period.period, 0);
872+
}
812873
}

0 commit comments

Comments
 (0)