-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathcmetrics.c
More file actions
233 lines (192 loc) · 6.31 KB
/
Copy pathcmetrics.c
File metadata and controls
233 lines (192 loc) · 6.31 KB
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
/* -*- 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_exp_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 <cmetrics/cmt_map.h>
#include <cmetrics/cmt_metric.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->exp_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_exp_histogram *eh;
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->exp_histograms) {
eh = cfl_list_entry(head, struct cmt_exp_histogram, _head);
cmt_exp_histogram_destroy(eh);
}
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;
}
static int cmt_expire_map(struct cmt_map *map, uint64_t min_timestamp)
{
int count = 0;
struct cfl_list *mhead;
struct cfl_list *mtmp;
struct cmt_metric *metric;
cfl_list_foreach_safe(mhead, mtmp, &map->metrics) {
metric = cfl_list_entry(mhead, struct cmt_metric, _head);
if (cmt_metric_get_timestamp(metric) < min_timestamp) {
cmt_map_metric_destroy(metric);
count++;
}
}
return count;
}
/*
* Remove from every metric map any label-set (cmt_metric) whose timestamp
* is strictly less than min_timestamp. Call this after a collection pass
* with the timestamp captured at the start of that pass so that entries for
* resources that disappeared (e.g. dead processes) are freed automatically.
*
* Only dynamic label-set entries (map->metrics list) are considered; the
* static no-label entry (map->metric) is never touched.
*
* Returns the number of purged label-sets.
*/
int cmt_expire(struct cmt *cmt, uint64_t min_timestamp)
{
int count = 0;
struct cfl_list *head;
struct cfl_list *tmp;
struct cfl_list *mhead;
struct cfl_list *mtmp;
struct cmt_counter *c;
struct cmt_gauge *g;
struct cmt_summary *s;
struct cmt_histogram *h;
struct cmt_exp_histogram *eh;
struct cmt_untyped *u;
struct cmt_metric *metric;
cfl_list_foreach_safe(head, tmp, &cmt->counters) {
c = cfl_list_entry(head, struct cmt_counter, _head);
count += cmt_expire_map(c->map, min_timestamp);
}
cfl_list_foreach_safe(head, tmp, &cmt->gauges) {
g = cfl_list_entry(head, struct cmt_gauge, _head);
count += cmt_expire_map(g->map, min_timestamp);
}
cfl_list_foreach_safe(head, tmp, &cmt->summaries) {
s = cfl_list_entry(head, struct cmt_summary, _head);
count += cmt_expire_map(s->map, min_timestamp);
}
cfl_list_foreach_safe(head, tmp, &cmt->histograms) {
h = cfl_list_entry(head, struct cmt_histogram, _head);
count += cmt_expire_map(h->map, min_timestamp);
}
cfl_list_foreach_safe(head, tmp, &cmt->exp_histograms) {
eh = cfl_list_entry(head, struct cmt_exp_histogram, _head);
count += cmt_expire_map(eh->map, min_timestamp);
}
cfl_list_foreach_safe(head, tmp, &cmt->untypeds) {
u = cfl_list_entry(head, struct cmt_untyped, _head);
count += cmt_expire_map(u->map, min_timestamp);
}
return count;
}