-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from alltilla/http-request-metrics-fix
http: bind `output_http_requests_total` counter lifecycle to worker's
- Loading branch information
Showing
10 changed files
with
232 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright (c) 2023-2024 Attila Szakacs <[email protected]> | ||
* Copyright (c) 2024 Balazs Scheidler <[email protected]> | ||
* Copyright (c) 2024 Axoflow | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* As an additional exemption you are allowed to compile & link against the | ||
* OpenSSL libraries as published by the OpenSSL project. See the file | ||
* COPYING for details. | ||
* | ||
*/ | ||
|
||
#include "metrics-cache.h" | ||
#include "stats/stats-cluster-single.h" | ||
|
||
struct _MetricsCache | ||
{ | ||
GHashTable *clusters; | ||
GArray *label_buffers; | ||
}; | ||
|
||
static StatsCluster * | ||
_register_single_cluster_locked(StatsClusterKey *key, gint stats_level) | ||
{ | ||
StatsCluster *cluster; | ||
|
||
stats_lock(); | ||
{ | ||
StatsCounterItem *counter; | ||
cluster = stats_register_dynamic_counter(stats_level, key, SC_TYPE_SINGLE_VALUE, &counter); | ||
} | ||
stats_unlock(); | ||
|
||
return cluster; | ||
} | ||
|
||
static void | ||
_unregister_single_cluster_locked(StatsCluster *cluster) | ||
{ | ||
stats_lock(); | ||
{ | ||
StatsCounterItem *counter = stats_cluster_single_get_counter(cluster); | ||
stats_unregister_dynamic_counter(cluster, SC_TYPE_SINGLE_VALUE, &counter); | ||
} | ||
stats_unlock(); | ||
} | ||
|
||
MetricsCache * | ||
metrics_cache_new(void) | ||
{ | ||
MetricsCache *self = g_new0(MetricsCache, 1); | ||
|
||
self->clusters = g_hash_table_new_full((GHashFunc) stats_cluster_key_hash, | ||
(GEqualFunc) stats_cluster_key_equal, | ||
NULL, | ||
(GDestroyNotify) _unregister_single_cluster_locked); | ||
self->label_buffers = g_array_new(FALSE, FALSE, sizeof(StatsClusterLabel)); | ||
|
||
return self; | ||
} | ||
|
||
void | ||
metrics_cache_free(MetricsCache *self) | ||
{ | ||
g_hash_table_destroy(self->clusters); | ||
g_array_free(self->label_buffers, TRUE); | ||
g_free(self); | ||
} | ||
|
||
StatsCounterItem * | ||
metrics_cache_get_counter(MetricsCache *self, StatsClusterKey *key, gint level) | ||
{ | ||
StatsCluster *cluster = g_hash_table_lookup(self->clusters, key); | ||
if (!cluster) | ||
{ | ||
cluster = _register_single_cluster_locked(key, level); | ||
if (cluster) | ||
g_hash_table_insert(self->clusters, &cluster->key, cluster); | ||
} | ||
|
||
return stats_cluster_single_get_counter(cluster); | ||
} | ||
|
||
void | ||
metrics_cache_reset_labels(MetricsCache *self) | ||
{ | ||
self->label_buffers = g_array_set_size(self->label_buffers, 0); | ||
} | ||
|
||
StatsClusterLabel * | ||
metrics_cache_alloc_label(MetricsCache *self) | ||
{ | ||
self->label_buffers = g_array_set_size(self->label_buffers, self->label_buffers->len + 1); | ||
return &g_array_index(self->label_buffers, StatsClusterLabel, self->label_buffers->len - 1); | ||
} | ||
|
||
StatsClusterLabel * | ||
metrics_cache_get_labels(MetricsCache *self) | ||
{ | ||
return (StatsClusterLabel *) self->label_buffers->data; | ||
} | ||
|
||
guint | ||
metrics_cache_get_labels_len(MetricsCache *self) | ||
{ | ||
return self->label_buffers->len; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2023-2024 Attila Szakacs <[email protected]> | ||
* Copyright (c) 2024 Balazs Scheidler <[email protected]> | ||
* Copyright (c) 2024 Axoflow | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* As an additional exemption you are allowed to compile & link against the | ||
* OpenSSL libraries as published by the OpenSSL project. See the file | ||
* COPYING for details. | ||
* | ||
*/ | ||
|
||
#ifndef METRICS_CACHE_H_INCLUDED | ||
#define METRICS_CACHE_H_INCLUDED | ||
|
||
#include "stats/stats-registry.h" | ||
|
||
/* | ||
* There is a recurring inconvenience with dynamic counters. | ||
* | ||
* Registering, changing and unregistering a counter makes it orphaned, | ||
* as no one is keeping it alive. Non-dynamic counters do not have this | ||
* issue, as they are always stored on their call-site, binding their | ||
* lifecycle to the call-site. | ||
* | ||
* This class intends to solve this problem by providing a cache, | ||
* which keeps alive its counters until the cache is freed. On the | ||
* call-site you only need to keep the cache alive, to keep the | ||
* counters alive. | ||
* | ||
* It also grants a label cache for performance optimization needs. | ||
* | ||
* Note: The cache is NOT thread safe, make sure to eliminate | ||
* concurrency on the call site. If you need a cache that is bound | ||
* to the current thread, see metrics/metrics-tls-cache.h. | ||
*/ | ||
|
||
typedef struct _MetricsCache MetricsCache; | ||
|
||
MetricsCache *metrics_cache_new(void); | ||
void metrics_cache_free(MetricsCache *self); | ||
|
||
StatsCounterItem *metrics_cache_get_counter(MetricsCache *self, StatsClusterKey *key, gint level); | ||
void metrics_cache_reset_labels(MetricsCache *self); | ||
StatsClusterLabel *metrics_cache_alloc_label(MetricsCache *self); | ||
StatsClusterLabel *metrics_cache_get_labels(MetricsCache *self); | ||
guint metrics_cache_get_labels_len(MetricsCache *self); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.