Skip to content

Commit

Permalink
Merge branch 'main' into david.lee/workflow-to-build-serverless-agent…
Browse files Browse the repository at this point in the history
…-testing
  • Loading branch information
thedavl authored Sep 25, 2023
2 parents a3b7135 + 301ced4 commit 5d74289
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 24 deletions.
4 changes: 4 additions & 0 deletions ddcommon-ffi/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub struct Vec<T: Sized> {
_marker: PhantomData<T>,
}

unsafe impl<T: Send> Send for Vec<T> {}

unsafe impl<T: Sync> Sync for Vec<T> {}

impl<T: PartialEq> PartialEq for Vec<T> {
fn eq(&self, other: &Self) -> bool {
self.len() == other.len() && self.iter().zip(other.iter()).all(|(s, o)| s == o)
Expand Down
19 changes: 12 additions & 7 deletions examples/ffi/exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ int main(int argc, char *argv[]) {

const ddog_prof_Slice_ValueType sample_types = {&wall_time, 1};
const ddog_prof_Period period = {wall_time, 60};
ddog_prof_Profile profile_data{
ddog_prof_Profile_new(sample_types, &period, nullptr)};
std::unique_ptr<ddog_prof_Profile, Deleter> profile{&profile_data};
ddog_prof_Profile_NewResult profile_new_result =
ddog_prof_Profile_new(sample_types, &period, nullptr);
if (profile_new_result.tag != DDOG_PROF_PROFILE_NEW_RESULT_OK) {
print_error("Failed to make new profile: ", profile_new_result.err);
ddog_Error_drop(&profile_new_result.err);
exit(EXIT_FAILURE);
}
std::unique_ptr<ddog_prof_Profile, Deleter> profile{&profile_new_result.ok};

ddog_prof_Location root_location = {
// yes, a zero-initialized mapping is valid
Expand Down Expand Up @@ -130,13 +135,13 @@ int main(int argc, char *argv[]) {
};

ddog_prof_Exporter_Slice_File files_to_export_unmodified = ddog_prof_Exporter_Slice_File_empty();

ddog_CharSlice internal_metadata_example = DDOG_CHARSLICE_C(
"{\"no_signals_workaround_enabled\": \"true\", \"execution_trace_enabled\": \"false\"}");

ddog_prof_Exporter_Request_BuildResult build_result =
ddog_prof_Exporter_Request_build(exporter, encoded_profile->start, encoded_profile->end,
files_to_compress_and_export, files_to_export_unmodified, nullptr, nullptr, &internal_metadata_example, 30000);
ddog_prof_Exporter_Request_BuildResult build_result = ddog_prof_Exporter_Request_build(
exporter, encoded_profile->start, encoded_profile->end, files_to_compress_and_export,
files_to_export_unmodified, nullptr, nullptr, &internal_metadata_example, 30000);
ddog_prof_EncodedProfile_drop(encoded_profile);

if (build_result.tag == DDOG_PROF_EXPORTER_REQUEST_BUILD_RESULT_ERR) {
Expand Down
17 changes: 13 additions & 4 deletions examples/ffi/profiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <datadog/common.h>
#include <datadog/profiling.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
const ddog_prof_ValueType wall_time = {
Expand All @@ -15,7 +16,15 @@ int main(void) {
const ddog_prof_Slice_ValueType sample_types = {&wall_time, 1};
const ddog_prof_Period period = {wall_time, 60};

ddog_prof_Profile profile = ddog_prof_Profile_new(sample_types, &period, NULL);
ddog_prof_Profile_NewResult new_result = ddog_prof_Profile_new(sample_types, &period, NULL);
if (new_result.tag != DDOG_PROF_PROFILE_NEW_RESULT_OK) {
ddog_CharSlice message = ddog_Error_message(&new_result.err);
fprintf(stderr, "%*s", (int)message.len, message.ptr);
ddog_Error_drop(&new_result.err);
exit(EXIT_FAILURE);
}

ddog_prof_Profile *profile = &new_result.ok;

ddog_prof_Location root_location = {
// yes, a zero-initialized mapping is valid
Expand All @@ -40,7 +49,7 @@ int main(void) {
for (int i = 0; i < 10000000; i++) {
label.num = i;

ddog_prof_Profile_Result add_result = ddog_prof_Profile_add(&profile, sample, 0);
ddog_prof_Profile_Result add_result = ddog_prof_Profile_add(profile, sample, 0);
if (add_result.tag != DDOG_PROF_PROFILE_RESULT_OK) {
ddog_CharSlice message = ddog_Error_message(&add_result.err);
fprintf(stderr, "%*s", (int)message.len, message.ptr);
Expand All @@ -51,13 +60,13 @@ int main(void) {
// printf("Press any key to reset and drop...");
// getchar();

ddog_prof_Profile_Result reset_result = ddog_prof_Profile_reset(&profile, NULL);
ddog_prof_Profile_Result reset_result = ddog_prof_Profile_reset(profile, NULL);
if (reset_result.tag != DDOG_PROF_PROFILE_RESULT_OK) {
ddog_CharSlice message = ddog_Error_message(&reset_result.err);
fprintf(stderr, "%*s", (int)message.len, message.ptr);
ddog_Error_drop(&reset_result.err);
}
ddog_prof_Profile_drop(&profile);
ddog_prof_Profile_drop(profile);

printf("Press any key to exit...");
getchar();
Expand Down
3 changes: 2 additions & 1 deletion profiling-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ renaming_overrides_prefixing = true

"ExporterNewResult" = "ddog_prof_Exporter_NewResult"
"File" = "ddog_prof_Exporter_File"
"ProfileResult" = "ddog_prof_Profile_Result"
"ProfileExporter" = "ddog_prof_Exporter"
"ProfileNewResult" = "ddog_prof_Profile_NewResult"
"ProfileResult" = "ddog_prof_Profile_Result"
"Request" = "ddog_prof_Exporter_Request"
"RequestBuildResult" = "ddog_prof_Exporter_Request_BuildResult"
"SendResult" = "ddog_prof_Exporter_SendResult"
Expand Down
61 changes: 49 additions & 12 deletions profiling-ffi/src/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ pub enum ProfileResult {
Err(Error),
}

/// Returned by [ddog_prof_Profile_new].
#[repr(C)]
pub enum ProfileNewResult {
Ok(Profile),
#[allow(dead_code)]
Err(Error),
}

#[repr(C)]
pub enum SerializeResult {
Ok(EncodedProfile),
Expand Down Expand Up @@ -339,15 +347,15 @@ pub unsafe extern "C" fn ddog_prof_Profile_new(
sample_types: Slice<ValueType>,
period: Option<&Period>,
start_time: Option<&Timespec>,
) -> Profile {
) -> ProfileNewResult {
let types: Vec<api::ValueType> = sample_types.into_slice().iter().map(Into::into).collect();

let builder = profile::Profile::builder()
.period(period.map(Into::into))
.sample_types(types)
.start_time(start_time.map(SystemTime::from));

Profile::new(builder.build())
ProfileNewResult::Ok(Profile::new(builder.build()))
}

/// # Safety
Expand All @@ -363,11 +371,21 @@ pub unsafe extern "C" fn ddog_prof_Profile_drop(profile: *mut Profile) {
}

#[cfg(test)]
impl From<ProfileResult> for Result<(), String> {
impl From<ProfileResult> for Result<(), Error> {
fn from(result: ProfileResult) -> Self {
match result {
ProfileResult::Ok(_) => Ok(()),
ProfileResult::Err(err) => Err(err.into()),
ProfileResult::Err(err) => Err(err),
}
}
}

#[cfg(test)]
impl From<ProfileNewResult> for Result<Profile, Error> {
fn from(result: ProfileNewResult) -> Self {
match result {
ProfileNewResult::Ok(p) => Ok(p),
ProfileNewResult::Err(err) => Err(err),
}
}
}
Expand Down Expand Up @@ -757,19 +775,28 @@ mod test {
use super::*;

#[test]
fn ctor_and_dtor() {
fn ctor_and_dtor() -> Result<(), Error> {
unsafe {
let sample_type: *const ValueType = &ValueType::new("samples", "count");
let mut profile = ddog_prof_Profile_new(Slice::new(sample_type, 1), None, None);
let mut profile = Result::from(ddog_prof_Profile_new(
Slice::new(sample_type, 1),
None,
None,
))?;
ddog_prof_Profile_drop(&mut profile);
Ok(())
}
}

#[test]
fn add_failure() {
fn add_failure() -> Result<(), Error> {
unsafe {
let sample_type: *const ValueType = &ValueType::new("samples", "count");
let mut profile = ddog_prof_Profile_new(Slice::new(sample_type, 1), None, None);
let mut profile = Result::from(ddog_prof_Profile_new(
Slice::new(sample_type, 1),
None,
None,
))?;

// wrong number of values (doesn't match sample types)
let values: &[i64] = &[];
Expand All @@ -783,6 +810,7 @@ mod test {
let result = Result::from(ddog_prof_Profile_add(&mut profile, sample, None));
result.unwrap_err();
ddog_prof_Profile_drop(&mut profile);
Ok(())
}
}

Expand All @@ -793,7 +821,11 @@ mod test {
fn aggregate_samples() -> anyhow::Result<()> {
unsafe {
let sample_type: *const ValueType = &ValueType::new("samples", "count");
let mut profile = ddog_prof_Profile_new(Slice::new(sample_type, 1), None, None);
let mut profile = Result::from(ddog_prof_Profile_new(
Slice::new(sample_type, 1),
None,
None,
))?;

let mapping = Mapping {
filename: "php".into(),
Expand Down Expand Up @@ -823,7 +855,7 @@ mod test {
labels: Slice::from(&labels),
};

Result::from(ddog_prof_Profile_add(&mut profile, sample, None)).unwrap();
Result::from(ddog_prof_Profile_add(&mut profile, sample, None))?;
assert_eq!(
profile
.inner
Expand All @@ -833,7 +865,7 @@ mod test {
1
);

Result::from(ddog_prof_Profile_add(&mut profile, sample, None)).unwrap();
Result::from(ddog_prof_Profile_add(&mut profile, sample, None))?;
assert_eq!(
profile
.inner
Expand All @@ -850,7 +882,12 @@ mod test {

unsafe fn provide_distinct_locations_ffi() -> Profile {
let sample_type: *const ValueType = &ValueType::new("samples", "count");
let mut profile = ddog_prof_Profile_new(Slice::new(sample_type, 1), None, None);
let mut profile = Result::from(ddog_prof_Profile_new(
Slice::new(sample_type, 1),
None,
None,
))
.unwrap();

let mapping = Mapping {
filename: "php".into(),
Expand Down

0 comments on commit 5d74289

Please sign in to comment.