Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable timestamp adjustment in grok parser #5488

Merged
merged 2 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
if node, ok := tbl.Fields["csv_column_names"]; ok {
if kv, ok := node.(*ast.KeyValue); ok {
Expand Down Expand Up @@ -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_pattern_files")
delete(tbl.Fields, "grok_timezone")
delete(tbl.Fields, "grok_unique_timestamp")
delete(tbl.Fields, "csv_column_names")
delete(tbl.Fields, "csv_column_types")
delete(tbl.Fields, "csv_comment")
Expand Down
3 changes: 3 additions & 0 deletions plugins/parsers/grok/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
## 3. UTC -- or blank/unspecified, will return timestamp in UTC
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
Expand Down
11 changes: 11 additions & 0 deletions plugins/parsers/grok/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ type Parser struct {
Timezone string
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,
// ie, {
// "%{TESTLOG}":
Expand Down Expand Up @@ -134,6 +137,10 @@ func (p *Parser) Compile() error {
return err
}

if p.UniqueTimestamp == "" {
p.UniqueTimestamp = "auto"
}

// Give Patterns fake names so that they can be treated as named
// "custom patterns"
p.NamedPatterns = make([]string, 0, len(p.Patterns))
Expand Down Expand Up @@ -358,6 +365,10 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
return nil, fmt.Errorf("grok: must have one or more fields")
}

if p.UniqueTimestamp != "auto" {
return metric.New(p.Measurement, tags, fields, timestamp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is backwards, isn't it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, not equals

}

return metric.New(p.Measurement, tags, fields, p.tsModder.tsMod(timestamp))
}

Expand Down
12 changes: 7 additions & 5 deletions plugins/parsers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type Config struct {
GrokCustomPatterns string `toml:"grok_custom_patterns"`
GrokCustomPatternFiles []string `toml:"grok_custom_pattern_files"`
GrokTimezone string `toml:"grok_timezone"`
GrokUniqueTimestamp string `toml:"grok_unique_timestamp"`

//csv configuration
CSVColumnNames []string `toml:"csv_column_names"`
Expand Down Expand Up @@ -189,7 +190,8 @@ func NewParser(config *Config) (Parser, error) {
config.GrokNamedPatterns,
config.GrokCustomPatterns,
config.GrokCustomPatternFiles,
config.GrokTimezone)
config.GrokTimezone,
config.GrokUniqueTimestamp)
case "csv":
parser, err = newCSVParser(config.MetricName,
config.CSVHeaderRowCount,
Expand Down Expand Up @@ -298,17 +300,17 @@ func newJSONParser(

//Deprecated: Use NewParser to get a JSONParser object
func newGrokParser(metricName string,
patterns []string,
nPatterns []string,
cPatterns string,
cPatternFiles []string, tZone string) (Parser, error) {
patterns []string, nPatterns []string,
cPatterns string, cPatternFiles []string,
tZone string, uniqueTimestamp string) (Parser, error) {
parser := grok.Parser{
Measurement: metricName,
Patterns: patterns,
NamedPatterns: nPatterns,
CustomPatterns: cPatterns,
CustomPatternFiles: cPatternFiles,
Timezone: tZone,
UniqueTimestamp: uniqueTimestamp,
}

err := parser.Compile()
Expand Down