You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example demonstrates how to use the CXX bindings for the libdd-crashtracker crate, providing a safer and more idiomatic C++ API compared to the traditional C FFI.
3
+
This directory contains C++ examples demonstrating the CXX bindings for libdatadog components.
4
4
5
-
## Features
5
+
CXX bindings provide a safer and more idiomatic C++ API compared to the traditional C FFI bindings, with automatic memory management and exception handling.
6
6
7
-
The CXX bindings provide access to:
7
+
## Examples
8
8
9
-
### Core Types
9
+
### Crashtracker (`crashinfo.cpp`)
10
+
11
+
Demonstrates building crash reports using the CXX bindings for `libdd-crashtracker`.
12
+
13
+
**Build and run:**
14
+
15
+
Unix (Linux/macOS):
16
+
```bash
17
+
./build-and-run-crashinfo.sh
18
+
```
19
+
20
+
Windows:
21
+
```powershell
22
+
.\build-and-run-crashinfo.ps1
23
+
```
24
+
25
+
**Key features:**
26
+
- Type-safe crash report builder API
27
+
- Support for stack traces, frames, and metadata
28
+
- Process and OS information
29
+
- Automatic memory management
30
+
- Exception-based error handling
31
+
32
+
**Core Types:**
10
33
-`CrashInfoBuilder` - Builder for constructing crash information
11
34
-`StackFrame` - Individual stack frame with debug info and addresses
12
35
-`StackTrace` - Collection of stack frames
@@ -15,115 +38,85 @@ The CXX bindings provide access to:
15
38
-`ProcInfo` - Process information
16
39
-`OsInfo` - Operating system information
17
40
18
-
### Enums
19
-
-`ErrorKind` - Type of error (Panic, UnhandledException, UnixSignal)
20
-
-`BuildIdType` - Build ID format (GNU, GO, PDB, SHA1)
21
-
-`FileType` - Binary file format (APK, ELF, PE)
41
+
**Enums:**
42
+
-`CxxErrorKind` - Type of error (Panic, UnhandledException, UnixSignal)
43
+
-`CxxBuildIdType` - Build ID format (GNU, GO, PDB, SHA1)
44
+
-`CxxFileType` - Binary file format (APK, ELF, PE)
22
45
23
-
### Key API
46
+
### Profiling (`profiling.cpp`)
24
47
25
-
**Object Creation:**
26
-
```cpp
27
-
auto builder = CrashInfoBuilder::create();
28
-
auto frame = StackFrame::create();
29
-
auto stacktrace = StackTrace::create();
30
-
```
48
+
Demonstrates building profiling data and exporting to Datadog using the CXX bindings for `libdd-profiling`.
31
49
32
-
**CrashInfoBuilder Methods:**
33
-
-`set_kind(CxxErrorKind)` - Set error type (Panic, UnhandledException, UnixSignal)
34
-
-`with_message(String)` - Set error message
35
-
-`with_counter(String, i64)` - Add a named counter
36
-
-`with_log_message(String, bool)` - Add a log message
37
-
-`with_fingerprint(String)` - Set crash fingerprint
38
-
-`with_incomplete(bool)` - Mark as incomplete
39
-
-`set_metadata(Metadata)` - Set library metadata
40
-
-`set_proc_info(ProcInfo)` - Set process information
41
-
-`set_os_info(OsInfo)` - Set OS information
42
-
-`add_stack(Box<StackTrace>)` - Add a stack trace
43
-
-`with_timestamp_now()` - Set current timestamp
44
-
-`with_file(String)` - Add a file to the report
45
-
46
-
**StackFrame Methods:**
47
-
-`with_function(String)`, `with_file(String)`, `with_line(u32)`, `with_column(u32)` - Set debug info
48
-
-`with_ip(usize)`, `with_sp(usize)` - Set instruction/stack pointers
49
-
-`with_module_base_address(usize)`, `with_symbol_address(usize)` - Set base addresses
50
-
-`with_build_id(String)` - Set build ID
51
-
-`build_id_type(CxxBuildIdType)` - Set build ID format (GNU, GO, PDB, SHA1)
52
-
-`file_type(CxxFileType)` - Set binary format (APK, ELF, PE)
53
-
-`with_path(String)` - Set module path
54
-
-`with_relative_address(usize)` - Set relative address
55
-
56
-
**StackTrace Methods:**
57
-
-`add_frame(Box<StackFrame>, bool)` - Add a frame (bool = incomplete)
58
-
-`mark_complete()` - Mark trace as complete
59
-
60
-
**Building & Output:**
61
-
```cpp
62
-
auto crash_info = crashinfo_build(std::move(builder));
63
-
auto json = crash_info->to_json();
64
-
```
65
-
66
-
## Building and Running
67
-
68
-
### Unix (Linux/macOS)
69
-
70
-
The `build-and-run-crashinfo.sh` script handles the entire build process:
50
+
**Build and run:**
71
51
52
+
Unix (Linux/macOS):
72
53
```bash
73
-
./examples/cxx/build-and-run-crashinfo.sh
54
+
./build-profiling.sh
74
55
```
75
56
76
-
### Windows
77
-
78
-
The `build-and-run-crashinfo.ps1` PowerShell script handles the build process on Windows:
79
-
57
+
Windows:
80
58
```powershell
81
-
.\examples\cxx\build-and-run-crashinfo.ps1
59
+
.\build-profiling.ps1
82
60
```
83
61
84
-
**Prerequisites for Windows:**
85
-
- Either MSVC (via Visual Studio) or MinGW/LLVM with C++ compiler
86
-
- PowerShell 5.0 or later (comes with Windows 10+)
87
-
- Rust toolchain
62
+
**Key features:**
63
+
- Type-safe API for building profiles
64
+
- Support for samples, locations, mappings, and labels
65
+
- String interning for efficient memory usage
66
+
- Upscaling rules (Poisson and Proportional)
67
+
- Endpoint tracking for web service profiling
68
+
- Pprof format serialization with zstd compression
69
+
-**Export to Datadog** via agent or agentless mode
70
+
- Support for attaching additional compressed files
71
+
- Per-profile tags and metadata
72
+
- Automatic memory management
73
+
- Exception-based error handling
74
+
- Modern C++20 syntax with designated initializers and `std::format`
88
75
89
-
The build script will:
90
-
1. Build libdd-crashtracker with the `cxx` feature enabled
91
-
2. Find the CXX bridge headers and libraries
92
-
3. Compile the C++ example (automatically detects MSVC or MinGW/Clang)
93
-
4. Run the example and display the output
76
+
**Core Types:**
77
+
-`Profile` - Profile builder for collecting samples
78
+
-`ProfileExporter` - Exporter for sending profiles to Datadog
79
+
-`Tag` - Key-value tags for profile metadata
80
+
-`AttachmentFile` - Additional file to attach to profile (name + data bytes)
94
81
95
-
## Example Output
82
+
**Export Modes:**
96
83
97
-
The example creates a crash report with:
98
-
- Error kind and message
99
-
- Library metadata with tags
100
-
- Process and OS information
101
-
- A stack trace with multiple frames (debug info + binary addresses)
102
-
- Counters and log messages
103
-
- Timestamp
84
+
By default, the example saves the profile to `profile.pprof`. To export to Datadog, set environment variables:
85
+
86
+
1.**Agent mode**: Sends profiles to the local Datadog agent
The output is a JSON object that can be sent to Datadog's crash tracking service.
98
+
See [`profiling.cpp`](profiling.cpp) for a complete example showing profile creation, sample collection, and exporting to Datadog with optional attachments and metadata.
106
99
107
-
## Notes
100
+
**Requirements:**
101
+
- C++20 compiler
102
+
- For agent mode: Datadog agent running (default: localhost:8126)
103
+
- For agentless mode: Valid Datadog API key
108
104
109
-
- The CXX bindings use `rust::String` types which need to be converted to `std::string` for use with standard C++ streams
110
-
- All functions that can fail will use exceptions (standard C++ exception handling)
111
-
- The bindings are type-safe and prevent many common C FFI errors
112
-
- Memory is managed automatically through RAII and smart pointers
105
+
## Build Scripts
113
106
114
-
## Comparison to C FFI
107
+
The examples use a consolidated build system:
115
108
116
-
The CXX bindings provide several advantages over the traditional C FFI:
0 commit comments