8
8
#include < thread>
9
9
10
10
static ddog_CharSlice to_slice_c_char (const char *s) { return {.ptr = s, .len = strlen (s)}; }
11
-
12
- struct Deleter {
13
- void operator ()(ddog_prof_Profile *object) { ddog_prof_Profile_drop (object); }
14
- };
11
+ static ddog_CharSlice to_slice_c_char (const char *s, std::size_t size) {
12
+ return {.ptr = s, .len = size};
13
+ }
14
+ static ddog_CharSlice to_slice_string (std::string const &s) {
15
+ return {.ptr = s.data (), .len = s.length ()};
16
+ }
15
17
16
18
void print_error (const char *s, const ddog_Error &err) {
17
19
auto charslice = ddog_Error_message (&err);
18
20
printf (" %s (%.*s)\n " , s, static_cast <int >(charslice.len ), charslice.ptr );
19
21
}
20
22
23
+ #define CHECK_RESULT (typ, ok_tag ) \
24
+ void check_result (typ result, const char *msg) { \
25
+ if (result.tag != ok_tag) { \
26
+ print_error (msg, result.err ); \
27
+ ddog_Error_drop (&result.err ); \
28
+ exit (EXIT_FAILURE); \
29
+ } \
30
+ }
31
+
32
+ CHECK_RESULT (ddog_VoidResult, DDOG_VOID_RESULT_OK)
33
+
34
+ #define EXTRACT_RESULT (typ, ok_tag ) \
35
+ struct typ ##Deleter { \
36
+ void operator ()(ddog_prof_Handle_##typ *object) { \
37
+ ddog_prof_##typ##_drop (object); \
38
+ delete object; \
39
+ } \
40
+ }; \
41
+ std::unique_ptr<ddog_prof_Handle_##typ, typ##Deleter> extract_result ( \
42
+ ddog_prof_Result_Handle##typ result, const char *msg) { \
43
+ if (result.tag != ok_tag) { \
44
+ print_error (msg, result.err ); \
45
+ ddog_Error_drop (&result.err ); \
46
+ exit (EXIT_FAILURE); \
47
+ } \
48
+ std::unique_ptr<ddog_prof_Handle_##typ, typ##Deleter> rval{ \
49
+ new ddog_prof_Handle_##typ{result.ok }}; \
50
+ return rval; \
51
+ }
52
+
53
+ EXTRACT_RESULT (Profile, DDOG_PROF_RESULT_HANDLE_PROFILE_OK_HANDLE_PROFILE)
54
+
21
55
int main(int argc, char *argv[]) {
22
56
if (argc != 2 ) {
23
57
printf (" Usage: exporter SERVICE_NAME\n " );
@@ -38,14 +72,8 @@ int main(int argc, char *argv[]) {
38
72
39
73
const ddog_prof_Slice_ValueType sample_types = {&wall_time, 1 };
40
74
const ddog_prof_Period period = {wall_time, 60 };
41
- ddog_prof_Profile_NewResult profile_new_result =
42
- ddog_prof_Profile_new (sample_types, &period, nullptr );
43
- if (profile_new_result.tag != DDOG_PROF_PROFILE_NEW_RESULT_OK) {
44
- print_error (" Failed to make new profile: " , profile_new_result.err );
45
- ddog_Error_drop (&profile_new_result.err );
46
- exit (EXIT_FAILURE);
47
- }
48
- std::unique_ptr<ddog_prof_Profile, Deleter> profile{&profile_new_result.ok };
75
+ auto profile =
76
+ extract_result (ddog_prof_Profile_new (sample_types, &period, nullptr ), " Can't get profile" );
49
77
50
78
ddog_prof_Location root_location = {
51
79
// yes, a zero-initialized mapping is valid
@@ -67,30 +95,18 @@ int main(int argc, char *argv[]) {
67
95
.values = {&value, 1 },
68
96
.labels = {&label, 1 },
69
97
};
70
- auto add_result = ddog_prof_Profile_add (profile.get (), sample, 0 );
71
- if (add_result.tag != DDOG_PROF_PROFILE_RESULT_OK) {
72
- print_error (" Failed to add sample to profile: " , add_result.err );
73
- ddog_Error_drop (&add_result.err );
74
- return 1 ;
75
- }
98
+ check_result (ddog_prof_Profile_add (profile.get (), sample, 0 ), " failed to add" );
76
99
77
100
uintptr_t offset[1 ] = {0 };
78
101
ddog_prof_Slice_Usize offsets_slice = {.ptr = offset, .len = 1 };
79
102
ddog_CharSlice empty_charslice = DDOG_CHARSLICE_C_BARE (" " );
80
103
81
- auto upscaling_addresult = ddog_prof_Profile_add_upscaling_rule_proportional (
82
- profile.get (), offsets_slice, empty_charslice, empty_charslice, 1 , 1 );
104
+ check_result (ddog_prof_Profile_add_upscaling_rule_proportional (
105
+ profile.get (), offsets_slice, empty_charslice, empty_charslice, 1 , 1 ),
106
+ " failed to add upscaling rule" );
83
107
84
- if (upscaling_addresult.tag == DDOG_PROF_PROFILE_RESULT_ERR) {
85
- print_error (" Failed to add an upscaling rule: " , upscaling_addresult.err );
86
- ddog_Error_drop (&upscaling_addresult.err );
87
- // in this specific case, we want to fail the execution. But in general, we should not
88
- return 1 ;
89
- }
90
-
91
- ddog_prof_Profile_SerializeResult serialize_result =
92
- ddog_prof_Profile_serialize (profile.get (), nullptr , nullptr , nullptr );
93
- if (serialize_result.tag == DDOG_PROF_PROFILE_SERIALIZE_RESULT_ERR) {
108
+ auto serialize_result = ddog_prof_Profile_serialize (profile.get (), nullptr , nullptr , nullptr );
109
+ if (serialize_result.tag != DDOG_PROF_RESULT_ENCODED_PROFILE_OK_ENCODED_PROFILE) {
94
110
print_error (" Failed to serialize profile: " , serialize_result.err );
95
111
ddog_Error_drop (&serialize_result.err );
96
112
return 1 ;
@@ -110,9 +126,9 @@ int main(int argc, char *argv[]) {
110
126
return 1 ;
111
127
}
112
128
113
- ddog_prof_Exporter_NewResult exporter_new_result =
114
- ddog_prof_Exporter_new ( DDOG_CHARSLICE_C_BARE (" exporter-example" ), DDOG_CHARSLICE_C_BARE (" 1.2.3" ),
115
- DDOG_CHARSLICE_C_BARE (" native" ), &tags, endpoint);
129
+ ddog_prof_Exporter_NewResult exporter_new_result = ddog_prof_Exporter_new (
130
+ DDOG_CHARSLICE_C_BARE (" exporter-example" ), DDOG_CHARSLICE_C_BARE (" 1.2.3" ),
131
+ DDOG_CHARSLICE_C_BARE (" native" ), &tags, endpoint);
116
132
ddog_Vec_Tag_drop (tags);
117
133
118
134
if (exporter_new_result.tag == DDOG_PROF_EXPORTER_NEW_RESULT_ERR) {
@@ -137,14 +153,15 @@ int main(int argc, char *argv[]) {
137
153
ddog_CharSlice internal_metadata_example = DDOG_CHARSLICE_C_BARE (
138
154
" {\" no_signals_workaround_enabled\" : \" true\" , \" execution_trace_enabled\" : \" false\" }" );
139
155
140
- ddog_CharSlice info_example = DDOG_CHARSLICE_C_BARE (
141
- " {\" application\" : {\" start_time\" : \" 2024-01-24T11:17:22+0000\" }, \" platform\" : {\" kernel\" : \" Darwin Kernel 22.5.0\" }}" );
156
+ ddog_CharSlice info_example =
157
+ DDOG_CHARSLICE_C_BARE (" {\" application\" : {\" start_time\" : \" 2024-01-24T11:17:22+0000\" }, "
158
+ " \" platform\" : {\" kernel\" : \" Darwin Kernel 22.5.0\" }}" );
142
159
143
160
auto res = ddog_prof_Exporter_set_timeout (exporter, 30000 );
144
161
if (res.tag == DDOG_PROF_OPTION_ERROR_SOME_ERROR) {
145
- print_error (" Failed to set the timeout" , res.some );
146
- ddog_Error_drop (&res.some );
147
- return 1 ;
162
+ print_error (" Failed to set the timeout" , res.some );
163
+ ddog_Error_drop (&res.some );
164
+ return 1 ;
148
165
}
149
166
150
167
ddog_prof_Exporter_Request_BuildResult build_result = ddog_prof_Exporter_Request_build (
0 commit comments