diff --git a/include/neuron/metrics.h b/include/neuron/metrics.h index 0c9d1b45..cc443b35 100644 --- a/include/neuron/metrics.h +++ b/include/neuron/metrics.h @@ -28,12 +28,14 @@ extern "C" { #include "define.h" #include "type.h" +#include "utils/rolling_counter.h" #include "utils/uthash.h" typedef enum { NEU_METRIC_TYPE_COUNTER, NEU_METRIC_TYPE_GAUAGE, NEU_METRIC_TYPE_COUNTER_SET, + NEU_METRIC_TYPE_ROLLING_COUNTER, } neu_metric_type_e; // node running state @@ -140,11 +142,12 @@ typedef enum { // metric entry typedef struct { - const char * name; // NOTE: should points to string literal - const char * help; // NOTE: should points to string literal - neu_metric_type_e type; // - uint64_t value; // - UT_hash_handle hh; // ordered by name + const char * name; // NOTE: should points to string literal + const char * help; // NOTE: should points to string literal + neu_metric_type_e type; // + uint64_t value; // + neu_rolling_counter_t *rcnt; // + UT_hash_handle hh; // ordered by name } neu_metric_entry_t; // group metrics @@ -202,6 +205,9 @@ int neu_metric_entries_add(neu_metric_entry_t **entries, const char *name, static inline void neu_metric_entry_free(neu_metric_entry_t *entry) { + if (NEU_METRIC_TYPE_ROLLING_COUNTER == entry->type) { + neu_rolling_counter_free(entry->rcnt); + } free(entry); } diff --git a/include/neuron/plugin.h b/include/neuron/plugin.h index 4a706e84..b721604b 100644 --- a/include/neuron/plugin.h +++ b/include/neuron/plugin.h @@ -36,6 +36,14 @@ extern "C" { #define NEURON_PLUGIN_VER_1_0 100 #define NEURON_PLUGIN_VER_2_0 200 +#define NEU_PLUGIN_REGISTER_METRIC(plugin, name, init) \ + plugin->common.adapter_callbacks->register_metric( \ + plugin->common.adapter, name, name##_HELP, name##_TYPE, init) + +#define NEU_PLUGIN_UPDATE_METRIC(plugin, name, val, grp) \ + plugin->common.adapter_callbacks->update_metric(plugin->common.adapter, \ + name, val, grp) + extern int64_t global_timestamp; typedef struct neu_plugin_common { diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index 61fcc12b..8ea17e12 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -417,6 +417,9 @@ static int adapter_update_metric(neu_adapter_t *adapter, if (NEU_METRIC_TYPE_COUNTER == entry->type) { entry->value += n; + } else if (NEU_METRIC_TYPE_ROLLING_COUNTER == entry->type) { + entry->value = + neu_rolling_counter_inc(entry->rcnt, global_timestamp, n); } else { entry->value = n; } @@ -1494,6 +1497,9 @@ int neu_adapter_update_group_metric(neu_adapter_t *adapter, if (NEU_METRIC_TYPE_COUNTER == entry->type) { entry->value += n; + } else if (NEU_METRIC_TYPE_ROLLING_COUNTER == entry->type) { + entry->value = + neu_rolling_counter_inc(entry->rcnt, global_timestamp, n); } else { entry->value = n; } diff --git a/src/base/metrics.c b/src/base/metrics.c index 5a8d198a..63331563 100644 --- a/src/base/metrics.c +++ b/src/base/metrics.c @@ -280,10 +280,19 @@ int neu_metric_entries_add(neu_metric_entry_t **entries, const char *name, return -1; } - entry->name = name; - entry->type = type; - entry->help = help; - entry->value = init; + if (NEU_METRIC_TYPE_ROLLING_COUNTER == type) { + entry->rcnt = neu_rolling_counter_new(init); + if (NULL == entry->rcnt) { + free(entry); + return -1; + } + } else { + entry->value = init; + } + + entry->name = name; + entry->type = type; + entry->help = help; HASH_ADD_STR(*entries, name, entry); return 0; }