Skip to content

Commit

Permalink
in_dummy: Use interval_sec/nsec instead of time_interval.
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Valente <[email protected]>
  • Loading branch information
franciscovalentecastro committed Oct 27, 2023
1 parent c3d99a8 commit e8c09e8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
41 changes: 28 additions & 13 deletions plugins/in_dummy/in_dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ static int configure(struct flb_dummy *ctx,
struct timespec *tm)
{
const char *msg;
double tm_interval_fractional;
int tm_interval_seconds;
int root_type;
int ret = -1;

Expand All @@ -216,14 +214,26 @@ static int configure(struct flb_dummy *ctx,
}

/* interval settings */
tm->tv_sec = ctx->time_interval;
if (ctx->interval_sec < 0 || ctx->interval_nsec < 0) {
/* Illegal settings. Override them. */
ctx->interval_sec = atoi(DEFAULT_INTERVAL_SEC);
ctx->interval_nsec = atoi(DEFAULT_INTERVAL_NSEC);
}

/* default settings */
tm->tv_sec = 1;
tm->tv_nsec = 0;

if (ctx->rate > 1) {
tm_interval_fractional = (double) ctx->time_interval / ctx->rate;
tm_interval_seconds = (int) tm_interval_fractional;
tm->tv_sec = tm_interval_seconds;
tm->tv_nsec = (tm_interval_fractional - tm_interval_seconds) * 1000000000;
if (ctx->interval_sec > 0 || ctx->interval_nsec > 0) {
/* Set using interval settings. */
tm->tv_sec = ctx->interval_sec;
tm->tv_nsec = ctx->interval_nsec;
} else {
if (ctx->rate > 1) {
/* Set using rate settings. */
tm->tv_sec = 0;
tm->tv_nsec = 1000000000 / ctx->rate;
}
}

/* dummy timestamp */
Expand Down Expand Up @@ -400,14 +410,19 @@ static struct flb_config_map config_map[] = {
"set the sample metadata to be generated. It should be a JSON object."
},
{
FLB_CONFIG_MAP_INT, "rate", "1",
FLB_CONFIG_MAP_INT, "rate", DEFAULT_RATE,
0, FLB_TRUE, offsetof(struct flb_dummy, rate),
"set a number of events per time interval."
"set a number of events per second."
},
{
FLB_CONFIG_MAP_INT, "interval_sec", DEFAULT_INTERVAL_SEC,
0, FLB_TRUE, offsetof(struct flb_dummy, interval_sec),
"set seconds of interval to generate events. overrides rate setting."
},
{
FLB_CONFIG_MAP_INT, "time_interval", "1",
0, FLB_TRUE, offsetof(struct flb_dummy, time_interval),
"set time interval to generate logs at the set rate."
FLB_CONFIG_MAP_INT, "interval_nsec", DEFAULT_INTERVAL_NSEC,
0, FLB_TRUE, offsetof(struct flb_dummy, interval_nsec),
"set nanoseconds of interval to generate events. overrides rate setting."
},
{
FLB_CONFIG_MAP_INT, "copies", "1",
Expand Down
6 changes: 5 additions & 1 deletion plugins/in_dummy/in_dummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

#define DEFAULT_DUMMY_MESSAGE "{\"message\":\"dummy\"}"
#define DEFAULT_DUMMY_METADATA "{}"
#define DEFAULT_RATE "1"
#define DEFAULT_INTERVAL_SEC "0"
#define DEFAULT_INTERVAL_NSEC "0"

struct flb_dummy {
int coll_fd;
Expand All @@ -34,7 +37,8 @@ struct flb_dummy {
int copies;
int samples;
int samples_count;
int time_interval;
int interval_sec;
int interval_nsec;

int dummy_timestamp_set;
struct flb_time base_timestamp;
Expand Down
28 changes: 18 additions & 10 deletions tests/runtime/in_simple_systems.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,19 @@ void flb_test_dummy_records_message_copies_100(struct callback_records *records)
TEST_CHECK(records->num_records >= 100);
}

void flb_test_dummy_records_message_time_interval_2_rate_5(struct callback_records *records)
void flb_test_dummy_records_message_rate(struct callback_records *records)
{
TEST_CHECK(records->num_records >= 5);
TEST_CHECK(records->num_records >= 20);
}

void flb_test_dummy_records_message_time_interval_3_rate_100(struct callback_records *records)
void flb_test_dummy_records_message_interval_sec(struct callback_records *records)
{
TEST_CHECK(records->num_records >= 100);
TEST_CHECK(records->num_records >= 1);
}

void flb_test_dummy_records_message_interval_nsec(struct callback_records *records)
{
TEST_CHECK(records->num_records >= 1);
}

void flb_test_in_dummy_flush()
Expand Down Expand Up @@ -573,13 +578,16 @@ void flb_test_in_dummy_flush()
do_test_records_single("dummy", flb_test_dummy_records_message_copies_100,
"copies", "100",
NULL);
do_test_records_wait_time("dummy", 2, flb_test_dummy_records_message_time_interval_2_rate_5,
"time_interval", "2",
"rate", "5",
do_test_records_wait_time("dummy", 1, flb_test_dummy_records_message_rate,
"rate", "20",
NULL);
do_test_records_wait_time("dummy", 2, flb_test_dummy_records_message_interval_sec,
"interval_sec", "2",
"interval_nsec", "0",
NULL);
do_test_records_wait_time("dummy", 3, flb_test_dummy_records_message_time_interval_3_rate_100,
"time_interval", "3",
"rate", "100",
do_test_records_wait_time("dummy", 1, flb_test_dummy_records_message_interval_nsec,
"interval_sec", "0",
"interval_nsec", "700000000",
NULL);
}

Expand Down

0 comments on commit e8c09e8

Please sign in to comment.