Skip to content

Commit 6b8fbdd

Browse files
committed
lib: add context support API for routing
This patch adds library-level API functions to support context variables in routing configurations. Signed-off-by: Eduardo Silva <[email protected]>
1 parent 1f5e2bc commit 6b8fbdd

File tree

1 file changed

+78
-8
lines changed

1 file changed

+78
-8
lines changed

src/flb_lib.c

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@
3434
#include <fluent-bit/flb_upstream.h>
3535
#include <fluent-bit/flb_downstream.h>
3636
#include <fluent-bit/tls/flb_tls.h>
37+
#include <fluent-bit/config_format/flb_cf.h>
3738

3839
#include <signal.h>
3940
#include <stdarg.h>
41+
#include <sys/stat.h>
42+
#include <unistd.h>
43+
#include <errno.h>
44+
#include <stdlib.h>
4045

4146
#ifdef FLB_HAVE_MTRACE
4247
#include <mcheck.h>
@@ -675,17 +680,81 @@ int flb_service_set(flb_ctx_t *ctx, ...)
675680
/* Load a configuration file that may be used by the input or output plugin */
676681
int flb_lib_config_file(struct flb_lib_ctx *ctx, const char *path)
677682
{
678-
if (access(path, R_OK) != 0) {
683+
struct flb_cf *cf;
684+
int ret;
685+
char tmp[PATH_MAX + 1];
686+
char *cfg = NULL;
687+
char *end;
688+
char *real_path;
689+
struct stat st;
690+
691+
/* Check if file exists and resolve path */
692+
ret = stat(path, &st);
693+
if (ret == -1 && errno == ENOENT) {
694+
/* Try to resolve the real path (if exists) */
695+
if (path[0] == '/') {
696+
fprintf(stderr, "Error: configuration file not found: %s\n", path);
697+
return -1;
698+
}
699+
700+
if (ctx->config->conf_path) {
701+
snprintf(tmp, PATH_MAX, "%s%s", ctx->config->conf_path, path);
702+
cfg = tmp;
703+
}
704+
else {
705+
cfg = (char *) path;
706+
}
707+
}
708+
else {
709+
cfg = (char *) path;
710+
}
711+
712+
if (access(cfg, R_OK) != 0) {
679713
perror("access");
714+
fprintf(stderr, "Error: cannot read configuration file: %s\n", cfg);
680715
return -1;
681716
}
682717

683-
ctx->config->file = mk_rconf_open(path);
684-
if (!ctx->config->file) {
685-
fprintf(stderr, "Error reading configuration file: %s\n", path);
718+
/* Use modern config format API that supports both .conf and .yaml/.yml */
719+
cf = flb_cf_create_from_file(NULL, cfg);
720+
if (!cf) {
721+
fprintf(stderr, "Error reading configuration file: %s\n", cfg);
686722
return -1;
687723
}
688724

725+
/* Set configuration root path */
726+
if (cfg) {
727+
real_path = realpath(cfg, NULL);
728+
if (real_path) {
729+
end = strrchr(real_path, FLB_DIRCHAR);
730+
if (end) {
731+
end++;
732+
*end = '\0';
733+
if (ctx->config->conf_path) {
734+
flb_free(ctx->config->conf_path);
735+
}
736+
ctx->config->conf_path = flb_strdup(real_path);
737+
}
738+
free(real_path);
739+
}
740+
}
741+
742+
/* Load the configuration format into the config */
743+
ret = flb_config_load_config_format(ctx->config, cf);
744+
if (ret != 0) {
745+
flb_cf_destroy(cf);
746+
fprintf(stderr, "Error loading configuration from file: %s\n", cfg);
747+
return -1;
748+
}
749+
750+
/* Destroy old cf_main if it exists (created by flb_config_init) */
751+
if (ctx->config->cf_main) {
752+
flb_cf_destroy(ctx->config->cf_main);
753+
}
754+
755+
/* Store the config format object */
756+
ctx->config->cf_main = cf;
757+
689758
return 0;
690759
}
691760

@@ -803,7 +872,7 @@ int flb_lib_push(flb_ctx_t *ctx, int ffd, const void *data, size_t len)
803872
/* Emulate some data from the response */
804873
int flb_lib_response(flb_ctx_t *ctx, int ffd, int status, const void *data, size_t len)
805874
{
806-
int ret;
875+
int ret = -1;
807876
struct flb_output_instance *o_ins;
808877

809878
if (ctx->status == FLB_LIB_NONE || ctx->status == FLB_LIB_ERROR) {
@@ -816,7 +885,7 @@ int flb_lib_response(flb_ctx_t *ctx, int ffd, int status, const void *data, size
816885
return -1;
817886
}
818887

819-
/* If input's test_formatter is registered, priorize to run it. */
888+
/* If output's test_response callback is registered, prioritize to run it. */
820889
if (o_ins->test_response.callback != NULL) {
821890
ret = flb_output_run_response(ctx, o_ins, status, data, len);
822891
}
@@ -965,8 +1034,9 @@ int flb_stop(flb_ctx_t *ctx)
9651034
return 0;
9661035
}
9671036

968-
if (ctx->config->file) {
969-
mk_rconf_free(ctx->config->file);
1037+
if (ctx->config->cf_main) {
1038+
flb_cf_destroy(ctx->config->cf_main);
1039+
ctx->config->cf_main = NULL;
9701040
}
9711041

9721042
flb_debug("[lib] sending STOP signal to the engine");

0 commit comments

Comments
 (0)