/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Fluent Bit * ========== * Copyright (C) 2015-2026 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. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* input plugin macro helpers */ #include #ifdef FLB_HAVE_METRICS #define FLB_INPUT_RATE_GATE_NSEC_PER_MSEC 1000000ULL static void flb_input_rate_gate_timer_cancel(struct flb_input_instance *ins); #endif #ifdef FLB_HAVE_CHUNK_TRACE #include #endif /* FLB_HAVE_CHUNK_TRACE */ struct flb_libco_in_params libco_in_param; pthread_key_t libco_in_param_key; static pthread_once_t libco_in_param_once = PTHREAD_ONCE_INIT; static int libco_in_param_init_result = -1; static void libco_in_param_key_destroy(void *data) { flb_free(data); } static void libco_in_param_key_create() { libco_in_param_init_result = pthread_key_create(&libco_in_param_key, libco_in_param_key_destroy); } int flb_input_coro_params_init() { int ret; ret = pthread_once(&libco_in_param_once, libco_in_param_key_create); if (ret != 0 || libco_in_param_init_result != 0) { return -1; } return 0; } #define protcmp(a, b) strncasecmp(a, b, strlen(a)) static void flb_input_ingress_primitives_destroy(struct flb_input_instance *ins) { if (ins == NULL) { return; } pthread_mutex_destroy(&ins->ingress_queue_lock); pthread_cond_destroy(&ins->ingress_queue_space_available); } static int flb_input_ingress_primitives_init(struct flb_input_instance *ins) { int result; pthread_condattr_t attr; result = pthread_mutex_init(&ins->ingress_queue_lock, NULL); if (result != 0) { return -1; } result = pthread_condattr_init(&attr); if (result != 0) { pthread_mutex_destroy(&ins->ingress_queue_lock); return -1; } #if defined(CLOCK_MONOTONIC) && !defined(FLB_SYSTEM_WINDOWS) && !defined(FLB_SYSTEM_MACOS) result = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); if (result != 0) { pthread_condattr_destroy(&attr); pthread_mutex_destroy(&ins->ingress_queue_lock); return -1; } #endif result = pthread_cond_init(&ins->ingress_queue_space_available, &attr); pthread_condattr_destroy(&attr); if (result != 0) { pthread_mutex_destroy(&ins->ingress_queue_lock); return -1; } return 0; } /* * Ring buffer capacity: by default we make space for 1024 entries that each * input instance can use to enqueue data. The capacity can be customized per * input instance through the 'thread.ring_buffer.capacity' property. The value * represents the number of slots and is converted to bytes when the ring buffer * is created. This affects only input plugins running in threaded mode. * * Ring buffer window: the current window size is set to 5% which means that the * ring buffer will emit a flush request whenever the window threshold is reached. * The window percentage can be tuned per input instance using the * 'thread.ring_buffer.window' property. */ #define FLB_INPUT_RING_BUFFER_CAPACITY 1024 #define FLB_INPUT_RING_BUFFER_SIZE (sizeof(void *) * FLB_INPUT_RING_BUFFER_CAPACITY) #define FLB_INPUT_RING_BUFFER_WINDOW (5) #ifdef FLB_HAVE_METRICS #define FLB_INPUT_RATE_WINDOW_DEFAULT "1s" #endif /* config map to register options available for all input plugins */ struct flb_config_map input_global_properties[] = { { FLB_CONFIG_MAP_STR, "tag", "default", 0, FLB_FALSE, 0, "Set a tag for the events generated by this input plugin" }, { FLB_CONFIG_MAP_STR, "log_level", "info", 0, FLB_FALSE, 0, "Specifies the log level for this input plugin. If not set, the plugin " "will use the global log level defined in the 'service' section. If the global " "log level is also not specified, it defaults to 'info'." }, { FLB_CONFIG_MAP_TIME, "log_suppress_interval", "0", 0, FLB_FALSE, 0, "Allows suppression of repetitive log messages plugins that appear similar within a specified " "time interval. It defaults to 0, which means no suppression." }, { FLB_CONFIG_MAP_BOOL, "routable", "true", 0, FLB_FALSE, 0, "If set to 'true', the data generated by the plugin will be routable, meaning that it can be " "forwarded to other plugins or outputs. If set to 'false', the data will be discarded." }, { FLB_CONFIG_MAP_STR, "alias", NULL, 0, FLB_FALSE, 0, "Sets an alias for the input instance. This is useful when running multiple instances of the same " "input plugin. If no alias is specified, a default name will be assigned using the plugin name " "followed by a dot and a sequence number (e.g., cpu.0, cpu.1)." }, { FLB_CONFIG_MAP_SIZE, "mem_buf_limit", "0", 0, FLB_FALSE, 0, "Set a memory buffer limit for the input plugin instance. If the limit is reached, the plugin will " "pause until the buffer is drained. The value is in bytes. If set to 0, the buffer limit is disabled."\ "Note that if the plugin has enabled filesystem buffering, this limit will not apply." }, { FLB_CONFIG_MAP_STR, "storage.type", "memory", 0, FLB_FALSE, 0, "Sets the storage type for this input, one of: filesystem, memory or memrb." }, { FLB_CONFIG_MAP_BOOL, "storage.pause_on_chunks_overlimit", "false", 0, FLB_FALSE, 0, "Enable pausing on an input when they reach their chunks limit" }, { FLB_CONFIG_MAP_BOOL, "threaded", "false", 0, FLB_FALSE, 0, "Enable threading on an input" }, { FLB_CONFIG_MAP_INT, "thread.ring_buffer.capacity", STR(FLB_INPUT_RING_BUFFER_CAPACITY), 0, FLB_FALSE, 0, "Set custom ring buffer capacity when the input runs in threaded mode" }, { FLB_CONFIG_MAP_INT, "thread.ring_buffer.window", STR(FLB_INPUT_RING_BUFFER_WINDOW), 0, FLB_FALSE, 0, "Set custom ring buffer window percentage for threaded inputs" }, #ifdef FLB_HAVE_METRICS { FLB_CONFIG_MAP_TIME, "rate_window", FLB_INPUT_RATE_WINDOW_DEFAULT, 0, FLB_FALSE, 0, "Set input rate window using a time unit (for example: 1s, 1m, 1h). " "The computed rate is always published in per-second units." }, { FLB_CONFIG_MAP_BOOL, "rate_gate", "false", 0, FLB_FALSE, 0, "Enable input rate gate control." }, { FLB_CONFIG_MAP_SIZE, "rate_gate.max_bytes", "0", 0, FLB_FALSE, 0, "Maximum input byte rate per second before pausing ingestion." }, { FLB_CONFIG_MAP_INT, "rate_gate.max_records", "0", 0, FLB_FALSE, 0, "Maximum input record rate per second before pausing ingestion." }, { FLB_CONFIG_MAP_BOOL, "rate_gate.backpressure", "true", 0, FLB_FALSE, 0, "Apply retry, busy chunk, and throttle-deferred route pressure " "when computing effective limits." }, { FLB_CONFIG_MAP_DOUBLE, "rate_gate.resume_ratio", "0.80", 0, FLB_FALSE, 0, "Hysteresis threshold used for resuming input rate gate." }, #endif {0} }; struct mk_list *flb_input_get_global_config_map(struct flb_config *config) { return flb_config_map_create(config, input_global_properties); } static int check_protocol(const char *prot, const char *output) { int len; char *separator; separator = strstr(output, "://"); if (separator != NULL && separator != output) { len = separator - output; } else { len = strlen(output); } if (strlen(prot) != (size_t) len) { return 0; } if (strncasecmp(prot, output, len) != 0) { return 0; } return 1; } static inline int instance_id(struct flb_input_plugin *p, struct flb_config *config) \ { int c = 0; struct mk_list *head; struct flb_input_instance *entry; mk_list_foreach(head, &config->inputs) { entry = mk_list_entry(head, struct flb_input_instance, _head); if (entry->id == c) { c++; } } return c; } /* Generate a new collector ID for the instance in question */ static int collector_id(struct flb_input_instance *ins) { int id = 0; struct flb_input_collector *collector; if (mk_list_is_empty(&ins->collectors) == 0) { return id; } collector = mk_list_entry_last(&ins->collectors, struct flb_input_collector, _head); return (collector->id + 1); } void flb_input_net_default_listener(const char *listen, int port, struct flb_input_instance *ins) { /* Set default network configuration */ if (!ins->host.listen) { ins->host.listen = flb_sds_create(listen); } if (ins->host.port == 0) { ins->host.port = port; } } /* Check input plugin's log level. * Not for core plugins but for Golang plugins. * Golang plugins do not have thread-local flb_worker_ctx information. */ int flb_input_log_check(struct flb_input_instance *ins, int l) { if (ins->log_level < l) { return FLB_FALSE; } return FLB_TRUE; } /* Create an input plugin instance */ struct flb_input_instance *flb_input_new(struct flb_config *config, const char *input, void *data, int public_only) { int id; int ret; int flags = 0; size_t input_name_length; const char *alias_target; const char *input_name; const char *separator; struct mk_list *head; struct flb_input_plugin *plugin = NULL; struct flb_input_instance *instance = NULL; /* use for locking the use of the chunk trace context. */ #ifdef FLB_HAVE_CHUNK_TRACE pthread_mutexattr_t attr = {0}; pthread_mutexattr_init(&attr); #endif if (!input) { return NULL; } input_name = input; /* Prefer an exact registered plugin name over an alias with the same name. */ mk_list_foreach(head, &config->in_plugins) { plugin = mk_list_entry(head, struct flb_input_plugin, _head); if (check_protocol(plugin->name, input_name)) { break; } plugin = NULL; } if (plugin == NULL) { separator = strstr(input, "://"); if (separator != NULL && separator != input) { input_name_length = separator - input; } else { input_name_length = strlen(input); } alias_target = flb_plugin_alias_get(FLB_PLUGIN_INPUT, input, input_name_length); if (alias_target == NULL) { return NULL; } input_name = alias_target; } mk_list_foreach(head, &config->in_plugins) { plugin = mk_list_entry(head, struct flb_input_plugin, _head); if (!check_protocol(plugin->name, input_name)) { plugin = NULL; continue; } /* * Check if the plugin is private and validate the 'public_only' * requirement. */ if (public_only == FLB_TRUE && plugin->flags & FLB_INPUT_PRIVATE) { return NULL; } /* Create plugin instance */ instance = flb_calloc(1, sizeof(struct flb_input_instance)); if (!instance) { flb_errno(); return NULL; } instance->ch_events[0] = FLB_UNINITIALIZED_SOCKET; instance->ch_events[1] = FLB_UNINITIALIZED_SOCKET; instance->http_server_config = flb_calloc(1, sizeof(struct flb_http_server_config)); if (!instance->http_server_config) { flb_errno(); flb_free(instance); return NULL; } flb_http_server_config_init(instance->http_server_config); instance->config = config; /* Get an ID */ id = instance_id(plugin, config); /* Index for log Chunks (hash table) */ instance->ht_log_chunks = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE, 512, 0); if (!instance->ht_log_chunks) { flb_free(instance->http_server_config); flb_free(instance); return NULL; } /* Index for metric Chunks (hash table) */ instance->ht_metric_chunks = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE, 512, 0); if (!instance->ht_metric_chunks) { flb_hash_table_destroy(instance->ht_log_chunks); flb_free(instance->http_server_config); flb_free(instance); return NULL; } /* Index for trace Chunks (hash table) */ instance->ht_trace_chunks = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE, 512, 0); if (!instance->ht_trace_chunks) { flb_hash_table_destroy(instance->ht_log_chunks); flb_hash_table_destroy(instance->ht_metric_chunks); flb_free(instance->http_server_config); flb_free(instance); return NULL; } /* Index for profile Chunks (hash table) */ instance->ht_profile_chunks = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE, 512, 0); if (!instance->ht_profile_chunks) { flb_hash_table_destroy(instance->ht_log_chunks); flb_hash_table_destroy(instance->ht_metric_chunks); flb_hash_table_destroy(instance->ht_trace_chunks); flb_free(instance->http_server_config); flb_free(instance); return NULL; } pthread_mutex_init(&instance->metrics_chunk_lock, NULL); /* format name (with instance id) */ snprintf(instance->name, sizeof(instance->name) - 1, "%s.%i", plugin->name, id); if (plugin->type == FLB_INPUT_PLUGIN_CORE) { instance->context = NULL; } else { struct flb_plugin_input_proxy_context *ctx; ctx = flb_calloc(1, sizeof(struct flb_plugin_input_proxy_context)); if (!ctx) { flb_errno(); pthread_mutex_destroy(&instance->metrics_chunk_lock); flb_hash_table_destroy(instance->ht_log_chunks); flb_hash_table_destroy(instance->ht_metric_chunks); flb_hash_table_destroy(instance->ht_trace_chunks); flb_hash_table_destroy(instance->ht_profile_chunks); flb_free(instance->http_server_config); flb_free(instance); return NULL; } ctx->proxy = plugin->proxy; instance->context = ctx; } /* initialize remaining vars */ instance->alias = NULL; instance->id = id; instance->flags = plugin->flags; instance->test_mode = FLB_FALSE; instance->p = plugin; instance->tag = NULL; instance->tag_len = 0; instance->tag_default = FLB_FALSE; instance->telemetry_metrics_logs_tag_records = -1; instance->routable = FLB_TRUE; instance->data = data; instance->storage = NULL; instance->storage_type = -1; instance->log_level = -1; instance->log_suppress_interval = -1; instance->runs_in_coroutine = FLB_FALSE; /* net */ instance->host.name = NULL; instance->host.address = NULL; instance->host.uri = NULL; instance->host.listen = NULL; instance->host.ipv6 = FLB_FALSE; /* Initialize list heads */ cfl_list_init(&instance->routes_direct); cfl_list_init(&instance->routes); mk_list_init(&instance->tasks); mk_list_init(&instance->chunks); mk_list_init(&instance->collectors); mk_list_init(&instance->input_coro_list); mk_list_init(&instance->input_coro_list_destroy); mk_list_init(&instance->downstreams); mk_list_init(&instance->upstreams); /* Initialize properties list */ flb_kv_init(&instance->properties); flb_kv_init(&instance->net_properties); flb_kv_init(&instance->http_server_properties); flb_kv_init(&instance->oauth2_jwt_properties); mk_list_init(&instance->ingress_queue); ret = flb_input_ingress_primitives_init(instance); if (ret != 0) { pthread_mutex_destroy(&instance->metrics_chunk_lock); if (instance->ht_log_chunks) { flb_hash_table_destroy(instance->ht_log_chunks); } if (instance->ht_metric_chunks) { flb_hash_table_destroy(instance->ht_metric_chunks); } if (instance->ht_trace_chunks) { flb_hash_table_destroy(instance->ht_trace_chunks); } if (instance->ht_profile_chunks) { flb_hash_table_destroy(instance->ht_profile_chunks); } if (plugin->type != FLB_INPUT_PLUGIN_CORE && instance->context) { flb_free(instance->context); } flb_free(instance->http_server_config); flb_free(instance); return NULL; } instance->ingress_queue_channels[0] = -1; instance->ingress_queue_channels[1] = -1; instance->ingress_queue_enabled = FLB_FALSE; instance->ingress_queue_collector_id = -1; instance->ingress_queue_signal_pending = FLB_FALSE; instance->ingress_queue_pending_events = 0; instance->ingress_queue_pending_bytes = 0; instance->ingress_queue_event_limit = FLB_HTTP_SERVER_INGRESS_QUEUE_EVENT_LIMIT; instance->ingress_queue_byte_limit = FLB_HTTP_SERVER_INGRESS_QUEUE_BYTE_LIMIT; /* Plugin use networking */ if (plugin->flags & (FLB_INPUT_NET | FLB_INPUT_NET_SERVER)) { if (strstr(input, "://") != NULL) { ret = flb_net_host_set(plugin->name, &instance->host, input); } else { ret = flb_net_host_set(plugin->name, &instance->host, input_name); } if (ret != 0) { if (instance->ht_log_chunks) { flb_hash_table_destroy(instance->ht_log_chunks); } if (instance->ht_metric_chunks) { flb_hash_table_destroy(instance->ht_metric_chunks); } if (instance->ht_trace_chunks) { flb_hash_table_destroy(instance->ht_trace_chunks); } if (instance->ht_profile_chunks) { flb_hash_table_destroy(instance->ht_profile_chunks); } if (plugin->type != FLB_INPUT_PLUGIN_CORE && instance->context) { flb_free(instance->context); } flb_input_ingress_primitives_destroy(instance); flb_free(instance->http_server_config); flb_free(instance); return NULL; } } /* initialize lock for access to chunk trace context. */ #ifdef FLB_HAVE_CHUNK_TRACE pthread_mutex_init(&instance->chunk_trace_lock, &attr); #endif /* Parent plugin flags */ flags = instance->flags; if (flags & FLB_IO_TCP) { instance->use_tls = FLB_FALSE; } else if (flags & FLB_IO_TLS) { instance->use_tls = FLB_TRUE; } else if (flags & FLB_IO_OPT_TLS) { /* TLS must be enabled manually in the config */ instance->use_tls = FLB_FALSE; instance->flags |= FLB_IO_TLS; } #ifdef FLB_HAVE_TLS instance->tls = NULL; instance->tls_debug = -1; instance->tls_verify = FLB_TRUE; instance->tls_verify_client = FLB_FALSE; instance->tls_verify_hostname = FLB_FALSE; instance->tls_vhost = NULL; instance->tls_ca_path = NULL; instance->tls_ca_file = NULL; instance->tls_crt_file = NULL; instance->tls_key_file = NULL; instance->tls_key_passwd = NULL; instance->tls_crl_file = NULL; #endif /* Plugin requires a co-routine context ? */ if (plugin->flags & FLB_INPUT_CORO) { instance->runs_in_coroutine = FLB_TRUE; } /* Plugin will run in a separate thread ? */ if (plugin->flags & FLB_INPUT_THREADED) { instance->is_threaded = FLB_TRUE; } /* set default ring buffer size and window */ instance->ring_buffer_size = FLB_INPUT_RING_BUFFER_SIZE; instance->ring_buffer_window = FLB_INPUT_RING_BUFFER_WINDOW; /* allocate a ring buffer */ instance->rb = flb_ring_buffer_create(instance->ring_buffer_size); if (!instance->rb) { flb_error("instance %s could not initialize ring buffer", flb_input_name(instance)); flb_hash_table_destroy(instance->ht_log_chunks); flb_hash_table_destroy(instance->ht_metric_chunks); flb_hash_table_destroy(instance->ht_trace_chunks); flb_hash_table_destroy(instance->ht_profile_chunks); if (plugin->type != FLB_INPUT_PLUGIN_CORE) { flb_free(instance->context); } flb_sds_destroy(instance->host.name); flb_sds_destroy(instance->host.address); flb_uri_destroy(instance->host.uri); flb_sds_destroy(instance->host.listen); flb_input_ingress_primitives_destroy(instance); flb_free(instance->http_server_config); flb_free(instance); return NULL; } instance->mem_buf_status = FLB_INPUT_RUNNING; instance->mem_buf_limit = 0; instance->mem_chunks_size = 0; #ifdef FLB_HAVE_METRICS instance->rate_window_size = FLB_NSEC_IN_SEC; instance->rate_gate_enabled = FLB_FALSE; instance->rate_gate_status = FLB_INPUT_RUNNING; instance->rate_gate_use_backpressure = FLB_TRUE; instance->rate_gate_resume_ratio = 0.80; instance->rate_gate_max_bytes = 0; instance->rate_gate_max_records = 0; instance->rate_gate_busy_chunks = 0; instance->rate_gate_retry_attempts = 0; instance->rate_gate_deferred_routes = 0; instance->rate_gate_timer = NULL; #endif instance->storage_buf_status = FLB_INPUT_RUNNING; mk_list_add(&instance->_head, &config->inputs); /* processor instance */ instance->processor = flb_processor_create(config, instance->name, instance, FLB_PLUGIN_INPUT); /* Tests */ instance->test_formatter.callback = plugin->test_formatter.callback; } return instance; } static inline int prop_key_check(const char *key, const char *kv, int k_len) { int len; len = strlen(key); if (strncasecmp(key, kv, k_len) == 0 && len == k_len) { return 0; } return -1; } struct flb_input_instance *flb_input_get_instance(struct flb_config *config, int ins_id) { struct mk_list *head; struct flb_input_instance *ins; mk_list_foreach(head, &config->inputs) { ins = mk_list_entry(head, struct flb_input_instance, _head); if (ins->id == ins_id) { break; } ins = NULL; } if (!ins) { return NULL; } return ins; } static void flb_input_coro_destroy(struct flb_input_coro *input_coro) { flb_debug("[input coro] destroy coro_id=%i", input_coro->id); mk_list_del(&input_coro->_head); flb_coro_destroy(input_coro->coro); flb_free(input_coro); } int flb_input_coro_finished(struct flb_config *config, int ins_id) { struct mk_list *tmp; struct mk_list *head; struct flb_input_instance *ins; struct flb_input_coro *input_coro; ins = flb_input_get_instance(config, ins_id); if (!ins) { return -1; } /* Look for input coroutines that needs to be destroyed */ mk_list_foreach_safe(head, tmp, &ins->input_coro_list_destroy) { input_coro = mk_list_entry(head, struct flb_input_coro, _head); flb_input_coro_destroy(input_coro); } return 0; } void flb_input_coro_prepare_destroy(struct flb_input_coro *input_coro) { struct flb_input_instance *ins = input_coro->ins; /* move flb_input_coro from 'input_coro_list' to 'input_coro_list_destroy' */ mk_list_del(&input_coro->_head); mk_list_add(&input_coro->_head, &ins->input_coro_list_destroy); } int flb_input_name_exists(const char *name, struct flb_config *config) { struct mk_list *head; struct flb_input_instance *ins; mk_list_foreach(head, &config->inputs) { ins = mk_list_entry(head, struct flb_input_instance, _head); if (strcmp(ins->name, name) == 0) { return FLB_TRUE; } if (ins->alias) { if (strcmp(ins->alias, name) == 0) { return FLB_TRUE; } } } return FLB_FALSE; } struct mk_event_loop *flb_input_event_loop_get(struct flb_input_instance *ins) { struct flb_input_thread_instance *thi; if (flb_input_is_threaded(ins)) { thi = ins->thi; return thi->evl; } return ins->config->evl; } /* Override a configuration property for the given input_instance plugin */ int flb_input_set_property(struct flb_input_instance *ins, const char *k, const char *v) { int len; int ret; #ifdef FLB_HAVE_METRICS int seconds; #endif int enabled; ssize_t limit; flb_sds_t tmp = NULL; #ifdef FLB_HAVE_METRICS char *end; double parsed_ratio; unsigned long long parsed; #endif struct flb_kv *kv; len = strlen(k); /* Resolve environment variables in the provided value */ tmp = flb_env_var_translate(ins->config->env, v); if (tmp) { if (flb_sds_len(tmp) == 0) { flb_sds_destroy(tmp); tmp = NULL; } } /* Check if the key is a known/shared property */ if (prop_key_check("tag", k, len) == 0 && tmp) { flb_utils_set_plugin_string_property("tag", &ins->tag, tmp); ins->tag_len = flb_sds_len(tmp); ins->tag_default = FLB_FALSE; } else if (prop_key_check("log_level", k, len) == 0 && tmp) { ret = flb_log_get_level_str(tmp); flb_sds_destroy(tmp); if (ret == -1) { return -1; } ins->log_level = ret; } else if (prop_key_check("log_suppress_interval", k, len) == 0 && tmp) { ret = flb_utils_time_to_seconds(tmp); flb_sds_destroy(tmp); if (ret == -1) { return -1; } ins->log_suppress_interval = ret; } else if (prop_key_check("routable", k, len) == 0 && tmp) { ins->routable = flb_utils_bool(tmp); flb_sds_destroy(tmp); } else if (prop_key_check("alias", k, len) == 0 && tmp) { flb_utils_set_plugin_string_property("alias", &ins->alias, tmp); } else if (prop_key_check("mem_buf_limit", k, len) == 0 && tmp) { limit = flb_utils_size_to_bytes(tmp); flb_sds_destroy(tmp); if (limit == -1) { return -1; } ins->mem_buf_limit = (size_t) limit; } else if (prop_key_check("listen", k, len) == 0) { flb_utils_set_plugin_string_property("listen", &ins->host.listen, tmp); } else if (prop_key_check("host", k, len) == 0) { flb_utils_set_plugin_string_property("host", &ins->host.name, tmp); } else if (prop_key_check("port", k, len) == 0) { if (tmp) { ins->host.port = atoi(tmp); flb_sds_destroy(tmp); } } else if (prop_key_check("ipv6", k, len) == 0 && tmp) { ins->host.ipv6 = flb_utils_bool(tmp); flb_sds_destroy(tmp); } else if (strncasecmp("net.", k, 4) == 0 && tmp) { kv = flb_kv_item_create(&ins->net_properties, (char *) k, NULL); if (!kv) { if (tmp) { flb_sds_destroy(tmp); } return -1; } kv->val = tmp; } else if (strncasecmp("oauth2", k, 6) == 0 && tmp) { kv = flb_kv_item_create(&ins->oauth2_jwt_properties, (char *) k, NULL); if (!kv) { if (tmp) { flb_sds_destroy(tmp); } return -1; } kv->val = tmp; } else if ((ins->p->flags & FLB_INPUT_HTTP_SERVER) != 0 && flb_http_server_property_is_allowed(k) == FLB_TRUE && tmp != NULL) { kv = flb_kv_item_create(&ins->http_server_properties, (char *) k, NULL); if (!kv) { flb_sds_destroy(tmp); return -1; } kv->val = tmp; } #ifdef FLB_HAVE_TLS else if (prop_key_check("tls", k, len) == 0 && tmp) { ins->use_tls = flb_utils_bool(tmp); if (ins->use_tls == FLB_TRUE && ((ins->flags & FLB_IO_TLS) == 0)) { flb_error("[config] %s does not support TLS", ins->name); flb_sds_destroy(tmp); return -1; } flb_sds_destroy(tmp); } else if (prop_key_check("tls.verify", k, len) == 0 && tmp) { ins->tls_verify = flb_utils_bool(tmp); flb_sds_destroy(tmp); } else if (prop_key_check("tls.verify_client_cert", k, len) == 0 && tmp) { ret = flb_utils_bool(tmp); flb_sds_destroy(tmp); if (ret == -1) { return -1; } ins->tls_verify_client = ret; } else if (prop_key_check("tls.verify_hostname", k, len) == 0 && tmp) { ins->tls_verify_hostname = flb_utils_bool(tmp); flb_sds_destroy(tmp); } else if (prop_key_check("tls.debug", k, len) == 0 && tmp) { ins->tls_debug = atoi(tmp); flb_sds_destroy(tmp); } else if (prop_key_check("tls.vhost", k, len) == 0) { flb_utils_set_plugin_string_property("tls.vhost", &ins->tls_vhost, tmp); } else if (prop_key_check("tls.ca_path", k, len) == 0) { flb_utils_set_plugin_string_property("tls.ca_path", &ins->tls_ca_path, tmp); } else if (prop_key_check("tls.ca_file", k, len) == 0) { flb_utils_set_plugin_string_property("tls.ca_file", &ins->tls_ca_file, tmp); } else if (prop_key_check("tls.crt_file", k, len) == 0) { flb_utils_set_plugin_string_property("tls.crt_file", &ins->tls_crt_file, tmp); } else if (prop_key_check("tls.key_file", k, len) == 0) { flb_utils_set_plugin_string_property("tls.key_file", &ins->tls_key_file, tmp); } else if (prop_key_check("tls.key_passwd", k, len) == 0) { flb_utils_set_plugin_string_property("tls.key_passwd", &ins->tls_key_passwd, tmp); } else if (prop_key_check("tls.crl_file", k, len) == 0) { flb_utils_set_plugin_string_property("tls.crl_file", &ins->tls_crl_file, tmp); } else if (prop_key_check("tls.min_version", k, len) == 0) { flb_utils_set_plugin_string_property("tls.min_version", &ins->tls_min_version, tmp); } else if (prop_key_check("tls.max_version", k, len) == 0) { flb_utils_set_plugin_string_property("tls.max_version", &ins->tls_max_version, tmp); } else if (prop_key_check("tls.ciphers", k, len) == 0) { flb_utils_set_plugin_string_property("tls.ciphers", &ins->tls_ciphers, tmp); } #endif else if (prop_key_check("storage.type", k, len) == 0 && tmp) { /* Set the storage type */ if (strcasecmp(tmp, "filesystem") == 0) { ins->storage_type = FLB_STORAGE_FS; } else if (strcasecmp(tmp, "memory") == 0) { ins->storage_type = FLB_STORAGE_MEM; } else if (strcasecmp(tmp, "memrb") == 0) { ins->storage_type = FLB_STORAGE_MEMRB; } else { flb_sds_destroy(tmp); return -1; } if (ins->storage_type != FLB_STORAGE_FS && ins->storage_pause_on_chunks_overlimit == FLB_TRUE) { flb_debug("[input] storage.pause_on_chunks_overlimit will be " "reset because storage.type is not filesystem"); ins->storage_pause_on_chunks_overlimit = FLB_FALSE; } flb_sds_destroy(tmp); } else if (prop_key_check("threaded", k, len) == 0 && tmp) { enabled = flb_utils_bool(tmp); flb_sds_destroy(tmp); if (enabled == -1) { return -1; } ins->is_threaded = enabled; } else if (prop_key_check("thread.ring_buffer.capacity", k, len) == 0 && tmp) { ret = atoi(tmp); flb_sds_destroy(tmp); if (ret <= 0) { return -1; } ins->ring_buffer_size = (size_t) ret * sizeof(void *); if (ins->rb) { flb_ring_buffer_destroy(ins->rb); ins->rb = flb_ring_buffer_create(ins->ring_buffer_size); if (!ins->rb) { flb_error("instance %s could not initialize ring buffer", flb_input_name(ins)); return -1; } } } else if (prop_key_check("thread.ring_buffer.window", k, len) == 0 && tmp) { ret = atoi(tmp); flb_sds_destroy(tmp); if (ret <= 0 || ret > 100) { flb_error("[input] thread.ring_buffer.window must be between 1 and 100"); return -1; } ins->ring_buffer_window = (uint8_t) ret; } #ifdef FLB_HAVE_METRICS else if (prop_key_check("rate_window", k, len) == 0 && tmp) { seconds = flb_utils_time_to_seconds(tmp); flb_sds_destroy(tmp); if (seconds <= 0) { flb_error("[input] invalid rate_window value"); return -1; } ins->rate_window_size = ((uint64_t) seconds) * FLB_NSEC_IN_SEC; } else if (prop_key_check("rate_gate", k, len) == 0 && tmp) { ret = flb_utils_bool(tmp); flb_sds_destroy(tmp); if (ret == -1) { return -1; } ins->rate_gate_enabled = ret; } else if (prop_key_check("rate_gate.max_bytes", k, len) == 0 && tmp) { limit = flb_utils_size_to_bytes(tmp); flb_sds_destroy(tmp); if (limit < 0) { return -1; } ins->rate_gate_max_bytes = (size_t) limit; } else if (prop_key_check("rate_gate.max_records", k, len) == 0 && tmp) { end = NULL; errno = 0; parsed = strtoull(tmp, &end, 10); if (end == tmp || *end != '\0' || strchr(tmp, '-') != NULL || errno == ERANGE || parsed > SIZE_MAX) { flb_sds_destroy(tmp); return -1; } flb_sds_destroy(tmp); ins->rate_gate_max_records = (size_t) parsed; } else if (prop_key_check("rate_gate.backpressure", k, len) == 0 && tmp) { ret = flb_utils_bool(tmp); flb_sds_destroy(tmp); if (ret == -1) { return -1; } ins->rate_gate_use_backpressure = ret; } else if (prop_key_check("rate_gate.resume_ratio", k, len) == 0 && tmp) { end = NULL; errno = 0; parsed_ratio = strtod(tmp, &end); if (end == tmp || *end != '\0' || errno == ERANGE || !(parsed_ratio > 0.0 && parsed_ratio < 1.0)) { flb_error("[input] rate_gate.resume_ratio must be between 0 and 1"); flb_sds_destroy(tmp); return -1; } flb_sds_destroy(tmp); ins->rate_gate_resume_ratio = parsed_ratio; } #endif else if (prop_key_check("storage.pause_on_chunks_overlimit", k, len) == 0 && tmp) { ret = flb_utils_bool(tmp); flb_sds_destroy(tmp); if (ret == -1) { return -1; } ins->storage_pause_on_chunks_overlimit = ret; } else { /* * Create the property, we don't pass the value since we will * map it directly to avoid an extra memory allocation. */ if (flb_config_map_property_has_dynamic_env(ins->p->config_map, k) == FLB_TRUE) { if (tmp) { flb_sds_destroy(tmp); } tmp = flb_sds_create(v); if (!tmp) { return -1; } } kv = flb_kv_item_create(&ins->properties, (char *) k, NULL); if (!kv) { if (tmp) { flb_sds_destroy(tmp); } return -1; } kv->val = tmp; } return 0; } const char *flb_input_get_property(const char *key, struct flb_input_instance *ins) { return flb_config_prop_get(key, &ins->properties); } #ifdef FLB_HAVE_METRICS void *flb_input_get_cmt_instance(struct flb_input_instance *ins) { return (void *)ins->cmt; } #endif /* Return an instance name or alias */ const char *flb_input_name(struct flb_input_instance *ins) { if (ins->alias) { return ins->alias; } return ins->name; } void flb_input_instance_destroy(struct flb_input_instance *ins) { struct mk_list *tmp; struct mk_list *head; struct flb_input_collector *collector; /* Threaded owners already stopped their processors before joining. */ if (!flb_input_is_threaded(ins)) { flb_processor_input_stop(ins->processor); } flb_input_ingress_destroy(ins); #ifdef FLB_HAVE_METRICS flb_input_rate_gate_timer_cancel(ins); #endif if (ins->alias) { flb_sds_destroy(ins->alias); } /* Remove URI context */ if (ins->host.uri) { flb_uri_destroy(ins->host.uri); } if (ins->host.name) { flb_sds_destroy(ins->host.name); } if (ins->host.address) { flb_sds_destroy(ins->host.address); } if (ins->host.listen) { flb_sds_destroy(ins->host.listen); } #ifdef FLB_HAVE_TLS if (ins->use_tls) { if (ins->tls != NULL) { flb_tls_destroy(ins->tls); } } if (ins->tls_config_map) { flb_config_map_destroy(ins->tls_config_map); } #endif if (ins->tls_vhost) { flb_sds_destroy(ins->tls_vhost); } if (ins->tls_ca_path) { flb_sds_destroy(ins->tls_ca_path); } if (ins->tls_ca_file) { flb_sds_destroy(ins->tls_ca_file); } if (ins->tls_crt_file) { flb_sds_destroy(ins->tls_crt_file); } if (ins->tls_key_file) { flb_sds_destroy(ins->tls_key_file); } if (ins->tls_crl_file) { flb_sds_destroy(ins->tls_crl_file); } if (ins->tls_key_passwd) { flb_sds_destroy(ins->tls_key_passwd); } if (ins->tls_min_version) { flb_sds_destroy(ins->tls_min_version); } if (ins->tls_max_version) { flb_sds_destroy(ins->tls_max_version); } if (ins->tls_ciphers) { flb_sds_destroy(ins->tls_ciphers); } /* release the tag if any */ flb_sds_destroy(ins->tag); /* Let the engine remove any pending task */ flb_engine_destroy_tasks(&ins->tasks); /* release properties */ flb_kv_release(&ins->properties); flb_kv_release(&ins->net_properties); flb_kv_release(&ins->http_server_properties); flb_kv_release(&ins->oauth2_jwt_properties); #ifdef FLB_HAVE_CHUNK_TRACE flb_chunk_trace_context_destroy(ins); #endif /* FLB_HAVE_CHUNK_TRACE */ /* Remove metrics */ #ifdef FLB_HAVE_METRICS if (ins->cmt) { cmt_destroy(ins->cmt); } if (ins->metrics) { flb_metrics_destroy(ins->metrics); } #endif if (ins->storage) { flb_storage_input_destroy(ins); } /* destroy config map */ if (ins->config_map) { flb_config_map_destroy(ins->config_map); } if (ins->net_config_map) { flb_config_map_destroy(ins->net_config_map); } if (ins->http_server_config_map) { flb_config_map_destroy(ins->http_server_config_map); } if (ins->http_server_config) { flb_free(ins->http_server_config); } if (ins->oauth2_jwt_config_map) { flb_config_map_destroy(ins->oauth2_jwt_config_map); } /* hash table for chunks */ if (ins->ht_log_chunks) { flb_hash_table_destroy(ins->ht_log_chunks); ins->ht_log_chunks = NULL; } if (ins->ht_metric_chunks) { flb_hash_table_destroy(ins->ht_metric_chunks); ins->ht_metric_chunks = NULL; } if (ins->ht_trace_chunks) { flb_hash_table_destroy(ins->ht_trace_chunks); ins->ht_trace_chunks = NULL; } if (ins->ht_profile_chunks) { flb_hash_table_destroy(ins->ht_profile_chunks); ins->ht_profile_chunks = NULL; } pthread_mutex_destroy(&ins->metrics_chunk_lock); if (ins->ch_events[0] > 0) { mk_event_closesocket(ins->ch_events[0]); ins->ch_events[0] = FLB_UNINITIALIZED_SOCKET; } if (ins->ch_events[1] > 0) { mk_event_closesocket(ins->ch_events[1]); ins->ch_events[1] = FLB_UNINITIALIZED_SOCKET; } if (ins->ingress_queue_channels[0] > 0) { mk_event_closesocket(ins->ingress_queue_channels[0]); ins->ingress_queue_channels[0] = FLB_UNINITIALIZED_SOCKET; } if (ins->ingress_queue_channels[1] > 0) { mk_event_closesocket(ins->ingress_queue_channels[1]); ins->ingress_queue_channels[1] = FLB_UNINITIALIZED_SOCKET; } /* Collectors */ mk_list_foreach_safe(head, tmp, &ins->collectors) { collector = mk_list_entry(head, struct flb_input_collector, _head); mk_list_del(&collector->_head); flb_input_collector_destroy(collector); } pthread_mutex_destroy(&ins->ingress_queue_lock); pthread_cond_destroy(&ins->ingress_queue_space_available); /* delete storage context */ flb_storage_input_destroy(ins); mk_list_del(&ins->_head); /* ring buffer */ if (ins->rb) { flb_input_chunk_ring_buffer_cleanup(ins); flb_ring_buffer_destroy(ins->rb); } /* processor */ if (ins->processor) { flb_processor_destroy(ins->processor); } flb_free(ins); } int flb_input_coro_id_get(struct flb_input_instance *ins) { int id; int max = (2 << 13) - 1; /* max for 14 bits */ id = ins->input_coro_id; ins->input_coro_id++; /* reset once it reach the maximum allowed */ if (ins->input_coro_id > max) { ins->input_coro_id = 0; } return id; } static int input_instance_channel_events_init(struct flb_input_instance *ins) { int ret; struct mk_event_loop *evl; evl = flb_input_event_loop_get(ins); /* Input event channel: used for co-routines to report return status */ ret = flb_pipe_event_channel_create(evl, &ins->ch_events[0], &ins->ch_events[1], ins); if (ret != 0) { flb_error("could not create events channels for '%s'", flb_input_name(ins)); return -1; } flb_debug("[%s:%s] created event channels: read=%i write=%i", ins->p->name, flb_input_name(ins), ins->ch_events[0], ins->ch_events[1]); /* * Note: flb_pipe_event_channel_create() sets a type = MK_EVENT_NOTIFICATION by * default, we need to overwrite this value so we can do a clean check * into the Engine when the event is triggered. */ ins->event.type = FLB_ENGINE_EV_INPUT; return 0; } int flb_input_net_property_check(struct flb_input_instance *ins, struct flb_config *config) { int ret = 0; /* Get Downstream or Upstream net_setup configmap */ if (ins->p->flags & FLB_INPUT_NET_SERVER) { ins->net_config_map = flb_downstream_get_config_map(config); if (!ins->net_config_map) { flb_input_instance_destroy(ins); return -1; } } else if (ins->p->flags & FLB_INPUT_NET) { ins->net_config_map = flb_upstream_get_config_map(config); if (!ins->net_config_map) { flb_input_instance_destroy(ins); return -1; } } /* * Validate 'net.*' properties: if the plugin use the Downstream interface, * it might receive some networking settings. */ if (mk_list_size(&ins->net_properties) > 0) { ret = flb_config_map_properties_check(ins->p->name, &ins->net_properties, ins->net_config_map); if (ret == -1) { if (config->program_name) { flb_helper("try the command: %s -i %s -h\n", config->program_name, ins->p->name); } return -1; } } return 0; } int flb_input_plugin_property_check(struct flb_input_instance *ins, struct flb_config *config) { int ret = 0; struct mk_list *tmp; struct mk_list *head; struct mk_list *config_map; struct flb_kv *kv; struct flb_kv *moved; struct flb_input_plugin *p = ins->p; if ((p->flags & FLB_INPUT_HTTP_SERVER) != 0) { mk_list_foreach_safe(head, tmp, &ins->properties) { kv = mk_list_entry(head, struct flb_kv, _head); if (flb_http_server_property_is_allowed(kv->key) == FLB_FALSE) { continue; } moved = flb_kv_item_create(&ins->http_server_properties, kv->key, NULL); if (moved == NULL) { return -1; } moved->val = kv->val; kv->val = NULL; mk_list_del(&kv->_head); flb_kv_item_destroy(kv); } } if (p->config_map) { /* * Create a dynamic version of the configmap that will be used by the specific * instance in question. */ config_map = flb_config_map_create(config, p->config_map); if (!config_map) { flb_error("[input] error loading config map for '%s' plugin", p->name); flb_input_instance_destroy(ins); return -1; } ins->config_map = config_map; /* Validate incoming properties against config map */ ret = flb_config_map_properties_check(ins->p->name, &ins->properties, ins->config_map); if (ret == -1) { if (config->program_name) { flb_helper("try the command: %s -i %s -h\n", config->program_name, ins->p->name); } return -1; } } return 0; } int flb_input_oauth2_jwt_property_check(struct flb_input_instance *ins, struct flb_config *config) { #ifdef FLB_HAVE_TLS int ret = 0; /* Get OAuth2 JWT configmap */ ins->oauth2_jwt_config_map = flb_oauth2_jwt_get_config_map(config); if (!ins->oauth2_jwt_config_map) { flb_input_instance_destroy(ins); return -1; } /* * Validate 'oauth2*' properties: if the plugin uses OAuth2 JWT, * it might receive OAuth2 JWT settings. */ if (mk_list_size(&ins->oauth2_jwt_properties) > 0) { ret = flb_config_map_properties_check(ins->p->name, &ins->oauth2_jwt_properties, ins->oauth2_jwt_config_map); if (ret == -1) { if (config->program_name) { flb_helper("try the command: %s -i %s -h\n", config->program_name, ins->p->name); } return -1; } } return 0; #else flb_error("OAuth2 JWT requires TLS support"); return -1; #endif } static int flb_input_http_server_property_check(struct flb_input_instance *ins, struct flb_config *config) { int ret = 0; if (ins->http_server_config_map == NULL) { ins->http_server_config_map = flb_http_server_get_config_map(config); if (!ins->http_server_config_map) { flb_input_instance_destroy(ins); return -1; } } if (mk_list_size(&ins->http_server_properties) > 0) { ret = flb_config_map_properties_check(ins->p->name, &ins->http_server_properties, ins->http_server_config_map); if (ret == -1) { if (config->program_name) { flb_helper("try the command: %s -i %s -h\n", config->program_name, ins->p->name); } return -1; } } return 0; } int flb_input_instance_init(struct flb_input_instance *ins, struct flb_config *config) { int ret; struct flb_config *ctx = ins->config; struct flb_input_plugin *p = ins->p; int tls_session_mode; /* Private inputs can be initialized while another input initializes. */ if (ins->initialized == FLB_TRUE) { return 0; } if (ins->log_level == -1 && config->log != NULL) { ins->log_level = config->log->level; } /* Skip pseudo input plugins */ if (!p) { return 0; } #ifdef FLB_HAVE_METRICS uint64_t ts; char *name; int logs_tag_records_enabled; name = (char *) flb_input_name(ins); ts = cfl_time_now(); /* resolve effective tag-records tracking: input override wins over service */ logs_tag_records_enabled = (ins->telemetry_metrics_logs_tag_records != -1) ? ins->telemetry_metrics_logs_tag_records : ctx->telemetry_metrics_logs_tag_records; /* CMetrics */ ins->cmt = cmt_create(); if (!ins->cmt) { flb_error("[input] could not create cmetrics context: %s", flb_input_name(ins)); return -1; } /* * Register generic input plugin metrics * ------------------------------------- */ /* fluentbit_input_bytes_total */ ins->cmt_bytes = \ cmt_counter_create(ins->cmt, "fluentbit", "input", "bytes_total", "Number of input bytes.", 1, (char *[]) {"name"}); cmt_counter_set(ins->cmt_bytes, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_records_total */ ins->cmt_records = \ cmt_counter_create(ins->cmt, "fluentbit", "input", "records_total", "Number of input records.", 1, (char *[]) {"name"}); cmt_counter_set(ins->cmt_records, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_rate_bytes */ ins->cmt_rate_bytes = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "rate_bytes", "Current input bytes per second.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_rate_bytes, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_rate_records */ ins->cmt_rate_records = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "rate_records", "Current input records per second.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_rate_records, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_rate_gate_limited */ ins->cmt_rate_gate_limited = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "rate_gate_limited", "Is the input rate gate currently limiting ingestion?", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_rate_gate_limited, ts, 0, 1, (char *[]) {name}); ins->cmt_rate_gate_busy_chunks = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "rate_gate_busy_chunks", "Busy chunks considered by the input rate gate.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_rate_gate_busy_chunks, ts, 0, 1, (char *[]) {name}); ins->cmt_rate_gate_retry_attempts = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "rate_gate_retry_attempts", "Retry attempts considered by the input rate gate.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_rate_gate_retry_attempts, ts, 0, 1, (char *[]) {name}); ins->cmt_rate_gate_deferred_routes = cmt_gauge_create(ins->cmt, "fluentbit", "input", "rate_gate_deferred_routes", "Throttle-deferred output routes considered by the input rate gate.", 1, (char *[]) {"name"}); if (ins->cmt_rate_gate_deferred_routes != NULL) { cmt_gauge_set(ins->cmt_rate_gate_deferred_routes, ts, 0, 1, (char *[]) {name}); } if (logs_tag_records_enabled == FLB_TRUE) { /* fluentbit_input_logs_tag_records_total */ ins->cmt_logs_tag_records = \ cmt_counter_create(ins->cmt, "fluentbit", "input", "logs_tag_records_total", "Number of input log records by tag.", 2, (char *[]) {"name", "tag"}); if (!ins->cmt_logs_tag_records) { return -1; } /* fluentbit_input_logs_tag_records_untracked_total */ ins->cmt_logs_tag_records_untracked = \ cmt_counter_create(ins->cmt, "fluentbit", "input", "logs_tag_records_untracked_total", "Number of input log records not tracked by tag.", 2, (char *[]) {"name", "reason"}); if (!ins->cmt_logs_tag_records_untracked) { return -1; } } /* fluentbit_input_ingestion_paused */ ins->cmt_ingestion_paused = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "ingestion_paused", "Is the input paused or not?", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_ingestion_paused, ts, 0, 1, (char *[]) {name}); /* Storage Metrics */ if (ctx->storage_metrics == FLB_TRUE) { /* fluentbit_input_storage_overlimit */ ins->cmt_storage_overlimit = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "storage_overlimit", "Is the input memory usage overlimit ?.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_storage_overlimit, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_storage_memory_bytes */ ins->cmt_storage_memory_bytes = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "storage_memory_bytes", "Memory bytes used by the chunks.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_storage_memory_bytes, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_storage_chunks */ ins->cmt_storage_chunks = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "storage_chunks", "Total number of chunks.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_storage_chunks, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_storage_chunks_up */ ins->cmt_storage_chunks_up = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "storage_chunks_up", "Total number of chunks up in memory.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_storage_chunks_up, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_storage_chunks_down */ ins->cmt_storage_chunks_down = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "storage_chunks_down", "Total number of chunks down.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_storage_chunks_down, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_storage_chunks_busy */ ins->cmt_storage_chunks_busy = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "storage_chunks_busy", "Total number of chunks in a busy state.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_storage_chunks_busy, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_storage_chunks_busy_bytes */ ins->cmt_storage_chunks_busy_bytes = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "storage_chunks_busy_bytes", "Total number of bytes used by chunks in a busy state.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_storage_chunks_busy_bytes, ts, 0, 1, (char *[]) {name}); } if (ins->storage_type == FLB_STORAGE_MEMRB) { /* fluentbit_input_memrb_dropped_chunks */ ins->cmt_memrb_dropped_chunks = cmt_counter_create(ins->cmt, "fluentbit", "input", "memrb_dropped_chunks", "Number of memrb dropped chunks.", 1, (char *[]) {"name"}); cmt_counter_set(ins->cmt_memrb_dropped_chunks, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_memrb_dropped_bytes */ ins->cmt_memrb_dropped_bytes = cmt_counter_create(ins->cmt, "fluentbit", "input", "memrb_dropped_bytes", "Number of memrb dropped bytes.", 1, (char *[]) {"name"}); cmt_counter_set(ins->cmt_memrb_dropped_bytes, ts, 0, 1, (char *[]) {name}); } /* fluentbit_input_ring_buffer_writes_total */ ins->cmt_ring_buffer_writes = \ cmt_counter_create(ins->cmt, "fluentbit", "input", "ring_buffer_writes_total", "Number of ring buffer writes.", 1, (char *[]) {"name"}); cmt_counter_set(ins->cmt_ring_buffer_writes, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_ring_buffer_retries_total */ ins->cmt_ring_buffer_retries = \ cmt_counter_create(ins->cmt, "fluentbit", "input", "ring_buffer_retries_total", "Number of ring buffer retries.", 1, (char *[]) {"name"}); cmt_counter_set(ins->cmt_ring_buffer_retries, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_ring_buffer_retry_failures_total */ ins->cmt_ring_buffer_retry_failures = \ cmt_counter_create(ins->cmt, "fluentbit", "input", "ring_buffer_retry_failures_total", "Number of ring buffer retry failures.", 1, (char *[]) {"name"}); cmt_counter_set(ins->cmt_ring_buffer_retry_failures, ts, 0, 1, (char *[]) {name}); if ((p->flags & FLB_INPUT_HTTP_SERVER) != 0) { /* fluentbit_input_http_server_ingress_queue_busy_total */ ins->cmt_ingress_queue_busy = \ cmt_counter_create(ins->cmt, "fluentbit", "input", "http_server_ingress_queue_busy_total", "Number of deferred HTTP server ingress queue busy events.", 1, (char *[]) {"name"}); cmt_counter_set(ins->cmt_ingress_queue_busy, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_http_server_ingress_queue_pending_events */ ins->cmt_ingress_queue_pending_events = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "http_server_ingress_queue_pending_events", "Current number of deferred HTTP server ingress queue events.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_ingress_queue_pending_events, ts, 0, 1, (char *[]) {name}); /* fluentbit_input_http_server_ingress_queue_pending_bytes */ ins->cmt_ingress_queue_pending_bytes = \ cmt_gauge_create(ins->cmt, "fluentbit", "input", "http_server_ingress_queue_pending_bytes", "Current number of deferred HTTP server ingress queue bytes.", 1, (char *[]) {"name"}); cmt_gauge_set(ins->cmt_ingress_queue_pending_bytes, ts, 0, 1, (char *[]) {name}); } /* OLD Metrics */ ins->metrics = flb_metrics_create(name); if (ins->metrics) { flb_metrics_add(FLB_METRIC_N_RECORDS, "records", ins->metrics); flb_metrics_add(FLB_METRIC_N_BYTES, "bytes", ins->metrics); } #endif /* * Before to call the initialization callback, make sure that the received * configuration parameters are valid if the plugin is registering a config map. */ if (flb_input_plugin_property_check(ins, config) == -1) { return -1; } /* * Validate 'oauth2*' properties: if the plugin uses OAuth2 JWT, * it might receive OAuth2 JWT settings. */ if (mk_list_size(&ins->oauth2_jwt_properties) > 0) { if (flb_input_oauth2_jwt_property_check(ins, config) == -1) { return -1; } } if ((p->flags & FLB_INPUT_HTTP_SERVER) != 0 || mk_list_size(&ins->http_server_properties) > 0) { if (flb_input_http_server_property_check(ins, config) == -1) { return -1; } } #ifdef FLB_HAVE_TLS if (ins->use_tls == FLB_TRUE) { if ((p->flags & FLB_INPUT_NET_SERVER) != 0) { if (ins->tls_crt_file == NULL) { flb_error("[input %s] error initializing TLS context " "(certificate file missing)", ins->name); return -1; } else if (ins->tls_key_file == NULL) { flb_error("[input %s] error initializing TLS context " "(private key file missing)", ins->name); return -1; } tls_session_mode = FLB_TLS_SERVER_MODE; } else { tls_session_mode = FLB_TLS_CLIENT_MODE; } ins->tls = flb_tls_create(tls_session_mode, ins->tls_verify, ins->tls_debug, ins->tls_vhost, ins->tls_ca_path, ins->tls_ca_file, ins->tls_crt_file, ins->tls_key_file, ins->tls_key_passwd); if (ins->tls == NULL) { flb_error("[input %s] error initializing TLS context", ins->name); return -1; } if (ins->tls_verify_hostname == FLB_TRUE) { ret = flb_tls_set_verify_hostname(ins->tls, ins->tls_verify_hostname); if (ret == -1) { flb_error("[input %s] error set up to verify hostname in TLS context", ins->name); return -1; } } if (ins->tls_verify_client == FLB_TRUE) { ret = flb_tls_set_verify_client(ins->tls, ins->tls_verify_client); if (ret == -1) { flb_error("[input %s] error set up to verify client certificate in TLS context", ins->name); return -1; } } if (ins->tls_crl_file != NULL) { ret = flb_tls_set_crl_file(ins->tls, ins->tls_crl_file); if (ret != 0) { flb_error("[input %s] error setting up TLS CRL file", ins->name); return -1; } } } struct flb_config_map *m; /* TLS config map (just for 'help' formatting purposes) */ ins->tls_config_map = flb_tls_get_config_map(config); if (ins->tls_config_map == NULL) { return -1; } /* Override first configmap value based on it plugin flag */ m = mk_list_entry_first(ins->tls_config_map, struct flb_config_map, _head); if (p->flags & FLB_IO_TLS) { m->value.val.boolean = FLB_TRUE; } else { m->value.val.boolean = FLB_FALSE; } #endif /* Init network defaults */ flb_net_setup_init(&ins->net_setup); if (flb_input_net_property_check(ins, config) == -1) { return -1; } #ifdef __EMSCRIPTEN__ /* Dedicated input threads are not supported by the browser runtime. */ if (flb_input_is_threaded(ins)) { flb_plg_error(ins, "threaded inputs are unsupported in the browser; set threaded: false"); return -1; } #endif /* Initialize the input */ if (p->cb_init) { flb_plg_info(ins, "initializing"); flb_plg_info(ins, "storage_strategy=%s", flb_storage_get_type(ins->storage_type)); /* Sanity check: all non-dynamic tag input plugins must have a tag */ if (!ins->tag) { flb_input_set_property(ins, "tag", ins->name); ins->tag_default = FLB_TRUE; } if (flb_input_is_threaded(ins)) { /* * Create a thread for a new instance. Now the plugin initialization callback will be invoked and report an early failure * or an 'ok' status, we will wait for that return value on flb_input_thread_instance_get_status() below. */ ret = flb_input_thread_instance_init(config, ins); if (ret != 0) { flb_error("failed initialize input %s", ins->name); return -1; } /* initialize channel events */ ret = input_instance_channel_events_init(ins); if (ret != 0) { flb_error("failed initialize channel events on input %s", ins->name); return -1; } /* register the ring buffer */ ret = flb_ring_buffer_add_event_loop(ins->rb, config->evl, ins->ring_buffer_window); if (ret) { flb_error("failed while registering ring buffer events on input %s", ins->name); return -1; } } else { /* initialize channel events */ ret = input_instance_channel_events_init(ins); if (ret != 0) { flb_error("failed initialize channel events on input %s", ins->name); } ins->notification_channel = config->notification_channels[1]; ins->processor->notification_channel = ins->notification_channel; ret = p->cb_init(ins, config, ins->data); if (ret != 0) { flb_error("failed initialize input %s", ins->name); return -1; } ret = flb_processor_init(ins->processor); if (ret == -1) { flb_error("failed initialize processors for input %s", ins->name); /* cb_init succeeded; release plugin-owned resources before * the caller destroys the input instance on this failure. */ flb_input_instance_exit(ins, config); ins->context = NULL; return -1; } } } ins->initialized = FLB_TRUE; return 0; } int flb_input_instance_pre_run(struct flb_input_instance *ins, struct flb_config *config) { int ret; if (flb_input_is_threaded(ins)) { return flb_input_thread_instance_pre_run(config, ins); } else if (ins->p->cb_pre_run) { ret = ins->p->cb_pre_run(ins, config, ins->context); if (ret == -1) { return -1; } return 0; } return 0; } /* Initialize all inputs */ int flb_input_init_all(struct flb_config *config) { int ret; struct mk_list *tmp; struct mk_list *head; struct flb_input_instance *ins; struct flb_input_plugin *p; /* Initialize thread-id table */ memset(&config->in_table_id, '\0', sizeof(config->in_table_id)); /* Iterate all active input instance plugins */ mk_list_foreach_safe(head, tmp, &config->inputs) { ins = mk_list_entry(head, struct flb_input_instance, _head); p = ins->p; /* Skip pseudo input plugins */ if (!p) { continue; } /* Initialize instance */ ret = flb_input_instance_init(ins, config); if (ret == -1) { flb_input_instance_destroy(ins); return -1; } #ifdef FLB_HAVE_TLS if (ins->tls_min_version != NULL || ins->tls_max_version != NULL) { ret = flb_tls_set_minmax_proto(ins->tls, ins->tls_min_version, ins->tls_max_version); if (ret != 0) { flb_error("[input %s] error setting up minmax protocol version of TLS", ins->name); flb_input_instance_destroy(ins); return -1; } } if (ins->tls_ciphers != NULL) { ret = flb_tls_set_ciphers(ins->tls, ins->tls_ciphers); if (ret != 0) { flb_error("[input %s] error setting up TLS ciphers up to TLSv1.2", ins->name); flb_input_instance_destroy(ins); return -1; } } #endif } return 0; } /* Invoke all pre-run input callbacks */ void flb_input_pre_run_all(struct flb_config *config) { struct mk_list *head; struct flb_input_instance *ins; struct flb_input_plugin *p; mk_list_foreach(head, &config->inputs) { ins = mk_list_entry(head, struct flb_input_instance, _head); p = ins->p; if (!p) { continue; } flb_input_instance_pre_run(ins, config); } } void flb_input_instance_exit(struct flb_input_instance *ins, struct flb_config *config) { struct flb_input_plugin *p; /* if the instance runs in a separate thread, signal the thread */ if (flb_input_is_threaded(ins)) { flb_input_thread_instance_exit(ins); return; } flb_processor_input_stop(ins->processor); p = ins->p; if (p->cb_exit && ins->context) { /* Multi-threaded input plugins use the same function signature for exit callbacks. */ p->cb_exit(ins->context, config); } } void flb_input_exit_all(struct flb_config *config) { struct mk_list *tmp; struct mk_list *head; struct flb_input_instance *ins; struct flb_input_plugin *p; mk_list_foreach_safe_r(head, tmp, &config->inputs) { ins = mk_list_entry(head, struct flb_input_instance, _head); p = ins->p; if (!p) { continue; } flb_input_instance_exit(ins, config); flb_input_instance_destroy(ins); } } /* Check that at least one Input is enabled */ int flb_input_check(struct flb_config *config) { if (mk_list_is_empty(&config->inputs) == 0) { return -1; } return 0; } /* * API for Input plugins * ===================== * Copyright (C) 2015-2026 The Fluent Bit Authors * The Input interface provides a certain number of functions that can be * used by Input plugins to configure it own behavior and request specific * * 1. flb_input_set_context() * * let an Input plugin set a context data reference that can be used * later when invoking other callbacks. * * 2. flb_input_set_collector_time() * * request the Engine to trigger a specific collector callback at a * certain interval time. Note that this callback will run in the main * thread so it computing time must be short, otherwise it will block * the main loop. * * The collector can runs in timeouts of the order of seconds.nanoseconds * * note: 1 Second = 1000000000 Nanosecond * * 3. flb_input_set_collector_event() * * for a registered file descriptor, associate the READ events to a * specified plugin. Every time there is some data to read, the collector * callback will be triggered. Oriented to a file descriptor that already * have information that may be read through iotctl(..FIONREAD..); * * 4. flb_input_set_collector_server() * * it register a collector based on TCP socket events. It register a socket * who did bind() and listen() and for each event on the socket it triggers * the registered callbacks. */ /* Assign an Configuration context to an Input */ void flb_input_set_context(struct flb_input_instance *in, void *context) { in->context = context; } int flb_input_channel_init(struct flb_input_instance *in) { return flb_pipe_create(in->channel); } static struct flb_input_collector *collector_create(int type, struct flb_input_instance *ins, int (*cb) ( struct flb_input_instance *, struct flb_config *, void *), struct flb_config *config) { struct flb_input_collector *coll; struct flb_input_thread_instance *thi; coll = flb_calloc(1, sizeof(struct flb_input_collector)); if (!coll) { flb_errno(); return NULL; } coll->id = collector_id(ins); coll->type = type; coll->running = FLB_FALSE; coll->fd_event = -1; coll->fd_timer = -1; coll->seconds = -1; coll->nanoseconds = -1; coll->cb_collect = cb; coll->instance = ins; MK_EVENT_ZERO(&coll->event); if (flb_input_is_threaded(ins)) { thi = ins->thi; coll->evl = thi->evl; } else { /* Non-threaded inputs must always attach collectors to the owner * engine loop. Using a transient TLS event loop here can orphan * FD-driven collectors created during plugin initialization. */ coll->evl = config->evl; } /* * Collectors created from a threaded input instance are only added to the * instance `collectors` list. For instances in non-threaded mode, they are * added to both lists, the global config collectors list and the instance * list. */ mk_list_add(&coll->_head, &ins->collectors); return coll; } int flb_input_set_collector_time(struct flb_input_instance *ins, int (*cb_collect) (struct flb_input_instance *, struct flb_config *, void *), time_t seconds, long nanoseconds, struct flb_config *config) { struct flb_input_collector *coll; coll = collector_create(FLB_COLLECT_TIME, ins, cb_collect, config); if (!coll) { return -1; } /* specific collector initialization */ coll->seconds = seconds; coll->nanoseconds = nanoseconds; return coll->id; } int flb_input_set_collector_event(struct flb_input_instance *ins, int (*cb_collect) (struct flb_input_instance *, struct flb_config *, void *), flb_pipefd_t fd, struct flb_config *config) { struct flb_input_collector *coll; coll = collector_create(FLB_COLLECT_FD_EVENT, ins, cb_collect, config); if (!coll) { return -1; } /* specific collector initialization */ coll->fd_event = fd; return coll->id; } int flb_input_set_collector_socket(struct flb_input_instance *ins, int (*cb_new_connection) (struct flb_input_instance *, struct flb_config *, void *), flb_pipefd_t fd, struct flb_config *config) { struct flb_input_collector *coll; coll = collector_create(FLB_COLLECT_FD_SERVER, ins, cb_new_connection, config); if (!coll) { return -1; } /* specific collector initialization */ coll->fd_event = fd; return coll->id; } static int collector_start(struct flb_input_collector *coll, struct flb_config *config) { int fd; int ret; struct mk_event *event; if (coll->running == FLB_TRUE) { return 0; } event = &coll->event; event->mask = MK_EVENT_EMPTY; event->status = MK_EVENT_NONE; if (coll->type == FLB_COLLECT_TIME) { fd = mk_event_timeout_create(coll->evl, coll->seconds, coll->nanoseconds, event); if (fd == -1) { flb_error("[input collector] COLLECT_TIME registration failed"); coll->running = FLB_FALSE; return -1; } coll->fd_timer = fd; } else if (coll->type & (FLB_COLLECT_FD_EVENT | FLB_COLLECT_FD_SERVER)) { event->fd = coll->fd_event; ret = mk_event_add(coll->evl, coll->fd_event, FLB_ENGINE_EV_CORE, MK_EVENT_READ, event); if (ret == -1) { flb_error("[input collector] COLLECT_EVENT registration failed"); mk_event_closesocket(coll->fd_event); coll->running = FLB_FALSE; return -1; } } coll->running = FLB_TRUE; return 0; } int flb_input_collector_start(int coll_id, struct flb_input_instance *in) { int ret; struct mk_list *head; struct flb_input_collector *coll; mk_list_foreach(head, &in->collectors) { coll = mk_list_entry(head, struct flb_input_collector, _head); if (coll->id == coll_id) { ret = collector_start(coll, in->config); if (ret == -1) { flb_error("[input] error starting collector #%i: %s", coll_id, in->name); } return ret; } } return -1; } #ifdef FLB_HAVE_METRICS static void flb_input_rate_gate_effective_limit(struct flb_input_instance *ins, double max_limit, int for_resume, double *effective_limit) { double pressure; double limit; limit = max_limit; if (ins->rate_gate_use_backpressure == FLB_TRUE && limit > 0.0) { pressure = (double) (ins->rate_gate_busy_chunks - ins->rate_gate_busy_deferred_chunks) + (double) ins->rate_gate_retry_attempts + (double) ins->rate_gate_deferred_routes; limit /= pressure + 1.0; } if (for_resume == FLB_TRUE) { limit *= ins->rate_gate_resume_ratio; } *effective_limit = limit; } static int flb_input_rate_gate_is_limited(struct flb_input_instance *ins) { double current_window_bytes_rate; double current_window_records_rate; double effective_max_bytes; double effective_max_records; double window_seconds; uint64_t window_size; if (ins == NULL || ins->rate_gate_enabled != FLB_TRUE) { return FLB_FALSE; } /* * 1. Record-based limit check */ flb_input_rate_gate_effective_limit(ins, (double) ins->rate_gate_max_records, FLB_FALSE, &effective_max_records); if (ins->rate_gate_max_records > 0) { window_size = ins->rate_window_size; if (window_size == 0) { window_seconds = 1.0; } else { window_seconds = (double) window_size / (double) FLB_NSEC_IN_SEC; if (window_seconds <= 0.0) { window_seconds = 1.0; } } current_window_records_rate = (double) ins->rate_window_records / window_seconds; /* Check the accumulated count in the active fixed window. */ if (current_window_records_rate > effective_max_records) { return FLB_TRUE; } } /* * 2. Byte-based limit check */ flb_input_rate_gate_effective_limit(ins, (double) ins->rate_gate_max_bytes, FLB_FALSE, &effective_max_bytes); if (ins->rate_gate_max_bytes > 0) { window_size = ins->rate_window_size; if (window_size == 0) { window_seconds = 1.0; } else { window_seconds = (double) window_size / (double) FLB_NSEC_IN_SEC; if (window_seconds <= 0.0) { window_seconds = 1.0; } } current_window_bytes_rate = (double) ins->rate_window_bytes / window_seconds; /* Check the accumulated bytes in the active fixed window. */ if (current_window_bytes_rate > effective_max_bytes) { return FLB_TRUE; } } return FLB_FALSE; } static int flb_input_rate_gate_can_resume(struct flb_input_instance *ins) { double current_window_bytes_rate; double current_window_records_rate; double resume_max_bytes; double resume_max_records; double window_seconds; uint64_t window_size; if (ins == NULL || ins->rate_gate_enabled != FLB_TRUE) { return FLB_FALSE; } flb_input_rate_gate_effective_limit(ins, (double) ins->rate_gate_max_records, FLB_TRUE, &resume_max_records); if (ins->rate_gate_max_records > 0) { window_size = ins->rate_window_size; if (window_size == 0) { window_seconds = 1.0; } else { window_seconds = (double) window_size / (double) FLB_NSEC_IN_SEC; if (window_seconds <= 0.0) { window_seconds = 1.0; } } current_window_records_rate = (double) ins->rate_window_records / window_seconds; if (current_window_records_rate > resume_max_records) { return FLB_FALSE; } } flb_input_rate_gate_effective_limit(ins, (double) ins->rate_gate_max_bytes, FLB_TRUE, &resume_max_bytes); if (ins->rate_gate_max_bytes > 0) { window_size = ins->rate_window_size; if (window_size == 0) { window_seconds = 1.0; } else { window_seconds = (double) window_size / (double) FLB_NSEC_IN_SEC; if (window_seconds <= 0.0) { window_seconds = 1.0; } } current_window_bytes_rate = (double) ins->rate_window_bytes / window_seconds; if (current_window_bytes_rate > resume_max_bytes) { return FLB_FALSE; } } return FLB_TRUE; } static void flb_input_rate_gate_collect_backpressure(struct flb_input_instance *ins) { struct mk_list *head; struct mk_list *retry_head; struct flb_input_chunk *ic; struct flb_task *task; struct flb_task_retry *retry; if (ins == NULL) { return; } ins->rate_gate_busy_chunks = 0; ins->rate_gate_busy_deferred_chunks = 0; ins->rate_gate_retry_attempts = 0; ins->rate_gate_deferred_routes = 0; if (ins->rate_gate_use_backpressure != FLB_TRUE) { return; } mk_list_foreach(head, &ins->chunks) { ic = mk_list_entry(head, struct flb_input_chunk, _head); if (ic->busy == FLB_TRUE) { if (ins->rate_gate_busy_chunks < SIZE_MAX) { ins->rate_gate_busy_chunks++; if (ic->task && ic->task->deferred_routes > 0) { ins->rate_gate_busy_deferred_chunks++; } } } } mk_list_foreach(head, &ins->tasks) { task = mk_list_entry(head, struct flb_task, _head); /* Deferred routes retain backlog without recording a failed attempt. */ if ((size_t) task->deferred_routes > SIZE_MAX - ins->rate_gate_deferred_routes) { ins->rate_gate_deferred_routes = SIZE_MAX; } else { ins->rate_gate_deferred_routes += (size_t) task->deferred_routes; } mk_list_foreach(retry_head, &task->retries) { retry = mk_list_entry(retry_head, struct flb_task_retry, _head); if (retry->attempts > 0 && (size_t) retry->attempts > SIZE_MAX - ins->rate_gate_retry_attempts) { ins->rate_gate_retry_attempts = SIZE_MAX; } else if (retry->attempts > 0) { ins->rate_gate_retry_attempts += (size_t) retry->attempts; } } } } static void flb_input_rate_gate_report(struct flb_input_instance *ins, uint64_t ts) { char *name; name = (char *) flb_input_name(ins); if (ins->cmt_rate_gate_limited != NULL) { cmt_gauge_set(ins->cmt_rate_gate_limited, ts, ins->rate_gate_status == FLB_INPUT_PAUSED, 1, (char *[]) {name}); } if (ins->cmt_rate_gate_busy_chunks != NULL) { cmt_gauge_set(ins->cmt_rate_gate_busy_chunks, ts, (double) ins->rate_gate_busy_chunks, 1, (char *[]) {name}); } if (ins->cmt_rate_gate_retry_attempts != NULL) { cmt_gauge_set(ins->cmt_rate_gate_retry_attempts, ts, (double) ins->rate_gate_retry_attempts, 1, (char *[]) {name}); } if (ins->cmt_rate_gate_deferred_routes != NULL) { cmt_gauge_set(ins->cmt_rate_gate_deferred_routes, ts, (double) ins->rate_gate_deferred_routes, 1, (char *[]) {name}); } } static void flb_input_rate_gate_timer_cancel(struct flb_input_instance *ins) { struct flb_sched_timer *timer; if (ins == NULL || ins->rate_gate_timer == NULL) { return; } timer = ins->rate_gate_timer; ins->rate_gate_timer = NULL; flb_sched_timer_cb_destroy(timer); } static void flb_input_rate_gate_timer_callback(struct flb_config *config, void *data) { struct flb_input_instance *ins; (void) config; ins = data; ins->rate_gate_timer = NULL; flb_input_rate_gate_maybe_resume(ins); } static int flb_input_rate_gate_timer_schedule(struct flb_input_instance *ins, uint64_t timestamp) { int delay; int ret; uint64_t deadline; uint64_t delay_milliseconds; uint64_t remaining; uint64_t window_size; if (ins == NULL) { return -1; } if (ins->rate_gate_timer != NULL) { return 0; } if (ins->config == NULL || ins->config->sched == NULL) { return -1; } window_size = ins->rate_window_size; if (window_size == 0) { window_size = FLB_NSEC_IN_SEC; } if (window_size > UINT64_MAX - ins->rate_window_start) { deadline = UINT64_MAX; } else { deadline = ins->rate_window_start + window_size; } if (deadline > timestamp) { remaining = deadline - timestamp; } else { remaining = 0; } delay_milliseconds = remaining / FLB_INPUT_RATE_GATE_NSEC_PER_MSEC; if (remaining % FLB_INPUT_RATE_GATE_NSEC_PER_MSEC != 0) { delay_milliseconds++; } if (delay_milliseconds == 0) { delay = 1; } else if (delay_milliseconds > INT_MAX) { delay = INT_MAX; } else { delay = (int) delay_milliseconds; } ret = flb_sched_timer_cb_create(ins->config->sched, FLB_SCHED_TIMER_CB_ONESHOT, delay, flb_input_rate_gate_timer_callback, ins, &ins->rate_gate_timer); if (ret == -1) { ins->rate_gate_timer = NULL; return -1; } return 0; } static void flb_input_rate_gate_resume(struct flb_input_instance *ins) { flb_input_rate_gate_timer_cancel(ins); ins->rate_gate_status = FLB_INPUT_RUNNING; if (ins->mem_buf_status == FLB_INPUT_RUNNING && ins->storage_buf_status == FLB_INPUT_RUNNING && ins->config->is_running == FLB_TRUE && ins->config->is_ingestion_active == FLB_TRUE) { flb_input_resume(ins); flb_info("[input] %s resume (rate gate)", flb_input_name(ins)); } } int flb_input_rate_gate_protect(struct flb_input_instance *ins) { int ret; uint64_t ts; if (ins == NULL || ins->rate_gate_enabled != FLB_TRUE) { return FLB_FALSE; } ts = cfl_time_now(); flb_input_rate_update(ins, ts, 0, 0); flb_input_rate_gate_collect_backpressure(ins); if (flb_input_rate_gate_is_limited(ins) == FLB_FALSE) { flb_input_rate_gate_report(ins, ts); return FLB_FALSE; } if (ins->rate_gate_status != FLB_INPUT_PAUSED) { ret = flb_input_rate_gate_timer_schedule(ins, ts); if (ret == -1) { flb_error("[input] %s could not schedule rate gate recovery; " "continuing ingestion", flb_input_name(ins)); flb_input_rate_gate_report(ins, ts); return FLB_FALSE; } flb_warn("[input] %s paused (rate gate limit exceeded)", flb_input_name(ins)); flb_input_pause(ins); ins->rate_gate_status = FLB_INPUT_PAUSED; } flb_input_rate_gate_report(ins, ts); return FLB_TRUE; } void flb_input_rate_gate_maybe_resume(struct flb_input_instance *ins) { int can_resume; uint64_t ts; if (ins == NULL || ins->rate_gate_enabled != FLB_TRUE) { return; } if (ins->rate_gate_status != FLB_INPUT_PAUSED) { return; } ts = cfl_time_now(); flb_input_rate_update(ins, ts, 0, 0); flb_input_rate_gate_collect_backpressure(ins); can_resume = flb_input_rate_gate_can_resume(ins); if (can_resume == FLB_TRUE) { flb_input_rate_gate_resume(ins); } else if (ins->rate_gate_timer == NULL) { if (flb_input_rate_gate_timer_schedule(ins, ts) == -1) { flb_error("[input] %s could not reschedule rate gate recovery; " "continuing ingestion", flb_input_name(ins)); flb_input_rate_gate_resume(ins); } } flb_input_rate_gate_report(ins, ts); } void flb_input_rate_update(struct flb_input_instance *ins, uint64_t timestamp, size_t records, size_t bytes) { double window_seconds; char *name; uint64_t elapsed; uint64_t elapsed_windows; uint64_t rate_window_size; if (ins == NULL) { return; } if (timestamp == 0) { timestamp = cfl_time_now(); } if (ins->rate_window_start == 0) { ins->rate_window_start = timestamp; } rate_window_size = ins->rate_window_size; if (rate_window_size == 0) { rate_window_size = FLB_NSEC_IN_SEC; } if (timestamp < ins->rate_window_start) { ins->rate_window_start = timestamp; ins->rate_window_records = 0; ins->rate_window_bytes = 0; ins->rate_records = 0.0; ins->rate_bytes = 0.0; } elapsed = timestamp - ins->rate_window_start; if (elapsed >= rate_window_size) { elapsed_windows = elapsed / rate_window_size; window_seconds = (double) rate_window_size / (double) FLB_NSEC_IN_SEC; /* If a complete idle window elapsed, the most recent rate is zero. */ if (elapsed_windows > 1) { ins->rate_records = 0.0; ins->rate_bytes = 0.0; } else { ins->rate_records = (double) ins->rate_window_records / window_seconds; ins->rate_bytes = (double) ins->rate_window_bytes / window_seconds; } name = (char *) flb_input_name(ins); if (ins->cmt_rate_records != NULL) { cmt_gauge_set(ins->cmt_rate_records, timestamp, ins->rate_records, 1, (char *[]) {name}); } if (ins->cmt_rate_bytes != NULL) { cmt_gauge_set(ins->cmt_rate_bytes, timestamp, ins->rate_bytes, 1, (char *[]) {name}); } ins->rate_window_start += elapsed_windows * rate_window_size; ins->rate_window_records = 0; ins->rate_window_bytes = 0; } if (records > SIZE_MAX - ins->rate_window_records) { ins->rate_window_records = SIZE_MAX; } else { ins->rate_window_records += records; } if (bytes > SIZE_MAX - ins->rate_window_bytes) { ins->rate_window_bytes = SIZE_MAX; } else { ins->rate_window_bytes += bytes; } } #endif /* start collectors for main thread, no threaded plugins */ int flb_input_collectors_signal_start(struct flb_input_instance *ins) { int ret; struct mk_list *head; struct flb_input_collector *coll; if (flb_input_is_threaded(ins)) { flb_error("input plugin '%s' is threaded", flb_input_name(ins)); return -1; } mk_list_foreach(head, &ins->collectors) { coll = mk_list_entry(head, struct flb_input_collector, _head); ret = flb_input_collector_start(coll->id, ins); if (ret < 0) { return -1; } } return 0; } /* * Start all collectors: this function is invoked from the engine interface and aim * to start the local collectors and also signal the threaded input plugins to start * their own collectors. */ int flb_input_collectors_start(struct flb_config *config) { int ret; struct mk_list *head; struct flb_input_instance *ins; /* Signal threaded input plugins to start their collectors */ mk_list_foreach(head, &config->inputs) { ins = mk_list_entry(head, struct flb_input_instance, _head); if (flb_input_is_threaded(ins)) { ret = flb_input_thread_collectors_signal_start(ins); if (ret != 0) { flb_error("could not start collectors for threaded plugin '%s'", flb_input_name(ins)); } } else { ret = flb_input_collectors_signal_start(ins); if (ret != 0) { flb_error("could not start collectors for plugin '%s'", flb_input_name(ins)); } } } return 0; } static struct flb_input_collector *get_collector(int id, struct flb_input_instance *in) { struct mk_list *head; struct flb_input_collector *coll; mk_list_foreach(head, &in->collectors) { coll = mk_list_entry(head, struct flb_input_collector, _head); if (coll->id == id) { return coll; } } return NULL; } int flb_input_collector_running(int coll_id, struct flb_input_instance *in) { struct flb_input_collector *coll; coll = get_collector(coll_id, in); if (!coll) { return FLB_FALSE; } return coll->running; } struct mk_event *flb_input_collector_get_event(int coll_id, struct flb_input_instance *ins) { struct flb_input_collector *collector; collector = get_collector(coll_id, ins); if (collector == NULL) { return NULL; } return &collector->event; } /* * TEST: this is a test function that can be used by input plugins to check the * 'pause' and 'resume' callback operations. * * After is invoked, it will schedule an internal event to wake up the instance * after 'sleep_seconds'. */ int flb_input_test_pause_resume(struct flb_input_instance *ins, int sleep_seconds) { /* * This is a fake pause/resume implementation since it's only used to test the plugin * callbacks for such purposes. */ /* pause the instance */ flb_input_pause(ins); /* wait */ sleep(sleep_seconds); /* resume again */ flb_input_resume(ins); return 0; } static void flb_input_ingestion_paused(struct flb_input_instance *ins) { if (ins->ingress_queue_enabled == FLB_TRUE && ins->ingress_queue_collector_id >= 0) { flb_input_collector_pause(ins->ingress_queue_collector_id, ins); } if (ins->cmt_ingestion_paused != NULL) { /* cmetrics */ cmt_gauge_set(ins->cmt_ingestion_paused, cfl_time_now(), 1, 1, (char *[]) { (char *) flb_input_name(ins)}); } } static void flb_input_ingestion_resumed(struct flb_input_instance *ins) { if (ins->ingress_queue_enabled == FLB_TRUE && ins->ingress_queue_collector_id >= 0) { flb_input_collector_resume(ins->ingress_queue_collector_id, ins); } if (ins->cmt_ingestion_paused != NULL) { /* cmetrics */ cmt_gauge_set(ins->cmt_ingestion_paused, cfl_time_now(), 0, 1, (char *[]) {(char *) flb_input_name(ins)}); } } int flb_input_plugin_pause(struct flb_input_instance *ins) { int ret; ret = 0; if (ins->context != NULL && ins->p->cb_pause_checked != NULL) { ret = ins->p->cb_pause_checked(ins->context, ins->config); } else if (ins->context != NULL && ins->p->cb_pause != NULL) { ins->p->cb_pause(ins->context, ins->config); } if (ret == 0) { flb_input_ingestion_paused(ins); } return ret; } int flb_input_plugin_resume(struct flb_input_instance *ins) { int ret; ret = 0; if (ins->context != NULL && ins->p->cb_resume_checked != NULL) { ret = ins->p->cb_resume_checked(ins->context, ins->config); } else if (ins->context != NULL && ins->p->cb_resume != NULL) { ins->p->cb_resume(ins->context, ins->config); } if (ret == 0) { flb_input_ingestion_resumed(ins); } return ret; } int flb_input_pause(struct flb_input_instance *ins) { /* if the instance is already paused, just return */ if (flb_input_buf_paused(ins) && (ins->config == NULL || ins->config->is_shutting_down == FLB_FALSE || (ins->flags & FLB_INPUT_SHUTDOWN_FLUSH) == 0)) { return -1; } if (ins->ingress_queue_enabled == FLB_TRUE && flb_input_is_threaded(ins)) { return flb_input_thread_instance_pause(ins); } /* Pause only if a callback is set and a local context exists */ if ((ins->p->cb_pause || ins->p->cb_pause_checked) && ins->context) { if (flb_input_is_threaded(ins)) { /* signal the thread event loop about the 'pause' operation */ return flb_input_thread_instance_pause(ins); } else { flb_info("[input] pausing %s", flb_input_name(ins)); return flb_input_plugin_pause(ins); } } flb_input_ingestion_paused(ins); return 0; } int flb_input_resume(struct flb_input_instance *ins) { if (ins->ingress_queue_enabled == FLB_TRUE && flb_input_is_threaded(ins)) { return flb_input_thread_instance_resume(ins); } if ((ins->p->cb_resume || ins->p->cb_resume_checked) && ins->context) { if (flb_input_is_threaded(ins)) { /* signal the thread event loop about the 'resume' operation */ return flb_input_thread_instance_resume(ins); } else { flb_info("[input] resume %s", flb_input_name(ins)); return flb_input_plugin_resume(ins); } } flb_input_ingestion_resumed(ins); return 0; } int flb_input_pause_all(struct flb_config *config) { int ret; int paused = 0; struct mk_list *head; struct flb_input_instance *ins; mk_list_foreach(head, &config->inputs) { ins = mk_list_entry(head, struct flb_input_instance, _head); /* * Inform the plugin that is being paused, the source type is set to 'FLB_INPUT_PAUSE_MEM_BUF', no real reason, we * just need to get it paused. */ ret = flb_input_pause(ins); if (ret == 0) { paused++; } } return paused; } int flb_input_collector_destroy(struct flb_input_collector *coll) { struct flb_config *config = coll->instance->config; if (coll->type == FLB_COLLECT_TIME) { if (coll->fd_timer > 0) { mk_event_timeout_destroy(config->evl, &coll->event); } } else { mk_event_del(config->evl, &coll->event); } flb_free(coll); return 0; } int flb_input_collector_pause(int coll_id, struct flb_input_instance *in) { int ret; struct flb_input_collector *coll; coll = get_collector(coll_id, in); if (!coll) { return -1; } if (coll->running == FLB_FALSE) { return 0; } if (coll->type == FLB_COLLECT_TIME) { /* * For a collector time, it's better to just remove the file * descriptor associated to the time out, when resumed a new * one can be created. * * Note: Invalidate fd_timer first in case closing a socket * invokes another event. */ coll->fd_timer = -1; mk_event_timeout_destroy(coll->evl, &coll->event); } else if (coll->type & (FLB_COLLECT_FD_SERVER | FLB_COLLECT_FD_EVENT)) { ret = mk_event_del(coll->evl, &coll->event); if (ret != 0) { flb_warn("[input] cannot disable event for %s", in->name); return -1; } } coll->running = FLB_FALSE; return 0; } int flb_input_collector_delete(int coll_id, struct flb_input_instance *in) { struct flb_input_collector *coll; coll = get_collector(coll_id, in); if (!coll) { return -1; } if (flb_input_collector_pause(coll_id, in) < 0) { return -1; } pthread_mutex_lock(&in->config->collectors_mutex); mk_list_del(&coll->_head); pthread_mutex_unlock(&in->config->collectors_mutex); flb_free(coll); return 0; } int flb_input_collector_resume(int coll_id, struct flb_input_instance *in) { int fd; int ret; struct flb_input_collector *coll; struct flb_config *config; struct mk_event *event; coll = get_collector(coll_id, in); if (!coll) { return -1; } if (coll->running == FLB_TRUE) { flb_error("[input] cannot resume collector %s:%i, already running", in->name, coll_id); return -1; } config = in->config; event = &coll->event; /* If data ingestion has been paused, the collector cannot resume */ if (config->is_ingestion_active == FLB_FALSE) { return 0; } if (coll->type == FLB_COLLECT_TIME) { event->mask = MK_EVENT_EMPTY; event->status = MK_EVENT_NONE; fd = mk_event_timeout_create(coll->evl, coll->seconds, coll->nanoseconds, event); if (fd == -1) { flb_error("[input collector] resume COLLECT_TIME failed"); return -1; } coll->fd_timer = fd; } else if (coll->type & (FLB_COLLECT_FD_SERVER | FLB_COLLECT_FD_EVENT)) { event->fd = coll->fd_event; event->mask = MK_EVENT_EMPTY; event->status = MK_EVENT_NONE; ret = mk_event_add(coll->evl, coll->fd_event, FLB_ENGINE_EV_CORE, MK_EVENT_READ, event); if (ret == -1) { flb_error("[input] cannot disable/pause event for %s", in->name); return -1; } } coll->running = FLB_TRUE; return 0; } int flb_input_collector_fd(flb_pipefd_t fd, struct flb_config *config) { struct mk_list *head; struct mk_list *head_coll; struct flb_input_instance *ins; struct flb_input_collector *collector = NULL; struct flb_input_coro *input_coro; mk_list_foreach(head, &config->inputs) { ins = mk_list_entry(head, struct flb_input_instance, _head); mk_list_foreach(head_coll, &ins->collectors) { collector = mk_list_entry(head_coll, struct flb_input_collector, _head); if (collector->fd_event == fd) { break; } else if (collector->fd_timer == fd) { flb_utils_timer_consume(fd); break; } collector = NULL; } if (collector) { break; } } /* No matches */ if (!collector) { return -1; } if (collector->running == FLB_FALSE) { return -1; } /* Trigger the collector callback */ if (collector->instance->runs_in_coroutine) { input_coro = flb_input_coro_collect(collector, config); if (!input_coro) { return -1; } flb_input_coro_resume(input_coro); } else { if (collector->cb_collect(collector->instance, config, collector->instance->context) == -1) { return -1; } } return 0; } int flb_input_upstream_set(struct flb_upstream *u, struct flb_input_instance *ins) { if (!u) { return -1; } /* * if the input instance runs in threaded mode, make sure to flag the * upstream context so the lists operations are done in thread safe mode */ if (flb_input_is_threaded(ins)) { flb_upstream_thread_safe(u); mk_list_add(&u->base._head, &ins->upstreams); } /* Set networking options 'net.*' received through instance properties */ memcpy(&u->base.net, &ins->net_setup, sizeof(struct flb_net_setup)); return 0; } int flb_input_downstream_set(struct flb_downstream *stream, struct flb_input_instance *ins) { if (stream == NULL) { return -1; } /* * If the input plugin will run in multiple threads, enable * the thread safe mode for the Downstream context. */ if (flb_input_is_threaded(ins)) { flb_stream_enable_thread_safety(&stream->base); mk_list_add(&stream->base._head, &ins->downstreams); } return 0; } int flb_input_handle_notification(struct flb_input_instance *ins) { return 0; }