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

tests: internal: log_event_encoder: Add test code #7736

Closed
wants to merge 5 commits into from
Closed
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
142 changes: 73 additions & 69 deletions include/fluent-bit/flb_log_event_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <fluent-bit/flb_mp.h>
#include <fluent-bit/flb_log_event.h>
#include <fluent-bit/flb_log_event_encoder_dynamic_field.h>

#include <inttypes.h>
#include <msgpack.h>

#define FLB_EVENT_ENCODER_SUCCESS 0
Expand All @@ -36,33 +36,35 @@
#define FLB_EVENT_ENCODER_ERROR_SERIALIZATION_FAILURE -5
#define FLB_EVENT_ENCODER_ERROR_INVALID_VALUE_TYPE -6

#define FLB_LOG_EVENT_APPEND_TERMINATOR_VALUE_TYPE 0

#define FLB_LOG_EVENT_STRING_LENGTH_VALUE_TYPE 1
#define FLB_LOG_EVENT_STRING_BODY_VALUE_TYPE 2
#define FLB_LOG_EVENT_BINARY_LENGTH_VALUE_TYPE 3
#define FLB_LOG_EVENT_BINARY_BODY_VALUE_TYPE 4
#define FLB_LOG_EVENT_EXT_LENGTH_VALUE_TYPE 5
#define FLB_LOG_EVENT_EXT_BODY_VALUE_TYPE 6
#define FLB_LOG_EVENT_NULL_VALUE_TYPE 7
#define FLB_LOG_EVENT_CHAR_VALUE_TYPE 8
#define FLB_LOG_EVENT_INT8_VALUE_TYPE 9
#define FLB_LOG_EVENT_INT16_VALUE_TYPE 10
#define FLB_LOG_EVENT_INT32_VALUE_TYPE 11
#define FLB_LOG_EVENT_INT64_VALUE_TYPE 12
#define FLB_LOG_EVENT_UINT8_VALUE_TYPE 13
#define FLB_LOG_EVENT_UINT16_VALUE_TYPE 14
#define FLB_LOG_EVENT_UINT32_VALUE_TYPE 15
#define FLB_LOG_EVENT_UINT64_VALUE_TYPE 16
#define FLB_LOG_EVENT_DOUBLE_VALUE_TYPE 17
#define FLB_LOG_EVENT_BOOLEAN_VALUE_TYPE 18
#define FLB_LOG_EVENT_MSGPACK_OBJECT_VALUE_TYPE 19
#define FLB_LOG_EVENT_MSGPACK_RAW_VALUE_TYPE 20
#define FLB_LOG_EVENT_TIMESTAMP_VALUE_TYPE 21
#define FLB_LOG_EVENT_LEGACY_TIMESTAMP_VALUE_TYPE 22
#define FLB_LOG_EVENT_FORWARD_V1_TIMESTAMP_VALUE_TYPE 23
#define FLB_LOG_EVENT_FLUENT_BIT_V1_TIMESTAMP_VALUE_TYPE 24
#define FLB_LOG_EVENT_FLUENT_BIT_V2_TIMESTAMP_VALUE_TYPE 25
#define flb_log_event_type_t int32_t

#define FLB_LOG_EVENT_APPEND_TERMINATOR_VALUE_TYPE (flb_log_event_type_t)0

