Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[profiling] Reduce copying and allocation in exporter #926

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 15 additions & 22 deletions examples/ffi/exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ int main(int argc, char *argv[]) {
return 1;
}

ddog_prof_EncodedProfile *encoded_profile = &serialize_result.ok;
auto *encoded_profile = &serialize_result.ok;

ddog_prof_Endpoint endpoint =
auto endpoint =
ddog_prof_Endpoint_agentless(DDOG_CHARSLICE_C_BARE("datad0g.com"), to_slice_c_char(api_key));

ddog_Vec_Tag tags = ddog_Vec_Tag_new();
Expand All @@ -110,9 +110,9 @@ int main(int argc, char *argv[]) {
return 1;
}

ddog_prof_Exporter_NewResult exporter_new_result =
ddog_prof_Exporter_new(DDOG_CHARSLICE_C_BARE("exporter-example"), DDOG_CHARSLICE_C_BARE("1.2.3"),
DDOG_CHARSLICE_C_BARE("native"), &tags, endpoint);
ddog_prof_Exporter_NewResult exporter_new_result = ddog_prof_Exporter_new(
DDOG_CHARSLICE_C_BARE("exporter-example"), DDOG_CHARSLICE_C_BARE("1.2.3"),
DDOG_CHARSLICE_C_BARE("native"), &tags, endpoint);
ddog_Vec_Tag_drop(tags);

if (exporter_new_result.tag == DDOG_PROF_EXPORTER_NEW_RESULT_ERR) {
Expand All @@ -123,33 +123,26 @@ int main(int argc, char *argv[]) {

auto exporter = exporter_new_result.ok;

ddog_prof_Exporter_File files_to_compress_and_export_[] = {{
.name = DDOG_CHARSLICE_C_BARE("auto.pprof"),
.file = ddog_Vec_U8_as_slice(&encoded_profile->buffer),
}};
ddog_prof_Exporter_Slice_File files_to_compress_and_export = {
.ptr = files_to_compress_and_export_,
.len = sizeof files_to_compress_and_export_ / sizeof *files_to_compress_and_export_,
};

ddog_prof_Exporter_Slice_File files_to_export_unmodified = ddog_prof_Exporter_Slice_File_empty();
auto files_to_compress_and_export = ddog_prof_Exporter_Slice_File_empty();
auto files_to_export_unmodified = ddog_prof_Exporter_Slice_File_empty();

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

ddog_CharSlice info_example = DDOG_CHARSLICE_C_BARE(
"{\"application\": {\"start_time\": \"2024-01-24T11:17:22+0000\"}, \"platform\": {\"kernel\": \"Darwin Kernel 22.5.0\"}}");
ddog_CharSlice info_example =
DDOG_CHARSLICE_C_BARE("{\"application\": {\"start_time\": \"2024-01-24T11:17:22+0000\"}, "
"\"platform\": {\"kernel\": \"Darwin Kernel 22.5.0\"}}");

auto res = ddog_prof_Exporter_set_timeout(exporter, 30000);
if (res.tag == DDOG_PROF_OPTION_ERROR_SOME_ERROR) {
print_error("Failed to set the timeout", res.some);
ddog_Error_drop(&res.some);
return 1;
print_error("Failed to set the timeout", res.some);
ddog_Error_drop(&res.some);
return 1;
}

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, &info_example);
exporter, encoded_profile, files_to_compress_and_export, files_to_export_unmodified, nullptr,
&internal_metadata_example, &info_example);
ddog_prof_EncodedProfile_drop(encoded_profile);

