66#include <stdio.h>
77#include <stdlib.h>
88
9+ // Number of samples to add with each API
10+ #define NUM_SAMPLES 5000000
11+
912int main (void ) {
1013 const ddog_prof_ValueType wall_time = {
1114 .type_ = DDOG_CHARSLICE_C ("wall-time" ),
@@ -14,16 +17,27 @@ int main(void) {
1417 const ddog_prof_Slice_ValueType sample_types = {& wall_time , 1 };
1518 const ddog_prof_Period period = {wall_time , 60 };
1619
17- ddog_prof_Profile_NewResult new_result = ddog_prof_Profile_new (sample_types , & period );
18- if (new_result .tag != DDOG_PROF_PROFILE_NEW_RESULT_OK ) {
19- ddog_CharSlice message = ddog_Error_message (& new_result .err );
20- fprintf (stderr , "%.*s" , (int )message .len , message .ptr );
21- ddog_Error_drop (& new_result .err );
20+ // Create a ProfilesDictionary for the new API
21+ ddog_prof_ProfilesDictionaryHandle dict = {0 };
22+ ddog_prof_Status dict_status = ddog_prof_ProfilesDictionary_new (& dict );
23+ if (dict_status .err != NULL ) {
24+ fprintf (stderr , "Failed to create dictionary: %s\n" , dict_status .err );
25+ ddog_prof_Status_drop (& dict_status );
2226 exit (EXIT_FAILURE );
2327 }
2428
25- ddog_prof_Profile * profile = & new_result .ok ;
29+ // Create profile using the dictionary
30+ ddog_prof_Profile profile = {0 };
31+ ddog_prof_Status profile_status =
32+ ddog_prof_Profile_with_dictionary (& profile , & dict , sample_types , & period );
33+ if (profile_status .err != NULL ) {
34+ fprintf (stderr , "Failed to create profile: %s\n" , profile_status .err );
35+ ddog_prof_Status_drop (& profile_status );
36+ ddog_prof_ProfilesDictionary_drop (& dict );
37+ exit (EXIT_FAILURE );
38+ }
2639
40+ // Original API sample
2741 ddog_prof_Location root_location = {
2842 // yes, a zero-initialized mapping is valid
2943 .mapping = (ddog_prof_Mapping ){0 },
@@ -44,28 +58,107 @@ int main(void) {
4458 .labels = {& label , 1 },
4559 };
4660
47- for (int i = 0 ; i < 10000000 ; i ++ ) {
61+ for (int i = 0 ; i < NUM_SAMPLES ; i ++ ) {
4862 label .num = i ;
4963
50- ddog_prof_Profile_Result add_result = ddog_prof_Profile_add (profile , sample , 0 );
64+ ddog_prof_Profile_Result add_result = ddog_prof_Profile_add (& profile , sample , 0 );
5165 if (add_result .tag != DDOG_PROF_PROFILE_RESULT_OK ) {
5266 ddog_CharSlice message = ddog_Error_message (& add_result .err );
5367 fprintf (stderr , "%.*s" , (int )message .len , message .ptr );
5468 ddog_Error_drop (& add_result .err );
5569 }
5670 }
5771
72+ // New API sample using the dictionary
73+ // Insert strings into the dictionary
74+ ddog_prof_StringId2 function_name_id , filename_id , label_key_id ;
75+
76+ dict_status = ddog_prof_ProfilesDictionary_insert_str (
77+ & function_name_id , dict , DDOG_CHARSLICE_C ("{main}" ), DDOG_PROF_UTF8_OPTION_ASSUME );
78+ if (dict_status .err != NULL ) {
79+ fprintf (stderr , "Failed to insert function name: %s\n" , dict_status .err );
80+ ddog_prof_Status_drop (& dict_status );
81+ goto cleanup ;
82+ }
83+
84+ dict_status = ddog_prof_ProfilesDictionary_insert_str (& filename_id , dict ,
85+ DDOG_CHARSLICE_C ("/srv/example/index.php" ),
86+ DDOG_PROF_UTF8_OPTION_ASSUME );
87+ if (dict_status .err != NULL ) {
88+ fprintf (stderr , "Failed to insert filename: %s\n" , dict_status .err );
89+ ddog_prof_Status_drop (& dict_status );
90+ goto cleanup ;
91+ }
92+
93+ dict_status = ddog_prof_ProfilesDictionary_insert_str (
94+ & label_key_id , dict , DDOG_CHARSLICE_C ("unique_counter" ), DDOG_PROF_UTF8_OPTION_ASSUME );
95+ if (dict_status .err != NULL ) {
96+ fprintf (stderr , "Failed to insert label key: %s\n" , dict_status .err );
97+ ddog_prof_Status_drop (& dict_status );
98+ goto cleanup ;
99+ }
100+
101+ // Create a function using the dictionary IDs
102+ ddog_prof_FunctionId2 function_id ;
103+ ddog_prof_Function2 function2 = {
104+ .name = function_name_id ,
105+ .system_name = DDOG_PROF_STRINGID2_EMPTY ,
106+ .file_name = filename_id ,
107+ };
108+
109+ dict_status = ddog_prof_ProfilesDictionary_insert_function (& function_id , dict , & function2 );
110+ if (dict_status .err != NULL ) {
111+ fprintf (stderr , "Failed to insert function: %s\n" , dict_status .err );
112+ ddog_prof_Status_drop (& dict_status );
113+ goto cleanup ;
114+ }
115+
116+ // Create a location using the dictionary IDs
117+ ddog_prof_Location2 location2 = {
118+ .mapping = (ddog_prof_MappingId2 ){0 }, // null mapping is valid
119+ .function = function_id ,
120+ .address = 0 ,
121+ .line = 0 ,
122+ };
123+
124+ // New API sample using dictionary IDs
125+ ddog_prof_Label2 label2 = {
126+ .key = label_key_id ,
127+ .str = DDOG_CHARSLICE_C ("" ),
128+ .num = 0 ,
129+ .num_unit = DDOG_CHARSLICE_C ("" ),
130+ };
131+ const ddog_prof_Sample2 sample2 = {
132+ .locations = {& location2 , 1 },
133+ .values = {& value , 1 },
134+ .labels = {& label2 , 1 },
135+ };
136+
137+ for (int i = 0 ; i < NUM_SAMPLES ; i ++ ) {
138+ label2 .num = i ;
139+
140+ ddog_prof_Status add2_status = ddog_prof_Profile_add2 (& profile , sample2 , 0 );
141+ if (add2_status .err != NULL ) {
142+ fprintf (stderr , "add2 error: %s\n" , add2_status .err );
143+ ddog_prof_Status_drop (& add2_status );
144+ }
145+ }
146+
58147 // printf("Press any key to reset and drop...");
59148 // getchar();
60149
61- ddog_prof_Profile_Result reset_result = ddog_prof_Profile_reset (profile );
150+ cleanup :
151+ ; // Can't have a declaration after a label pre-C23, so use an empty statement.
152+ ddog_prof_Profile_Result reset_result = ddog_prof_Profile_reset (& profile );
62153 if (reset_result .tag != DDOG_PROF_PROFILE_RESULT_OK ) {
63154 ddog_CharSlice message = ddog_Error_message (& reset_result .err );
64155 fprintf (stderr , "%.*s" , (int )message .len , message .ptr );
65156 ddog_Error_drop (& reset_result .err );
66157 }
67- ddog_prof_Profile_drop (profile );
158+ ddog_prof_Profile_drop (& profile );
68159
160+ // Drop the dictionary
161+ ddog_prof_ProfilesDictionary_drop (& dict );
69162
70163 return 0 ;
71- }
164+ }
0 commit comments