Skip to content

Commit 060d01a

Browse files
committed
Cleanup ffi examples and cxx api
1 parent 57c66f3 commit 060d01a

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

examples/cxx/profiling.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,34 @@ int main() {
297297
std::cout << "✅ Profile written to profile.pprof" << std::endl;
298298
}
299299
} else {
300-
// Save to file
301-
std::cout << "\n=== Saving to File ===" << std::endl;
302-
std::cout << "Serializing profile..." << std::endl;
303-
auto serialized = profile->serialize_to_vec();
304-
std::cout << "✅ Profile serialized to " << serialized.size() << " bytes" << std::endl;
300+
// Export to file using file exporter
301+
std::cout << "\n=== Exporting to File ===" << std::endl;
302+
std::cout << "Creating file exporter (profile_dump.txt)..." << std::endl;
305303

306-
std::ofstream out("profile.pprof", std::ios::binary);
307-
out.write(reinterpret_cast<const char*>(serialized.data()), serialized.size());
308-
out.close();
309-
std::cout << "✅ Profile written to profile.pprof" << std::endl;
304+
// The file exporter writes the raw HTTP request to a file for debugging/testing
305+
auto exporter = ProfileExporter::create_file_exporter(
306+
"dd-trace-cpp",
307+
"1.0.0",
308+
"native",
309+
{
310+
Tag{.key = "service", .value = "profiling-example"},
311+
Tag{.key = "env", .value = "dev"},
312+
Tag{.key = "example", .value = "cxx"}
313+
},
314+
"profile_dump.txt" // Output file path
315+
);
316+
std::cout << "✅ Exporter created" << std::endl;
317+
318+
std::cout << "Exporting profile to file..." << std::endl;
319+
exporter->send_profile(
320+
*profile,
321+
{}, // No additional files
322+
{}, // No additional tags
323+
"", // No process tags
324+
"", // No internal metadata
325+
"" // No system info
326+
);
327+
std::cout << "✅ Profile exported to profile_dump.txt (raw HTTP request)" << std::endl;
310328

311329
std::cout << "\nℹ️ To export to Datadog instead, set environment variables:" << std::endl;
312330
std::cout << " Agent mode: DD_AGENT_URL=http://localhost:8126" << std::endl;

examples/ffi/exporter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ int main(int argc, char *argv[]) {
169169
&info_example,
170170
&cancel);
171171

172-
// encoded_profile is consumed by send, but we still need to drop it
173-
ddog_prof_EncodedProfile_drop(encoded_profile);
172+
// Note: encoded_profile is consumed by send (no need to drop it)
174173

175174
if (send_result.tag == DDOG_PROF_RESULT_HTTP_STATUS_ERR_HTTP_STATUS) {
176175
print_error("Failed to send profile: ", send_result.err);

libdd-profiling/src/cxx.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ pub mod ffi {
145145
timeout_ms: u64,
146146
) -> Result<Box<ProfileExporter>>;
147147

148+
#[Self = "ProfileExporter"]
149+
fn create_file_exporter(
150+
profiling_library_name: &str,
151+
profiling_library_version: &str,
152+
family: &str,
153+
tags: Vec<Tag>,
154+
output_path: &str,
155+
) -> Result<Box<ProfileExporter>>;
156+
148157
// ProfileExporter methods
149158
/// Sends a profile to Datadog.
150159
///
@@ -507,6 +516,31 @@ impl ProfileExporter {
507516
Ok(Box::new(ProfileExporter { inner }))
508517
}
509518

519+
pub fn create_file_exporter(
520+
profiling_library_name: &str,
521+
profiling_library_version: &str,
522+
family: &str,
523+
tags: Vec<ffi::Tag>,
524+
output_path: &str,
525+
) -> anyhow::Result<Box<ProfileExporter>> {
526+
let endpoint = exporter::config::file(output_path)?;
527+
528+
let tags_vec: Vec<libdd_common::tag::Tag> = tags
529+
.iter()
530+
.map(TryInto::try_into)
531+
.collect::<Result<Vec<_>, _>>()?;
532+
533+
let inner = exporter::ProfileExporter::new(
534+
profiling_library_name,
535+
profiling_library_version,
536+
family,
537+
tags_vec,
538+
endpoint,
539+
)?;
540+
541+
Ok(Box::new(ProfileExporter { inner }))
542+
}
543+
510544
/// Sends a profile to Datadog.
511545
///
512546
/// # Arguments

0 commit comments

Comments
 (0)