Skip to content

Commit

Permalink
fix(parsers.avro): Handle timestamp format checking correctly (#13855)
Browse files Browse the repository at this point in the history
  • Loading branch information
athornton authored Sep 7, 2023
1 parent 7fe6bb3 commit bfbe195
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 9 deletions.
3 changes: 2 additions & 1 deletion plugins/parsers/avro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ The message is supposed to be encoded as follows:
## be used for the measurement timestamp.
# avro_timestamp = ""
## 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"

## Used to separate parts of array structures. As above, the default
Expand Down
14 changes: 7 additions & 7 deletions plugins/parsers/avro/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func (p *Parser) Init() error {
if (p.Schema == "" && p.SchemaRegistry == "") || (p.Schema != "" && p.SchemaRegistry != "") {
return errors.New("exactly one of 'schema_registry' or 'schema' must be specified")
}
if p.TimestampFormat == "" {
if p.Timestamp != "" {
return errors.New("if 'timestamp' field is specified, 'timestamp_format' must be as well")
}
if p.TimestampFormat != "unix" && p.TimestampFormat != "unix_us" && p.TimestampFormat != "unix_ms" && p.TimestampFormat != "unix_ns" {
return fmt.Errorf("invalid timestamp format '%v'", p.TimestampFormat)
}
switch p.TimestampFormat {
case "":
p.TimestampFormat = "unix"
case "unix", "unix_ns", "unix_us", "unix_ms":
// Valid values
default:
return fmt.Errorf("invalid timestamp format '%v'", p.TimestampFormat)
}
if p.SchemaRegistry != "" {
p.registryObj = newSchemaRegistry(p.SchemaRegistry)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
could not instantiate parser: invalid timestamp format 'unix_ps'
Empty file.
Empty file.
29 changes: 29 additions & 0 deletions plugins/parsers/avro/testdata/bad-timestamp-format/telegraf.conf
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
'''

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
measurement,tag=test_tag field=19i,timestamp=1664296121000000i 1664296121000000
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_tag&�������

0 comments on commit bfbe195

Please sign in to comment.