Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions libdd-profiling-ffi/src/profiles/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ impl From<anyhow::Result<internal::EncodedProfile>> for SerializeResult {
/// Re-exported from libdd_profiling::api for FFI use.
pub use api::SampleType;

/// Returns the pprof type string for a `SampleType` (e.g. `"cpu-time"`).
/// The returned slice points to a static string; it is always valid and
/// never needs to be freed.
#[no_mangle]
pub extern "C" fn ddog_prof_SampleType_type_str(sample_type: SampleType) -> CharSlice<'static> {
let vt: api::ValueType = sample_type.into();
CharSlice::from(vt.r#type)
}

/// Returns the pprof unit string for a `SampleType` (e.g. `"nanoseconds"`).
/// The returned slice points to a static string; it is always valid and
/// never needs to be freed.
#[no_mangle]
pub extern "C" fn ddog_prof_SampleType_unit_str(sample_type: SampleType) -> CharSlice<'static> {
let vt: api::ValueType = sample_type.into();
CharSlice::from(vt.unit)
}

/// Re-export Period from the API for consistency.
/// The FFI Period is identical to the API Period.
pub use api::Period;
Expand Down Expand Up @@ -1094,4 +1112,36 @@ mod tests {
ddog_prof_Profile_drop(&mut provide_distinct_locations_ffi());
}
}

#[test]
fn sample_type_str_functions() {
// Verify that the type/unit strings match expected pprof column names.
// These strings are what the backend uses to identify profile types, so
// any change here would be a breaking change for consumers.
let cases: &[(SampleType, &str, &str)] = &[
(SampleType::CpuTime, "cpu-time", "nanoseconds"),
(SampleType::CpuSamples, "cpu-samples", "count"),
(SampleType::AllocSpace, "alloc-space", "bytes"),
(SampleType::AllocSamples, "alloc-samples", "count"),
(SampleType::InuseSpace, "inuse-space", "bytes"),
(SampleType::InuseObjects, "inuse-objects", "count"),
(SampleType::Tracepoint, "tracepoint", "events"),
(SampleType::WallTime, "wall-time", "nanoseconds"),
(SampleType::WallSamples, "wall-samples", "count"),
];
for &(sample_type, expected_type, expected_unit) in cases {
let type_str = ddog_prof_SampleType_type_str(sample_type);
let unit_str = ddog_prof_SampleType_unit_str(sample_type);
assert_eq!(
type_str.try_to_utf8().unwrap(),
expected_type,
"type string mismatch for {sample_type:?}"
);
assert_eq!(
unit_str.try_to_utf8().unwrap(),
expected_unit,
"unit string mismatch for {sample_type:?}"
);
}
}
}
3 changes: 3 additions & 0 deletions libdd-profiling/src/api/sample_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub enum SampleType {
ObjectsLegacy,
RequestTime,
Sample,
Tracepoint,
SocketReadSize,
SocketReadSizeSamples,
SocketReadTime,
Expand Down Expand Up @@ -137,6 +138,7 @@ impl From<SampleType> for ValueType<'static> {
SampleType::ObjectsLegacy => ValueType::new("objects", "count"),
SampleType::RequestTime => ValueType::new("request-time", "nanoseconds"),
SampleType::Sample => ValueType::new("sample", "count"),
SampleType::Tracepoint => ValueType::new("tracepoint", "events"),
SampleType::SocketReadSize => ValueType::new("socket-read-size", "bytes"),
SampleType::SocketReadSizeSamples => {
ValueType::new("socket-read-size-samples", "count")
Expand Down Expand Up @@ -210,6 +212,7 @@ impl<'a> TryFrom<ValueType<'a>> for SampleType {
("objects", "count") => SampleType::ObjectsLegacy,
("request-time", "nanoseconds") => SampleType::RequestTime,
("sample", "count") => SampleType::Sample,
("tracepoint", "events") => SampleType::Tracepoint,
("socket-read-size", "bytes") => SampleType::SocketReadSize,
("socket-read-size-samples", "count") => SampleType::SocketReadSizeSamples,
("socket-read-time", "nanoseconds") => SampleType::SocketReadTime,
Expand Down
2 changes: 2 additions & 0 deletions libdd-profiling/src/cxx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub mod ffi {
ObjectsLegacy, // LEGACY: Use InuseObjects instead
RequestTime,
Sample,
Tracepoint,
SocketReadSize,
SocketReadSizeSamples,
SocketReadTime,
Expand Down Expand Up @@ -376,6 +377,7 @@ impl TryFrom<ffi::SampleType> for api::SampleType {
ffi::SampleType::ObjectsLegacy => api::SampleType::ObjectsLegacy,
ffi::SampleType::RequestTime => api::SampleType::RequestTime,
ffi::SampleType::Sample => api::SampleType::Sample,
ffi::SampleType::Tracepoint => api::SampleType::Tracepoint,
ffi::SampleType::SocketReadSize => api::SampleType::SocketReadSize,
ffi::SampleType::SocketReadSizeSamples => api::SampleType::SocketReadSizeSamples,
ffi::SampleType::SocketReadTime => api::SampleType::SocketReadTime,
Expand Down
Loading