Skip to content

Commit 491b4e4

Browse files
committed
in_http: add configurable health endpoint
Add optional health endpoint accessible via GET /health when enabled. The endpoint returns JSON with status, version, git hash and timestamp instead of the original Matrix movie quote. The endpoint is disabled by default for security. The health_check configuration directive enables the endpoint: health_check true Fixes #10003 Supersedes PR #10020
1 parent 9e84b08 commit 491b4e4

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

plugins/in_http/http.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ static struct flb_config_map config_map[] = {
234234
"Set successful response code. 200, 201 and 204 are supported."
235235
},
236236

237+
{
238+
FLB_CONFIG_MAP_BOOL, "health_check", "false",
239+
0, FLB_TRUE, offsetof(struct flb_http, enable_health_endpoint),
240+
"Enable health check endpoint accessible via GET /health"
241+
},
237242

238243
/* EOF */
239244
{0}

plugins/in_http/http.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ struct flb_http {
6161
struct mk_server *server;
6262

6363
int collector_id;
64+
65+
/* Health endpoint configuration */
66+
int enable_health_endpoint;
6467
};
6568

6669

plugins/in_http/http_prot.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#define HTTP_CONTENT_JSON 0
3636
#define HTTP_CONTENT_URLENCODED 1
37+
#define HTTP_HEALTH_ENDPOINT "/health"
3738

3839
static inline char hex2nibble(char c)
3940
{
@@ -95,18 +96,19 @@ cfl_sds_t get_health_message()
9596
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
9697
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, tm.tm.tv_nsec);
9798

98-
msg = cfl_sds_create_size(256);
99+
msg = cfl_sds_create_size(512);
99100
if (!msg) {
100101
return NULL;
101102
}
102103

103104
cfl_sds_printf(&msg,
104105
"{\n"
105106
" \"status\": \"ok\",\n"
106-
" \"message\": \"I can only show you the door. You're the one that has to walk through it.\",\n"
107+
" \"version\": \"%s\",\n"
108+
" \"git_hash\": \"%s\",\n"
107109
" \"timestamp\": \"%s\"\n"
108110
"}\n",
109-
time_str);
111+
FLB_VERSION_STR, FLB_GIT_HASH, time_str);
110112

111113
return msg;
112114
}
@@ -990,7 +992,7 @@ int http_prot_handle(struct flb_http *ctx, struct http_conn *conn,
990992
request->_content_length.data = NULL;
991993
}
992994

993-
if (request->method == MK_METHOD_GET && strcmp(uri, "/health") == 0) {
995+
if (request->method == MK_METHOD_GET && strcmp(uri, HTTP_HEALTH_ENDPOINT) == 0 && ctx->enable_health_endpoint) {
994996
flb_sds_destroy(tag);
995997
mk_mem_free(uri);
996998
health = get_health_message();
@@ -1324,7 +1326,7 @@ int http_prot_handle_ng(struct flb_http_request *request,
13241326
return -1;
13251327
}
13261328

1327-
if (request->method == HTTP_METHOD_GET && strcmp(request->path, "/health") == 0) {
1329+
if (request->method == HTTP_METHOD_GET && strcmp(request->path, HTTP_HEALTH_ENDPOINT) == 0 && ctx->enable_health_endpoint) {
13281330
flb_sds_destroy(tag);
13291331
health = get_health_message();
13301332
send_response_ng(response, 200, health);

0 commit comments

Comments
 (0)