Skip to content

Commit b6810ab

Browse files
committed
plugin_alias: Implement a mechanism of plugin aliases
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent 66ffbe4 commit b6810ab

3 files changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2015-2026 The Fluent Bit Authors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#ifndef FLB_PLUGIN_ALIAS_H
21+
#define FLB_PLUGIN_ALIAS_H
22+
23+
#include <stddef.h>
24+
25+
/*
26+
* Returned by flb_plugin_alias_rewrite() when an alias exists but an internal
27+
* error prevents generating a rewritten string.
28+
*/
29+
#define FLB_PLUGIN_ALIAS_ERR ((char *) -1)
30+
31+
/*
32+
* Returns the canonical plugin name for alias_name when a mapping exists,
33+
* otherwise returns NULL.
34+
*/
35+
const char *flb_plugin_alias_get(int plugin_type, const char *alias_name,
36+
size_t alias_name_length);
37+
38+
/*
39+
* Rewrites plugin_reference when it starts with a known alias.
40+
*
41+
* Return values:
42+
* - NULL: no rewrite needed
43+
* - FLB_PLUGIN_ALIAS_ERR: rewrite needed but failed
44+
* - allocated string: rewritten plugin reference (caller must free)
45+
*/
46+
char *flb_plugin_alias_rewrite(int plugin_type, const char *plugin_reference);
47+
48+
#endif

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ set(src
6767
flb_crypto.c
6868
flb_random.c
6969
flb_plugin.c
70+
flb_plugin_alias.c
7071
flb_gzip.c
7172
flb_snappy.c
7273
flb_zstd.c

src/flb_plugin_alias.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2015-2026 The Fluent Bit Authors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#include <stdio.h>
21+
#include <string.h>
22+
23+
#include <fluent-bit/flb_mem.h>
24+
#include <fluent-bit/flb_config.h>
25+
#include <fluent-bit/flb_plugin.h>
26+
#include <fluent-bit/flb_plugin_alias.h>
27+
28+
struct flb_plugin_alias_entry {
29+
int plugin_type;
30+
const char *alias_name;
31+
const char *plugin_name;
32+
};
33+
34+
/*
35+
* Table that maps user-facing aliases to plugin short names.
36+
*
37+
* Keep this table focused on backwards/forwards compatibility names where the
38+
* historical short name is still used internally by the plugin implementation.
39+
*/
40+
static struct flb_plugin_alias_entry plugin_aliases[] = {
41+
{
42+
FLB_PLUGIN_OUTPUT,
43+
"elasticsearch",
44+
"es"
45+
},
46+
{
47+
0,
48+
NULL,
49+
NULL
50+
}
51+
};
52+
53+
static size_t protocol_part_length(const char *plugin_reference)
54+
{
55+
char *separator;
56+
57+
separator = strstr(plugin_reference, "://");
58+
if (separator != NULL && separator != plugin_reference) {
59+
return (size_t) (separator - plugin_reference);
60+
}
61+
62+
return strlen(plugin_reference);
63+
}
64+
65+
const char *flb_plugin_alias_get(int plugin_type, const char *alias_name,
66+
size_t alias_name_length)
67+
{
68+
int index;
69+
struct flb_plugin_alias_entry *entry;
70+
71+
if (alias_name == NULL || alias_name_length == 0) {
72+
return NULL;
73+
}
74+
75+
for (index = 0; plugin_aliases[index].alias_name != NULL; index++) {
76+
entry = &plugin_aliases[index];
77+
78+
if (entry->plugin_type != plugin_type) {
79+
continue;
80+
}
81+
82+
if (strlen(entry->alias_name) != alias_name_length) {
83+
continue;
84+
}
85+
86+
if (strncasecmp(entry->alias_name, alias_name, alias_name_length) == 0) {
87+
return entry->plugin_name;
88+
}
89+
}
90+
91+
return NULL;
92+
}
93+
94+
char *flb_plugin_alias_rewrite(int plugin_type, const char *plugin_reference)
95+
{
96+
int ret;
97+
size_t reference_length;
98+
size_t protocol_length;
99+
size_t plugin_name_length;
100+
char *rewritten_reference;
101+
const char *plugin_name;
102+
103+
if (plugin_reference == NULL) {
104+
return NULL;
105+
}
106+
107+
protocol_length = protocol_part_length(plugin_reference);
108+
if (protocol_length == 0) {
109+
return NULL;
110+
}
111+
112+
plugin_name = flb_plugin_alias_get(plugin_type, plugin_reference,
113+
protocol_length);
114+
if (plugin_name == NULL) {
115+
return NULL;
116+
}
117+
118+
plugin_name_length = strlen(plugin_name);
119+
120+
if (plugin_name_length == protocol_length &&
121+
strncasecmp(plugin_name, plugin_reference, protocol_length) == 0) {
122+
return NULL;
123+
}
124+
125+
reference_length = strlen(plugin_reference);
126+
rewritten_reference = flb_calloc(1, reference_length - protocol_length +
127+
plugin_name_length + 1);
128+
if (rewritten_reference == NULL) {
129+
flb_errno();
130+
return FLB_PLUGIN_ALIAS_ERR;
131+
}
132+
133+
memcpy(rewritten_reference, plugin_name, plugin_name_length);
134+
135+
ret = snprintf(rewritten_reference + plugin_name_length,
136+
reference_length - protocol_length + 1,
137+
"%s", plugin_reference + protocol_length);
138+
if (ret < 0) {
139+
flb_free(rewritten_reference);
140+
return FLB_PLUGIN_ALIAS_ERR;
141+
}
142+
143+
return rewritten_reference;
144+
}

0 commit comments

Comments
 (0)