Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_dummy: support new property start_time_sec/nsec #2107

Merged
merged 3 commits into from
May 5, 2020
Merged
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
2 changes: 2 additions & 0 deletions include/fluent-bit/flb_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ static inline int flb_time_equal(struct flb_time *t0, struct flb_time *t1) {
int flb_time_get(struct flb_time *tm);
int flb_time_msleep(uint32_t ms);
double flb_time_to_double(struct flb_time *tm);
int flb_time_add(struct flb_time *base, struct flb_time *duration,
struct flb_time *result);
int flb_time_diff(struct flb_time *time1,
struct flb_time *time0, struct flb_time *result);
int flb_time_append_to_msgpack(struct flb_time *tm, msgpack_packer *pk, int fmt);
Expand Down
52 changes: 51 additions & 1 deletion plugins/in_dummy/in_dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@
#include "in_dummy.h"


static int set_dummy_timestamp(msgpack_packer *mp_pck, struct flb_dummy *ctx)
{
struct flb_time t;
struct flb_time diff;
struct flb_time dummy_time;
int ret;

if (ctx->base_timestamp == NULL) {
ctx->base_timestamp = flb_malloc(sizeof(struct flb_time));
flb_time_get(ctx->base_timestamp);
ret = flb_time_append_to_msgpack(ctx->dummy_timestamp, mp_pck, 0);
} else {
flb_time_get(&t);
flb_time_diff(&t, ctx->base_timestamp, &diff);
flb_time_add(ctx->dummy_timestamp, &diff, &dummy_time);
ret = flb_time_append_to_msgpack(&dummy_time, mp_pck, 0);
}

return ret;
}

/* cb_collect callback */
static int in_dummy_collect(struct flb_input_instance *ins,
struct flb_config *config, void *in_context)
Expand Down Expand Up @@ -62,7 +83,11 @@ static int in_dummy_collect(struct flb_input_instance *ins,
if (result.data.type == MSGPACK_OBJECT_MAP) {
/* { map => val, map => val, map => val } */
msgpack_pack_array(&mp_pck, 2);
flb_pack_time_now(&mp_pck);
if (ctx->dummy_timestamp != NULL){
set_dummy_timestamp(&mp_pck, ctx);
} else {
flb_pack_time_now(&mp_pck);
}
msgpack_pack_str_body(&mp_pck, pack + start, off - start);
}
start = off;
Expand All @@ -80,6 +105,8 @@ static int in_dummy_collect(struct flb_input_instance *ins,

static int config_destroy(struct flb_dummy *ctx)
{
flb_free(ctx->dummy_timestamp);
flb_free(ctx->base_timestamp);
flb_free(ctx->dummy_message);
flb_free(ctx->ref_msgpack);
flb_free(ctx);
Expand All @@ -92,6 +119,8 @@ static int configure(struct flb_dummy *ctx,
struct timespec *tm)
{
const char *str = NULL;
struct flb_time dummy_time;
int dummy_time_enabled = FLB_FALSE;
int root_type;
int ret = -1;
long val = 0;
Expand Down Expand Up @@ -124,6 +153,27 @@ static int configure(struct flb_dummy *ctx,
tm->tv_nsec = 1000000000 / val;
}

/* dummy timestamp */
ctx->dummy_timestamp = NULL;
ctx->base_timestamp = NULL;
flb_time_zero(&dummy_time);

str = flb_input_get_property("start_time_sec", in);
if (str != NULL && (val = atoi(str)) >= 0) {
dummy_time_enabled = FLB_TRUE;
dummy_time.tm.tv_sec = val;
}
str = flb_input_get_property("start_time_nsec", in);
if (str != NULL && (val = atoi(str)) >= 0) {
dummy_time_enabled = FLB_TRUE;
dummy_time.tm.tv_nsec = val;
}

if (dummy_time_enabled) {
ctx->dummy_timestamp = flb_malloc(sizeof(struct flb_time));
flb_time_copy(ctx->dummy_timestamp, &dummy_time);
}

ret = flb_pack_json(ctx->dummy_message,
ctx->dummy_message_len,
&ctx->ref_msgpack, &ctx->ref_msgpack_size, &root_type);
Expand Down
3 changes: 3 additions & 0 deletions plugins/in_dummy/in_dummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ struct flb_dummy {

char *ref_msgpack;
size_t ref_msgpack_size;

struct flb_time *dummy_timestamp;
struct flb_time *base_timestamp;
struct flb_input_instance *ins;
};

Expand Down
19 changes: 19 additions & 0 deletions src/flb_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ double flb_time_to_double(struct flb_time *tm)
return (double)(tm->tm.tv_sec) + ((double)tm->tm.tv_nsec/(double)ONESEC_IN_NSEC);
}

int flb_time_add(struct flb_time *base, struct flb_time *duration, struct flb_time *result)
{
if (base == NULL || duration == NULL|| result == NULL) {
return -1;
}
result->tm.tv_sec = base->tm.tv_sec + duration->tm.tv_sec;
result->tm.tv_nsec = base->tm.tv_nsec + duration->tm.tv_nsec;

if (result->tm.tv_nsec > ONESEC_IN_NSEC) {
result->tm.tv_nsec -= ONESEC_IN_NSEC;
result->tm.tv_sec++;
} else if (result->tm.tv_nsec < 0) {
result->tm.tv_nsec += ONESEC_IN_NSEC;
result->tm.tv_sec--;
}

return 0;
}

int flb_time_diff(struct flb_time *time1,
struct flb_time *time0,struct flb_time *result)
{
Expand Down