Skip to content

Commit a69df55

Browse files
committed
syslog-ng-ctl: stats and get query now both supports prometheus and csv output formats
Signed-off-by: Hofi <hofione@gmail.com>
1 parent 43a6484 commit a69df55

File tree

5 files changed

+93
-28
lines changed

5 files changed

+93
-28
lines changed

lib/stats/stats-csv.c

+26-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "stats/stats-registry.h"
2626
#include "stats/stats-cluster.h"
2727
#include "utf8utils.h"
28+
#include "scratch-buffers.h"
2829

2930
#include <string.h>
3031

@@ -58,16 +59,13 @@ stats_format_csv_escapevar(const gchar *var)
5859
return escaped_result;
5960
}
6061

61-
static void
62-
stats_format_csv(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointer user_data)
62+
GString *
63+
stats_csv_format_counter(StatsCluster *sc, gint type, StatsCounterItem *counter)
6364
{
64-
gpointer *args = (gpointer *) user_data;
65-
StatsCSVRecordFunc process_record = (StatsCSVRecordFunc) args[0];
66-
gpointer process_record_arg = args[1];
6765
gchar *s_id, *s_instance, *tag_name;
6866
gchar buf[32];
6967
gchar state;
70-
GString *csv = g_string_sized_new(512);
68+
GString *csv = scratch_buffers_alloc();
7169

7270
s_id = stats_format_csv_escapevar(sc->key.legacy.id);
7371
s_instance = stats_format_csv_escapevar(sc->key.legacy.instance);
@@ -80,14 +78,34 @@ stats_format_csv(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointe
8078
state = 'a';
8179

8280
tag_name = stats_format_csv_escapevar(stats_cluster_get_type_name(sc, type));
81+
8382
g_string_printf(csv, "%s;%s;%s;%c;%s;%"G_GSIZE_FORMAT"\n",
8483
stats_cluster_get_component_name(sc, buf, sizeof(buf)),
8584
s_id, s_instance, state, tag_name, stats_counter_get(&sc->counter_group.counters[type]));
86-
process_record(csv->str, process_record_arg);
87-
g_string_free(csv, TRUE);
85+
8886
g_free(tag_name);
8987
g_free(s_id);
9088
g_free(s_instance);
89+
90+
return csv;
91+
}
92+
93+
static void
94+
stats_format_csv(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointer user_data)
95+
{
96+
gpointer *args = (gpointer *) user_data;
97+
StatsCSVRecordFunc process_record = (StatsCSVRecordFunc) args[0];
98+
gpointer process_record_arg = args[1];
99+
100+
ScratchBuffersMarker marker;
101+
scratch_buffers_mark(&marker);
102+
103+
GString *record = stats_csv_format_counter(sc, type, counter);
104+
if (!record)
105+
return;
106+
107+
process_record(record->str, process_record_arg);
108+
scratch_buffers_reclaim_marked(marker);
91109
}
92110

93111
void

lib/stats/stats-csv.h

+3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
#define STATS_CSV_H_INCLUDED 1
2626

2727
#include "syslog-ng.h"
28+
#include "stats-cluster.h"
2829

2930
typedef void (*StatsCSVRecordFunc)(const char *record, gpointer user_data);
3031

32+
GString *stats_csv_format_counter(StatsCluster *sc, gint type, StatsCounterItem *counter);
33+
3134
void stats_generate_csv(StatsCSVRecordFunc process_record, gpointer user_data, gboolean *cancelled);
3235

3336
#endif

lib/stats/stats-query-commands.c

+45-7
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323

2424
#include "messages.h"
2525
#include "stats-query-commands.h"
26+
#include "stats-prometheus.h"
27+
#include "stats-csv.h"
2628
#include "stats/stats-query.h"
2729
#include "control/control-connection.h"
30+
#include "scratch-buffers.h"
2831

2932
typedef enum _QueryCommand
3033
{
@@ -58,19 +61,54 @@ static gboolean
5861
_ctl_format_get(gpointer user_data)
5962
{
6063
gpointer *args = (gpointer *) user_data;
61-
StatsCounterItem *ctr = (StatsCounterItem *) args[0];
62-
GString *str = (GString *)args[1];
63-
g_string_append_printf(str, "%s=%"G_GSIZE_FORMAT"\n", stats_counter_get_name(ctr), stats_counter_get(ctr));
64+
StatsCluster *sc = (StatsCluster *) args[0];
65+
gint type = GPOINTER_TO_INT(args[1]);
66+
StatsCounterItem *ctr = (StatsCounterItem *) args[2];
67+
const gchar *fmt = (const gchar *) args[3];
68+
GString *str = (GString *)args[4];
69+
70+
if (g_str_equal(fmt, "kv"))
71+
g_string_append_printf(str, "%s=%"G_GSIZE_FORMAT"\n", stats_counter_get_name(ctr), stats_counter_get(ctr));
72+
else if (g_str_equal(fmt, "prometheus"))
73+
{
74+
ScratchBuffersMarker marker;
75+
scratch_buffers_mark(&marker);
76+
77+
GString *record = stats_prometheus_format_counter(sc, type, ctr);
78+
if (record == NULL)
79+
return FALSE;
80+
81+
g_string_append(str, record->str);
82+
scratch_buffers_reclaim_marked(marker);
83+
}
84+
else if (g_str_equal(fmt, "csv"))
85+
{
86+
ScratchBuffersMarker marker;
87+
scratch_buffers_mark(&marker);
88+
89+
GString *record = stats_csv_format_counter(sc, type, ctr);
90+
if (record == NULL)
91+
return FALSE;
92+
93+
g_string_append(str, record->str);
94+
scratch_buffers_reclaim_marked(marker);
95+
}
96+
6497
return TRUE;
6598
}
6699

67100
static gboolean
68101
_ctl_format_name_without_value(gpointer user_data)
69102
{
70103
gpointer *args = (gpointer *) user_data;
71-
StatsCounterItem *ctr = (StatsCounterItem *) args[0];
72-
GString *str = (GString *)args[1];
104+
//StatsCluster *sc = (StatsCluster *) args[0];
105+
//gint type = (gint) args[1];
106+
StatsCounterItem *ctr = (StatsCounterItem *) args[2];
107+
//const gchar *fmt = (const gchar *) args[3];
108+
GString *str = (GString *)args[4];
109+
73110
g_string_append_printf(str, "%s\n", stats_counter_get_name(ctr));
111+
74112
return TRUE;
75113
}
76114

@@ -89,15 +127,15 @@ _ctl_format_get_sum(gpointer user_data)
89127
static gboolean
90128
_query_get(const gchar *output_fmt, const gchar *filter_expr, GString *result)
91129
{
92-
return stats_query_get(filter_expr, _ctl_format_get, (gpointer)result);
130+
return stats_query_get(filter_expr, _ctl_format_get, output_fmt, (gpointer)result);
93131
}
94132

95133
static gboolean
96134
_query_get_and_reset(const gchar *output_fmt, const gchar *filter_expr, GString *result)
97135
{
98136
gboolean found_match;
99137

100-
found_match = stats_query_get_and_reset_counters(filter_expr, _ctl_format_get, (gpointer)result);
138+
found_match = stats_query_get_and_reset_counters(filter_expr, _ctl_format_get, output_fmt, (gpointer)result);
101139
_append_reset_msg_if_found_matching_counters(found_match, result);
102140

103141
return found_match;

lib/stats/stats-query.c

+16-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929

3030
#include <string.h>
3131

32-
typedef void (*ProcessCounterCb)(StatsCounterItem *counter, gpointer user_data, StatsFormatCb format_cb,
32+
typedef void (*ProcessCounterCb)(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointer user_data,
33+
StatsFormatCb format_cb,
3334
gpointer result);
3435

3536
static const gchar *
@@ -75,7 +76,7 @@ _process_counter_if_matching(StatsCluster *sc, gint type, StatsCounterItem *coun
7576
{
7677
*found = TRUE;
7778

78-
process_func(counter, process_func_user_data, format_cb, format_cb_user_data);
79+
process_func(sc, type, counter, process_func_user_data, format_cb, format_cb_user_data);
7980
if (must_reset)
8081
stats_cluster_reset_counter_if_needed(sc, counter);
8182

@@ -118,29 +119,32 @@ _process_matching_counters(const gchar *key_str, ProcessCounterCb process_func,
118119
}
119120

120121
static void
121-
_format_selected_counter(StatsCounterItem *counter, gpointer user_data, StatsFormatCb format_cb, gpointer result)
122+
_format_selected_counter(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointer user_data,
123+
StatsFormatCb format_cb, gpointer result)
122124
{
123-
gpointer args[] = {counter, result};
125+
gpointer args[] = {sc, GINT_TO_POINTER(type), counter, user_data, result};
124126
format_cb(args);
125127
}
126128

127129
gboolean
128-
_stats_query_get(const gchar *expr, StatsFormatCb format_cb, gpointer result, gboolean must_reset)
130+
_stats_query_get(const gchar *expr, StatsFormatCb format_cb, const gchar *output_fmt, gpointer result,
131+
gboolean must_reset)
129132
{
130133
const gchar *key_str = _setup_filter_expression(expr);
131-
return _process_matching_counters(key_str, _format_selected_counter, NULL, format_cb, result, must_reset);
134+
return _process_matching_counters(key_str, _format_selected_counter, (gpointer) output_fmt, format_cb, result,
135+
must_reset);
132136
}
133137

134138
gboolean
135-
stats_query_get(const gchar *expr, StatsFormatCb format_cb, gpointer result)
139+
stats_query_get(const gchar *expr, StatsFormatCb format_cb, const gchar *output_fmt, gpointer result)
136140
{
137-
return _stats_query_get(expr, format_cb, result, FALSE);
141+
return _stats_query_get(expr, format_cb, output_fmt, result, FALSE);
138142
}
139143

140144
gboolean
141-
stats_query_get_and_reset_counters(const gchar *expr, StatsFormatCb format_cb, gpointer result)
145+
stats_query_get_and_reset_counters(const gchar *expr, StatsFormatCb format_cb, const gchar *output_fmt, gpointer result)
142146
{
143-
return _stats_query_get(expr, format_cb, result, TRUE);
147+
return _stats_query_get(expr, format_cb, output_fmt, result, TRUE);
144148
}
145149

146150
static gboolean
@@ -151,7 +155,8 @@ _is_timestamp(gchar *counter_name)
151155
}
152156

153157
void
154-
_sum_selected_counters(StatsCounterItem *counter, gpointer user_data, StatsFormatCb format_cb, gpointer result)
158+
_sum_selected_counters(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointer user_data,
159+
StatsFormatCb format_cb, gpointer result)
155160
{
156161
gpointer *args = (gpointer *) user_data;
157162
gint64 *sum = (gint64 *) args[1];

lib/stats/stats-query.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ typedef gboolean (*StatsFormatCb)(gpointer user_data);
3131

3232
gboolean stats_query_list(const gchar *expr, StatsFormatCb format_cb, gpointer result);
3333
gboolean stats_query_list_and_reset_counters(const gchar *expr, StatsFormatCb format_cb, gpointer result);
34-
gboolean stats_query_get(const gchar *expr, StatsFormatCb format_cb, gpointer result);
35-
gboolean stats_query_get_and_reset_counters(const gchar *expr, StatsFormatCb format_cb, gpointer result);
34+
gboolean stats_query_get(const gchar *expr, StatsFormatCb format_cb, const gchar *output_fmt, gpointer result);
35+
gboolean stats_query_get_and_reset_counters(const gchar *expr, StatsFormatCb format_cb, const gchar *output_fmt,
36+
gpointer result);
3637
gboolean stats_query_get_sum(const gchar *expr, StatsFormatCb format_cb, gpointer result);
3738
gboolean stats_query_get_sum_and_reset_counters(const gchar *expr, StatsFormatCb format_cb, gpointer result);
3839

0 commit comments

Comments
 (0)