Skip to content

Commit 498515e

Browse files
committed
logproto-prometheus-scraper-responder: First impl of a prometheus scraper responder with syslog-ng stats prometheus outptut response
Signed-off-by: Hofi <hofione@gmail.com>
1 parent 74c36ca commit 498515e

6 files changed

+205
-0
lines changed

lib/logproto/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(LOGPROTO_HEADERS
77
logproto/logproto-framed-server.h
88
logproto/logproto.h
99
logproto/logproto-http-server.h
10+
logproto/logproto-http-scraper-responder.h
1011
logproto/logproto-record-server.h
1112
logproto/logproto-server.h
1213
logproto/logproto-text-client.h
@@ -21,6 +22,7 @@ set(LOGPROTO_SOURCES
2122
logproto/logproto-framed-client.c
2223
logproto/logproto-framed-server.c
2324
logproto/logproto-http-server.c
25+
logproto/logproto-http-scraper-responder.c
2426
logproto/logproto-record-server.c
2527
logproto/logproto-server.c
2628
logproto/logproto-text-client.c

lib/logproto/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ logprotoinclude_HEADERS = \
1212
lib/logproto/logproto-text-client.h \
1313
lib/logproto/logproto-text-server.h \
1414
lib/logproto/logproto-http-server.h \
15+
lib/logproto/logproto-http-scraper-responder.h \
1516
lib/logproto/logproto-record-server.h \
1617
lib/logproto/logproto-builtins.h \
1718
lib/logproto/logproto.h
@@ -26,6 +27,7 @@ logproto_sources = \
2627
lib/logproto/logproto-text-client.c \
2728
lib/logproto/logproto-text-server.c \
2829
lib/logproto/logproto-http-server.c \
30+
lib/logproto/logproto-http-scraper-responder.h \
2931
lib/logproto/logproto-record-server.c \
3032
lib/logproto/logproto-builtins.c
3133

lib/logproto/logproto-builtins.c

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "logproto-framed-client.h"
2828
#include "logproto-framed-server.h"
2929
#include "logproto/logproto-http-server.h"
30+
#include "logproto/logproto-http-scraper-responder.h"
3031
#include "plugin.h"
3132
#include "plugin-types.h"
3233

@@ -39,6 +40,7 @@ DEFINE_LOG_PROTO_CLIENT(log_proto_text);
3940
DEFINE_LOG_PROTO_SERVER(log_proto_text);
4041
DEFINE_LOG_PROTO_SERVER(log_proto_text_with_nuls);
4142
DEFINE_LOG_PROTO_SERVER(log_proto_http);
43+
DEFINE_LOG_PROTO_SERVER(log_proto_http_scraper_responder);
4244
DEFINE_LOG_PROTO_CLIENT(log_proto_framed);
4345
DEFINE_LOG_PROTO_SERVER(log_proto_framed);
4446

@@ -51,6 +53,7 @@ static Plugin framed_server_plugins[] =
5153
LOG_PROTO_SERVER_PLUGIN(log_proto_text, "text"),
5254
LOG_PROTO_SERVER_PLUGIN(log_proto_text_with_nuls, "text-with-nuls"),
5355
LOG_PROTO_SERVER_PLUGIN(log_proto_http, "http"),
56+
LOG_PROTO_SERVER_PLUGIN(log_proto_http_scraper_responder, "http-scraper"),
5457
LOG_PROTO_CLIENT_PLUGIN(log_proto_framed, "framed"),
5558
LOG_PROTO_SERVER_PLUGIN(log_proto_framed, "framed"),
5659
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (c) 2025 One Identity
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As an additional exemption you are allowed to compile & link against the
19+
* OpenSSL libraries as published by the OpenSSL project. See the file
20+
* COPYING for details.
21+
*
22+
*/
23+
#include "logproto/logproto-http-scraper-responder.h"
24+
#include "stats/stats-prometheus.h"
25+
#include "stats/stats-query-commands.h"
26+
#include "messages.h"
27+
28+
static void
29+
_generate_batched_response(const gchar *record, gpointer user_data)
30+
{
31+
gpointer *args = (gpointer *) user_data;
32+
//LogProtoHTTPScraperResponder *self = (LogProtoHTTPScraperResponder *)args[0];
33+
GString **batch = (GString **) args[1];
34+
35+
g_string_append_printf(*batch, "%s", record);
36+
}
37+
38+
static GString *
39+
_compose_response_body(LogProtoHTTPServer *s)
40+
{
41+
LogProtoHTTPScraperResponder *self = (LogProtoHTTPScraperResponder *)s;
42+
43+
GString *stats = NULL;
44+
gboolean cancelled = FALSE;
45+
46+
if (TRUE)//self->options->stat_type == STT_STAT)
47+
{
48+
stats = g_string_new(NULL);
49+
gpointer args[] = {self, &stats};
50+
gboolean with_legacy = TRUE;
51+
stats_generate_prometheus(_generate_batched_response, args, with_legacy, &cancelled);
52+
}
53+
else
54+
stats = stats_execute_query_command("QUERY GET prometheus *", self, &cancelled);
55+
56+
return stats;
57+
}
58+
59+
static gboolean
60+
_check_request_headers(LogProtoHTTPServer *s, gchar *buffer_start, gsize buffer_bytes)
61+
{
62+
//LogProtoHTTPScraperResponder *self = (LogProtoHTTPScraperResponder *)s;
63+
64+
// TODO: add a generic header pareser to LogProtoHTTPServer and use it here
65+
gchar **lines = g_strsplit(buffer_start, "\r\n", 2);
66+
67+
// First line must be like 'GET /metrics HTTP/1.1\x0d\x0a'
68+
gchar *line = lines && lines[0] ? lines[0] : buffer_start;
69+
gchar **tokens = g_strsplit(line, " ", 3);
70+
71+
gboolean broken = (tokens == NULL || tokens[0] == NULL || strcmp(tokens[0], "GET")
72+
|| tokens[1] == NULL || strcmp(tokens[1], "/metrics"));
73+
74+
// TODO: Check further headers as well to support options like compression, etc.
75+
if (broken)
76+
msg_error("Unknown request", evt_tag_str("http-scraper-responder", buffer_start));
77+
78+
if (tokens)
79+
g_strfreev(tokens);
80+
if (lines)
81+
g_strfreev(lines);
82+
return FALSE == broken;
83+
}
84+
85+
void
86+
log_proto_http_scraper_responder_server_init(LogProtoHTTPScraperResponder *self, LogTransport *transport,
87+
const LogProtoServerOptions *options)
88+
{
89+
log_proto_http_server_init((LogProtoHTTPServer *)self, transport, options); //&options->super);
90+
self->options = options;
91+
self->super.request_header_checker = _check_request_headers;
92+
self->super.response_body_composer = _compose_response_body;
93+
}
94+
95+
LogProtoServer *
96+
log_proto_http_scraper_responder_server_new(LogTransport *transport,
97+
const LogProtoServerOptions *options)
98+
{
99+
LogProtoHTTPScraperResponder *self = g_new0(LogProtoHTTPScraperResponder, 1);
100+
101+
log_proto_http_scraper_responder_server_init(self, transport, options);
102+
return &self->super.super.super.super;
103+
}
104+
105+
/* Options */
106+
107+
void
108+
log_proto_http_scraper_responder_options_defaults(LogProtoHTTPScraperResponderOptions *options)
109+
{
110+
memset(options, 0, sizeof(*options));
111+
options->stat_type = STT_STAT;
112+
}
113+
114+
void
115+
log_proto_http_scraper_responder_options_init(LogProtoHTTPScraperResponderOptions *options,
116+
GlobalConfig *cfg)
117+
{
118+
if (options->initialized)
119+
return;
120+
log_proto_server_options_init(&options->super, cfg);
121+
options->initialized = TRUE;
122+
}
123+
124+
void
125+
log_proto_http_scraper_responder_destroy(LogProtoHTTPScraperResponderOptions *options)
126+
{
127+
// if (options->destroy)
128+
// options->destroy(options);
129+
options->initialized = FALSE;
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2025 One Identity
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As an additional exemption you are allowed to compile & link against the
19+
* OpenSSL libraries as published by the OpenSSL project. See the file
20+
* COPYING for details.
21+
*
22+
*/
23+
#ifndef LOGPROTO_HTTP_SCRAPER_RESPONDER_INCLUDED
24+
#define LOGPROTO_HTTP_SCRAPER_RESPONDER_INCLUDED
25+
26+
#include "logproto/logproto-http-server.h"
27+
28+
/* Stat type flags */
29+
#define STT_STAT 0x01
30+
#define STT_QUERY 0x02
31+
32+
/* options */
33+
typedef struct _LogProtoHTTPScraperResponderOptions LogProtoHTTPScraperResponderOptions;
34+
struct _LogProtoHTTPScraperResponderOptions
35+
{
36+
LogProtoServerOptions /* LogProtoHTTPServerOptions */ super;
37+
guint8 stat_type;
38+
gboolean initialized;
39+
};
40+
41+
/* LogProtoHTTPScraperResponder */
42+
typedef struct _LogProtoHTTPScraperResponder LogProtoHTTPScraperResponder;
43+
struct _LogProtoHTTPScraperResponder
44+
{
45+
LogProtoHTTPServer super;
46+
LogProtoServerOptions/*Storage*/ *options;
47+
};
48+
49+
void log_proto_http_scraper_responder_options_defaults(LogProtoHTTPScraperResponderOptions *options);
50+
void log_proto_http_scraper_responder_options_init(LogProtoHTTPScraperResponderOptions *options,
51+
GlobalConfig *cfg);
52+
void log_proto_http_scraper_responder_destroy(LogProtoHTTPScraperResponderOptions *options);
53+
54+
LogProtoServer *log_proto_http_scraper_responder_server_new(
55+
LogTransport *transport,
56+
const LogProtoServerOptions *options);
57+
void log_proto_http_scraper_responder_server_init(LogProtoHTTPScraperResponder *self,
58+
LogTransport *transport,
59+
const LogProtoServerOptions *options);
60+
61+
#endif

modules/afsocket/transport-mapper-inet.c

+7
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,13 @@ transport_mapper_network_apply_transport(TransportMapper *s, GlobalConfig *cfg)
446446
self->super.sock_proto = IPPROTO_TCP;
447447
self->super.transport_name = g_strdup("http");
448448
}
449+
else if (strcasecmp(transport, "http-scraper") == 0)
450+
{
451+
self->super.logproto = "http-scraper";
452+
self->super.sock_type = SOCK_STREAM;
453+
self->super.sock_proto = IPPROTO_TCP;
454+
self->super.transport_name = g_strdup("http-scraper");
455+
}
449456
else
450457
{
451458
self->super.logproto = self->super.transport;

0 commit comments

Comments
 (0)