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 1 commit
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
Prev Previous commit
Set auto and finish configuring option
  • Loading branch information
glinton committed Feb 26, 2019
commit 73df739781763808b9f9a69968c27854171a7291
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
4 changes: 2 additions & 2 deletions plugins/parsers/grok/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ you will find the https://grokdebug.herokuapp.com application quite useful!
## 3. UTC -- or blank/unspecified, will return timestamp in UTC
grok_timezone = "Canada/Eastern"

## Setting to true will not increment the timestamp if there is a duplicate.
# disable_time_mod = false
## 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
10 changes: 7 additions & 3 deletions plugins/parsers/grok/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ type Parser struct {
Timezone string
loc *time.Location

// DisableTimeMod will not increment the timestamp if there is a duplicate.
DisableTimeMod bool
// 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, {
Expand Down Expand Up @@ -137,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 @@ -361,7 +365,7 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
return nil, fmt.Errorf("grok: must have one or more fields")
}

if p.DisableTimeMod {
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

}

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