Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions include/fluent-bit/flb_plugin_alias.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* -*- 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.
*/

#ifndef FLB_PLUGIN_ALIAS_H
#define FLB_PLUGIN_ALIAS_H

#include <stddef.h>

/*
* Returned by flb_plugin_alias_rewrite() when an alias exists but an internal
* error prevents generating a rewritten string.
*/
#define FLB_PLUGIN_ALIAS_ERR ((char *) -1)

struct flb_plugin_alias_entry {
int plugin_type;
const char *alias_name;
const char *plugin_name;
};

/*
* Returns the canonical plugin name for alias_name when a mapping exists,
* otherwise returns NULL.
*/
const char *flb_plugin_alias_get(int plugin_type, const char *alias_name,
size_t alias_name_length);

/*
* Rewrites plugin_reference when it starts with a known alias.
*
* Return values:
* - NULL: no rewrite needed
* - FLB_PLUGIN_ALIAS_ERR: rewrite needed but failed
* - allocated string: rewritten plugin reference (caller must free)
*/
char *flb_plugin_alias_rewrite(int plugin_type, const char *plugin_reference);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

void flb_plugin_alias_set_custom_entries(
const struct flb_plugin_alias_entry *entries);
void flb_plugin_alias_reset_custom_entries(void);

#endif
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set(src
flb_crypto.c
flb_random.c
flb_plugin.c
flb_plugin_alias.c
flb_gzip.c
flb_snappy.c
flb_zstd.c
Expand Down
25 changes: 23 additions & 2 deletions src/flb_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_metrics.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_plugin.h>
#include <fluent-bit/flb_plugin_alias.h>
#include <chunkio/chunkio.h>

#ifdef FLB_HAVE_CHUNK_TRACE
Expand Down Expand Up @@ -435,6 +437,8 @@ struct flb_filter_instance *flb_filter_new(struct flb_config *config,
const char *filter, void *data)
{
int id;
const char *alias_target;
const char *effective_filter_name;
struct mk_list *head;
struct flb_filter_plugin *plugin;
struct flb_filter_instance *instance = NULL;
Expand All @@ -443,14 +447,32 @@ struct flb_filter_instance *flb_filter_new(struct flb_config *config,
return NULL;
}

effective_filter_name = filter;

mk_list_foreach(head, &config->filter_plugins) {
plugin = mk_list_entry(head, struct flb_filter_plugin, _head);
if (strcasecmp(plugin->name, filter) == 0) {
if (strcasecmp(plugin->name, effective_filter_name) == 0) {
break;
}
plugin = NULL;
}

if (plugin == NULL) {
alias_target = flb_plugin_alias_get(FLB_PLUGIN_FILTER, filter,
strlen(filter));
if (alias_target != NULL) {
effective_filter_name = alias_target;

mk_list_foreach(head, &config->filter_plugins) {
plugin = mk_list_entry(head, struct flb_filter_plugin, _head);
if (strcasecmp(plugin->name, effective_filter_name) == 0) {
break;
}
plugin = NULL;
}
}
}

if (!plugin) {
return NULL;
}
Expand Down Expand Up @@ -493,7 +515,6 @@ struct flb_filter_instance *flb_filter_new(struct flb_config *config,

mk_list_init(&instance->properties);
mk_list_add(&instance->_head, &config->filters);

return instance;
}

Expand Down
57 changes: 51 additions & 6 deletions src/flb_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <fluent-bit/flb_ring_buffer.h>
#include <fluent-bit/flb_processor.h>
#include <fluent-bit/flb_oauth2_jwt.h>
#include <fluent-bit/flb_plugin_alias.h>

/* input plugin macro helpers */
#include <fluent-bit/flb_input_plugin.h>
Expand Down Expand Up @@ -197,13 +198,21 @@ struct mk_list *flb_input_get_global_config_map(struct flb_config *config)
static int check_protocol(const char *prot, const char *output)
{
int len;
char *separator;

len = strlen(prot);
if (len != strlen(output)) {
separator = strstr(output, "://");
if (separator != NULL && separator != output) {
len = separator - output;
}
else {
len = strlen(output);
}

if (strlen(prot) != (size_t) len) {
return 0;
}

if (protcmp(prot, output) != 0) {
if (strncasecmp(prot, output, len) != 0) {
return 0;
}

Expand Down Expand Up @@ -275,8 +284,12 @@ struct flb_input_instance *flb_input_new(struct flb_config *config,
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;
struct flb_input_plugin *plugin = NULL;
struct flb_input_instance *instance = NULL;

/* use for locking the use of the chunk trace context. */
Expand All @@ -289,9 +302,36 @@ struct flb_input_instance *flb_input_new(struct flb_config *config,
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)) {
if (!check_protocol(plugin->name, input_name)) {
plugin = NULL;
continue;
}
Expand Down Expand Up @@ -471,7 +511,12 @@ struct flb_input_instance *flb_input_new(struct flb_config *config,

/* Plugin use networking */
if (plugin->flags & (FLB_INPUT_NET | FLB_INPUT_NET_SERVER)) {
ret = flb_net_host_set(plugin->name, &instance->host, input);
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);
Expand Down
20 changes: 13 additions & 7 deletions src/flb_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,26 @@ int flb_net_host_set(const char *plugin_name, struct flb_net_host *host, const c
int len;
int olen;
const char *s, *e, *u;
const char *separator;

memset(host, '\0', sizeof(struct flb_net_host));

olen = strlen(address);
if (olen == strlen(plugin_name)) {
return 0;
separator = strstr(address, "://");
if (separator != NULL && separator != address) {
s = separator + 3;
}
else {
if (olen == strlen(plugin_name)) {
return 0;
}

len = strlen(plugin_name) + 3;
if (olen < len) {
return -1;
len = strlen(plugin_name) + 3;
if (olen < len) {
return -1;
}
s = address + len;
}

s = address + len;
if (*s == '[') {
/* IPv6 address (RFC 3986) */
e = strchr(++s, ']');
Expand Down
65 changes: 56 additions & 9 deletions src/flb_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <fluent-bit/flb_macros.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_plugin.h>
#include <fluent-bit/flb_plugin_alias.h>
#include <fluent-bit/flb_plugin_proxy.h>
#include <fluent-bit/flb_http_client_debug.h>
#include <fluent-bit/flb_output_thread.h>
Expand Down Expand Up @@ -137,12 +138,9 @@ static int check_protocol(const char *prot, const char *output)
len = strlen(output);
}

if (strlen(prot) != len) {
return 0;
}

/* Output plugin match */
if (strncasecmp(prot, output, len) == 0) {
if (strlen(prot) == (size_t) len &&
strncasecmp(prot, output, len) == 0) {
return 1;
}

Expand Down Expand Up @@ -676,17 +674,24 @@ struct flb_output_instance *flb_output_new(struct flb_config *config,
{
int ret = -1;
int flags = 0;
size_t output_name_length;
const char *alias_target;
const char *output_name;
const char *separator;
struct mk_list *head;
struct flb_output_plugin *plugin;
struct flb_output_plugin *plugin = NULL;
struct flb_output_instance *instance = NULL;

if (!output) {
return NULL;
}

output_name = output;

/* Prefer an exact registered plugin name over an alias with the same name. */
mk_list_foreach(head, &config->out_plugins) {
plugin = mk_list_entry(head, struct flb_output_plugin, _head);
if (!check_protocol(plugin->name, output)) {
if (!check_protocol(plugin->name, output_name)) {
plugin = NULL;
continue;
}
Expand All @@ -697,6 +702,34 @@ struct flb_output_instance *flb_output_new(struct flb_config *config,
break;
}

if (plugin == NULL) {
separator = strstr(output, "://");
if (separator != NULL && separator != output) {
output_name_length = separator - output;
}
else {
output_name_length = strlen(output);
}
alias_target = flb_plugin_alias_get(FLB_PLUGIN_OUTPUT, output,
output_name_length);
if (alias_target != NULL) {
output_name = alias_target;

mk_list_foreach(head, &config->out_plugins) {
plugin = mk_list_entry(head, struct flb_output_plugin, _head);
if (!check_protocol(plugin->name, output_name)) {
plugin = NULL;
continue;
}

if (public_only && plugin->flags & FLB_OUTPUT_PRIVATE) {
return NULL;
}
break;
}
}
}

if (!plugin) {
return NULL;
}
Expand Down Expand Up @@ -819,11 +852,25 @@ struct flb_output_instance *flb_output_new(struct flb_config *config,
#endif

if (plugin->flags & FLB_OUTPUT_NET) {
ret = flb_net_host_set(plugin->name, &instance->host, output);
if (strstr(output, "://") != NULL) {
ret = flb_net_host_set(plugin->name, &instance->host, output);
}
else {
ret = flb_net_host_set(plugin->name, &instance->host, output_name);
}

if (ret != 0) {
if (instance->flags & FLB_OUTPUT_SYNCHRONOUS) {
if ((instance->flags & FLB_OUTPUT_SYNCHRONOUS) &&
instance->singleplex_queue != NULL) {
flb_task_queue_destroy(instance->singleplex_queue);
}
if (instance->callback != NULL) {
flb_callback_destroy(instance->callback);
}
if (plugin->type != FLB_OUTPUT_PLUGIN_CORE &&
instance->context != NULL) {
flb_free(instance->context);
}
flb_free(instance->http_server_config);
flb_free(instance);
return NULL;
Expand Down
Loading
Loading