Skip to content

Commit

Permalink
parser: fix time zone offsets being dropped on Windows (#6368)
Browse files Browse the repository at this point in the history
* parser: add test for time zones

This adds a unit test for parsing logs that contain time stamps with
a time zone offset.

This test deliberately fails on Windows; a subsequent commit fixes
the underlying issue and makes the test pass.

Signed-off-by: Jeff Erbrecht <[email protected]>

* time: add portable wrapper struct for tm

This wrapper struct adds an explicit time zone offset field on
platforms that do not include such a field within `struct tm`,
such as on Windows. This gives us a place to store parsed
time zone information so that we don't have to drop it.

Signed-off-by: Jeff Erbrecht <[email protected]>

* parser: use flb_tm wrapper struct

This updates the parser APIs to use flb_tm..

Signed-off-by: Jeff Erbrecht <[email protected]>

* strptime: use flb_tm instead of tm

This changes the strptime APIs to use flb_tm. Any implementation
conditional on FLB_HAVE_GMTOFF has been reverted since flb_tm
now has a field to store the time zone offset on all platforms.

Signed-off-by: Jeff Erbrecht <[email protected]>

* parser: json: use flb_tm

Signed-off-by: Jeff Erbrecht <[email protected]>

* parser: regex: use flb_tm

Signed-off-by: Jeff Erbrecht <[email protected]>

* parser: logfmt: use flb_tm

Signed-off-by: Jeff Erbrecht <[email protected]>

* parser: ltsv: use flb_tm

Signed-off-by: Jeff Erbrecht <[email protected]>

* parser: remove spurious trailing space

Signed-off-by: Jeff Erbrecht <[email protected]>

* parser: fix test

Signed-off-by: Jeff Erbrecht <[email protected]>

Signed-off-by: Jeff Erbrecht <[email protected]>
  • Loading branch information
jefferbrecht authored Nov 22, 2022
1 parent d3a0697 commit cae9a8f
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 94 deletions.
12 changes: 4 additions & 8 deletions include/fluent-bit/flb_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,13 @@ enum {
FLB_PARSER_TYPE_HEX,
};

static inline time_t flb_parser_tm2time(const struct tm *src)
static inline time_t flb_parser_tm2time(const struct flb_tm *src)
{
struct tm tmp;
time_t res;

tmp = *src;
#ifdef FLB_HAVE_GMTOFF
res = timegm(&tmp) - src->tm_gmtoff;
#else
res = timegm(&tmp);
#endif
tmp = src->tm;
res = timegm(&tmp) - flb_tm_gmtoff(src);
return res;
}

Expand All @@ -109,7 +105,7 @@ void flb_parser_exit(struct flb_config *config);
int flb_parser_tzone_offset(const char *str, int len, int *tmdiff);
int flb_parser_time_lookup(const char *time, size_t tsize, time_t now,
struct flb_parser *parser,
struct tm *tm, double *ns);
struct flb_tm *tm, double *ns);
int flb_parser_typecast(const char *key, int key_len,
const char *val, int val_len,
msgpack_packer *pck,
Expand Down
2 changes: 1 addition & 1 deletion include/fluent-bit/flb_strptime.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
#ifndef FLB_STRPTIME_H
#define FLB_STRPTIME_H

char *flb_strptime(const char *s, const char *format, struct tm *tm);
char *flb_strptime(const char *s, const char *format, struct flb_tm *tm);

#endif
13 changes: 13 additions & 0 deletions include/fluent-bit/flb_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ struct flb_time {
struct timespec tm;
};

struct flb_tm {
struct tm tm;
#ifndef FLB_HAVE_GMTOFF
long int tm_gmtoff;
#endif
};

#ifndef FLB_HAVE_GMTOFF
#define flb_tm_gmtoff(x) (x)->tm_gmtoff
#else
#define flb_tm_gmtoff(x) (x)->tm.tm_gmtoff
#endif

/*
to represent eventtime of fluentd
see also
Expand Down
10 changes: 4 additions & 6 deletions src/flb_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ static int parse_subseconds(char *str, int len, double *subsec)
int flb_parser_time_lookup(const char *time_str, size_t tsize,
time_t now,
struct flb_parser *parser,
struct tm *tm, double *ns)
struct flb_tm *tm, double *ns)
{
int ret;
time_t time_now;
Expand Down Expand Up @@ -1059,8 +1059,8 @@ int flb_parser_time_lookup(const char *time_str, size_t tsize,
gmtime_r(&time_now, &tmy);

/* Make the timestamp default to today */
tm->tm_mon = tmy.tm_mon;
tm->tm_mday = tmy.tm_mday;
tm->tm.tm_mon = tmy.tm_mon;
tm->tm.tm_mday = tmy.tm_mday;

uint64_t t = tmy.tm_year + 1900;

Expand Down Expand Up @@ -1127,11 +1127,9 @@ int flb_parser_time_lookup(const char *time_str, size_t tsize,
}
}

#ifdef FLB_HAVE_GMTOFF
if (parser->time_with_tz == FLB_FALSE) {
tm->tm_gmtoff = parser->time_offset;
flb_tm_gmtoff(tm) = parser->time_offset;
}
#endif

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/flb_parser_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int flb_parser_json_do(struct flb_parser *parser,
msgpack_object *k = NULL;
msgpack_object *v = NULL;
time_t time_lookup;
struct tm tm = {0};
struct flb_tm tm = {0};
struct flb_time *t;

/* Convert incoming in_buf JSON message to message pack format */
Expand Down
2 changes: 1 addition & 1 deletion src/flb_parser_logfmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static int logfmt_parser(struct flb_parser *parser,
size_t *map_size)
{
int ret;
struct tm tm = {0};
struct flb_tm tm = {0};
const unsigned char *key = NULL;
size_t key_len = 0;
const unsigned char *value = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/flb_parser_ltsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static int ltsv_parser(struct flb_parser *parser,
size_t *map_size)
{
int ret;
struct tm tm = {0};
struct flb_tm tm = {0};
const unsigned char *label = NULL;
size_t label_len = 0;
const unsigned char *field = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/flb_parser_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void cb_results(const char *name, const char *value,
char tmp[255];
struct regex_cb_ctx *pcb = data;
struct flb_parser *parser = pcb->parser;
struct tm tm = {0};
struct flb_tm tm = {0};
(void) data;

if (vlen == 0 && parser->skip_empty) {
Expand Down
Loading

0 comments on commit cae9a8f

Please sign in to comment.