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

Parser processor #4551

Merged
merged 24 commits into from
Aug 22, 2018
Merged

Parser processor #4551

merged 24 commits into from
Aug 22, 2018

Conversation

Ayrdrie
Copy link
Contributor

@Ayrdrie Ayrdrie commented Aug 13, 2018

Required for all PRs:

closes #4427

  • Signed CLA.
  • Associated README.md updated.
  • Has appropriate unit tests.

// holds a default sample config
var SampleConfig = `

## specify the name of the field[s] whose value will be parsed
Copy link
Contributor

Choose a reason for hiding this comment

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

Double space the sample config and remove the empty line on line 20.


for _, metric := range metrics {
for _, key := range p.parseFields {
value := metric.Fields()[key]
Copy link
Contributor

Choose a reason for hiding this comment

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

It might not matter, but we could probably do if value, ok := metric.Fields()[key]; ok { here, to ensure there is a field value for the key.

Enhance tests
Ensure useable by telegraf
)

type Parser struct {
Config parsers.Config `toml:"config"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Use an embedded field for the parsers.Config:


type Parser struct {
	parsers.Config `toml:"config"`

This should make it so you can specify the options directly on the Parser object, similar to how it is set in input plugins:

[[processors.parser]]
  parse_fields = ["message"]
  original = "merge"
  data_format = "json"
  tag_keys = ["verb", "request"]

}
sMetrics := []telegraf.Metric{}
for _, key := range p.ParseFields {
if value, ok := metric.Fields()[key]; ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't use .Fields() since it allocates a new map, (I really need to add a comment suggesting that it not be used). Instead use .FieldList(). You will probably want to reorder the loops since it think ParseFields should always be shorter than Fields.

sMetrics := []telegraf.Metric{}
for _, key := range p.ParseFields {
if value, ok := metric.Fields()[key]; ok {
strVal := fmt.Sprintf("%v", value)
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's not a string then log a warning and skip.

log.Printf("E! [processors.parser] could not parse field %v: %v", key, err)
switch p.Original {
case "keep":
return metrics
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem right... don't we need to parse all fields before we can return?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch, the tests missed this.

case "keep":
return metrics
case "merge":
nMetrics = metrics
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm all confused by the variable names: rMetric, sMetric, nMetric. Can you try to improve these? The Metric part is less interesting than the r, s, n parts, since we can easily lookup the type.

}
sMetrics = append(sMetrics, nMetrics...)
} else {
fmt.Println("key not found", key)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would probably ignore this situation to allow fields to be optional.

}

func (p Parser) setName(name string, metrics ...telegraf.Metric) []telegraf.Metric {
if len(metrics) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't need this, if you remove it then it will just range over nothing and return metrics.

return nil
}

rMetric := metrics[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a problem if more than one metric is passed in to Apply. Secretly this never happens but the interface doesn't promise this.

Copy link
Contributor

Choose a reason for hiding this comment

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

More than one metric does get passed into this function, it is intended for all the tags and fields to get merged down to the first metric

Copy link
Contributor

Choose a reason for hiding this comment

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

More than one metric does get passed into this function, it is intended for all the tags and fields to get merged down to the first metric

@danielnelson danielnelson changed the title Fieldparser Parser processor Aug 21, 2018
@danielnelson danielnelson added this to the 1.8.0 milestone Aug 22, 2018
@danielnelson danielnelson merged commit 9f8de25 into master Aug 22, 2018
@danielnelson danielnelson deleted the fieldparser branch August 22, 2018 23:28
@russorat russorat mentioned this pull request Aug 27, 2018
rgitzel pushed a commit to rgitzel/telegraf that referenced this pull request Oct 17, 2018
otherpirate pushed a commit to otherpirate/telegraf that referenced this pull request Mar 15, 2019
otherpirate pushed a commit to otherpirate/telegraf that referenced this pull request Mar 15, 2019
dupondje pushed a commit to dupondje/telegraf that referenced this pull request Apr 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add logfmt Parser Plugin
3 participants