summaryrefslogtreecommitdiffstats
path: root/fluent-bit/src/flb_metrics.c
blob: 9a9e9c7e7d8543f07cf2fca54bd0e25633439857 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit 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.
 */

/*
 * Metrics interface is a helper to gather general metrics from the core or
 * plugins at runtime.
 */

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_version.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_metrics.h>
#include <msgpack.h>

static int id_exists(int id, struct flb_metrics *metrics)
{
    struct mk_list *head;
    struct flb_metric *metric;

    mk_list_foreach(head, &metrics->list) {
        metric = mk_list_entry(head, struct flb_metric, _head);
        if (metric->id == id) {
            return FLB_TRUE;
        }
    }

    return FLB_FALSE;
}

static int id_get(struct flb_metrics *metrics)
{
    int id;
    int ret = FLB_FALSE;

    /* Try to use 'count' as an id */
    id = metrics->count;

    while ((ret = id_exists(id, metrics)) == FLB_TRUE) {
        id++;
    }

    return id;
}

struct flb_metric *flb_metrics_get_id(int id, struct flb_metrics *metrics)
{
    struct mk_list *head;
    struct flb_metric *m;

    mk_list_foreach(head, &metrics->list) {
        m = mk_list_entry(head, struct flb_metric, _head);
        if (m->id == id) {
            return m;
        }
    }

    return NULL;
}

struct flb_metrics *flb_metrics_create(const char *title)
{
    int ret;
    struct flb_metrics *metrics;

    /* Create a metrics parent context */
    metrics = flb_malloc(sizeof(struct flb_metrics));
    if (!metrics) {
        flb_errno();
        return NULL;
    }
    metrics->count = 0;

    /* Set metrics title */
    ret = flb_metrics_title(title, metrics);
    if (ret == -1) {
        flb_free(metrics);
        return NULL;
    }

    /* List head for specific metrics under the context */
    mk_list_init(&metrics->list);
    return metrics;
}

int flb_metrics_title(const char *title, struct flb_metrics *metrics)
{
    int ret;
    size_t size = sizeof(metrics->title) - 1;

    ret = snprintf(metrics->title, size, "%s", title);
    if (ret == -1) {
        flb_errno();
        return -1;
    }
    else if (ret >= size){
        flb_warn("[%s] title '%s' was truncated", __FUNCTION__, title);
    }
    metrics->title_len = strlen(metrics->title);
    return 0;
}

int flb_metrics_add(int id, const char *title, struct flb_metrics *metrics)
{
    int ret;
    struct flb_metric *m;
    size_t size;

    /* Create context */
    m = flb_malloc(sizeof(struct flb_metric));
    if (!m) {
        flb_errno();
        return -1;
    }
    m->val = 0;
    size = sizeof(m->title) - 1;

    /* Write title */
    ret = snprintf(m->title, size, "%s", title);
    if (ret == -1) {
        flb_errno();
        flb_free(m);
        return -1;
    }
    else if (ret >= size) {
        flb_warn("[%s] title '%s' was truncated", __FUNCTION__, title);
    }

    m->title_len = strlen(m->title);

    /* Assign an ID */
    if (id >= 0) {
        /* Check this new ID is available */
        if (id_exists(id, metrics) == FLB_TRUE) {
            flb_error("[metrics] id=%i already exists for metric '%s'",
                      id, metrics->title);
            flb_free(m);
            return -1;
        }
    }
    else {
        id = id_get(metrics);
    }

    /* Link to parent list */
    mk_list_add(&m->_head, &metrics->list);
    m->id = id;
    metrics->count++;

    return id;
}

int flb_metrics_sum(int id, size_t val, struct flb_metrics *metrics)
{
    struct flb_metric *m;

    m = flb_metrics_get_id(id, metrics);
    if (!m) {
        return -1;
    }

    m->val += val;
    return 0;
}

int flb_metrics_destroy(struct flb_metrics *metrics)
{
    int count = 0;
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_metric *m;

    mk_list_foreach_safe(head, tmp, &metrics->list) {
        m = mk_list_entry(head, struct flb_metric, _head);
        mk_list_del(&m->_head);
        flb_free(m);
        count++;
    }

    flb_free(metrics);
    return count;
}

int flb_metrics_print(struct flb_metrics *metrics)
{
    struct mk_list *head;
    struct flb_metric *m;

    printf("[metric dump] title => '%s'", metrics->title);

    mk_list_foreach(head, &metrics->list) {
        m = mk_list_entry(head, struct flb_metric, _head);
        printf(", '%s' => %lu", m->title, m->val);
    }
    printf("\n");

    return 0;
}

