1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  Fluent Bit
4  *  ==========
5  *  Copyright (C) 2019-2021 The Fluent Bit Authors
6  *  Copyright (C) 2015-2018 Treasure Data Inc.
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 #include <fluent-bit/flb_info.h>
22 #include <fluent-bit/flb_config.h>
23 #include <monkey/mk_core.h>
24 
25 #ifdef FLB_HAVE_METRICS
26 #ifndef FLB_METRICS_H
27 #define FLB_METRICS_H
28 
29 /* CMetrics */
30 #include <cmetrics/cmetrics.h>
31 #include <cmetrics/cmt_counter.h>
32 #include <cmetrics/cmt_gauge.h>
33 #include <cmetrics/cmt_untyped.h>
34 #include <cmetrics/cmt_cat.h>
35 #include <cmetrics/cmt_decode_msgpack.h>
36 #include <cmetrics/cmt_encode_influx.h>
37 #include <cmetrics/cmt_encode_text.h>
38 #include <cmetrics/cmt_encode_prometheus.h>
39 #include <cmetrics/cmt_encode_prometheus_remote_write.h>
40 #include <cmetrics/cmt_encode_msgpack.h>
41 
42 /* Metrics IDs for general purpose (used by core and Plugins */
43 #define FLB_METRIC_N_RECORDS   0
44 #define FLB_METRIC_N_BYTES     1
45 #define FLB_METRIC_N_DROPPED   2
46 #define FLB_METRIC_N_ADDED     3
47 
48 /* Genaral output plugin metrics */
49 #define FLB_METRIC_OUT_OK_RECORDS      10       /* proc_records   */
50 #define FLB_METRIC_OUT_OK_BYTES        11       /* proc_bytes     */
51 #define FLB_METRIC_OUT_ERROR           12       /* errors         */
52 #define FLB_METRIC_OUT_RETRY           13       /* retries        */
53 #define FLB_METRIC_OUT_RETRY_FAILED    14       /* retries_failed */
54 #define FLB_METRIC_OUT_DROPPED_RECORDS 15       /* dropped_records_total */
55 #define FLB_METRIC_OUT_RETRIED_RECORDS 16       /* retried_records_total */
56 
57 struct flb_metric {
58     int id;
59     int title_len;
60     char title[64];
61     size_t val;
62     struct mk_list _head;
63 };
64 
65 struct flb_metrics {
66     int title_len;         /* Title string length */
67     char title[64];        /* Title or id for this metrics context */
68     int count;             /* Total count of metrics registered */
69     struct mk_list list;   /* Head of metrics list */
70 };
71 
72 struct flb_metrics *flb_metrics_create(const char *title);
73 int flb_metrics_title(const char *title, struct flb_metrics *metrics);
74 
75 struct flb_metric *flb_metrics_get_id(int id, struct flb_metrics *metrics);
76 int flb_metrics_add(int id, const char *title, struct flb_metrics *metrics);
77 int flb_metrics_sum(int id, size_t val, struct flb_metrics *metrics);
78 int flb_metrics_print(struct flb_metrics *metrics);
79 int flb_metrics_dump_values(char **out_buf, size_t *out_size,
80                             struct flb_metrics *me);
81 int flb_metrics_destroy(struct flb_metrics *metrics);
82 int flb_metrics_fluentbit_add(struct flb_config *ctx, struct cmt *cmt);
83 
84 #endif
85 #endif /* FLB_HAVE_METRICS */
86