#define FLB_LOG_EVENT_STRING_LENGTH_VALUE_TYPE (flb_log_event_type_t)1
#define FLB_LOG_EVENT_STRING_BODY_VALUE_TYPE (flb_log_event_type_t)2
#define FLB_LOG_EVENT_BINARY_LENGTH_VALUE_TYPE (flb_log_event_type_t)3
#define FLB_LOG_EVENT_BINARY_BODY_VALUE_TYPE (flb_log_event_type_t)4
#define FLB_LOG_EVENT_EXT_LENGTH_VALUE_TYPE (flb_log_event_type_t)5
#define FLB_LOG_EVENT_EXT_BODY_VALUE_TYPE (flb_log_event_type_t)6
#define FLB_LOG_EVENT_NULL_VALUE_TYPE (flb_log_event_type_t)7
#define FLB_LOG_EVENT_CHAR_VALUE_TYPE (flb_log_event_type_t)8
#define FLB_LOG_EVENT_INT8_VALUE_TYPE (flb_log_event_type_t)9
#define FLB_LOG_EVENT_INT16_VALUE_TYPE (flb_log_event_type_t)10
#define FLB_LOG_EVENT_INT32_VALUE_TYPE (flb_log_event_type_t)11
#define FLB_LOG_EVENT_INT64_VALUE_TYPE (flb_log_event_type_t)12
#define FLB_LOG_EVENT_UINT8_VALUE_TYPE (flb_log_event_type_t)13
#define FLB_LOG_EVENT_UINT16_VALUE_TYPE (flb_log_event_type_t)14
#define FLB_LOG_EVENT_UINT32_VALUE_TYPE (flb_log_event_type_t)15
#define FLB_LOG_EVENT_UINT64_VALUE_TYPE (flb_log_event_type_t)16
#define FLB_LOG_EVENT_DOUBLE_VALUE_TYPE (flb_log_event_type_t)17
#define FLB_LOG_EVENT_BOOLEAN_VALUE_TYPE (flb_log_event_type_t)18
#define FLB_LOG_EVENT_MSGPACK_OBJECT_VALUE_TYPE (flb_log_event_type_t)19
#define FLB_LOG_EVENT_MSGPACK_RAW_VALUE_TYPE (flb_log_event_type_t)20
#define FLB_LOG_EVENT_TIMESTAMP_VALUE_TYPE (flb_log_event_type_t)21
#define FLB_LOG_EVENT_LEGACY_TIMESTAMP_VALUE_TYPE (flb_log_event_type_t)22
#define FLB_LOG_EVENT_FORWARD_V1_TIMESTAMP_VALUE_TYPE (flb_log_event_type_t)23
#define FLB_LOG_EVENT_FLUENT_BIT_V1_TIMESTAMP_VALUE_TYPE (flb_log_event_type_t)24
#define FLB_LOG_EVENT_FLUENT_BIT_V2_TIMESTAMP_VALUE_TYPE (flb_log_event_type_t)25

#define FLB_LOG_EVENT_STRING_MIN_VALUE_TYPE FLB_LOG_EVENT_STRING_LENGTH_VALUE_TYPE
#define FLB_LOG_EVENT_STRING_MAX_VALUE_TYPE FLB_LOG_EVENT_FLUENT_BIT_V2_TIMESTAMP_VALUE_TYPE
Expand All @@ -75,112 +77,114 @@

#define FLB_LOG_EVENT_APPEND_UNTIL_TERMINATOR -1



#define FLB_LOG_EVENT_VALUE_LIST_TERMINATOR() \
(int) FLB_LOG_EVENT_APPEND_TERMINATOR_VALUE_TYPE
(flb_log_event_type_t) FLB_LOG_EVENT_APPEND_TERMINATOR_VALUE_TYPE

#define FLB_LOG_EVENT_STRING_LENGTH_VALUE(length) \
(int) FLB_LOG_EVENT_STRING_LENGTH_VALUE_TYPE, \
(size_t) length
(flb_log_event_type_t) FLB_LOG_EVENT_STRING_LENGTH_VALUE_TYPE, \
(flb_log_event_size_t) length

#define FLB_LOG_EVENT_STRING_BODY_VALUE(buffer, length) \
(int) FLB_LOG_EVENT_STRING_BODY_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_STRING_BODY_VALUE_TYPE, \
(char *) buffer, \
(size_t) length
(flb_log_event_size_t) length

#define FLB_LOG_EVENT_BINARY_LENGTH_VALUE(length) \
(int) FLB_LOG_EVENT_BINARY_LENGTH_VALUE_TYPE, \
(size_t) length
(flb_log_event_type_t) FLB_LOG_EVENT_BINARY_LENGTH_VALUE_TYPE, \
(flb_log_event_size_t) length

#define FLB_LOG_EVENT_BINARY_BODY_VALUE(buffer, length) \
(int) FLB_LOG_EVENT_BINARY_BODY_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_BINARY_BODY_VALUE_TYPE, \
(char *) buffer, \
(size_t) length
(flb_log_event_size_t) length

#define FLB_LOG_EVENT_EXT_LENGTH_VALUE(type_, length) \
(int) FLB_LOG_EVENT_EXT_LENGTH_VALUE_TYPE, \
(int) type_, \
(size_t) length
(flb_log_event_type_t) FLB_LOG_EVENT_EXT_LENGTH_VALUE_TYPE, \
(int8_t) type_, \
(flb_log_event_size_t) length

#define FLB_LOG_EVENT_EXT_BODY_VALUE(buffer, length) \
(int) FLB_LOG_EVENT_EXT_BODY_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_EXT_BODY_VALUE_TYPE, \
(char *) buffer, \
(size_t) length
(flb_log_event_size_t) length

#define FLB_LOG_EVENT_TIMESTAMP_VALUE(value) \
(int) FLB_LOG_EVENT_TIMESTAMP_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_TIMESTAMP_VALUE_TYPE, \
(char *) value

#define FLB_LOG_EVENT_LEGACY_TIMESTAMP_VALUE(value) \
(int) FLB_LOG_EVENT_LEGACY_TIMESTAMP_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_LEGACY_TIMESTAMP_VALUE_TYPE, \
(char *) value

