summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/cmetrics/tests/histogram.c
blob: d5fc80bbbad76f0df819c6c48958cd487945ca25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  CMetrics
 *  ========
 *  Copyright 2021-2022 The CMetrics Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <cmetrics/cmetrics.h>
#include <cmetrics/cmt_histogram.h>
#include <cmetrics/cmt_encode_prometheus.h>
#include <cmetrics/cmt_map.h>
#include "cmt_tests.h"

#include <math.h>
#include <float.h>
#include <stdbool.h>

/* values to observe in a histogram */
double hist_observe_values[10] = {
                                  0.0 , 1.02, 2.04, 3.06,
                                  4.08, 5.10, 6.12, 7.14,
                                  8.16, 9.18
                                 };

/*
 * histogram bucket values: the values computed in the buckets,
 * all of them are uint64_t.
 *
 * Note that on all examples we use the default buckets values, created manually
 * and through the API:
 *
 * - 11 bucket values
 * -  1 +Inf bucket value
 */
uint64_t hist_buckets_values[12] = {1, 1, 1, 1, 1, 1, 1, 1,
                                    3, 5, 10, 10};

/* histogram _count value */
uint64_t hist_count = 10;

/* histogram _sum value */
double hist_sum = 45.9;

bool fequal(double a, double b)
{
    return (fabs(a - b) < (DBL_EPSILON * fabs(a + b)));
}

static void histogram_check(struct cmt_histogram *h,
                            int labels_count, char **labels_vals)
{
    int i;
    int ret;
    uint64_t val;
    struct cmt_metric *metric;

    /* retrieve the metric context */
    metric = cmt_map_metric_get(&h->opts, h->map,
                                labels_count, labels_vals, CMT_TRUE);
    TEST_CHECK(metric != NULL);

    /* check bucket values */
    for (i = 0; i < (sizeof(hist_buckets_values)/sizeof(uint64_t)); i++) {
        val = cmt_metric_hist_get_value(metric, i);
        TEST_CHECK(val == hist_buckets_values[i]);
    }

    /* check _count */
    TEST_CHECK(hist_count == cmt_metric_hist_get_count_value(metric));

    /* check _sum */
    ret = fequal(hist_sum, cmt_metric_hist_get_sum_value(metric));
    TEST_CHECK(ret != 0);
}

static int histogram_observe_all(struct cmt_histogram *h,
                                 uint64_t timestamp,
                                 int labels_count, char **labels_vals)
{
    int i;
    double val;

    for (i = 0; i < sizeof(hist_observe_values)/(sizeof(double)); i++) {
        val = hist_observe_values[i];
        cmt_histogram_observe(h, timestamp, val, labels_count, labels_vals);
    }

    return i;
}

static void prometheus_encode_test(struct cmt *cmt)
{
    cfl_sds_t buf;

    buf = cmt_encode_prometheus_create(cmt, CMT_FALSE);
    printf("\n%s\n", buf);
    cmt_encode_prometheus_destroy(buf);
}


void test_histogram()
{
    uint64_t ts;
    struct cmt *cmt;
    struct cmt_histogram *h;
    struct cmt_histogram_buckets *buckets;

    cmt_initialize();

    /* Timestamp */
    ts = cfl_time_now();

    /* CMetrics context */
    cmt = cmt_create();
    TEST_CHECK(cmt != NULL);

    /* Create buckets */
    buckets = cmt_histogram_buckets_create(11,
                                           0.005, 0.01, 0.025, 0.05,
                                           0.1, 0.25, 0.5, 1.0, 2.5,
                                           5.0, 10.0);
    TEST_CHECK(buckets != NULL);

    /* Create a gauge metric type */
    h = cmt_histogram_create(cmt,
                             "k8s", "network", "load", "Network load",
                             buckets,
                             1, (char *[]) {"my_label"});
    TEST_CHECK(h != NULL);

    /* no labels */
    histogram_observe_all(h, ts, 0, NULL);
    histogram_check(h, 0, NULL);
    prometheus_encode_test(cmt);

    /* static label: register static label for the context */
    cmt_label_add(cmt, "static", "test");
    histogram_check(h, 0, NULL);
    prometheus_encode_test(cmt);

    /* defined labels: add a custom label value */
    histogram_observe_all(h, ts, 1, (char *[]) {"val"});
    histogram_check(h, 1, (char *[]) {"val"});
    prometheus_encode_test(cmt);

    cmt_destroy(cmt);
}

void test_set_defaults()
{
    uint64_t ts;
    struct cmt *cmt;
    struct cmt_histogram *h;
    struct cmt_histogram_buckets *buckets;

    cmt_initialize();

    /* Timestamp */
    ts = cfl_time_now();

    /* CMetrics context */
    cmt = cmt_create();
    TEST_CHECK(cmt != NULL);

    /* Create buckets */
    buckets = cmt_histogram_buckets_default_create();
    TEST_CHECK(buckets != NULL);

    /* Create a gauge metric type */
    h = cmt_histogram_create(cmt,
                             "k8s", "network", "load", "Network load",
                             buckets,
                             1, (char *[]) {"my_label"});
    TEST_CHECK(h != NULL);

    /* set default buckets values / no labels */
    cmt_histogram_set_default(h, ts,
                              hist_buckets_values,
                              hist_sum, hist_count, 0, NULL);
    histogram_check(h, 0, NULL);
    prometheus_encode_test(cmt);

    /* static label: register static label for the context */
    cmt_label_add(cmt, "static", "test");
    histogram_check(h, 0, NULL);
    prometheus_encode_test(cmt);

    /* perform observation with labels */
    histogram_observe_all(h, ts, 1, (char *[]) {"val"});
    histogram_check(h, 1, (char *[]) {"val"});
    prometheus_encode_test(cmt);

    cmt_destroy(cmt);
}

TEST_LIST = {
    {"histogram"   , test_histogram},
    {"set_defaults", test_set_defaults},
    { 0 }
};