-
Notifications
You must be signed in to change notification settings - Fork 1.9k
in_premetheus_remote_write: Implement handler of payloads of prometheus remote write protocol #8725
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f024a4
in_prometheus_remote_write: Implement prometheus remote write input p…
cosmo0920 4781bdd
in_prometheus_remote_write: Use abbreviated names for promethues remo…
cosmo0920 04da871
in_prometheus_remote_write: Remove a needless FIXME
cosmo0920 1b985cc
in_prometheus_remote_write: Use one line declarations
cosmo0920 95bfcf0
in_prometheus_remote_write: Check return value from flb_io_net_write
cosmo0920 4dc32b4
in_prometheus_remote_write: Add allocation check for mk_server
cosmo0920 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| if(NOT FLB_METRICS) | ||
| message(FATAL_ERROR "Prometheus remote write input plugin requires FLB_HTTP_SERVER=On.") | ||
| endif() | ||
|
|
||
| set(src | ||
| in_prometheus_remote_write.c | ||
| in_prometheus_remote_write_prot.c | ||
| in_prometheus_remote_write_conn.c | ||
| in_prometheus_remote_write_config.c | ||
| ) | ||
|
|
||
| FLB_PLUGIN(in_prometheus_remote_write "${src}" "monkey-core-static") |
250 changes: 250 additions & 0 deletions
250
plugins/in_prometheus_remote_write/in_prometheus_remote_write.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | ||
|
|
||
| /* Fluent Bit | ||
| * ========== | ||
| * Copyright (C) 2015-2024 The Fluent Bit Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.in_in (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.in_in | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
|
|
||
| #include <fluent-bit/flb_input_plugin.h> | ||
| #include <fluent-bit/flb_downstream.h> | ||
| #include <fluent-bit/flb_network.h> | ||
| #include <fluent-bit/flb_config.h> | ||
|
|
||
| #include "in_prometheus_remote_write.h" | ||
| #include "in_prometheus_remote_write_conn.h" | ||
| #include "in_prometheus_remote_write_prot.h" | ||
| #include "in_prometheus_remote_write_config.h" | ||
|
|
||
| /* | ||
| * For a server event, the collection event means a new client have arrived, we | ||
| * accept the connection and create a new TCP instance which will wait for | ||
| * JSON map messages. | ||
| */ | ||
| static int in_prometheus_remote_write_collect(struct flb_input_instance *ins, | ||
| struct flb_config *config, void *in_context) | ||
| { | ||
| struct flb_connection *connection; | ||
| struct in_prometheus_remote_write_conn *conn; | ||
| struct flb_in_prometheus_remote_write *ctx; | ||
|
|
||
| ctx = in_context; | ||
|
|
||
| connection = flb_downstream_conn_get(ctx->downstream); | ||
|
|
||
| if (connection == NULL) { | ||
| flb_plg_error(ctx->ins, "could not accept new connection"); | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| flb_plg_trace(ctx->ins, "new TCP connection arrived FD=%i", connection->fd); | ||
|
|
||
| conn = in_prometheus_remote_write_conn_add(connection, ctx); | ||
|
|
||
| if (conn == NULL) { | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int in_prometheus_remote_write_init(struct flb_input_instance *ins, | ||
| struct flb_config *config, void *data) | ||
| { | ||
| unsigned short int port; | ||
| int ret; | ||
| struct flb_in_prometheus_remote_write *ctx; | ||
|
|
||
| (void) data; | ||
|
|
||
| /* Create context and basic conf */ | ||
| ctx = in_prometheus_remote_write_config_create(ins); | ||
| if (!ctx) { | ||
| return -1; | ||
| } | ||
| ctx->collector_id = -1; | ||
|
|
||
| /* Populate context with config map defaults and incoming properties */ | ||
| ret = flb_input_config_map_set(ins, (void *) ctx); | ||
| if (ret == -1) { | ||
| flb_plg_error(ctx->ins, "configuration error"); | ||
| in_prometheus_remote_write_config_destroy(ctx); | ||
| return -1; | ||
| } | ||
|
|
||
| /* Set the context */ | ||
| flb_input_set_context(ins, ctx); | ||
|
|
||
| port = (unsigned short int) strtoul(ctx->tcp_port, NULL, 10); | ||
|
|
||
| if (ctx->enable_http2) { | ||
| ret = flb_http_server_init(&ctx->http_server, | ||
| HTTP_PROTOCOL_AUTODETECT, | ||
| FLB_HTTP_SERVER_FLAG_AUTO_INFLATE, | ||
| NULL, | ||
| ins->host.listen, | ||
| ins->host.port, | ||
| ins->tls, | ||
| ins->flags, | ||
| &ins->net_setup, | ||
| flb_input_event_loop_get(ins), | ||
| ins->config, | ||
| (void *) ctx); | ||
|
|
||
| if (ret != 0) { | ||
| flb_plg_error(ctx->ins, | ||
| "could not initialize http server on %s:%u. Aborting", | ||
| ins->host.listen, ins->host.port); | ||
|
|
||
| in_prometheus_remote_write_config_destroy(ctx); | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| ret = flb_http_server_start(&ctx->http_server); | ||
|
|
||
| if (ret != 0) { | ||
| flb_plg_error(ctx->ins, | ||
| "could not start http server on %s:%u. Aborting", | ||
| ins->host.listen, ins->host.port); | ||
|
|
||
| in_prometheus_remote_write_config_destroy(ctx); | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| ctx->http_server.request_callback = in_prometheus_remote_write_prot_handle_ng; | ||
|
|
||
| flb_input_downstream_set(ctx->http_server.downstream, ctx->ins); | ||
| } | ||
| else { | ||
| ctx->downstream = flb_downstream_create(FLB_TRANSPORT_TCP, | ||
| ins->flags, | ||
| ctx->listen, | ||
| port, | ||
| ins->tls, | ||
| config, | ||
| &ins->net_setup); | ||
|
|
||
| if (ctx->downstream == NULL) { | ||
| flb_plg_error(ctx->ins, | ||
| "could not initialize downstream on %s:%s. Aborting", | ||
| ctx->listen, ctx->tcp_port); | ||
|
|
||
| in_prometheus_remote_write_config_destroy(ctx); | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| flb_input_downstream_set(ctx->downstream, ctx->ins); | ||
|
|
||
| /* Collect upon data available on the standard input */ | ||
| ret = flb_input_set_collector_socket(ins, | ||
| in_prometheus_remote_write_collect, | ||
| ctx->downstream->server_fd, | ||
| config); | ||
| if (ret == -1) { | ||
| flb_plg_error(ctx->ins, "Could not set collector for IN_TCP input plugin"); | ||
| in_prometheus_remote_write_config_destroy(ctx); | ||
| return -1; | ||
| } | ||
|
|
||
| ctx->collector_id = ret; | ||
| } | ||
|
|
||
| flb_plg_info(ctx->ins, "listening on %s:%s", ctx->listen, ctx->tcp_port); | ||
|
|
||
| if (ctx->successful_response_code != 200 && | ||
| ctx->successful_response_code != 201 && | ||
| ctx->successful_response_code != 204) { | ||
| flb_plg_error(ctx->ins, "%d is not supported response code. Use default 201", | ||
| ctx->successful_response_code); | ||
| ctx->successful_response_code = 201; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int in_prometheus_remote_write_exit(void *data, struct flb_config *config) | ||
| { | ||
| struct flb_in_prometheus_remote_write *ctx; | ||
|
|
||
| (void) config; | ||
|
|
||
| ctx = data; | ||
|
|
||
| if (ctx != NULL) { | ||
| in_prometheus_remote_write_config_destroy(ctx); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /* Configuration properties map */ | ||
| static struct flb_config_map config_map[] = { | ||
| { | ||
| FLB_CONFIG_MAP_BOOL, "http2", "true", | ||
| 0, FLB_TRUE, offsetof(struct flb_in_prometheus_remote_write, enable_http2), | ||
| NULL | ||
| }, | ||
|
|
||
| { | ||
| FLB_CONFIG_MAP_SIZE, "buffer_max_size", HTTP_BUFFER_MAX_SIZE, | ||
| 0, FLB_TRUE, offsetof(struct flb_in_prometheus_remote_write, buffer_max_size), | ||
| "" | ||
| }, | ||
|
|
||
| { | ||
| FLB_CONFIG_MAP_SIZE, "buffer_chunk_size", HTTP_BUFFER_CHUNK_SIZE, | ||
| 0, FLB_TRUE, offsetof(struct flb_in_prometheus_remote_write, buffer_chunk_size), | ||
| "" | ||
| }, | ||
|
|
||
| { | ||
| FLB_CONFIG_MAP_STR, "uri", NULL, | ||
| 0, FLB_TRUE, offsetof(struct flb_in_prometheus_remote_write, uri), | ||
| "Specify an optional HTTP URI for the target web server, e.g: /something" | ||
| }, | ||
|
|
||
| { | ||
| FLB_CONFIG_MAP_BOOL, "tag_from_uri", "true", | ||
| 0, FLB_TRUE, offsetof(struct flb_in_prometheus_remote_write, tag_from_uri), | ||
| "If true, tag will be created from uri. e.g. v1_metrics from /v1/metrics ." | ||
| }, | ||
| { | ||
| FLB_CONFIG_MAP_INT, "successful_response_code", "201", | ||
| 0, FLB_TRUE, offsetof(struct flb_in_prometheus_remote_write, successful_response_code), | ||
| "Set successful response code. 200, 201 and 204 are supported." | ||
| }, | ||
|
|
||
| /* EOF */ | ||
| {0} | ||
| }; | ||
|
|
||
| /* Plugin reference */ | ||
| struct flb_input_plugin in_prometheus_remote_write_plugin = { | ||
| .name = "prometheus_remote_write", | ||
| .description = "Prometheus Remote Write input", | ||
| .cb_init = in_prometheus_remote_write_init, | ||
| .cb_pre_run = NULL, | ||
| .cb_collect = in_prometheus_remote_write_collect, | ||
| .cb_flush_buf = NULL, | ||
| .cb_pause = NULL, | ||
| .cb_resume = NULL, | ||
| .cb_exit = in_prometheus_remote_write_exit, | ||
| .config_map = config_map, | ||
| .flags = FLB_INPUT_NET_SERVER | FLB_IO_OPT_TLS | ||
| }; |
60 changes: 60 additions & 0 deletions
60
plugins/in_prometheus_remote_write/in_prometheus_remote_write.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | ||
|
|
||
| /* Fluent Bit | ||
| * ========== | ||
| * Copyright (C) 2015-2024 The Fluent Bit Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef FLB_IN_PROMETHEUS_REMOTE_WRITE_H | ||
| #define FLB_IN_PROMETHEUS_REMOTE_WRITE_H | ||
|
|
||
| #include <fluent-bit/flb_config.h> | ||
| #include <fluent-bit/flb_input.h> | ||
| #include <fluent-bit/flb_utils.h> | ||
|
|
||
| #include <monkey/monkey.h> | ||
| #include <fluent-bit/http_server/flb_http_server.h> | ||
|
|
||
| #define HTTP_BUFFER_MAX_SIZE "4M" | ||
| #define HTTP_BUFFER_CHUNK_SIZE "512K" | ||
|
|
||
| struct flb_in_prometheus_remote_write { | ||
| int successful_response_code; | ||
| flb_sds_t listen; | ||
| flb_sds_t tcp_port; | ||
| int tag_from_uri; | ||
|
|
||
| struct flb_input_instance *ins; | ||
|
|
||
| /* HTTP URI */ | ||
| char *uri; | ||
|
|
||
| /* New gen HTTP server */ | ||
| int enable_http2; | ||
| struct flb_http_server http_server; | ||
|
|
||
| /* Legacy HTTP server */ | ||
| size_t buffer_max_size; /* Maximum buffer size */ | ||
| size_t buffer_chunk_size; /* Chunk allocation size */ | ||
|
|
||
| int collector_id; /* Listener collector id */ | ||
| struct flb_downstream *downstream; /* Client manager */ | ||
| struct mk_list connections; /* linked list of connections */ | ||
|
|
||
| struct mk_server *server; | ||
| }; | ||
|
|
||
|
|
||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.