Skip to content

Commit 4c43d30

Browse files
committed
tests: runtime: cover Prometheus exporter HTTP options
Verify that the configured buffer chunk size and worker count reach the embedded HTTP server and create a worker runtime. Signed-off-by: Stefano Tondo <stondo@gmail.com>
1 parent 4f5b5b6 commit 4c43d30

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

tests/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ if(FLB_IN_LIB)
262262
FLB_RT_TEST(FLB_OUT_LOKI "out_loki.c")
263263
FLB_RT_TEST(FLB_OUT_NULL "out_null.c")
264264
FLB_RT_TEST(FLB_OUT_PLOT "out_plot.c")
265+
FLB_RT_TEST(FLB_OUT_PROMETHEUS_EXPORTER "out_prometheus_exporter.c")
265266
FLB_RT_TEST(FLB_OUT_RETRY "out_retry.c")
266267
FLB_RT_TEST(FLB_OUT_SPLUNK "out_splunk.c")
267268
FLB_RT_TEST(FLB_OUT_STDOUT "out_stdout.c")
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
#include <fluent-bit.h>
4+
#include <fluent-bit/flb_output.h>
5+
6+
#include "../../plugins/out_prometheus_exporter/prom.h"
7+
#include "../../plugins/out_prometheus_exporter/prom_http.h"
8+
9+
#include "flb_tests_runtime.h"
10+
11+
static int get_free_port(void)
12+
{
13+
int ret;
14+
int port;
15+
flb_sockfd_t sock;
16+
socklen_t len;
17+
struct sockaddr_in addr;
18+
19+
len = sizeof(addr);
20+
memset(&addr, 0, sizeof(addr));
21+
22+
sock = socket(AF_INET, SOCK_STREAM, 0);
23+
if (sock < 0) {
24+
return -1;
25+
}
26+
27+
addr.sin_family = AF_INET;
28+
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
29+
addr.sin_port = htons(0);
30+
31+
ret = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
32+
if (ret != 0) {
33+
flb_socket_close(sock);
34+
return -1;
35+
}
36+
37+
ret = getsockname(sock, (struct sockaddr *) &addr, &len);
38+
if (ret != 0) {
39+
flb_socket_close(sock);
40+
return -1;
41+
}
42+
43+
port = ntohs(addr.sin_port);
44+
flb_socket_close(sock);
45+
46+
return port;
47+
}
48+
49+
static void test_http_server_options(void)
50+
{
51+
int ret;
52+
int port;
53+
int in_ffd;
54+
int out_ffd;
55+
char port_string[16];
56+
flb_ctx_t *ctx;
57+
struct prom_exporter *prom_ctx;
58+
struct prom_http *prom_http;
59+
struct flb_output_instance *out;
60+
61+
port = get_free_port();
62+
TEST_CHECK(port > 0);
63+
snprintf(port_string, sizeof(port_string), "%d", port);
64+
65+
ctx = flb_create();
66+
TEST_CHECK(ctx != NULL);
67+
68+
ret = flb_service_set(ctx,
69+
"Flush", "1",
70+
"Grace", "1",
71+
"Log_Level", "error",
72+
NULL);
73+
TEST_CHECK(ret == 0);
74+
75+
in_ffd = flb_input(ctx, "fluentbit_metrics", NULL);
76+
TEST_CHECK(in_ffd >= 0);
77+
ret = flb_input_set(ctx, in_ffd,
78+
"tag", "metrics",
79+
"scrape_interval", "1",
80+
NULL);
81+
TEST_CHECK(ret == 0);
82+
83+
out_ffd = flb_output(ctx, "prometheus_exporter", NULL);
84+
TEST_CHECK(out_ffd >= 0);
85+
ret = flb_output_set(ctx, out_ffd,
86+
"match", "*",
87+
"host", "127.0.0.1",
88+
"port", port_string,
89+
"http_server.buffer_chunk_size", "64K",
90+
"http_server.workers", "2",
91+
NULL);
92+
TEST_CHECK(ret == 0);
93+
94+
ret = flb_start(ctx);
95+
TEST_CHECK(ret == 0);
96+
97+
out = flb_output_get_instance(ctx->config, out_ffd);
98+
TEST_CHECK(out != NULL);
99+
100+
prom_ctx = out->context;
101+
TEST_CHECK(prom_ctx != NULL);
102+
TEST_CHECK(prom_ctx->http != NULL);
103+
104+
prom_http = prom_ctx->http;
105+
TEST_CHECK(prom_http->server.buffer_chunk_size ==
106+
out->http_server_config->buffer_chunk_size);
107+
TEST_CHECK(prom_http->server.workers == 2);
108+
TEST_CHECK(prom_http->server.runtime != NULL);
109+
110+
flb_stop(ctx);
111+
flb_destroy(ctx);
112+
}
113+
114+
TEST_LIST = {
115+
{"http_server_options", test_http_server_options},
116+
{NULL, NULL}
117+
};

0 commit comments

Comments
 (0)