summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/cmetrics/src/cmetrics.c
blob: 9817809d12cec28d35eaa7d3dc458886d012582d (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
/* -*- 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 <stdlib.h>

#include <cmetrics/cmetrics.h>
#include <cmetrics/cmt_log.h>
#include <cmetrics/cmt_counter.h>
#include <cmetrics/cmt_gauge.h>
#include <cmetrics/cmt_summary.h>
#include <cmetrics/cmt_histogram.h>
#include <cmetrics/cmt_untyped.h>
#include <cmetrics/cmt_atomic.h>
#include <cmetrics/cmt_compat.h>
#include <cmetrics/cmt_label.h>
#include <cmetrics/cmt_version.h>

#include <cfl/cfl_kvlist.h>

void cmt_initialize()
{
    cmt_atomic_initialize();
}

struct cmt *cmt_create()
{
    struct cmt *cmt;

    cmt = calloc(1, sizeof(struct cmt));
    if (!cmt) {
        cmt_errno();
        return NULL;
    }

    cmt->static_labels = cmt_labels_create();
    if (!cmt->static_labels) {
        free(cmt);
        return NULL;
    }

    cmt->internal_metadata = cfl_kvlist_create();

    if (cmt->internal_metadata == NULL) {
        cmt_labels_destroy(cmt->static_labels);

        free(cmt);
        return NULL;
    }

    cmt->external_metadata = cfl_kvlist_create();

    if (cmt->external_metadata == NULL) {
        cfl_kvlist_destroy(cmt->internal_metadata);
        cmt_labels_destroy(cmt->static_labels);

        free(cmt);
        return NULL;
    }

    cfl_list_init(&cmt->counters);
    cfl_list_init(&cmt->gauges);
    cfl_list_init(&cmt->histograms);
    cfl_list_init(&cmt->summaries);
    cfl_list_init(&cmt->untypeds);

    cmt->log_level = CMT_LOG_ERROR;

    cfl_list_entry_init(&cmt->_head);
    
    return cmt;
}

void cmt_destroy(struct cmt *cmt)
{
    struct cfl_list *tmp;
    struct cfl_list *head;
    struct cmt_counter *c;
    struct cmt_gauge *g;
    struct cmt_summary *s;
    struct cmt_histogram *h;
    struct cmt_untyped *u;

    cfl_list_foreach_safe(head, tmp, &cmt->counters) {
        c = cfl_list_entry(head, struct cmt_counter, _head);
        cmt_counter_destroy(c);
    }

    cfl_list_foreach_safe(head, tmp, &cmt->gauges) {
        g = cfl_list_entry(head, struct cmt_gauge, _head);
        cmt_gauge_destroy(g);
    }

    cfl_list_foreach_safe(head, tmp, &cmt->summaries) {
        s = cfl_list_entry(head, struct cmt_summary, _head);
        cmt_summary_destroy(s);
    }

    cfl_list_foreach_safe(head, tmp, &cmt->histograms) {
        h = cfl_list_entry(head, struct cmt_histogram, _head);
        cmt_histogram_destroy(h);
    }

    cfl_list_foreach_safe(head, tmp, &cmt->untypeds) {
        u = cfl_list_entry(head, struct cmt_untyped, _head);
        cmt_untyped_destroy(u);
    }

    if (cmt->static_labels) {
        cmt_labels_destroy(cmt->static_labels);
    }

    if (cmt->internal_metadata != NULL) {
        cfl_kvlist_destroy(cmt->internal_metadata);
    }

    if (cmt->external_metadata != NULL) {
        cfl_kvlist_destroy(cmt->external_metadata);
    }

    free(cmt);
}

int cmt_label_add(struct cmt *cmt, char *key, char *val)
{
    return cmt_labels_add_kv(cmt->static_labels, key, val);
}

char *cmt_version()
{
    return CMT_VERSION_STR;
}