From 0c6c63c8ebc1bba03bb85b5bc135d3ee20c9ca02 Mon Sep 17 00:00:00 2001 From: Daniel Meiners Date: Thu, 3 May 2018 18:52:51 +0200 Subject: [PATCH 1/2] Fix JSON parsing with UTF8 BOM in JSON parser plugin --- plugins/parsers/json/parser.go | 5 +++++ plugins/parsers/json/parser_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/plugins/parsers/json/parser.go b/plugins/parsers/json/parser.go index ecf9996af0ad7..8a3d15be7d18e 100644 --- a/plugins/parsers/json/parser.go +++ b/plugins/parsers/json/parser.go @@ -12,6 +12,10 @@ import ( "github.com/influxdata/telegraf/metric" ) +var ( + utf8BOM = []byte("\xef\xbb\xbf") +) + type JSONParser struct { MetricName string TagKeys []string @@ -68,6 +72,7 @@ func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]i func (p *JSONParser) Parse(buf []byte) ([]telegraf.Metric, error) { buf = bytes.TrimSpace(buf) + buf = bytes.TrimPrefix(buf, utf8BOM) if len(buf) == 0 { return make([]telegraf.Metric, 0), nil } diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index f21961787fe9e..d67ce5a7fc54a 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -428,3 +428,15 @@ func TestParseArrayWithTagKeys(t *testing.T) { "othertag": "baz", }, metrics[1].Tags()) } + +var jsonBOM = []byte("\xef\xbb\xbf[{\"value\":17}]") + +func TestHttpJsonBOM(t *testing.T) { + parser := JSONParser{ + MetricName: "json_test", + } + + // Most basic vanilla test + _, err := parser.Parse(jsonBOM) + assert.NoError(t, err) +} \ No newline at end of file From 90f33a3827e7deffbbaaa17abf0e6bca59225bd7 Mon Sep 17 00:00:00 2001 From: Daniel Meiners Date: Thu, 3 May 2018 19:06:24 +0200 Subject: [PATCH 2/2] Add missing newline at end of file --- plugins/parsers/json/parser_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index d67ce5a7fc54a..c4f9463640c18 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -439,4 +439,4 @@ func TestHttpJsonBOM(t *testing.T) { // Most basic vanilla test _, err := parser.Parse(jsonBOM) assert.NoError(t, err) -} \ No newline at end of file +}