Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_http: add new /health endpoint #10020

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 70 additions & 10 deletions plugins/in_http/http_prot.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ static int sds_uri_decode(flb_sds_t s)
return 0;
}

cfl_sds_t get_health_message()
{
char time_str[128];
cfl_sds_t msg = NULL;
struct tm tm_time;
struct flb_time tm;

flb_time_get(&tm);
gmtime_r(&tm.tm.tv_sec, &tm_time);

snprintf(time_str, sizeof(time_str) - 1, "%04d-%02d-%02dT%02d:%02d:%02d.%09ldZ",
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, tm.tm.tv_nsec);

msg = cfl_sds_create_size(256);
if (!msg) {
return NULL;
}

cfl_sds_printf(&msg,
"{\n"
" \"status\": \"ok\",\n"
" \"message\": \"I can only show you the door. You're the one that has to walk through it.\",\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this message?

Maybe we can return the version information instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please, version info would be great

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this message?

The Matrix

Maybe we can return the version information instead?

makes sense

" \"timestamp\": \"%s\"\n"
"}\n",
time_str);

return msg;
}

static int send_response(struct http_conn *conn, int http_status, char *message)
{
struct flb_http *context;
Expand Down Expand Up @@ -112,13 +142,25 @@ static int send_response(struct http_conn *conn, int http_status, char *message)
context->success_headers_str);
}
else if (http_status == 200) {
flb_sds_printf(&out,
"HTTP/1.1 200 OK\r\n"
"Server: Fluent Bit v%s\r\n"
"%s"
"Content-Length: 0\r\n\r\n",
FLB_VERSION_STR,
context->success_headers_str);
if (len > 0) {
flb_sds_printf(&out,
"HTTP/1.1 200 OK\r\n"
"Server: Fluent Bit v%s\r\n"
"%s"
"Content-Length: %i\r\n\r\n%s",
FLB_VERSION_STR,
context->success_headers_str,
len, message);
}
else {
flb_sds_printf(&out,
"HTTP/1.1 200 OK\r\n"
"Server: Fluent Bit v%s\r\n"
"%s"
"Content-Length: 0\r\n\r\n",
FLB_VERSION_STR,
context->success_headers_str);
}
}
else if (http_status == 204) {
flb_sds_printf(&out,
Expand Down Expand Up @@ -734,7 +776,7 @@ static int http_prot_uncompress(struct flb_http *ctx,
return 0;
}

static int process_payload(struct flb_http *ctx, struct http_conn *conn,
static int process_payload(struct flb_http *ctx, struct http_conn *conn,
flb_sds_t tag,
struct mk_http_session *session,
struct mk_http_request *request)
Expand Down Expand Up @@ -868,6 +910,7 @@ int http_prot_handle(struct flb_http *ctx, struct http_conn *conn,
int len;
char *uri;
char *qs;
flb_sds_t health;
off_t diff;
flb_sds_t tag;
struct mk_http_header *header;
Expand Down Expand Up @@ -920,8 +963,6 @@ int http_prot_handle(struct flb_http *ctx, struct http_conn *conn,
}
}

mk_mem_free(uri);

/* Check if we have a Host header: Hostname ; port */
mk_http_point_header(&request->host, &session->parser, MK_HEADER_HOST);

Expand All @@ -932,6 +973,7 @@ int http_prot_handle(struct flb_http *ctx, struct http_conn *conn,
/* HTTP/1.1 needs Host header */
if (!request->host.data && request->protocol == MK_HTTP_PROTOCOL_11) {
flb_sds_destroy(tag);
mk_mem_free(uri);
return -1;
}

Expand All @@ -948,6 +990,15 @@ int http_prot_handle(struct flb_http *ctx, struct http_conn *conn,
request->_content_length.data = NULL;
}

if (request->method == MK_METHOD_GET && strcmp(uri, "/health") == 0) {
flb_sds_destroy(tag);
mk_mem_free(uri);
health = get_health_message();
ret = send_response(conn, 200, health);
flb_sds_destroy(health);
return ret;
}

if (request->method != MK_METHOD_POST) {
flb_sds_destroy(tag);
send_response(conn, 400, "error: invalid HTTP method\n");
Expand Down Expand Up @@ -1233,8 +1284,10 @@ int http_prot_handle_ng(struct flb_http_request *request,
int ret;
int len;
flb_sds_t tag;
flb_sds_t health;
struct flb_http *ctx;


ctx = (struct flb_http *) response->stream->user_data;
if (request->path[0] != '/') {
send_response_ng(response, 400, "error: invalid request\n");
Expand Down Expand Up @@ -1271,6 +1324,13 @@ int http_prot_handle_ng(struct flb_http_request *request,
return -1;
}

if (request->method == HTTP_METHOD_GET && strcmp(request->path, "/health") == 0) {
flb_sds_destroy(tag);
health = get_health_message();
send_response_ng(response, 200, health);
flb_sds_destroy(health);
return 0;
}
if (request->method != HTTP_METHOD_POST) {
send_response_ng(response, 400, "error: invalid HTTP method\n");
flb_sds_destroy(tag);
Expand Down
Loading