stats-metrics.c revision 9c675f5e259c19f9fdc40808439479131b32421e
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainenstats_metric_settings_to_query(const struct stats_metric_settings *set,
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen /* generate fields for event filter */
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen const char *const *filters;
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen unsigned int i, count;
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen filter_fields = t_new(struct event_filter_field, count + 1);
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen for (i = 0; i < count; i++) {
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen /* add query to the event filter */
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen query_r->categories = t_strsplit_spaces(set->categories, " ");
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen query_r->source_filename = t_strcut(set->source_location, ':');
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen query_r->source_linenum = set->parsed_source_linenum;
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainenstatic void stats_metrics_add_set(struct stats_metrics *metrics,
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen const char *const *fields;
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen metric = p_new(metrics->pool, struct metric, 1);
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen metric->name = p_strdup(metrics->pool, set->name);
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen metric->fields_count = str_array_length(fields);
a0d34d3982507f513a9d800082481e9faeb9a943Timo Sirainen metric->fields = p_new(metrics->pool, struct metric_field,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen for (unsigned int i = 0; i < metric->fields_count; i++) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstats_metrics_add_from_settings(struct stats_metrics *metrics,
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen struct stats_metric_settings *const *metric_setp;
9315dd69233d554452df0c12bc57002d2042a8f4Timo Sirainen p_array_init(&metrics->metrics, metrics->pool, 0);
9315dd69233d554452df0c12bc57002d2042a8f4Timo Sirainen p_array_init(&metrics->metrics, metrics->pool,
9315dd69233d554452df0c12bc57002d2042a8f4Timo Sirainen array_foreach(&set->metrics, metric_setp) T_BEGIN {
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainenstruct stats_metrics *stats_metrics_init(const struct stats_settings *set)
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen pool_t pool = pool_alloconly_create("stats metrics", 1024);
945631faab2bf1aed8d95a1fd0c317a9ce153725Timo Sirainen metrics = p_new(pool, struct stats_metrics, 1);
538c58fc95200fcc5e91abdda8b912b574a2f968Timo Sirainen stats_metrics_add_from_settings(metrics, set);
1cad0dd34667548ba39f794ddeb9fc486cf4c666Timo Sirainenstatic void stats_metric_free(struct metric *metric)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (unsigned int i = 0; i < metric->fields_count; i++)
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainenvoid stats_metrics_deinit(struct stats_metrics **_metrics)
1cad0dd34667548ba39f794ddeb9fc486cf4c666Timo Sirainenvoid stats_metrics_reset(struct stats_metrics *metrics)
1cad0dd34667548ba39f794ddeb9fc486cf4c666Timo Sirainen for (unsigned int i = 0; i < (*metricp)->fields_count; i++)
1cad0dd34667548ba39f794ddeb9fc486cf4c666Timo Sirainenstats_metrics_get_event_filter(struct stats_metrics *metrics)
1cad0dd34667548ba39f794ddeb9fc486cf4c666Timo Sirainenstats_metric_event(struct metric *metric, struct event *event)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (event_get_last_send_time(event, &tv_end)) {
1cad0dd34667548ba39f794ddeb9fc486cf4c666Timo Sirainen duration = timeval_diff_usecs(&tv_end, &tv_start);
1cad0dd34667548ba39f794ddeb9fc486cf4c666Timo Sirainen timing_add_usecs(metric->duration_timing, duration);
1cad0dd34667548ba39f794ddeb9fc486cf4c666Timo Sirainen for (unsigned int i = 0; i < metric->fields_count; i++) {
1cad0dd34667548ba39f794ddeb9fc486cf4c666Timo Sirainen event_find_field(event, metric->fields[i].field_key);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen num = field->value.timeval.tv_sec * 1000000ULL +
fa5957ffc9b676bfd649fa9953e63e72ee4ebeb4Timo Sirainen timing_add_usecs(metric->fields[i].timing, num);
fa5957ffc9b676bfd649fa9953e63e72ee4ebeb4Timo Sirainenvoid stats_metrics_event(struct stats_metrics *metrics, struct event *event)
fa5957ffc9b676bfd649fa9953e63e72ee4ebeb4Timo Sirainen iter = event_filter_match_iter_init(metrics->filter, event);
fa5957ffc9b676bfd649fa9953e63e72ee4ebeb4Timo Sirainen while ((metric = event_filter_match_iter_next(iter)) != NULL)
fa5957ffc9b676bfd649fa9953e63e72ee4ebeb4Timo Sirainen unsigned int idx;
fa5957ffc9b676bfd649fa9953e63e72ee4ebeb4Timo Sirainenstats_metrics_iterate_init(struct stats_metrics *metrics)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst struct metric *stats_metrics_iterate(struct stats_metrics_iter *iter)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int count;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen metrics = array_get(&iter->metrics->metrics, &count);