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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
|
/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
#include "stats-common.h"
#include "array.h"
#include "str.h"
#include "str-sanitize.h"
#include "stats-dist.h"
#include "time-util.h"
#include "event-filter.h"
#include "event-exporter.h"
#include "stats-settings.h"
#include "stats-metrics.h"
#include "settings-parser.h"
#include <ctype.h>
#define LOG_EXPORTER_LONG_FIELD_TRUNCATE_LEN 1000
struct stats_metrics {
pool_t pool;
struct event_filter *filter; /* stats & export */
ARRAY(struct exporter *) exporters;
ARRAY(struct metric *) metrics;
};
static void
stats_metric_event(struct metric *metric, struct event *event, pool_t pool);
static struct metric *
stats_metric_sub_metric_alloc(struct metric *metric, const char *name, pool_t pool);
static void stats_metric_free(struct metric *metric);
static void stats_exporters_add_set(struct stats_metrics *metrics,
const struct stats_exporter_settings *set)
{
struct exporter *exporter;
exporter = p_new(metrics->pool, struct exporter, 1);
exporter->name = p_strdup(metrics->pool, set->name);
exporter->transport_args = p_strdup(metrics->pool, set->transport_args);
exporter->transport_timeout = set->transport_timeout;
exporter->time_format = set->parsed_time_format;
/* TODO: The following should be plugable.
*
* Note: Make sure to mirror any changes to the below code in
* stats_exporter_settings_check().
*/
if (strcmp(set->format, "none") == 0) {
exporter->format = event_export_fmt_none;
exporter->format_mime_type = "application/octet-stream";
} else if (strcmp(set->format, "json") == 0) {
exporter->format = event_export_fmt_json;
exporter->format_mime_type = "application/json";
} else if (strcmp(set->format, "tab-text") == 0) {
exporter->format = event_export_fmt_tabescaped_text;
exporter->format_mime_type = "text/plain";
} else {
i_unreached();
}
/* TODO: The following should be plugable.
*
* Note: Make sure to mirror any changes to the below code in
* stats_exporter_settings_check().
*/
if (strcmp(set->transport, "drop") == 0) {
exporter->transport = event_export_transport_drop;
} else if (strcmp(set->transport, "http-post") == 0) {
exporter->transport = event_export_transport_http_post;
} else if (strcmp(set->transport, "log") == 0) {
exporter->transport = event_export_transport_log;
exporter->format_max_field_len =
LOG_EXPORTER_LONG_FIELD_TRUNCATE_LEN;
} else {
i_unreached();
}
exporter->transport_args = set->transport_args;
array_push_back(&metrics->exporters, &exporter);
}
static struct metric *
stats_metric_alloc(pool_t pool, const char *name,
const struct stats_metric_settings *set,
const char *const *fields)
{
struct metric *metric = p_new(pool, struct metric, 1);
metric->name = p_strdup(pool, name);
metric->set = set;
metric->duration_stats = stats_dist_init();
metric->fields_count = str_array_length(fields);
if (metric->fields_count > 0) {
metric->fields = p_new(pool, struct metric_field,
metric->fields_count);
for (unsigned int i = 0; i < metric->fields_count; i++) {
metric->fields[i].field_key = p_strdup(pool, fields[i]);
metric->fields[i].stats = stats_dist_init();
}
}
return metric;
}
static void stats_metrics_add_set(struct stats_metrics *metrics,
const struct stats_metric_settings *set)
{
struct exporter *exporter;
struct metric *metric;
const char *const *fields;
const char *const *tmp;
fields = t_strsplit_spaces(set->fields, " ");
metric = stats_metric_alloc(metrics->pool, set->metric_name, set, fields);
if (array_is_created(&set->parsed_group_by))
metric->group_by = array_get(&set->parsed_group_by,
&metric->group_by_count);
array_push_back(&metrics->metrics, &metric);
event_filter_merge_with_context(metrics->filter, set->parsed_filter, metric);
/*
* Metrics may also be exported - make sure exporter info is set
*/
if (set->exporter[0] == '\0')
return; /* not exported */
array_foreach_elem(&metrics->exporters, exporter) {
if (strcmp(set->exporter, exporter->name) == 0) {
metric->export_info.exporter = exporter;
break;
}
}
if (metric->export_info.exporter == NULL)
i_panic("Could not find exporter (%s) for metric (%s)",
set->exporter, set->metric_name);
/* Defaults */
metric->export_info.include = EVENT_EXPORTER_INCL_NONE;
tmp = t_strsplit_spaces(set->exporter_include, " ");
for (; *tmp != NULL; tmp++) {
if (strcmp(*tmp, "name") == 0)
metric->export_info.include |= EVENT_EXPORTER_INCL_NAME;
else if (strcmp(*tmp, "hostname") == 0)
metric->export_info.include |= EVENT_EXPORTER_INCL_HOSTNAME;
else if (strcmp(*tmp, "timestamps") == 0)
metric->export_info.include |= EVENT_EXPORTER_INCL_TIMESTAMPS;
else if (strcmp(*tmp, "categories") == 0)
metric->export_info.include |= EVENT_EXPORTER_INCL_CATEGORIES;
else if (strcmp(*tmp, "fields") == 0)
metric->export_info.include |= EVENT_EXPORTER_INCL_FIELDS;
else
i_warning("Ignoring unknown exporter include '%s'", *tmp);
}
}
static struct stats_metric_settings *
stats_metric_settings_dup(pool_t pool, const struct stats_metric_settings *src)
{
struct stats_metric_settings *set = p_new(pool, struct stats_metric_settings, 1);
set->metric_name = p_strdup(pool, src->metric_name);
set->description = p_strdup(pool, src->description);
set->fields = p_strdup(pool, src->fields);
set->group_by = p_strdup(pool, src->group_by);
set->filter = p_strdup(pool, src->filter);
set->exporter = p_strdup(pool, src->exporter);
set->exporter_include = p_strdup(pool, src->exporter_include);
return set;
}
static struct metric *
stats_metrics_find(struct stats_metrics *metrics,
const char *name, unsigned int *idx_r)
{
struct metric *const *m;
array_foreach(&metrics->metrics, m) {
if (strcmp((*m)->name, name) == 0) {
*idx_r = array_foreach_idx(&metrics->metrics, m);
return *m;
}
}
return NULL;
}
bool stats_metrics_add_dynamic(struct stats_metrics *metrics,
struct stats_metric_settings *set,
const char **error_r)
{
unsigned int existing_idx ATTR_UNUSED;
if (stats_metrics_find(metrics, set->metric_name, &existing_idx) != NULL) {
*error_r = "Metric already exists";
return FALSE;
}
struct stats_metric_settings *_set =
stats_metric_settings_dup(metrics->pool, set);
if (!stats_metric_setting_parser_info.check_func(_set, metrics->pool, error_r))
return FALSE;
stats_metrics_add_set(metrics, _set);
return TRUE;
}
bool stats_metrics_remove_dynamic(struct stats_metrics *metrics,
const char *name)
{
unsigned int m_idx;
bool ret = FALSE;
struct metric *m = stats_metrics_find(metrics, name, &m_idx);
if (m != NULL) {
array_delete(&metrics->metrics, m_idx, 1);
ret = event_filter_remove_queries_with_context(metrics->filter, m);
stats_metric_free(m);
}
return ret;
}
static void
stats_metrics_add_from_settings(struct stats_metrics *metrics,
const struct stats_settings *set)
{
/* add all the exporters first */
if (!array_is_created(&set->exporters)) {
p_array_init(&metrics->exporters, metrics->pool, 0);
} else {
struct stats_exporter_settings *exporter_set;
p_array_init(&metrics->exporters, metrics->pool,
array_count(&set->exporters));
array_foreach_elem(&set->exporters, exporter_set)
stats_exporters_add_set(metrics, exporter_set);
}
/* then add all the metrics */
if (!array_is_created(&set->metrics)) {
p_array_init(&metrics->metrics, metrics->pool, 0);
} else {
struct stats_metric_settings *metric_set;
p_array_init(&metrics->metrics, metrics->pool,
array_count(&set->metrics));
array_foreach_elem(&set->metrics, metric_set) T_BEGIN {
stats_metrics_add_set(metrics, metric_set);
} T_END;
}
}
struct stats_metrics *stats_metrics_init(const struct stats_settings *set)
{
struct stats_metrics *metrics;
pool_t pool = pool_alloconly_create("stats metrics", 1024);
metrics = p_new(pool, struct stats_metrics, 1);
metrics->pool = pool;
metrics->filter = event_filter_create();
stats_metrics_add_from_settings(metrics, set);
return metrics;
}
static void stats_metric_free(struct metric *metric)
{
struct metric *sub_metric;
stats_dist_deinit(&metric->duration_stats);
for (unsigned int i = 0; i < metric->fields_count; i++)
stats_dist_deinit(&metric->fields[i].stats);
if (!array_is_created(&metric->sub_metrics))
return;
array_foreach_elem(&metric->sub_metrics, sub_metric)
stats_metric_free(sub_metric);
}
static void stats_export_deinit(void)
{
/* no need for event_export_transport_drop_deinit() - no-op */
event_export_transport_http_post_deinit();
/* no need for event_export_transport_log_deinit() - no-op */
}
void stats_metrics_deinit(struct stats_metrics **_metrics)
{
struct stats_metrics *metrics = *_metrics;
struct metric *metric;
*_metrics = NULL;
stats_export_deinit();
array_foreach_elem(&metrics->metrics, metric)
stats_metric_free(metric);
event_filter_unref(&metrics->filter);
pool_unref(&metrics->pool);
}
static void stats_metric_reset(struct metric *metric)
{
struct metric *sub_metric;
stats_dist_reset(metric->duration_stats);
for (unsigned int i = 0; i < metric->fields_count; i++)
stats_dist_reset(metric->fields[i].stats);
if (!array_is_created(&metric->sub_metrics))
return;
array_foreach_elem(&metric->sub_metrics, sub_metric)
stats_metric_reset(sub_metric);
}
void stats_metrics_reset(struct stats_metrics *metrics)
{
struct metric *metric;
array_foreach_elem(&metrics->metrics, metric)
stats_metric_reset(metric);
}
struct event_filter *
stats_metrics_get_event_filter(struct stats_metrics *metrics)
{
return metrics->filter;
}
static struct metric *
stats_metric_find_sub_metric(struct metric *metric,
const struct metric_value *value)
{
struct metric *sub_metrics;
/* lookup sub-metric */
array_foreach_elem(&metric->sub_metrics, sub_metrics) {
switch (sub_metrics->group_value.type) {
case METRIC_VALUE_TYPE_STR:
if (memcmp(sub_metrics->group_value.hash, value->hash,
SHA1_RESULTLEN) == 0)
return sub_metrics;
break;
case METRIC_VALUE_TYPE_INT:
if (sub_metrics->group_value.intmax == value->intmax)
return sub_metrics;
break;
case METRIC_VALUE_TYPE_BUCKET_INDEX:
if (sub_metrics->group_value.intmax == value->intmax)
return sub_metrics;
break;
}
}
return NULL;
}
static struct metric *
stats_metric_sub_metric_alloc(struct metric *metric, const char *name, pool_t pool)
{
struct metric *sub_metric;
ARRAY_TYPE(const_string) fields;
t_array_init(&fields, metric->fields_count);
for (unsigned int i = 0; i < metric->fields_count; i++)
array_append(&fields, &metric->fields[i].field_key, 1);
array_append_zero(&fields);
sub_metric = stats_metric_alloc(pool, metric->name, metric->set,
array_idx(&fields, 0));
sub_metric->sub_name = p_strdup(pool, str_sanitize_utf8(name, 32));
array_append(&metric->sub_metrics, &sub_metric, 1);
return sub_metric;
}
static bool
stats_metric_group_by_discrete(const struct event_field *field,
struct metric_value *value_r)
{
switch (field->value_type) {
case EVENT_FIELD_VALUE_TYPE_STR:
value_r->type = METRIC_VALUE_TYPE_STR;
/* use sha1 of value to avoid excessive memory usage in case the
actual value is quite long */
sha1_get_digest(field->value.str, strlen(field->value.str),
value_r->hash);
return TRUE;
case EVENT_FIELD_VALUE_TYPE_INTMAX:
value_r->type = METRIC_VALUE_TYPE_INT;
value_r->intmax = field->value.intmax;
return TRUE;
case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
return FALSE;
case EVENT_FIELD_VALUE_TYPE_STRLIST:
return FALSE;
}
i_unreached();
}
/* convert the value to a bucket index */
static bool
stats_metric_group_by_quantized(const struct event_field *field,
struct metric_value *value_r,
const struct stats_metric_settings_group_by *group_by)
{
switch (field->value_type) {
case EVENT_FIELD_VALUE_TYPE_STR:
case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
case EVENT_FIELD_VALUE_TYPE_STRLIST:
return FALSE;
case EVENT_FIELD_VALUE_TYPE_INTMAX:
break;
}
value_r->type = METRIC_VALUE_TYPE_BUCKET_INDEX;
for (unsigned int i = 0; i < group_by->num_ranges; i++) {
if ((field->value.intmax <= group_by->ranges[i].min) ||
(field->value.intmax > group_by->ranges[i].max))
continue;
value_r->intmax = i;
return TRUE;
}
i_panic("failed to find a matching bucket for '%s'=%jd",
group_by->field, field->value.intmax);
}
/* convert value to a bucket label */
static const char *
stats_metric_group_by_quantized_label(const struct event_field *field,
const struct stats_metric_settings_group_by *group_by,
const size_t bucket_index)
{
const struct stats_metric_settings_bucket_range *range = &group_by->ranges[bucket_index];
const char *name = group_by->field;
const char *label;
switch (field->value_type) {
case EVENT_FIELD_VALUE_TYPE_STR:
case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
case EVENT_FIELD_VALUE_TYPE_STRLIST:
i_unreached();
case EVENT_FIELD_VALUE_TYPE_INTMAX:
break;
}
if (range->min == INTMAX_MIN)
label = t_strdup_printf("%s_ninf_%jd", name, range->max);
else if (range->max == INTMAX_MAX)
label = t_strdup_printf("%s_%jd_inf", name, range->min + 1);
else
label = t_strdup_printf("%s_%jd_%jd", name,
range->min + 1, range->max);
return label;
}
static bool
stats_metric_group_by_get_value(const struct event_field *field,
const struct stats_metric_settings_group_by *group_by,
struct metric_value *value_r)
{
switch (group_by->func) {
case STATS_METRIC_GROUPBY_DISCRETE:
if (!stats_metric_group_by_discrete(field, value_r))
return FALSE;
return TRUE;
case STATS_METRIC_GROUPBY_QUANTIZED:
if (!stats_metric_group_by_quantized(field, value_r, group_by))
return FALSE;
return TRUE;
}
i_panic("unknown group-by function %d", group_by->func);
}
static const char *
stats_metric_group_by_get_label(const struct event_field *field,
const struct stats_metric_settings_group_by *group_by,
const struct metric_value *value)
{
switch (group_by->func) {
case STATS_METRIC_GROUPBY_DISCRETE:
i_unreached();
case STATS_METRIC_GROUPBY_QUANTIZED:
return stats_metric_group_by_quantized_label(field, group_by,
value->intmax);
}
i_panic("unknown group-by function %d", group_by->func);
}
static const char *
stats_metric_group_by_value_label(const struct event_field *field,
const struct stats_metric_settings_group_by *group_by,
const struct metric_value *value)
{
switch (value->type) {
case METRIC_VALUE_TYPE_STR:
return field->value.str;
case METRIC_VALUE_TYPE_INT:
return dec2str(field->value.intmax);
case METRIC_VALUE_TYPE_BUCKET_INDEX:
return stats_metric_group_by_get_label(field, group_by, value);
}
i_unreached();
}
static struct metric *
stats_metric_get_sub_metric(struct metric *metric,
const struct event_field *field,
const struct metric_value *value,
pool_t pool)
{
struct metric *sub_metric;
sub_metric = stats_metric_find_sub_metric(metric, value);
if (sub_metric != NULL)
return sub_metric;
T_BEGIN {
const char *value_label =
stats_metric_group_by_value_label(field,
&metric->group_by[0], value);
sub_metric = stats_metric_sub_metric_alloc(metric, value_label,
pool);
} T_END;
if (metric->group_by_count > 1) {
sub_metric->group_by_count = metric->group_by_count - 1;
sub_metric->group_by = &metric->group_by[1];
}
sub_metric->group_value.type = value->type;
sub_metric->group_value.intmax = value->intmax;
memcpy(sub_metric->group_value.hash, value->hash, SHA1_RESULTLEN);
return sub_metric;
}
static void
stats_metric_group_by_field(struct metric *metric, struct event *event,
const struct event_field *field, pool_t pool)
{
struct metric *sub_metric;
struct metric_value value;
if (!stats_metric_group_by_get_value(field, &metric->group_by[0], &value))
return;
if (!array_is_created(&metric->sub_metrics))
p_array_init(&metric->sub_metrics, pool, 8);
sub_metric = stats_metric_get_sub_metric(metric, field, &value, pool);
/* sub-metrics are recursive, so each sub-metric can have additional
sub-metrics. */
stats_metric_event(sub_metric, event, pool);
}
static void
stats_event_get_strlist(struct event *event, const char *name,
ARRAY_TYPE(const_string) *strings)
{
if (event == NULL)
return;
const struct event_field *field =
event_find_field_nonrecursive(event, name);
if (field != NULL) {
const char *str;
array_foreach_elem(&field->value.strlist, str)
array_push_back(strings, &str);
}
stats_event_get_strlist(event_get_parent(event), name, strings);
}
static void
stats_metric_group_by(struct metric *metric, struct event *event, pool_t pool)
{
const struct event_field *field =
event_find_field_recursive(event, metric->group_by[0].field);
/* ignore missing field */
if (field == NULL)
return;
if (field->value_type != EVENT_FIELD_VALUE_TYPE_STRLIST)
stats_metric_group_by_field(metric, event, field, pool);
else {
/* Handle each string in strlist separately. The strlist needs
to be combined from the event and its parents, as well as
the global event and its parents. */
ARRAY_TYPE(const_string) strings;
t_array_init(&strings, 8);
stats_event_get_strlist(event, metric->group_by[0].field,
&strings);
stats_event_get_strlist(event_get_global(),
metric->group_by[0].field, &strings);
struct event_field str_field = {
.value_type = EVENT_FIELD_VALUE_TYPE_STR,
};
const char *str;
/* sort strings so duplicates can be easily skipped */
array_sort(&strings, i_strcmp_p);
array_foreach_elem(&strings, str) {
if (str_field.value.str == NULL ||
strcmp(str_field.value.str, str) != 0) {
str_field.value.str = str;
stats_metric_group_by_field(metric, event,
&str_field, pool);
}
}
}
}
static void
stats_metric_event_field(struct event *event, const char *fieldname,
struct stats_dist *stats)
{
const struct event_field *field =
event_find_field_recursive(event, fieldname);
intmax_t num = 0;
if (field == NULL)
return;
switch (field->value_type) {
case EVENT_FIELD_VALUE_TYPE_STR:
case EVENT_FIELD_VALUE_TYPE_STRLIST:
break;
case EVENT_FIELD_VALUE_TYPE_INTMAX:
num = field->value.intmax;
break;
case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
num = field->value.timeval.tv_sec * 1000000ULL +
field->value.timeval.tv_usec;
break;
}
stats_dist_add(stats, num);
}
static void
stats_metric_event(struct metric *metric, struct event *event, pool_t pool)
{
/* duration is special - we always add it */
stats_metric_event_field(event, STATS_EVENT_FIELD_NAME_DURATION,
metric->duration_stats);
for (unsigned int i = 0; i < metric->fields_count; i++)
stats_metric_event_field(event,
metric->fields[i].field_key,
metric->fields[i].stats);
if (metric->group_by != NULL)
stats_metric_group_by(metric, event, pool);
}
static void
stats_export_event(struct metric *metric, struct event *oldevent)
{
const struct metric_export_info *info = &metric->export_info;
const struct exporter *exporter = info->exporter;
struct event *event;
i_assert(exporter != NULL);
event = event_flatten(oldevent);
T_BEGIN {
buffer_t *buf;
buf = t_buffer_create(128);
exporter->format(metric, event, buf);
exporter->transport(exporter, buf);
} T_END;
event_unref(&event);
}
void stats_metrics_event(struct stats_metrics *metrics, struct event *event,
const struct failure_context *ctx)
{
struct event_filter_match_iter *iter;
struct metric *metric;
uintmax_t duration;
/* Note: Adding the field here means that it will get exported
below. This is necessary to allow group-by functions to quantize
based on the event duration. */
event_get_last_duration(event, &duration);
event_add_int(event, STATS_EVENT_FIELD_NAME_DURATION, duration);
/* process stats & exports */
iter = event_filter_match_iter_init(metrics->filter, event, ctx);
while ((metric = event_filter_match_iter_next(iter)) != NULL) T_BEGIN {
/* every metric is fed into stats */
stats_metric_event(metric, event, metrics->pool);
/* some metrics are exported */
if (metric->export_info.exporter != NULL)
stats_export_event(metric, event);
} T_END;
event_filter_match_iter_deinit(&iter);
}
struct stats_metrics_iter {
struct stats_metrics *metrics;
unsigned int idx;
};
struct stats_metrics_iter *
stats_metrics_iterate_init(struct stats_metrics *metrics)
{
struct stats_metrics_iter *iter;
iter = i_new(struct stats_metrics_iter, 1);
iter->metrics = metrics;
return iter;
}
const struct metric *stats_metrics_iterate(struct stats_metrics_iter *iter)
{
struct metric *const *metrics;
unsigned int count;
metrics = array_get(&iter->metrics->metrics, &count);
if (iter->idx >= count)
return NULL;
return metrics[iter->idx++];
}
void stats_metrics_iterate_deinit(struct stats_metrics_iter **_iter)
{
struct stats_metrics_iter *iter = *_iter;
*_iter = NULL;
i_free(iter);
}
|