/* Write metrics in messagepack format */
int flb_metrics_dump_values(char **out_buf, size_t *out_size,
                            struct flb_metrics *me)
{
    struct mk_list *head;
    struct flb_metric *m;
    msgpack_sbuffer mp_sbuf;
    msgpack_packer mp_pck;

    /* Prepare new outgoing buffer */
    msgpack_sbuffer_init(&mp_sbuf);
    msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);

    msgpack_pack_map(&mp_pck, me->count);

    mk_list_foreach(head, &me->list) {
        m = mk_list_entry(head, struct flb_metric, _head);
        msgpack_pack_str(&mp_pck, m->title_len);
        msgpack_pack_str_body(&mp_pck, m->title, m->title_len);
        msgpack_pack_uint64(&mp_pck, m->val);
    }

    *out_buf  = mp_sbuf.data;
    *out_size = mp_sbuf.size;

    return 0;
}

static int attach_uptime(struct flb_config *ctx, struct cmt *cmt,
                         uint64_t ts, char *hostname)
{
    double uptime;
    struct cmt_counter *c;

    /* uptime */
    c = cmt_counter_create(cmt, "fluentbit", "", "uptime",
                           "Number of seconds that Fluent Bit has been running.",
                           1, (char *[]) {"hostname"});
    if (!c) {
        return -1;
    }

    uptime = time(NULL) - ctx->init_time;

    cmt_counter_set(c, ts, uptime, 1, (char *[]) {hostname});
    return 0;
}

static int attach_process_start_time_seconds(struct flb_config *ctx,
                                             struct cmt *cmt,
                                             uint64_t ts, char *hostname)
{
    double val;
    struct cmt_gauge *g;

    g = cmt_gauge_create(cmt, "fluentbit", "", "process_start_time_seconds",
                         "Start time of the process since unix epoch in seconds.",
                         1, (char *[]) {"hostname"});
    if (!g) {
        return -1;
    }

    val = (double) ctx->init_time;
    cmt_gauge_set(g, ts, val, 1, (char *[]) {hostname});
    return 0;
}

static char *get_os_name()
{
#ifdef _WIN64
    return "win64";
#elif _WIN32
    return "win32";
#elif __APPLE__ || __MACH__
    return "macos";
#elif __linux__
    return "linux";
#elif __FreeBSD__
    return "freebsd";
#elif __unix || __unix__
    return "unix";
#else
    return "other";
#endif
}

static int attach_build_info(struct flb_config *ctx, struct cmt *cmt, uint64_t ts,
                             char *hostname)
{
    double val;
    char *os;
    struct cmt_gauge *g;

    g = cmt_gauge_create(cmt, "fluentbit", "build", "info",
                         "Build version information.",
                         3, (char *[]) {"hostname", "version", "os"});
    if (!g) {
        return -1;
    }

    val = (double) ctx->init_time;
    os = get_os_name();

    cmt_gauge_set(g, ts, val, 3, (char *[]) {hostname, FLB_VERSION_STR, os});
    return 0;
}

static int attach_hot_reload_info(struct flb_config *ctx, struct cmt *cmt, uint64_t ts,
                                  char *hostname)
{
    double val;
    struct cmt_gauge *g;

    g = cmt_gauge_create(cmt, "fluentbit", "", "hot_reloaded_times",
                         "Collect the count of hot reloaded times.",
                         1, (char *[]) {"hostname"});
    if (!g) {
        return -1;
    }

    val = (double) ctx->hot_reloaded_count;

    cmt_gauge_set(g, ts, val, 1, (char *[]) {hostname});
    return 0;
}

/* Append internal Fluent Bit metrics to context */
int flb_metrics_fluentbit_add(struct flb_config *ctx, struct cmt *cmt)
{
    int ret;
    size_t ts;
    char hostname[128];

    /* current timestamp */
    ts = cfl_time_now();

    /* get hostname */
    ret = gethostname(hostname, sizeof(hostname) - 1);
    if (ret == -1) {
        strcpy(hostname, "unknown");
    }

    /* Attach metrics to cmetrics context */
    attach_uptime(ctx, cmt, ts, hostname);
    attach_process_start_time_seconds(ctx, cmt, ts, hostname);
    attach_build_info(ctx, cmt, ts, hostname);
    attach_hot_reload_info(ctx, cmt, ts, hostname);

    return 0;
}