1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2023-2024 László Várady
*
* 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 "stats/stats-prometheus.h"
#include "stats/stats-registry.h"
#include "stats/stats-cluster.h"
#include "stats/stats-counter.h"
#include "timeutils/unixtime.h"
#include "str-utils.h"
#include "scratch-buffers.h"
#include <string.h>
/* Exposition format:
*
* A label value can be any sequence of UTF-8 characters, but the
* backslash (\), double-quote ("), and line feed (\n) characters have to be
* escaped as \\, \", and \n, respectively.
*/
static gchar *
stats_format_prometheus_sanitize_label_value(const gchar *value)
{
GString *sanitized_value = scratch_buffers_alloc();
const gchar *value_end = value + strlen(value);
while (value < value_end)
{
gunichar uchar = g_utf8_get_char_validated(value, value_end - value);
if (G_UNLIKELY(uchar == (gunichar) -1 || uchar == (gunichar) -2))
{
/* double backslash to conform to the format */
g_string_append_printf(sanitized_value, "\\\\x%02x", *(guint8 *) value);
value++;
continue;
}
switch (uchar)
{
case '\\':
g_string_append(sanitized_value, "\\\\");
break;
case '"':
g_string_append(sanitized_value, "\\\"");
break;
case '\n':
g_string_append(sanitized_value, "\\n");
break;
default:
g_string_append_unichar_optimized(sanitized_value, uchar);
break;
}
value = g_utf8_next_char(value);
}
return sanitized_value->str;
}
static inline gboolean
_is_valid_name_char(gchar c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_') || (c >= '0' && c <= '9');
}
static inline gboolean
_is_str_empty(const gchar *str)
{
return !str || strcmp(str, "") == 0;
}
static gchar *
stats_format_prometheus_sanitize_name(const gchar *name)
{
GString *sanitized_name = scratch_buffers_alloc();
for (const gchar *c = name; *c; ++c)
{
if (_is_valid_name_char(*c))
g_string_append_c(sanitized_name, *c);
else if (*c == '.' || *c == '-')
g_string_append_c(sanitized_name, '_');
/* remove otherwise */
}
return sanitized_name->str;
}
gchar *
stats_format_prometheus_format_value(const StatsClusterKey *key, StatsCounterItem *counter)
{
GString *value = scratch_buffers_alloc();
gsize stored_value = stats_counter_get(counter);
guint64 converted_int = stored_value;
gdouble converted_double = stored_value;
gchar double_buf[G_ASCII_DTOSTR_BUF_SIZE];
switch (key->formatting.stored_unit)
{
case SCU_GIB:
converted_int *= 1024;
case SCU_MIB:
converted_int *= 1024;
case SCU_KIB:
converted_int *= 1024;
g_string_printf(value, "%"G_GUINT64_FORMAT, converted_int);
break;
case SCU_HOURS:
converted_int *= 60;
case SCU_MINUTES:
converted_int *= 60;
case SCU_SECONDS:
if (key->formatting.frame_of_reference == SCFOR_RELATIVE_TO_TIME_OF_QUERY)
{
UnixTime now;
unix_time_set_now(&now);
converted_int = now.ut_sec - converted_int;
}
g_string_printf(value, "%"G_GUINT64_FORMAT, converted_int);
break;
case SCU_NANOSECONDS:
converted_double /= 1e9;
g_string_assign(value, g_ascii_dtostr(double_buf, G_N_ELEMENTS(double_buf), converted_double));
break;
case SCU_MILLISECONDS:
converted_double /= 1e3;
g_string_assign(value, g_ascii_dtostr(double_buf, G_N_ELEMENTS(double_buf), converted_double));
break;
default:
/* no conversion */
g_string_printf(value, "%"G_GSIZE_FORMAT, stored_value);
break;
}
return value->str;
}
static inline void
_append_formatted_label(GString *serialized_labels, const StatsClusterLabel *label)
{
if (!label->value)
return;
g_string_append_printf(serialized_labels, "%s=\"%s\"",
stats_format_prometheus_sanitize_name(label->name),
stats_format_prometheus_sanitize_label_value(label->value));
}
static inline gboolean
_is_timestamp(StatsCluster *sc, gint type)
{
return strcmp(stats_cluster_get_type_name(sc, type), "stamp") == 0;
}
static const gchar *
_format_labels(StatsCluster *sc, gint type)
{
StatsClusterLabel type_label;
gboolean needs_type_label = stats_cluster_get_type_label(sc, type, &type_label);
if (!sc->key.labels_len && !needs_type_label)
return NULL;
GString *serialized_labels = scratch_buffers_alloc();
gboolean comma_needed = FALSE;
for (gsize i = 0; i < sc->key.labels_len; ++i)
{
const StatsClusterLabel *label = &sc->key.labels[i];
if (_is_str_empty(label->value))
continue;
if (comma_needed)
g_string_append_c(serialized_labels, ',');
else
comma_needed = TRUE;
_append_formatted_label(serialized_labels, label);
}
if (needs_type_label)
{
if (comma_needed)
g_string_append_c(serialized_labels, ',');
_append_formatted_label(serialized_labels, &type_label);
}
if (serialized_labels->len == 0)
return NULL;
return serialized_labels->str;
}
static GString *
_format_legacy(StatsCluster *sc, gint type, StatsCounterItem *counter)
{
GString *record = scratch_buffers_alloc();
GString *labels = scratch_buffers_alloc();
gchar component[64];
g_string_append_printf(record, PROMETHEUS_METRIC_PREFIX "%s",
stats_format_prometheus_sanitize_name(stats_cluster_get_component_name(sc, component, sizeof(component))));
if (!sc->key.legacy.component || sc->key.legacy.component == SCS_GLOBAL)
{
if (!_is_str_empty(sc->key.legacy.id))
g_string_append_printf(record, "_%s", stats_format_prometheus_sanitize_name(sc->key.legacy.id));
}
else
{
gboolean has_id = !_is_str_empty(sc->key.legacy.id);
if (has_id)
g_string_append_printf(labels, "%s=\"%s\"", "id", stats_format_prometheus_sanitize_label_value(sc->key.legacy.id));
if (!_is_str_empty(sc->key.legacy.instance))
{
if (has_id)
g_string_append_c(labels, ',');
g_string_append_printf(labels, "%s=\"%s\"", "stat_instance",
stats_format_prometheus_sanitize_label_value(sc->key.legacy.instance));
}
}
const gchar *type_name = stats_cluster_get_type_name(sc, type);
if (g_strcmp0(type_name, "value") != 0)
g_string_append_printf(record, "_%s", stats_format_prometheus_sanitize_name(type_name));
if (labels->len != 0)
g_string_append_printf(record, "{%s}", labels->str);
const gchar *metric_value = stats_format_prometheus_format_value(&sc->key, &sc->counter_group.counters[type]);
g_string_append_printf(record, " %s\n", metric_value);
return record;
}
GString *
stats_prometheus_format_counter(StatsCluster *sc, gint type, StatsCounterItem *counter)
{
if (_is_timestamp(sc, type))
return NULL;
if (!sc->key.name)
return _format_legacy(sc, type, counter);
GString *record = scratch_buffers_alloc();
g_string_append_printf(record, PROMETHEUS_METRIC_PREFIX "%s", stats_format_prometheus_sanitize_name(sc->key.name));
const gchar *labels = _format_labels(sc, type);
if (labels)
g_string_append_printf(record, "{%s}", labels);
const gchar *metric_value = stats_format_prometheus_format_value(&sc->key, &sc->counter_group.counters[type]);
g_string_append_printf(record, " %s\n", metric_value);
return record;
}
static void
stats_format_prometheus(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointer user_data)
{
gpointer *args = (gpointer *) user_data;
StatsPrometheusRecordFunc process_record = (StatsPrometheusRecordFunc) args[0];
gpointer process_record_arg = args[1];
gboolean with_legacy = GPOINTER_TO_INT(args[2]);
if (!sc->key.name && !with_legacy)
return;
if (stats_cluster_is_orphaned(sc))
return;
ScratchBuffersMarker marker;
scratch_buffers_mark(&marker);
GString *record = stats_prometheus_format_counter(sc, type, counter);
if (!record)
return;
process_record(record->str, process_record_arg);
scratch_buffers_reclaim_marked(marker);
}
void
stats_generate_prometheus(StatsPrometheusRecordFunc process_record, gpointer user_data, gboolean with_legacy,
gboolean *cancelled)
{
gpointer format_prometheus_args[] = {process_record, user_data, GINT_TO_POINTER(with_legacy)};
stats_lock();
stats_foreach_counter(stats_format_prometheus, format_prometheus_args, cancelled);
stats_unlock();
}
|