-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Conversation
There was a problem hiding this 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 @@ | |||
|
There was a problem hiding this comment.
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 @@ | |||
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
513ec70
to
7fa27f4
Compare
plugins/parsers/registry.go
Outdated
default: | ||
err = fmt.Errorf("Invalid data format: %s", config.DataFormat) | ||
} | ||
return parser, err | ||
} | ||
|
||
func NewGrokParser(metricName string, |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
internal/config/config.go
Outdated
delete(tbl.Fields, "patterns") | ||
delete(tbl.Fields, "custom_patterns") | ||
delete(tbl.Fields, "custom_pattern_files") | ||
delete(tbl.Fields, "timezone") |
There was a problem hiding this comment.
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_
plugins/inputs/reader/reader.go
Outdated
## 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 = "" |
There was a problem hiding this comment.
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
.
plugins/inputs/reader/reader.go
Outdated
func (r *Reader) refreshFilePaths() { | ||
var allFiles []string | ||
for _, filepath := range r.Filepaths { | ||
g, err := globpath.Compile(filepath) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
plugins/inputs/reader/reader.go
Outdated
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) |
There was a problem hiding this comment.
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.
plugins/inputs/reader/reader.go
Outdated
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) |
There was a problem hiding this comment.
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.
plugins/inputs/reader/reader.go
Outdated
log.Printf("E! Metric could not be parsed from: %v, on line %v", k, i) | ||
continue | ||
} | ||
acc.AddFields(m.Name(), m.Fields(), m.Tags()) |
There was a problem hiding this comment.
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().
plugins/inputs/reader/reader_test.go
Outdated
assert.Equal(t, 2, len(acc.Metrics)) | ||
} | ||
|
||
func getPluginDir() string { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
plugins/parsers/grok/influx-patterns
Outdated
@@ -0,0 +1,73 @@ | |||
# Captures are a slightly modified version of logstash "grok" patterns, with |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
plugins/parsers/grok/parser.go
Outdated
NamedPatterns []string | ||
CustomPatterns string | ||
CustomPatternFiles []string | ||
Measurement string |
There was a problem hiding this comment.
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.
plugins/parsers/grok/parser_test.go
Outdated
@@ -0,0 +1,19 @@ | |||
package grok |
There was a problem hiding this comment.
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 = ''' |
There was a problem hiding this comment.
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 | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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.
plugins/inputs/reader/reader.go
Outdated
) | ||
|
||
type Reader struct { | ||
Filepaths []string `toml:"files"` |
There was a problem hiding this comment.
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.
plugins/inputs/reader/reader.go
Outdated
FromBeginning bool | ||
parser parsers.Parser | ||
|
||
Filenames []string |
There was a problem hiding this comment.
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.
plugins/inputs/reader/reader.go
Outdated
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) |
There was a problem hiding this comment.
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
plugins/parsers/grok/parser.go
Outdated
} | ||
|
||
func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) { | ||
lines := strings.Split(string(buf), "\n") |
There was a problem hiding this comment.
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.
plugins/parsers/grok/parser.go
Outdated
CustomPatterns string | ||
CustomPatternFiles []string | ||
Measurement string | ||
MetricName string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep this named Measurement.
plugins/parsers/grok/influx-patterns
Outdated
@@ -0,0 +1,73 @@ | |||
# Captures are a slightly modified version of logstash "grok" patterns, with |
There was a problem hiding this comment.
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] | ||
} |
There was a problem hiding this comment.
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.
plugins/inputs/reader/reader_test.go
Outdated
assert.Equal(t, 2, len(acc.Metrics)) | ||
} | ||
|
||
func getPluginDir() string { |
There was a problem hiding this comment.
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.
closes #3479
closes #3883
Required for all PRs: