Support configuring a default timezone in JSON parser (#5472)
This commit is contained in:
committed by
Daniel Nelson
parent
eb794ec30f
commit
1886676e14
@@ -49,9 +49,21 @@ ignored unless specified in the `tag_key` or `json_string_fields` options.
|
||||
## https://golang.org/pkg/time/#Time.Format
|
||||
## ex: json_time_format = "Mon Jan 2 15:04:05 -0700 MST 2006"
|
||||
## json_time_format = "2006-01-02T15:04:05Z07:00"
|
||||
## json_time_format = "01/02/2006 15:04:05"
|
||||
## json_time_format = "unix"
|
||||
## json_time_format = "unix_ms"
|
||||
json_time_format = ""
|
||||
|
||||
## Timezone allows you to provide an override for timestamps that
|
||||
## don't already include an offset
|
||||
## e.g. 04/06/2016 12:41:45
|
||||
##
|
||||
## Default: "" which renders UTC
|
||||
## Options are as follows:
|
||||
## 1. Local -- interpret based on machine localtime
|
||||
## 2. "America/New_York" -- Unix TZ values like those found in https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
||||
## 3. UTC -- or blank/unspecified, will return timestamp in UTC
|
||||
json_timezone = ""
|
||||
```
|
||||
|
||||
#### json_query
|
||||
@@ -62,7 +74,7 @@ query should contain a JSON object or an array of objects.
|
||||
|
||||
Consult the GJSON [path syntax][gjson syntax] for details and examples.
|
||||
|
||||
#### json_time_key, json_time_format
|
||||
#### json_time_key, json_time_format, json_timezone
|
||||
|
||||
By default the current time will be used for all created metrics, to set the
|
||||
time using the JSON document you can use the `json_time_key` and
|
||||
@@ -77,6 +89,12 @@ the Go "reference time" which is defined to be the specific time:
|
||||
Consult the Go [time][time parse] package for details and additional examples
|
||||
on how to set the time format.
|
||||
|
||||
When parsing times that don't include a timezone specifier, times are assumed
|
||||
to be UTC. To default to another timezone, or to local time, specify the
|
||||
`json_timezone` option. This option should be set to a
|
||||
[Unix TZ value](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones),
|
||||
such as `America/New_York`, to `Local` to utilize the system timezone, or to `UTC`.
|
||||
|
||||
### Examples
|
||||
|
||||
#### Basic Parsing
|
||||
|
||||
@@ -28,6 +28,7 @@ type JSONParser struct {
|
||||
JSONQuery string
|
||||
JSONTimeKey string
|
||||
JSONTimeFormat string
|
||||
JSONTimezone string
|
||||
DefaultTags map[string]string
|
||||
}
|
||||
|
||||
@@ -82,7 +83,7 @@ func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]i
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nTime, err = internal.ParseTimestamp(f.Fields[p.JSONTimeKey], p.JSONTimeFormat)
|
||||
nTime, err = internal.ParseTimestampWithLocation(f.Fields[p.JSONTimeKey], p.JSONTimeFormat, p.JSONTimezone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -599,6 +599,23 @@ func TestTimeParser(t *testing.T) {
|
||||
require.Equal(t, false, metrics[0].Time() == metrics[1].Time())
|
||||
}
|
||||
|
||||
func TestTimeParserWithTimezone(t *testing.T) {
|
||||
testString := `{
|
||||
"time": "04 Jan 06 15:04"
|
||||
}`
|
||||
|
||||
parser := JSONParser{
|
||||
MetricName: "json_test",
|
||||
JSONTimeKey: "time",
|
||||
JSONTimeFormat: "02 Jan 06 15:04",
|
||||
JSONTimezone: "America/New_York",
|
||||
}
|
||||
metrics, err := parser.Parse([]byte(testString))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(metrics))
|
||||
require.EqualValues(t, int64(1136405040000000000), metrics[0].Time().UnixNano())
|
||||
}
|
||||
|
||||
func TestUnixTimeParser(t *testing.T) {
|
||||
testString := `[
|
||||
{
|
||||
|
||||
@@ -85,6 +85,9 @@ type Config struct {
|
||||
// time format
|
||||
JSONTimeFormat string `toml:"json_time_format"`
|
||||
|
||||
// default timezone
|
||||
JSONTimezone string `toml:"json_timezone"`
|
||||
|
||||
// Authentication file for collectd
|
||||
CollectdAuthFile string `toml:"collectd_auth_file"`
|
||||
// One of none (default), sign, or encrypt
|
||||
@@ -152,6 +155,7 @@ func NewParser(config *Config) (Parser, error) {
|
||||
config.JSONQuery,
|
||||
config.JSONTimeKey,
|
||||
config.JSONTimeFormat,
|
||||
config.JSONTimezone,
|
||||
config.DefaultTags)
|
||||
case "value":
|
||||
parser, err = NewValueParser(config.MetricName,
|
||||
@@ -275,6 +279,7 @@ func newJSONParser(
|
||||
jsonQuery string,
|
||||
timeKey string,
|
||||
timeFormat string,
|
||||
timezone string,
|
||||
defaultTags map[string]string,
|
||||
) Parser {
|
||||
parser := &json.JSONParser{
|
||||
@@ -285,6 +290,7 @@ func newJSONParser(
|
||||
JSONQuery: jsonQuery,
|
||||
JSONTimeKey: timeKey,
|
||||
JSONTimeFormat: timeFormat,
|
||||
JSONTimezone: timezone,
|
||||
DefaultTags: defaultTags,
|
||||
}
|
||||
return parser
|
||||
|
||||
Reference in New Issue
Block a user