Skip to content

Commit

Permalink
Migrate logfmt parser to the new-style.
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Jun 29, 2022
1 parent d8c293e commit c536cd9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
1 change: 1 addition & 0 deletions plugins/parsers/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/parsers/graphite"
_ "github.com/influxdata/telegraf/plugins/parsers/json"
_ "github.com/influxdata/telegraf/plugins/parsers/json_v2"
_ "github.com/influxdata/telegraf/plugins/parsers/logfmt"
_ "github.com/influxdata/telegraf/plugins/parsers/value"
_ "github.com/influxdata/telegraf/plugins/parsers/wavefront"
_ "github.com/influxdata/telegraf/plugins/parsers/xpath"
Expand Down
28 changes: 19 additions & 9 deletions plugins/parsers/logfmt/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/parsers"
)

var ErrNoMetric = errors.New("no metric in line")
Expand All @@ -24,15 +25,6 @@ type Parser struct {
tagFilter filter.Filter
}

// NewParser creates a parser.
func NewParser(metricName string, defaultTags map[string]string, tagKeys []string) *Parser {
return &Parser{
metricName: metricName,
DefaultTags: defaultTags,
TagKeys: tagKeys,
}
}

// Parse converts a slice of bytes in logfmt format to metrics.
func (p *Parser) Parse(b []byte) ([]telegraf.Metric, error) {
reader := bytes.NewReader(b)
Expand Down Expand Up @@ -122,3 +114,21 @@ func (p *Parser) Init() error {

return nil
}

func init() {
// Register parser
parsers.Add("logfmt",
func(defaultMetricName string) telegraf.Parser {
return &Parser{metricName: defaultMetricName}
},
)
}

// InitFromConfig is a compatibility function to construct the parser the old way
func (p *Parser) InitFromConfig(config *parsers.Config) error {
p.metricName = config.MetricName
p.DefaultTags = config.DefaultTags
p.TagKeys = append(p.TagKeys, config.LogFmtTagKeys...)

return p.Init()
}
15 changes: 9 additions & 6 deletions plugins/parsers/logfmt/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParse(t *testing.T) {
Expand Down Expand Up @@ -261,15 +261,18 @@ func TestTags(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := NewParser(tt.measurement, map[string]string{}, tt.tagKeys)
assert.NoError(t, l.Init())
l := &Parser{
metricName: tt.measurement,
DefaultTags: map[string]string{},
TagKeys: tt.tagKeys,
}
require.NoError(t, l.Init())

got, err := l.ParseLine(tt.s)

if tt.wantErr {
assert.Error(t, err)
require.Error(t, err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
testutil.RequireMetricEqual(t, tt.want, got, testutil.IgnoreTime())
})
Expand Down
10 changes: 0 additions & 10 deletions plugins/parsers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"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/logfmt"
"github.com/influxdata/telegraf/plugins/parsers/nagios"
"github.com/influxdata/telegraf/plugins/parsers/prometheus"
"github.com/influxdata/telegraf/plugins/parsers/prometheusremotewrite"
Expand Down Expand Up @@ -227,8 +226,6 @@ func NewParser(config *Config) (Parser, error) {
config.GrokCustomPatternFiles,
config.GrokTimezone,
config.GrokUniqueTimestamp)
case "logfmt":
parser, err = NewLogFmtParser(config.MetricName, config.DefaultTags, config.LogFmtTagKeys)
case "prometheus":
parser, err = NewPrometheusParser(
config.DefaultTags,
Expand Down Expand Up @@ -310,13 +307,6 @@ func NewDropwizardParser(
return parser, err
}

// NewLogFmtParser returns a logfmt parser with the default options.
func NewLogFmtParser(metricName string, defaultTags map[string]string, tagKeys []string) (Parser, error) {
parser := logfmt.NewParser(metricName, defaultTags, tagKeys)
err := parser.Init()
return parser, err
}

func NewPrometheusParser(defaultTags map[string]string, ignoreTimestamp bool) (Parser, error) {
return &prometheus.Parser{
DefaultTags: defaultTags,
Expand Down

0 comments on commit c536cd9

Please sign in to comment.