2121#include <cmetrics/cmt_counter.h>
2222#include <cmetrics/cmt_gauge.h>
2323#include <cmetrics/cmt_untyped.h>
24+ #include <cmetrics/cmt_histogram.h>
25+ #include <cmetrics/cmt_summary.h>
2426#include <cmetrics/cmt_encode_text.h>
2527#include <cmetrics/cmt_cat.h>
2628
2729#include "cmt_tests.h"
2830
31+ /* values to observe in a histogram */
32+ double hist_observe_values [10 ] = {
33+ 0.0 , 1.02 , 2.04 , 3.06 ,
34+ 4.08 , 5.10 , 6.12 , 7.14 ,
35+ 8.16 , 9.18
36+ };
37+
38+ /*
39+ * histogram bucket values: the values computed in the buckets,
40+ * all of them are uint64_t.
41+ *
42+ * Note that on all examples we use the default buckets values, created manually
43+ * and through the API:
44+ *
45+ * - 11 bucket values
46+ * - 1 +Inf bucket value
47+ */
48+ uint64_t hist_buckets_values [12 ] = {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
49+ 3 , 5 , 10 , 10 };
50+ /* histogram _count value */
51+ uint64_t hist_count = 10 ;
52+
53+ /* histogram _sum value */
54+ double hist_sum = 45.9 ;
55+
2956void test_cat ()
3057{
58+ int i ;
3159 int ret ;
60+ uint64_t val ;
3261 uint64_t ts ;
3362 cfl_sds_t text ;
63+ double sum ;
64+ uint64_t count ;
65+ double q [6 ];
66+ double r [6 ];
3467 struct cmt * cmt1 ;
3568 struct cmt * cmt2 ;
3669 struct cmt * cmt3 ;
70+ struct cmt * cmt4 ;
71+ struct cmt * cmt5 ;
3772 struct cmt_counter * c ;
3873 struct cmt_gauge * g ;
3974 struct cmt_untyped * u ;
75+ struct cmt_histogram * h ;
76+ struct cmt_histogram_buckets * buckets ;
77+ struct cmt_summary * s ;
4078
4179 /* cmetrics 1 */
4280 cmt1 = cmt_create ();
@@ -99,8 +137,89 @@ void test_cat()
99137 ret = cmt_cat (cmt3 , cmt2 );
100138 TEST_CHECK (ret == 0 );
101139
140+ /* Create buckets */
141+ buckets = cmt_histogram_buckets_create (11 ,
142+ 0.005 , 0.01 , 0.025 , 0.05 ,
143+ 0.1 , 0.25 , 0.5 , 1.0 , 2.5 ,
144+ 5.0 , 10.0 );
145+ TEST_CHECK (buckets != NULL );
146+
147+ cmt4 = cmt_create ();
148+ TEST_CHECK (cmt4 != NULL );
149+
150+ /* Create a histogram metric type */
151+ h = cmt_histogram_create (cmt4 ,
152+ "k8s" , "network" , "load" , "Network load" ,
153+ buckets ,
154+ 1 , (char * []) {"my_label" });
155+ TEST_CHECK (h != NULL );
156+
157+ ts = cfl_time_now ();
158+ for (i = 0 ; i < sizeof (hist_observe_values )/(sizeof (double )); i ++ ) {
159+ val = hist_observe_values [i ];
160+ cmt_histogram_observe (h , ts , val , 1 , (char * []) {"my_label" });
161+ }
162+
163+ ret = cmt_cat (cmt4 , cmt3 );
164+ TEST_CHECK (ret == 0 );
165+
166+ cmt5 = cmt_create ();
167+ TEST_CHECK (cmt5 != NULL );
168+
169+ ts = cfl_time_now ();
170+
171+ /* set quantiles */
172+ q [0 ] = 0.1 ;
173+ q [1 ] = 0.2 ;
174+ q [2 ] = 0.3 ;
175+ q [3 ] = 0.4 ;
176+ q [4 ] = 0.5 ;
177+ q [5 ] = 1.0 ;
178+
179+ r [0 ] = 1 ;
180+ r [1 ] = 2 ;
181+ r [2 ] = 3 ;
182+ r [3 ] = 4 ;
183+ r [4 ] = 5 ;
184+ r [5 ] = 6 ;
185+
186+ /* Create a gauge metric type */
187+ s = cmt_summary_create (cmt5 ,
188+ "spring" , "kafka_listener" , "seconds" , "Kafka Listener Timer" ,
189+ 6 , q ,
190+ 3 , (char * []) {"exception" , "name" , "result" });
191+ TEST_CHECK (s != NULL );
192+
193+ /* no quantiles, labels */
194+ sum = 0.0 ;
195+ count = 1 ;
196+
197+ cmt_summary_set_default (s , ts , NULL , sum , count ,
198+ 3 , (char * []) {"ListenerExecutionFailedException" ,
199+ "org.springframework.kafka.KafkaListenerEndpointContainer#0-0" ,
200+ "failure" });
201+
202+ /* no quantiles, labels */
203+ sum = 0.1 ;
204+ count = 2 ;
205+ cmt_summary_set_default (s , ts , NULL , sum , count ,
206+ 3 , (char * []) {"none" ,
207+ "org.springframework.kafka.KafkaListenerEndpointContainer#0-0" ,
208+ "success" });
209+
210+ /* quantiles, labels */
211+ sum = 0.2 ;
212+ count = 3 ;
213+ cmt_summary_set_default (s , ts , r , sum , count ,
214+ 3 , (char * []) {"extra test" ,
215+ "org.springframework.kafka.KafkaListenerEndpointContainer#0-0" ,
216+ "success" });
217+
218+ ret = cmt_cat (cmt5 , cmt4 );
219+ TEST_CHECK (ret == 0 );
220+
102221 /* check output */
103- text = cmt_encode_text_create (cmt3 );
222+ text = cmt_encode_text_create (cmt5 );
104223 printf ("====>\n%s\n" , text );
105224
106225 cmt_encode_text_destroy (text );
@@ -109,6 +228,8 @@ void test_cat()
109228 cmt_destroy (cmt1 );
110229 cmt_destroy (cmt2 );
111230 cmt_destroy (cmt3 );
231+ cmt_destroy (cmt4 );
232+ cmt_destroy (cmt5 );
112233}
113234
114235TEST_LIST = {
0 commit comments