if (build_result.tag == DDOG_PROF_EXPORTER_REQUEST_BUILD_RESULT_ERR) {
Expand Down
136 changes: 32 additions & 104 deletions profiling-ffi/src/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use datadog_profiling::exporter;
use datadog_profiling::exporter::{ProfileExporter, Request};
use datadog_profiling::internal::ProfiledEndpointsStats;
use datadog_profiling::internal::EncodedProfile;
use ddcommon::tag::Tag;
use ddcommon_ffi::slice::{AsBytes, ByteSlice, CharSlice, Slice};
use ddcommon_ffi::{Error, MaybeError, Timespec};
use ddcommon_ffi::{Error, Handle, MaybeError, ToInner};
use std::borrow::Cow;
use std::ptr::NonNull;
use std::str::FromStr;
Expand Down Expand Up @@ -259,16 +259,15 @@ impl From<RequestBuildResult> for Result<Box<Request>, String> {
/// valid objects created by this module.
/// NULL is allowed for `optional_additional_tags`, `optional_endpoints_stats`,
/// `optional_internal_metadata_json` and `optional_info_json`.
/// Consumes the `SerializedProfile`
#[no_mangle]
#[must_use]
pub unsafe extern "C" fn ddog_prof_Exporter_Request_build(
exporter: Option<&mut ProfileExporter>,
start: Timespec,
end: Timespec,
profile: *mut Handle<EncodedProfile>,
files_to_compress_and_export: Slice<File>,
files_to_export_unmodified: Slice<File>,
optional_additional_tags: Option<&ddcommon_ffi::Vec<Tag>>,
optional_endpoints_stats: Option<&ProfiledEndpointsStats>,
optional_internal_metadata_json: Option<&CharSlice>,
optional_info_json: Option<&CharSlice>,
) -> RequestBuildResult {
Expand All @@ -290,13 +289,20 @@ pub unsafe extern "C" fn ddog_prof_Exporter_Request_build(
Err(err) => return RequestBuildResult::Err(err.into()),
};

if profile.is_null() {
return RequestBuildResult::Err(anyhow::anyhow!("profile pointer was null").into());
}

let profile = match (*profile).take() {
Ok(p) => *p,
Err(e) => return RequestBuildResult::Err(e.into()),
};

match exporter.build(
start.into(),
end.into(),
profile,
files_to_compress_and_export.as_slice(),
files_to_export_unmodified.as_slice(),
tags.as_ref(),
optional_endpoints_stats,
internal_metadata,
info,
) {
Expand Down Expand Up @@ -575,19 +581,7 @@ mod tests {
ExporterNewResult::Err(_) => panic!("Should not occur!"),
};

let files_to_compress_and_export: &[File] = &[File {
name: CharSlice::from("foo.pprof"),
file: ByteSlice::from(b"dummy contents" as &[u8]),
}];

let start = Timespec {
seconds: 12,
nanoseconds: 34,
};
let finish = Timespec {
seconds: 56,
nanoseconds: 78,
};
let profile = &mut EncodedProfile::test_instance().unwrap().into();
let timeout_milliseconds = 90;
unsafe {
ddog_prof_Exporter_set_timeout(Some(exporter.as_mut()), timeout_milliseconds)
Expand All @@ -597,11 +591,9 @@ mod tests {
let build_result = unsafe {
ddog_prof_Exporter_Request_build(
Some(exporter.as_mut()),
start,
finish,
Slice::from(files_to_compress_and_export),
profile,
Slice::empty(),
Slice::empty(),
None,
None,
None,
None,
Expand All @@ -610,7 +602,7 @@ mod tests {

let parsed_event_json = parsed_event_json(build_result);

assert_eq!(parsed_event_json["attachments"], json!(["foo.pprof"]));
assert_eq!(parsed_event_json["attachments"], json!(["profile.pprof"]));
assert_eq!(parsed_event_json["endpoint_counts"], json!(null));
assert_eq!(
parsed_event_json["start"],
Expand Down Expand Up @@ -649,19 +641,7 @@ mod tests {
ExporterNewResult::Err(_) => panic!("Should not occur!"),
};

let files: &[File] = &[File {
name: CharSlice::from("foo.pprof"),
file: ByteSlice::from(b"dummy contents" as &[u8]),
}];

let start = Timespec {
seconds: 12,
nanoseconds: 34,
};
let finish = Timespec {
seconds: 56,
nanoseconds: 78,
};
let profile = &mut EncodedProfile::test_instance().unwrap().into();
let timeout_milliseconds = 90;
unsafe {
ddog_prof_Exporter_set_timeout(Some(exporter.as_mut()), timeout_milliseconds)
Expand All @@ -681,11 +661,9 @@ mod tests {
let build_result = unsafe {
ddog_prof_Exporter_Request_build(
Some(exporter.as_mut()),
start,
finish,
Slice::from(files),
profile,
Slice::empty(),
Slice::empty(),
None,
None,
Some(&raw_internal_metadata),
None,
Expand Down Expand Up @@ -724,19 +702,8 @@ mod tests {
ExporterNewResult::Err(_) => panic!("Should not occur!"),
};

let files: &[File] = &[File {
name: CharSlice::from("foo.pprof"),
file: ByteSlice::from(b"dummy contents" as &[u8]),
}];
let profile = &mut EncodedProfile::test_instance().unwrap().into();

let start = Timespec {
seconds: 12,
nanoseconds: 34,
};
let finish = Timespec {
seconds: 56,
nanoseconds: 78,
};
let timeout_milliseconds = 90;
unsafe {
ddog_prof_Exporter_set_timeout(Some(exporter.as_mut()), timeout_milliseconds)
Expand All @@ -748,11 +715,9 @@ mod tests {
let build_result = unsafe {
ddog_prof_Exporter_Request_build(
Some(exporter.as_mut()),
start,
finish,
Slice::from(files),
profile,
Slice::empty(),
Slice::empty(),
None,
None,
Some(&raw_internal_metadata),
None,
Expand Down Expand Up @@ -787,19 +752,7 @@ mod tests {
ExporterNewResult::Err(_) => panic!("Should not occur!"),
};

let files: &[File] = &[File {
name: CharSlice::from("foo.pprof"),
file: ByteSlice::from(b"dummy contents" as &[u8]),
}];

let start = Timespec {
seconds: 12,
nanoseconds: 34,
};
let finish = Timespec {
seconds: 56,
nanoseconds: 78,
};
let profile = &mut EncodedProfile::test_instance().unwrap().into();
let timeout_milliseconds = 90;
unsafe {
ddog_prof_Exporter_set_timeout(Some(exporter.as_mut()), timeout_milliseconds)
Expand Down Expand Up @@ -840,11 +793,9 @@ mod tests {
let build_result = unsafe {
ddog_prof_Exporter_Request_build(
Some(exporter.as_mut()),
start,
finish,
Slice::from(files),
profile,
Slice::empty(),
Slice::empty(),
None,
None,
None,
Some(&raw_info),
Expand Down Expand Up @@ -904,19 +855,7 @@ mod tests {
ExporterNewResult::Err(_) => panic!("Should not occur!"),
};

let files: &[File] = &[File {
name: CharSlice::from("foo.pprof"),
file: ByteSlice::from(b"dummy contents" as &[u8]),
}];

let start = Timespec {
seconds: 12,
nanoseconds: 34,
};
let finish = Timespec {
seconds: 56,
nanoseconds: 78,
};
let profile = &mut EncodedProfile::test_instance().unwrap().into();
let timeout_milliseconds = 90;
unsafe {
ddog_prof_Exporter_set_timeout(Some(exporter.as_mut()), timeout_milliseconds)
Expand All @@ -928,11 +867,9 @@ mod tests {
let build_result = unsafe {
ddog_prof_Exporter_Request_build(
Some(exporter.as_mut()),
start,
finish,
Slice::from(files),
profile,
Slice::empty(),
Slice::empty(),
None,
None,
None,
Some(&raw_info),
Expand All @@ -949,26 +886,17 @@ mod tests {

#[test]
fn test_build_failure() {
let start = Timespec {
seconds: 12,
nanoseconds: 34,
};
let finish = Timespec {
seconds: 56,
nanoseconds: 78,
};
let profile = &mut EncodedProfile::test_instance().unwrap().into();

let build_result = unsafe {
ddog_prof_Exporter_Request_build(
None, // No exporter, will fail
start,
finish,
profile,
Slice::empty(),
Slice::empty(),
None,
None,
None,
None,
)
};

Expand Down
Loading
Loading