#define FLB_LOG_EVENT_FORWARD_V1_TIMESTAMP_VALUE(value) \
(int) FLB_LOG_EVENT_FORWARD_V1_TIMESTAMP_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_FORWARD_V1_TIMESTAMP_VALUE_TYPE, \
(char *) value

#define FLB_LOG_EVENT_FLUENT_BIT_V1_TIMESTAMP_VALUE(value) \
(int) FLB_LOG_EVENT_FLUENT_BIT_V1_TIMESTAMP_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_FLUENT_BIT_V1_TIMESTAMP_VALUE_TYPE, \
(char *) value

#define FLB_LOG_EVENT_NULL_VALUE() \
(int) FLB_LOG_EVENT_NULL_VALUE_TYPE
(flb_log_event_type_t) FLB_LOG_EVENT_NULL_VALUE_TYPE

#define FLB_LOG_EVENT_CHAR_VALUE(value) \
(int) FLB_LOG_EVENT_CHAR_VALUE_TYPE, \
(int) value
(flb_log_event_type_t) FLB_LOG_EVENT_CHAR_VALUE_TYPE, \
(int32_t) value

#define FLB_LOG_EVENT_INT8_VALUE(value) \
(int) FLB_LOG_EVENT_INT8_VALUE_TYPE, \
(int) value
(flb_log_event_type_t) FLB_LOG_EVENT_INT8_VALUE_TYPE, \
(int32_t) value

#define FLB_LOG_EVENT_INT16_VALUE(value) \
(int) FLB_LOG_EVENT_INT16_VALUE_TYPE, \
(int) value
(flb_log_event_type_t) FLB_LOG_EVENT_INT16_VALUE_TYPE, \
(int32_t) value

#define FLB_LOG_EVENT_INT32_VALUE(value) \
(int) FLB_LOG_EVENT_INT32_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_INT32_VALUE_TYPE, \
(int32_t) value

#define FLB_LOG_EVENT_INT64_VALUE(value) \
(int) FLB_LOG_EVENT_INT64_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_INT64_VALUE_TYPE, \
(int64_t) value

#define FLB_LOG_EVENT_UINT8_VALUE(value) \
(int) FLB_LOG_EVENT_UINT8_VALUE_TYPE, \
(unsigned int) value
(flb_log_event_type_t) FLB_LOG_EVENT_UINT8_VALUE_TYPE, \
(uint32_t) value

#define FLB_LOG_EVENT_UINT16_VALUE(value) \
(int) FLB_LOG_EVENT_UINT16_VALUE_TYPE, \
(unsigned int) value
(flb_log_event_type_t) FLB_LOG_EVENT_UINT16_VALUE_TYPE, \
(uint32_t) value

#define FLB_LOG_EVENT_UINT32_VALUE(value) \
(int) FLB_LOG_EVENT_UINT32_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_UINT32_VALUE_TYPE, \
(uint32_t) value

#define FLB_LOG_EVENT_UINT64_VALUE(value) \
(int) FLB_LOG_EVENT_UINT64_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_UINT64_VALUE_TYPE, \
(uint64_t) value

#define FLB_LOG_EVENT_DOUBLE_VALUE(value) \
(int) FLB_LOG_EVENT_DOUBLE_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_DOUBLE_VALUE_TYPE, \
(double) value

#define FLB_LOG_EVENT_BOOLEAN_VALUE(value) \
(int) FLB_LOG_EVENT_BOOLEAN_VALUE_TYPE, \
(int) value
(flb_log_event_type_t) FLB_LOG_EVENT_BOOLEAN_VALUE_TYPE, \
(int32_t) value

#define FLB_LOG_EVENT_MSGPACK_OBJECT_VALUE(value) \
(int) FLB_LOG_EVENT_MSGPACK_OBJECT_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_MSGPACK_OBJECT_VALUE_TYPE, \
(char *) value

#define FLB_LOG_EVENT_MSGPACK_RAW_VALUE(buffer, length) \
(int) FLB_LOG_EVENT_MSGPACK_RAW_VALUE_TYPE, \
(flb_log_event_type_t) FLB_LOG_EVENT_MSGPACK_RAW_VALUE_TYPE, \
(char *) buffer, \
(size_t) length
(flb_log_event_size_t) length

#define FLB_LOG_EVENT_STRING_VALUE(buffer, length) \
FLB_LOG_EVENT_STRING_LENGTH_VALUE(length), \
FLB_LOG_EVENT_STRING_BODY_VALUE(buffer, length)
FLB_LOG_EVENT_STRING_LENGTH_VALUE((flb_log_event_size_t)length), \
FLB_LOG_EVENT_STRING_BODY_VALUE(buffer, (flb_log_event_size_t)length)

