diff --git a/plugins/parsers/avro/README.md b/plugins/parsers/avro/README.md index 36089f441..a36e05915 100644 --- a/plugins/parsers/avro/README.md +++ b/plugins/parsers/avro/README.md @@ -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 diff --git a/plugins/parsers/avro/parser.go b/plugins/parsers/avro/parser.go index 44011581c..0b7d95dc9 100644 --- a/plugins/parsers/avro/parser.go +++ b/plugins/parsers/avro/parser.go @@ -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) diff --git a/plugins/parsers/avro/testdata/bad-timestamp-format/expected.err b/plugins/parsers/avro/testdata/bad-timestamp-format/expected.err new file mode 100644 index 000000000..a0674214f --- /dev/null +++ b/plugins/parsers/avro/testdata/bad-timestamp-format/expected.err @@ -0,0 +1 @@ +could not instantiate parser: invalid timestamp format 'unix_ps' diff --git a/plugins/parsers/avro/testdata/bad-timestamp-format/expected.out b/plugins/parsers/avro/testdata/bad-timestamp-format/expected.out new file mode 100644 index 000000000..e69de29bb diff --git a/plugins/parsers/avro/testdata/bad-timestamp-format/message.avro b/plugins/parsers/avro/testdata/bad-timestamp-format/message.avro new file mode 100644 index 000000000..e69de29bb diff --git a/plugins/parsers/avro/testdata/bad-timestamp-format/telegraf.conf b/plugins/parsers/avro/testdata/bad-timestamp-format/telegraf.conf new file mode 100644 index 000000000..b4a89fad7 --- /dev/null +++ b/plugins/parsers/avro/testdata/bad-timestamp-format/telegraf.conf @@ -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" + } + ] +} +''' diff --git a/plugins/parsers/avro/testdata/no-timestamp-format/expected.err b/plugins/parsers/avro/testdata/no-timestamp-format/expected.err deleted file mode 100644 index 8bebe6fb3..000000000 --- a/plugins/parsers/avro/testdata/no-timestamp-format/expected.err +++ /dev/null @@ -1 +0,0 @@ -could not instantiate parser: if 'timestamp' field is specified, 'timestamp_format' must be as well diff --git a/plugins/parsers/avro/testdata/no-timestamp-format/expected.out b/plugins/parsers/avro/testdata/no-timestamp-format/expected.out index e69de29bb..c831d4ad4 100644 --- a/plugins/parsers/avro/testdata/no-timestamp-format/expected.out +++ b/plugins/parsers/avro/testdata/no-timestamp-format/expected.out @@ -0,0 +1 @@ +measurement,tag=test_tag field=19i,timestamp=1664296121000000i 1664296121000000 diff --git a/plugins/parsers/avro/testdata/no-timestamp-format/message.avro b/plugins/parsers/avro/testdata/no-timestamp-format/message.avro index e69de29bb..5e4d4d9b0 100644 --- a/plugins/parsers/avro/testdata/no-timestamp-format/message.avro +++ b/plugins/parsers/avro/testdata/no-timestamp-format/message.avro @@ -0,0 +1 @@ +test_tag& \ No newline at end of file