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

Plugin/reader each interval #4332

Merged
merged 46 commits into from
Jul 14, 2018
Merged

Plugin/reader each interval #4332

merged 46 commits into from
Jul 14, 2018

Conversation

maxunt
Copy link
Contributor

@maxunt maxunt commented Jun 21, 2018

closes #3479
closes #3883

Required for all PRs:

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

Copy link
Contributor

@glinton glinton left a comment

Choose a reason for hiding this comment

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

Reviewing specifically for the plugin dev framework

@@ -0,0 +1,106 @@

Copy link
Contributor

Choose a reason for hiding this comment

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

Slim this file down by removing comments and default values. See other example.

telegraf.conf Outdated
@@ -0,0 +1,104 @@

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this file entirely, no need to have a global telegraf.conf

entrypoint:
- /telegraf
- --config
- /telegraf.conf
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: add newline at the end of this and json_a.log

Makefile Outdated
@@ -92,4 +92,16 @@ docker-image:
plugins/parsers/influx/machine.go: plugins/parsers/influx/machine.go.rl
ragel -Z -G2 $^ -o $@

.PHONY: deps telegraf install test test-windows lint vet test-all package clean docker-image fmtcheck uint64
static:
Copy link
Contributor

Choose a reason for hiding this comment

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

This may cause a merge conflict when we merge #4324 unless you merged that branch into yours.

Copy link
Contributor

Choose a reason for hiding this comment

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

What I do when I need to have parts from another commit/branch is either cherry-pick that commit or make the change to the shared file, and just not commit the changes to that file from my new branch.

@glinton glinton force-pushed the plugin/reader branch 2 times, most recently from 513ec70 to 7fa27f4 Compare June 26, 2018 23:06
@glinton
Copy link
Contributor

glinton commented Jun 27, 2018

Resolves #3479
Resolves #3883

default:
err = fmt.Errorf("Invalid data format: %s", config.DataFormat)
}
return parser, err
}

func NewGrokParser(metricName string,
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to a comment on 4351, lets not export this function.

Copy link
Contributor

@danielnelson danielnelson left a comment

Choose a reason for hiding this comment

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

In addition to the comments, we should also update logparser to use the new grok parser internally.

delete(tbl.Fields, "patterns")
delete(tbl.Fields, "custom_patterns")
delete(tbl.Fields, "custom_pattern_files")
delete(tbl.Fields, "timezone")
Copy link
Contributor

Choose a reason for hiding this comment

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

Since these all are at plugin level now, lets prefix them with grok_

## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = ""
Copy link
Contributor

Choose a reason for hiding this comment

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

Use 2 space indention in the sample config, make sure to update the README as well. You can generate the config for the readme with telegraf -usage reader.

func (r *Reader) refreshFilePaths() {
var allFiles []string
for _, filepath := range r.Filepaths {
g, err := globpath.Compile(filepath)
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 compile only once and store the result on the Reader struct. Since we don't have a constructor function yet, I would probably check if the value on the struct is nil and if so compile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thought was that if people want to specify a directory via some globpath, then this will check for any new files added there during runtime

for _, filepath := range r.Filepaths {
g, err := globpath.Compile(filepath)
if err != nil {
log.Printf("E! Error Glob %s failed to compile, %s", filepath, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This error should probably stop the input since it indicates a user error, if any glob fails return the error up and out of Gather.

func (r *Reader) readMetric(filename string) ([]telegraf.Metric, error) {
fileContents, err := ioutil.ReadFile(filename)
if err != nil {
log.Printf("E! File could not be opened: %v", filename)
Copy link
Contributor

Choose a reason for hiding this comment

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

Error should be along the lines of "File could not be read" for accuracy, but also this error should be returned instead of logged. In general it is not safe to do anything with the return value if an error occurs.

log.Printf("E! Metric could not be parsed from: %v, on line %v", k, i)
continue
}
acc.AddFields(m.Name(), m.Fields(), m.Tags())
Copy link
Contributor

Choose a reason for hiding this comment

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

Also add the m.Time().

assert.Equal(t, 2, len(acc.Metrics))
}

func getPluginDir() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the cwd is actually guaranteed by go test to be the directory containing the test file.

Copy link
Contributor

Choose a reason for hiding this comment

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

Check if this is required and if not remove.

@@ -0,0 +1,73 @@
# Captures are a slightly modified version of logstash "grok" patterns, with
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like I should know this, but... any idea why we need this file when we have influx_patterns.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure if it was something relevant. I was under the impression that it was simply the text from influx_patterns.go. I think it can be removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Go ahead and remove this.

NamedPatterns []string
CustomPatterns string
CustomPatternFiles []string
Measurement string
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's remove Measurement, we already have name_override which does the same thing.

@@ -0,0 +1,19 @@
package grok
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs more tests, probably easiest to just bring over all tests from grok_test.go

grok_custom_pattern_files = []

## Custom patterns can also be defined here. Put one pattern per line.
grok_custom_patterns = '''
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need a closing ''' for this below.

return err
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like we are removing l.parsers, I think this is good but also remove the reflect bit above 156:175 and the field from the LogParserPlugin struct.

)

type Reader struct {
Filepaths []string `toml:"files"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Call this Files, if its good enough for the config its good enough for the struct, and it will be less confusing.

FromBeginning bool
parser parsers.Parser

Filenames []string
Copy link
Contributor

Choose a reason for hiding this comment

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

Make this a unexported field with a lowercase letter, otherwise you can set this from the config file which is undesired.

for _, filepath := range r.Filepaths {
g, err := globpath.Compile(filepath)
if err != nil {
return fmt.Errorf("E! Error Glob: %v could not be compiled, %s", filepath, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to my comment on the json parser, since this is an error don't include E! and try to shorten: could not compile glob %q: %v

}

func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
lines := strings.Split(string(buf), "\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

Use bufio.Scanner so that line splitting will work on files with DOS line endings.

CustomPatterns string
CustomPatternFiles []string
Measurement string
MetricName string
Copy link
Contributor

Choose a reason for hiding this comment

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

Keep this named Measurement.

@@ -0,0 +1,73 @@
# Captures are a slightly modified version of logstash "grok" patterns, with
Copy link
Contributor

Choose a reason for hiding this comment

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

Go ahead and remove this.

"ignored_string": "hello, world!"
},
"another_list": [4]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add end of line for this file, also fix the indention.

assert.Equal(t, 2, len(acc.Metrics))
}

func getPluginDir() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Check if this is required and if not remove.

@danielnelson danielnelson added this to the 1.8.0 milestone Jul 14, 2018
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.

Input to read and parse a file every interval Add Grok as top level parser to be used with any plugin
3 participants