fix(metrics): correct registered rolling counter entry ref count

This commit is contained in:
eeff
2024-02-04 18:40:08 +08:00
parent 59f1f5d97d
commit be831d7333
+6 -4
View File
@@ -263,8 +263,8 @@ int neu_metric_entries_add(neu_metric_entry_t **entries, const char *name,
}
if (NEU_METRIC_TYPE_ROLLING_COUNTER == type) {
entry->rcnt = neu_rolling_counter_new(init);
if (NULL == entry->rcnt) {
// only allocate rolling counter for nonzero time span
if (init > 0 && NULL == (entry->rcnt = neu_rolling_counter_new(init))) {
free(entry);
return -1;
}
@@ -311,9 +311,11 @@ int neu_metrics_register_entry(const char *name, const char *help,
neu_metric_entry_t *e = NULL;
pthread_rwlock_wrlock(&g_metrics_mtx_);
// use `value` field as reference counter, initialize to zero
// and we don't need to allocate rolling counter for register entries
rv = neu_metric_entries_add(&g_metrics_.registered_metrics, name, help,
type, 1);
if (1 == rv) {
type, 0);
if (-1 != rv) {
HASH_FIND_STR(g_metrics_.registered_metrics, name, e);
++e->value;
rv = 0;