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

feat: migrate grok to new parser style #11408

Merged
merged 3 commits into from
Jun 30, 2022
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
15 changes: 9 additions & 6 deletions plugins/inputs/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/csv"
"github.com/influxdata/telegraf/plugins/parsers/grok"
"github.com/influxdata/telegraf/plugins/parsers/json"
"github.com/influxdata/telegraf/testutil"
)
Expand Down Expand Up @@ -90,12 +90,15 @@ func TestGrokParser(t *testing.T) {
err := r.Init()
require.NoError(t, err)

parserConfig := parsers.Config{
DataFormat: "grok",
GrokPatterns: []string{"%{COMMON_LOG_FORMAT}"},
}
r.SetParserFunc(func() (telegraf.Parser, error) {
parser := &grok.Parser{
Patterns: []string{"%{COMMON_LOG_FORMAT}"},
Log: testutil.Logger{},
}
err := parser.Init()

r.SetParserFunc(func() (telegraf.Parser, error) { return parsers.NewParser(&parserConfig) })
return parser, err
})

err = r.Gather(&acc)
require.NoError(t, err)
Expand Down
23 changes: 11 additions & 12 deletions plugins/inputs/logparser/logparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/grok"
)

// DO NOT REMOVE THE NEXT TWO LINES! This is required to embed the sampleConfig data.
Expand Down Expand Up @@ -117,22 +118,20 @@ func (l *LogParserPlugin) Start(acc telegraf.Accumulator) error {
}

// Looks for fields which implement LogParser interface
config := &parsers.Config{
MetricName: mName,
GrokPatterns: l.GrokConfig.Patterns,
GrokNamedPatterns: l.GrokConfig.NamedPatterns,
GrokCustomPatterns: l.GrokConfig.CustomPatterns,
GrokCustomPatternFiles: l.GrokConfig.CustomPatternFiles,
GrokTimezone: l.GrokConfig.Timezone,
GrokUniqueTimestamp: l.GrokConfig.UniqueTimestamp,
DataFormat: "grok",
parser := grok.Parser{
Measurement: mName,
Patterns: l.GrokConfig.Patterns,
NamedPatterns: l.GrokConfig.NamedPatterns,
CustomPatterns: l.GrokConfig.CustomPatterns,
CustomPatternFiles: l.GrokConfig.CustomPatternFiles,
Timezone: l.GrokConfig.Timezone,
UniqueTimestamp: l.GrokConfig.UniqueTimestamp,
}

var err error
l.GrokParser, err = parsers.NewParser(config)
err := parser.Init()
if err != nil {
return err
}
l.GrokParser = &parser
models.SetLoggerOnPlugin(l.GrokParser, l.Log)

l.wg.Add(1)
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/logparser/logparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestStartNoParsers(t *testing.T) {
}

acc := testutil.Accumulator{}
require.Error(t, logparser.Start(&acc))
require.NoError(t, logparser.Start(&acc))
}