#define FLB_LOG_EVENT_BINARY_VALUE(buffer, length) \
FLB_LOG_EVENT_BINARY_LENGTH_VALUE(length), \
Expand Down
32 changes: 21 additions & 11 deletions include/fluent-bit/flb_log_event_encoder_primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
#ifndef FLB_LOG_EVENT_ENCODER_PRIMITIVES_H
#define FLB_LOG_EVENT_ENCODER_PRIMITIVES_H

#ifdef FLB_SYSTEM_WINDOWS
/* Fluent-bit on Windows x86_64 passes size_t as dword if it is compiled using /O2.
* It causes encoder error since encoder functions handles size_t as qword.
* We define dword size flb_log_event_size_t to prevent invalid casting as a workaround.
*/
#define flb_log_event_size_t uint32_t
#else
#define flb_log_event_size_t size_t
#endif

int flb_log_event_encoder_append_values_unsafe(
struct flb_log_event_encoder *context,
int field,
Expand All @@ -28,36 +38,36 @@ int flb_log_event_encoder_append_values_unsafe(
int flb_log_event_encoder_append_binary_length(
struct flb_log_event_encoder *context,
int target_field,
size_t length);
flb_log_event_size_t length);

int flb_log_event_encoder_append_binary_body(
struct flb_log_event_encoder *context,
int target_field,
char *value,
size_t length);
flb_log_event_size_t length);

int flb_log_event_encoder_append_ext_length(
struct flb_log_event_encoder *context,
int target_field,
int8_t type,
size_t length);
flb_log_event_size_t length);

int flb_log_event_encoder_append_ext_body(
struct flb_log_event_encoder *context,
int target_field,
char *value,
size_t length);
flb_log_event_size_t length);

int flb_log_event_encoder_append_string_length(
struct flb_log_event_encoder *context,
int target_field,
size_t length);
flb_log_event_size_t length);

int flb_log_event_encoder_append_string_body(
struct flb_log_event_encoder *context,
int target_field,
char *value,
size_t length);
flb_log_event_size_t length);

int flb_log_event_encoder_append_int8(
struct flb_log_event_encoder *context,
Expand Down Expand Up @@ -107,7 +117,7 @@ int flb_log_event_encoder_append_double(
int flb_log_event_encoder_append_boolean(
struct flb_log_event_encoder *context,
int target_field,
int value);
char value);

int flb_log_event_encoder_append_character(
struct flb_log_event_encoder *context,
Expand All @@ -118,20 +128,20 @@ int flb_log_event_encoder_append_binary(
struct flb_log_event_encoder *context,
int target_field,
char *value,
size_t length);
flb_log_event_size_t length);

int flb_log_event_encoder_append_string(
struct flb_log_event_encoder *context,
int target_field,
char *value,
size_t length);
flb_log_event_size_t length);

int flb_log_event_encoder_append_ext(
struct flb_log_event_encoder *context,
int target_field,
int8_t type,
char *value,
size_t length);
flb_log_event_size_t length);

int flb_log_event_encoder_append_cstring(
struct flb_log_event_encoder *context,
Expand All @@ -151,7 +161,7 @@ int flb_log_event_encoder_append_raw_msgpack(
struct flb_log_event_encoder *context,
int target_field,
char *value_buffer,
size_t value_size);
flb_log_event_size_t value_size);

int flb_log_event_encoder_append_timestamp(
struct flb_log_event_encoder *context,
Expand Down
12 changes: 12 additions & 0 deletions src/flb_log_event_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ int flb_log_event_encoder_emit_raw_record(struct flb_log_event_encoder *context,
{
int result;

if (context == NULL) {
return FLB_EVENT_ENCODER_ERROR_INVALID_CONTEXT;
}

if (buffer == NULL || length == 0) {
return FLB_EVENT_ENCODER_ERROR_INVALID_ARGUMENT;
}

result = msgpack_pack_str_body(&context->packer, buffer, length);

if (result != 0) {
Expand Down Expand Up @@ -229,6 +237,10 @@ int flb_log_event_encoder_emit_record(struct flb_log_event_encoder *context)

int flb_log_event_encoder_reset_record(struct flb_log_event_encoder *context)
{
if (context == NULL) {
return FLB_EVENT_ENCODER_ERROR_INVALID_CONTEXT;
}

flb_log_event_encoder_dynamic_field_reset(&context->metadata);
flb_log_event_encoder_dynamic_field_reset(&context->body);
flb_log_event_encoder_dynamic_field_reset(&context->root);
Expand Down
Loading