fix(parsers.avro): Handle timestamp format checking correctly (#13855)

This commit is contained in:
Adam Thornton 2023-09-07 13:08:47 -07:00 committed by GitHub
parent 7fe6bb3fe0
commit bfbe195607
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 41 additions and 9 deletions

View File

@ -74,7 +74,8 @@ The message is supposed to be encoded as follows:
## be used for the measurement timestamp. ## be used for the measurement timestamp.
# avro_timestamp = "" # avro_timestamp = ""
## If avro_timestamp is specified, avro_timestamp_format must be set ## If avro_timestamp is specified, avro_timestamp_format must be set
## to one of 'unix', 'unix_ms', 'unix_us', or 'unix_ns' ## to one of 'unix', 'unix_ms', 'unix_us', or 'unix_ns'. It will
## default to 'unix'.
# avro_timestamp_format = "unix" # avro_timestamp_format = "unix"
## Used to separate parts of array structures. As above, the default ## Used to separate parts of array structures. As above, the default

View File

@ -53,13 +53,13 @@ func (p *Parser) Init() error {
if (p.Schema == "" && p.SchemaRegistry == "") || (p.Schema != "" && p.SchemaRegistry != "") { if (p.Schema == "" && p.SchemaRegistry == "") || (p.Schema != "" && p.SchemaRegistry != "") {
return errors.New("exactly one of 'schema_registry' or 'schema' must be specified") return errors.New("exactly one of 'schema_registry' or 'schema' must be specified")
} }
if p.TimestampFormat == "" { switch p.TimestampFormat {
if p.Timestamp != "" { case "":
return errors.New("if 'timestamp' field is specified, 'timestamp_format' must be as well") p.TimestampFormat = "unix"
} case "unix", "unix_ns", "unix_us", "unix_ms":
if p.TimestampFormat != "unix" && p.TimestampFormat != "unix_us" && p.TimestampFormat != "unix_ms" && p.TimestampFormat != "unix_ns" { // Valid values
return fmt.Errorf("invalid timestamp format '%v'", p.TimestampFormat) default:
} return fmt.Errorf("invalid timestamp format '%v'", p.TimestampFormat)
} }
if p.SchemaRegistry != "" { if p.SchemaRegistry != "" {
p.registryObj = newSchemaRegistry(p.SchemaRegistry) p.registryObj = newSchemaRegistry(p.SchemaRegistry)

View File

@ -0,0 +1 @@
could not instantiate parser: invalid timestamp format 'unix_ps'

View File

@ -0,0 +1,29 @@
[[ inputs.file ]]
files = ["./testdata/bad-timestamp-format/message.avro"]
data_format = "avro"
avro_measurement = "measurement"
avro_tags = [ "tag" ]
avro_timestamp = "timestamp"
avro_timestamp_format = "unix_ps"
avro_schema = '''
{
"type":"record",
"name":"Value",
"namespace":"com.example",
"fields":[
{
"name":"tag",
"type":"string"
},
{
"name":"field",
"type":"long"
},
{
"name":"timestamp",
"type":"long"
}
]
}
'''

View File

@ -1 +0,0 @@
could not instantiate parser: if 'timestamp' field is specified, 'timestamp_format' must be as well

View File

@ -0,0 +1 @@
measurement,tag=test_tag field=19i,timestamp=1664296121000000i 1664296121000000

View File

@ -0,0 +1 @@
test_tag&€<>¿±äêô