func TestGrokParseLogFilesNonExistPattern(t *testing.T) {
Expand Down
13 changes: 7 additions & 6 deletions plugins/inputs/tail/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/csv"
"github.com/influxdata/telegraf/plugins/parsers/grok"
"github.com/influxdata/telegraf/plugins/parsers/influx"
"github.com/influxdata/telegraf/plugins/parsers/json"
"github.com/influxdata/telegraf/testutil"
Expand Down Expand Up @@ -306,13 +307,13 @@ func TestGrokParseLogFilesWithMultilineTailerCloseFlushesMultilineBuffer(t *test
}

func createGrokParser() (parsers.Parser, error) {
grokConfig := &parsers.Config{
MetricName: "tail_grok",
GrokPatterns: []string{"%{TEST_LOG_MULTILINE}"},
GrokCustomPatternFiles: []string{filepath.Join(testdataDir, "test-patterns")},
DataFormat: "grok",
parser := &grok.Parser{
Measurement: "tail_grok",
Patterns: []string{"%{TEST_LOG_MULTILINE}"},
CustomPatternFiles: []string{filepath.Join(testdataDir, "test-patterns")},
Log: testutil.Logger{},
}
parser, err := parsers.NewParser(grokConfig)
err := parser.Init()
return parser, err
}

Expand Down
1 change: 1 addition & 0 deletions plugins/parsers/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/parsers/dropwizard"
_ "github.com/influxdata/telegraf/plugins/parsers/form_urlencoded"
_ "github.com/influxdata/telegraf/plugins/parsers/graphite"
_ "github.com/influxdata/telegraf/plugins/parsers/grok"
_ "github.com/influxdata/telegraf/plugins/parsers/json"
_ "github.com/influxdata/telegraf/plugins/parsers/json_v2"
_ "github.com/influxdata/telegraf/plugins/parsers/logfmt"
Expand Down
59 changes: 50 additions & 9 deletions plugins/parsers/grok/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/parsers"
)

var timeLayouts = map[string]string{
Expand Down Expand Up @@ -66,17 +67,17 @@ var (

// Parser is the primary struct to handle and grok-patterns defined in the config toml
type Parser struct {
Patterns []string
Patterns []string `toml:"grok_patterns"`
// namedPatterns is a list of internally-assigned names to the patterns
// specified by the user in Patterns.
// They will look like:
// GROK_INTERNAL_PATTERN_0, GROK_INTERNAL_PATTERN_1, etc.
NamedPatterns []string
CustomPatterns string
CustomPatternFiles []string
Measurement string
DefaultTags map[string]string
Log telegraf.Logger `toml:"-"`
NamedPatterns []string `toml:"grok_named_patterns"`
CustomPatterns string `toml:"grok_custom_patterns"`
CustomPatternFiles []string `toml:"grok_custom_pattern_files"`
Measurement string `toml:"-"`
DefaultTags map[string]string `toml:"-"`
Log telegraf.Logger `toml:"-"`

// Timezone is an optional component to help render log dates to
// your chosen zone.
Expand All @@ -85,11 +86,11 @@ type Parser struct {
// 1. Local -- interpret based on machine localtime
// 2. "America/Chicago" -- 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
Timezone string
Timezone string `toml:"grok_timezone"`
loc *time.Location

// UniqueTimestamp when set to "disable", timestamp will not incremented if there is a duplicate.
UniqueTimestamp string
UniqueTimestamp string `toml:"grok_unique_timestamp"`

// typeMap is a map of patterns -> capture name -> modifier,
// ie, {
Expand Down Expand Up @@ -558,3 +559,43 @@ func (t *tsModder) tsMod(ts time.Time) time.Time {
}
return ts.Add(t.incr*t.incrn + t.rollover)
}

// InitFromConfig is a compatibility function to construct the parser the old way
func (p *Parser) InitFromConfig(config *parsers.Config) error {
p.Measurement = config.MetricName
p.DefaultTags = config.DefaultTags
p.CustomPatterns = config.GrokCustomPatterns
p.CustomPatternFiles = config.GrokCustomPatternFiles
p.NamedPatterns = config.GrokNamedPatterns
p.Patterns = config.GrokPatterns
p.Timezone = config.GrokTimezone
p.UniqueTimestamp = config.GrokUniqueTimestamp

return p.Init()
}

func (p *Parser) Init() error {
if len(p.Patterns) == 0 {
p.Patterns = []string{"%{COMBINED_LOG_FORMAT}"}
}

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

if p.Timezone == "" {
p.Timezone = "Canada/Eastern"
}

return p.Compile()
}

func init() {
parsers.Add("grok",
func(defaultMetricName string) telegraf.Parser {
return &Parser{
Measurement: defaultMetricName,
}
},
)
}
28 changes: 0 additions & 28 deletions plugins/parsers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/parsers/grok"
"github.com/influxdata/telegraf/plugins/parsers/influx"
"github.com/influxdata/telegraf/plugins/parsers/influx/influx_upstream"
"github.com/influxdata/telegraf/plugins/parsers/nagios"
Expand Down Expand Up @@ -206,15 +205,6 @@ func NewParser(config *Config) (Parser, error) {
}
case "nagios":
parser, err = NewNagiosParser()
case "grok":
parser, err = newGrokParser(
config.MetricName,
config.GrokPatterns,
config.GrokNamedPatterns,
config.GrokCustomPatterns,
config.GrokCustomPatternFiles,
config.GrokTimezone,
config.GrokUniqueTimestamp)
case "prometheus":
parser, err = NewPrometheusParser(
config.DefaultTags,
Expand All @@ -240,24 +230,6 @@ func NewParser(config *Config) (Parser, error) {
return parser, err
}

func newGrokParser(metricName string,
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()
return &parser, err
}

func NewNagiosParser() (Parser, error) {
return &nagios.NagiosParser{}, nil
}
Expand Down