Skip to content

Commit 0d4ebbe

Browse files
authored
feat(profiling): Simpler API for profile exporter (#1423)
feat(profiling): Simpler API for profile exporter Co-authored-by: daniel.schwartznarbonne <[email protected]>
1 parent e3314e9 commit 0d4ebbe

File tree

6 files changed

+142
-539
lines changed

6 files changed

+142
-539
lines changed

examples/ffi/exporter.cpp

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ int main(int argc, char *argv[]) {
9797

9898
auto *encoded_profile = &serialize_result.ok;
9999

100+
// Set a custom timeout of 10000ms (10 seconds) instead of the default 3000ms
100101
auto endpoint =
101-
ddog_prof_Endpoint_agentless(DDOG_CHARSLICE_C_BARE("datad0g.com"), to_slice_c_char(api_key));
102+
ddog_prof_Endpoint_agentless(DDOG_CHARSLICE_C_BARE("datad0g.com"), to_slice_c_char(api_key), 10000);
102103

103104
ddog_Vec_Tag tags = ddog_Vec_Tag_new();
104105
ddog_Vec_Tag_PushResult tag_result =
@@ -123,7 +124,6 @@ int main(int argc, char *argv[]) {
123124
auto exporter = &exporter_new_result.ok;
124125

125126
auto files_to_compress_and_export = ddog_prof_Exporter_Slice_File_empty();
126-
auto files_to_export_unmodified = ddog_prof_Exporter_Slice_File_empty();
127127

128128
ddog_CharSlice internal_metadata_example = DDOG_CHARSLICE_C_BARE(
129129
"{\"no_signals_workaround_enabled\": \"true\", \"execution_trace_enabled\": \"false\"}");
@@ -132,32 +132,12 @@ int main(int argc, char *argv[]) {
132132
DDOG_CHARSLICE_C_BARE("{\"application\": {\"start_time\": \"2024-01-24T11:17:22+0000\"}, "
133133
"\"platform\": {\"kernel\": \"Darwin Kernel 22.5.0\"}}");
134134

135-
auto res = ddog_prof_Exporter_set_timeout(exporter, 30000);
136-
if (res.tag == DDOG_VOID_RESULT_ERR) {
137-
print_error("Failed to set the timeout", res.err);
138-
ddog_Error_drop(&res.err);
139-
return 1;
140-
}
141-
142-
auto build_result = ddog_prof_Exporter_Request_build(
143-
exporter, encoded_profile, files_to_compress_and_export, files_to_export_unmodified, nullptr, nullptr,
144-
&internal_metadata_example, &info_example);
145-
ddog_prof_EncodedProfile_drop(encoded_profile);
146-
147-
if (build_result.tag == DDOG_PROF_REQUEST_RESULT_ERR_HANDLE_REQUEST) {
148-
print_error("Failed to build request: ", build_result.err);
149-
ddog_Error_drop(&build_result.err);
150-
return 1;
151-
}
152-
153-
auto request = &build_result.ok;
154-
155135
auto cancel = ddog_CancellationToken_new();
156136
auto cancel_for_background_thread = ddog_CancellationToken_clone(&cancel);
157137

158138
// As an example of CancellationToken usage, here we create a background
159139
// thread that sleeps for some time and then cancels a request early (e.g.
160-
// before the timeout in ddog_ProfileExporter_send is hit).
140+
// before the timeout in ddog_prof_Exporter_send_blocking is hit).
161141
//
162142
// If the request is faster than the sleep time, no cancellation takes place.
163143
std::thread trigger_cancel_if_request_takes_too_long_thread(
@@ -174,7 +154,9 @@ int main(int argc, char *argv[]) {
174154
trigger_cancel_if_request_takes_too_long_thread.detach();
175155

176156
int exit_code = 0;
177-
auto send_result = ddog_prof_Exporter_send(exporter, request, &cancel);
157+
auto send_result = ddog_prof_Exporter_send_blocking(
158+
exporter, encoded_profile, files_to_compress_and_export,
159+
nullptr, nullptr, &internal_metadata_example, &info_example, &cancel);
178160
if (send_result.tag == DDOG_PROF_RESULT_HTTP_STATUS_ERR_HTTP_STATUS) {
179161
print_error("Failed to send profile: ", send_result.err);
180162
exit_code = 1;
@@ -183,7 +165,6 @@ int main(int argc, char *argv[]) {
183165
printf("Response code: %d\n", send_result.ok.code);
184166
}
185167

186-
ddog_prof_Exporter_Request_drop(request);
187168
ddog_prof_Exporter_drop(exporter);
188169
ddog_CancellationToken_drop(&cancel);
189170
return exit_code;

libdd-common/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,21 @@ impl Endpoint {
303303
pub fn is_file_endpoint(&self) -> bool {
304304
self.url.scheme_str() == Some("file")
305305
}
306+
307+
/// Set a custom timeout for this endpoint.
308+
/// If not called, uses the default timeout of 3000ms.
309+
///
310+
/// # Arguments
311+
/// * `timeout_ms` - Timeout in milliseconds. Pass 0 to use the default timeout (3000ms).
312+
///
313+
/// # Returns
314+
/// Self with the timeout set, allowing for method chaining
315+
pub fn with_timeout(mut self, timeout_ms: u64) -> Self {
316+
self.timeout_ms = if timeout_ms == 0 {
317+
Self::DEFAULT_TIMEOUT
318+
} else {
319+
timeout_ms
320+
};
321+
self
322+
}
306323
}

0 commit comments

Comments
 (0)