Add option to disable timestamp adjustment in grok parser (#5488)

This commit is contained in:
Greg 2019-02-26 18:35:57 -07:00 committed by Daniel Nelson
parent ec746cc32a
commit 85617887c4
4 changed files with 30 additions and 5 deletions

View File

@ -1508,6 +1508,14 @@ func getParserConfig(name string, tbl *ast.Table) (*parsers.Config, error) {
} }
} }
if node, ok := tbl.Fields["grok_unique_timestamp"]; ok {
if kv, ok := node.(*ast.KeyValue); ok {
if str, ok := kv.Value.(*ast.String); ok {
c.GrokUniqueTimestamp = str.Value
}
}
}
//for csv parser //for csv parser
if node, ok := tbl.Fields["csv_column_names"]; ok { if node, ok := tbl.Fields["csv_column_names"]; ok {
if kv, ok := node.(*ast.KeyValue); ok { if kv, ok := node.(*ast.KeyValue); ok {
@ -1661,6 +1669,7 @@ func getParserConfig(name string, tbl *ast.Table) (*parsers.Config, error) {
delete(tbl.Fields, "grok_custom_patterns") delete(tbl.Fields, "grok_custom_patterns")
delete(tbl.Fields, "grok_custom_pattern_files") delete(tbl.Fields, "grok_custom_pattern_files")
delete(tbl.Fields, "grok_timezone") delete(tbl.Fields, "grok_timezone")
delete(tbl.Fields, "grok_unique_timestamp")
delete(tbl.Fields, "csv_column_names") delete(tbl.Fields, "csv_column_names")
delete(tbl.Fields, "csv_column_types") delete(tbl.Fields, "csv_column_types")
delete(tbl.Fields, "csv_comment") delete(tbl.Fields, "csv_comment")

View File

@ -110,6 +110,9 @@ you will find the https://grokdebug.herokuapp.com application quite useful!
## 2. "Canada/Eastern" -- Unix TZ values like those found in https://en.wikipedia.org/wiki/List_of_tz_database_time_zones ## 2. "Canada/Eastern" -- 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 ## 3. UTC -- or blank/unspecified, will return timestamp in UTC
grok_timezone = "Canada/Eastern" grok_timezone = "Canada/Eastern"
## When grok_unique_timestamp is set to "disable", timestamp will not incremented if there is a duplicate. Default is "auto"
# grok_unique_timestamp = "auto"
``` ```
#### Timestamp Examples #### Timestamp Examples

View File

@ -86,6 +86,9 @@ type Parser struct {
Timezone string Timezone string
loc *time.Location loc *time.Location
// UniqueTimestamp when set to "disable", timestamp will not incremented if there is a duplicate.
UniqueTimestamp string
// typeMap is a map of patterns -> capture name -> modifier, // typeMap is a map of patterns -> capture name -> modifier,
// ie, { // ie, {
// "%{TESTLOG}": // "%{TESTLOG}":
@ -134,6 +137,10 @@ func (p *Parser) Compile() error {
return err return err
} }
if p.UniqueTimestamp == "" {
p.UniqueTimestamp = "auto"
}
// Give Patterns fake names so that they can be treated as named // Give Patterns fake names so that they can be treated as named
// "custom patterns" // "custom patterns"
p.NamedPatterns = make([]string, 0, len(p.Patterns)) p.NamedPatterns = make([]string, 0, len(p.Patterns))
@ -358,6 +365,10 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
return nil, fmt.Errorf("grok: must have one or more fields") return nil, fmt.Errorf("grok: must have one or more fields")
} }
if p.UniqueTimestamp != "auto" {
return metric.New(p.Measurement, tags, fields, timestamp)
}
return metric.New(p.Measurement, tags, fields, p.tsModder.tsMod(timestamp)) return metric.New(p.Measurement, tags, fields, p.tsModder.tsMod(timestamp))
} }

View File

@ -126,6 +126,7 @@ type Config struct {
GrokCustomPatterns string `toml:"grok_custom_patterns"` GrokCustomPatterns string `toml:"grok_custom_patterns"`
GrokCustomPatternFiles []string `toml:"grok_custom_pattern_files"` GrokCustomPatternFiles []string `toml:"grok_custom_pattern_files"`
GrokTimezone string `toml:"grok_timezone"` GrokTimezone string `toml:"grok_timezone"`
GrokUniqueTimestamp string `toml:"grok_unique_timestamp"`
//csv configuration //csv configuration
CSVColumnNames []string `toml:"csv_column_names"` CSVColumnNames []string `toml:"csv_column_names"`
@ -189,7 +190,8 @@ func NewParser(config *Config) (Parser, error) {
config.GrokNamedPatterns, config.GrokNamedPatterns,
config.GrokCustomPatterns, config.GrokCustomPatterns,
config.GrokCustomPatternFiles, config.GrokCustomPatternFiles,
config.GrokTimezone) config.GrokTimezone,
config.GrokUniqueTimestamp)
case "csv": case "csv":
parser, err = newCSVParser(config.MetricName, parser, err = newCSVParser(config.MetricName,
config.CSVHeaderRowCount, config.CSVHeaderRowCount,
@ -298,10 +300,9 @@ func newJSONParser(
//Deprecated: Use NewParser to get a JSONParser object //Deprecated: Use NewParser to get a JSONParser object
func newGrokParser(metricName string, func newGrokParser(metricName string,
patterns []string, patterns []string, nPatterns []string,
nPatterns []string, cPatterns string, cPatternFiles []string,
cPatterns string, tZone string, uniqueTimestamp string) (Parser, error) {
cPatternFiles []string, tZone string) (Parser, error) {
parser := grok.Parser{ parser := grok.Parser{
Measurement: metricName, Measurement: metricName,
Patterns: patterns, Patterns: patterns,
@ -309,6 +310,7 @@ func newGrokParser(metricName string,
CustomPatterns: cPatterns, CustomPatterns: cPatterns,
CustomPatternFiles: cPatternFiles, CustomPatternFiles: cPatternFiles,
Timezone: tZone, Timezone: tZone,
UniqueTimestamp: uniqueTimestamp,
} }
err := parser.Compile() err := parser.Compile()