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
20 changes: 20 additions & 0 deletions plugins/out_logdna/logdna.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static inline int primary_key_check(msgpack_object k, char *name, int len)
* - level or severity
* - file
* - app
* - hostname
* - meta
*/
static int record_append_primary_keys(struct flb_logdna *ctx,
Expand All @@ -60,6 +61,7 @@ static int record_append_primary_keys(struct flb_logdna *ctx,
msgpack_object *level = NULL;
msgpack_object *file = NULL;
msgpack_object *app = NULL;
msgpack_object *hostname = NULL;
msgpack_object *meta = NULL;
msgpack_object k;
msgpack_object v;
Expand Down Expand Up @@ -105,6 +107,15 @@ static int record_append_primary_keys(struct flb_logdna *ctx,
msgpack_pack_object(mp_sbuf, v);
c++;
}

/* Hostname */
if (!hostname && primary_key_check(k, "hostname", 8) == FLB_TRUE) {
hostname = &k;
msgpack_pack_str(mp_sbuf, 8);
msgpack_pack_str_body(mp_sbuf, "hostname", 8);
msgpack_pack_object(mp_sbuf, v);
c++;
}
}

/* Set the global file name if the record did not provided one */
Expand All @@ -126,6 +137,15 @@ static int record_append_primary_keys(struct flb_logdna *ctx,
c++;
}

/* If no hostname is set in the record, set the default */
if (!hostname && ctx->_hostname) {
msgpack_pack_str(mp_sbuf, 8);
msgpack_pack_str_body(mp_sbuf, "hostname", 8);
msgpack_pack_str(mp_sbuf, flb_sds_len(ctx->_hostname));
msgpack_pack_str_body(mp_sbuf, ctx->_hostname, flb_sds_len(ctx->_hostname));
c++;
}

return c;
}

Expand Down
1 change: 1 addition & 0 deletions tests/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ if(FLB_IN_LIB)
FLB_RT_TEST(FLB_OUT_KAFKA "out_kafka.c")
FLB_RT_TEST(FLB_OUT_LIB "out_lib.c")
FLB_RT_TEST(FLB_OUT_LOKI "out_loki.c")
FLB_RT_TEST(FLB_OUT_LOGDNA "out_logdna.c")
FLB_RT_TEST(FLB_OUT_NULL "out_null.c")
FLB_RT_TEST(FLB_OUT_PLOT "out_plot.c")
FLB_RT_TEST(FLB_OUT_RETRY "out_retry.c")
Expand Down
129 changes: 129 additions & 0 deletions tests/runtime/out_logdna.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* -*- 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 <fluent-bit.h>
#include <fluent-bit/flb_sds.h>
#include "flb_tests_runtime.h"

static void cb_check_record_hostname(void *ctx, int ffd,
int res_ret, void *res_data, size_t res_size,
void *data)
{
flb_sds_t out_js = res_data;

TEST_CHECK(res_ret == 0);
TEST_CHECK(out_js != NULL);
TEST_CHECK(strstr(out_js, "\"hostname\":\"record-host\"") != NULL);

flb_sds_destroy(out_js);
}

static void cb_check_default_hostname(void *ctx, int ffd,
int res_ret, void *res_data, size_t res_size,
void *data)
{
flb_sds_t out_js = res_data;

TEST_CHECK(res_ret == 0);
TEST_CHECK(out_js != NULL);
TEST_CHECK(strstr(out_js, "\"hostname\":\"config-host\"") != NULL);

flb_sds_destroy(out_js);
}

void flb_test_record_hostname_field()
{
int ret;
int size = sizeof("[12345678, {\"hostname\":\"record-host\",\"message\":\"hello\"}]") - 1;
flb_ctx_t *ctx;
int in_ffd;
int out_ffd;

ctx = flb_create();
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);

in_ffd = flb_input(ctx, (char *) "lib", NULL);
flb_input_set(ctx, in_ffd, "tag", "test", NULL);

out_ffd = flb_output(ctx, (char *) "logdna", NULL);
flb_output_set(ctx, out_ffd,
"match", "test",
"api_key", "dummy-api-key",
NULL);

ret = flb_output_set_test(ctx, out_ffd, "formatter",
cb_check_record_hostname,
NULL, NULL);
TEST_CHECK(ret == 0);

ret = flb_start(ctx);
TEST_CHECK(ret == 0);

flb_lib_push(ctx, in_ffd,
(char *) "[12345678, {\"hostname\":\"record-host\",\"message\":\"hello\"}]",
size);

sleep(2);
flb_stop(ctx);
flb_destroy(ctx);
}

void flb_test_default_hostname_field()
{
int ret;
int size = sizeof("[12345678, {\"message\":\"hello\"}]") - 1;
flb_ctx_t *ctx;
int in_ffd;
int out_ffd;

ctx = flb_create();
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);

in_ffd = flb_input(ctx, (char *) "lib", NULL);
flb_input_set(ctx, in_ffd, "tag", "test", NULL);

out_ffd = flb_output(ctx, (char *) "logdna", NULL);
flb_output_set(ctx, out_ffd,
"match", "test",
"api_key", "dummy-api-key",
"hostname", "config-host",
NULL);

ret = flb_output_set_test(ctx, out_ffd, "formatter",
cb_check_default_hostname,
NULL, NULL);
TEST_CHECK(ret == 0);

ret = flb_start(ctx);
TEST_CHECK(ret == 0);

flb_lib_push(ctx, in_ffd,
(char *) "[12345678, {\"message\":\"hello\"}]",
size);

sleep(2);
flb_stop(ctx);
flb_destroy(ctx);
}

TEST_LIST = {
{"record_hostname_field", flb_test_record_hostname_field},
{"default_hostname_field", flb_test_default_hostname_field},
{NULL, NULL}
};