]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
stats: Check if metric already exists when adding dynamically.
authorsergey.kitov <sergey.kitov@open-xchange.com>
Wed, 29 Sep 2021 11:57:34 +0000 (14:57 +0300)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Wed, 29 Sep 2021 18:04:33 +0000 (18:04 +0000)
src/stats/stats-metrics.c

index 3529262bf206c1bdb39df645f5c5a7a32d1c6ca9..12bc1cae88e592b635dbb1ad51cd11fe841b8855 100644 (file)
@@ -173,10 +173,26 @@ stats_metric_settings_dup(pool_t pool, const struct stats_metric_settings *src)
        return set;
 }
 
+static struct metric *
+stats_metrics_find(struct stats_metrics *metrics, const char *name)
+{
+       struct metric *m;
+       array_foreach_elem(&metrics->metrics, m) {
+               if (strcmp(m->name, name) == 0)
+                       return m;
+       }
+       return NULL;
+}
+
 bool stats_metrics_add_dynamic(struct stats_metrics *metrics,
                               struct stats_metric_settings *set,
                               const char **error_r)
 {
+       if (stats_metrics_find(metrics, set->metric_name) != 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))
@@ -188,11 +204,9 @@ bool stats_metrics_add_dynamic(struct stats_metrics *metrics,
 bool stats_metrics_remove_dynamic(struct stats_metrics *metrics,
                                  const char *name)
 {
-       struct metric *m;
-       array_foreach_elem(&metrics->metrics, m) {
-               if (strcmp(m->name, name) == 0)
-                       return event_filter_remove_queries_with_context(metrics->filter, m);
-        }
+       struct metric *m = stats_metrics_find(metrics, name);
+       if (m != NULL)
+               return event_filter_remove_queries_with_context(metrics->filter, m);
        return FALSE;
 }