-
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
Add dmcache input plugin #1667
Add dmcache input plugin #1667
Conversation
Checking in on this PR, the CircleCI issues weren't fails from the dmcache code but from other components. |
yes, it does appear so, I'll slate this for 1.3 and try to get it reviewed soon |
plugins/inputs/dmcache/README.md
Outdated
|
||
``` | ||
[[inputs.dmcache]] | ||
## Whether to report per-device stats or not |
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.
these lines should both be indented by two spaces
plugins/inputs/dmcache/dmcache.go
Outdated
} | ||
|
||
var sampleConfig = ` | ||
## Whether to report per-device stats or not |
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.
indent by 2 spaces instead of tabs
} | ||
|
||
func parseDMSetupStatus(line string) (status map[string]interface{}, err error) { | ||
defer func() { |
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.
why would this function panic? you should write your code to avoid panics rather than write a recover()
} | ||
}() | ||
|
||
values := strings.Split(line, " ") |
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.
you might want to use strings.Fields
here in case of multiple spaces or tabs
values := strings.Split(line, " ") | ||
status = make(map[string]interface{}) | ||
|
||
status["device"] = values[0][:len(values[0])-1] |
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.
all of these should check length of the array to avoid panicking.
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.
or you can check that the array is of a minimum length, and just ignore all metrics if it isn't.
func toInt(s string) int { | ||
i, err := strconv.Atoi(s) | ||
if err != nil { | ||
panic(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.
don't panic, return error
@@ -0,0 +1,94 @@ | |||
// +build linux |
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 believe you can run these tests on non-linux system as well
plugins/inputs/dmcache/dmcache.go
Outdated
return &DMCache{ | ||
PerDevice: true, | ||
rawStatus: func() ([]string, error) { | ||
out, err := exec.Command("/bin/sh", "-c", "sudo /sbin/dmsetup status --target cache").Output() |
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.
you need to document that this plugin requires sudo, and that the telegraf user needs to be setup to be able to execute sudo without a password.
You should also document that sudo /sbin/dmsetup status --target cache
is the full command that telegraf will run for debugging purposes.
fields[f] = calculateSize(data, f) | ||
} | ||
|
||
if c.PerDevice { |
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 if-statement doesn't seem correct to me.
If the user sets per_device = true, then it should report the per-device stats AND total stats.
Is there a reason users would not want total stats? If so it should be a separate config option.
All requested changes were done. |
plugins/inputs/dmcache/dmcache.go
Outdated
return &DMCache{ | ||
PerDevice: true, | ||
rawStatus: func() ([]string, error) { | ||
out, err := exec.Command("/bin/sh", "-c", "sudo /sbin/dmsetup status --target cache").Output() |
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.
Why is this being ran in a shell instead of just being directly executed?
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.
Maybe it is required for sudo?
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.
Yes. It's required for sudo.
plugins/inputs/dmcache/dmcache.go
Outdated
inputs.Add("dmcache", func() telegraf.Input { | ||
return &DMCache{ | ||
PerDevice: true, | ||
rawStatus: func() ([]string, error) { |
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 rather have this be a named function that you pass in to the struct.
plugins/inputs/dmcache/dmcache.go
Outdated
return nil, err | ||
} | ||
if string(out) == "No devices found\n" { | ||
return nil, nil |
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.
Return an empty slice instead of nil
plugins/inputs/dmcache/dmcache.go
Outdated
} | ||
|
||
status := strings.Split(string(out), "\n") | ||
status = status[:len(status)-1] // removing last empty line |
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 will panic if there is status is not at least length 1, I suggest you strings.TrimRight(out, "\n") before splitting lines.
status := make(map[string]interface{}) | ||
values := strings.Fields(line) | ||
if len(values) < 15 { | ||
return nil, errors.New("dmsetup status data have invalid 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.
Grammar, I suggest "Output from dmsetup could not be parsed"
if err != nil { | ||
return nil, err | ||
} | ||
status["cache_used"], err = strconv.Atoi(strings.Split(values[7], "/")[0]) |
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.
Split once, make sure it return len of 2
|
||
func parseDMSetupStatus(line string) (map[string]interface{}, error) { | ||
var err error | ||
status := make(map[string]interface{}) |
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.
You should define a struct for these values, that way less casting is needed later
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.
But, in this case, function "calculateSize" will require to use reflection to get value by key.
return status, nil | ||
} | ||
|
||
func calculateSize(data map[string]interface{}, key string) (value int) { |
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.
If I understand this correctly, we are synthesizing a few aggregate values in this function? Why not just provide the values dmsetup returns, since the user can always calculate these at query time.
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.
#1581 - this is the source of this solution.
@daviesalex could you comment?
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.
Thats exactly what is going on. I had a look at it and I agree, these should be do-able at query time without drama (in very old versions of InfluxDB it was a challenge to do everything at query time if you wanted to select if I recall, which is why I think this started off this way).
@vlasad I suggest we remove this. If we did want to do this, the neat way to do it is probably a output plugin in telegraf.
@sebito91 FYI
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.
removed
"dirty": 0, | ||
} | ||
acc.AssertContainsTaggedFields(t, "dmcache", fields, 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.
The tests need to be made more comprehensive. Please try to think of some more corner cases and error conditions to test for. Some things that come to mind is the "No devices found" error and errors from rawOutput(). Try checking the coverage for more ideas.
plugins/inputs/dmcache/dmcache.go
Outdated
|
||
type DMCache struct { | ||
PerDevice bool `toml:"per_device"` | ||
rawStatus func() ([]string, error) |
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 the name rawStatus
is not fitting, since this functions both executes the command and does some processing on the output.
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 noticed while reading through the manpages for dmsetup that there is a stats subcommand. From what I gathered it requires the user to define a "region" before it can be used. Have you used this before? How do the stats it provides compare to the ones from the status command?
plugins/inputs/dmcache/README.md
Outdated
[[inputs.dmcache]] | ||
## Whether to report per-device stats or not | ||
per_device = true | ||
``` |
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 sure you document the tags/fields and stick to the format of the example as closely as possible (for instance little things such as three #'s for Configuration.)
https://raw.githubusercontent.com/influxdata/telegraf/master/plugins/inputs/EXAMPLE_README.md
How did you go about naming the fields? Can you link to any documentation you may have used?
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.
All of the fields are basically taken from the output of the dmsetup status --target=cache
output for the given disks. In this case, we want to split all the data into multiple lines per disk we set up.
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.
So the output of that command, based on the test cases, is more or less a table of values. Where did you find out what each column represents?
plugins/inputs/dmcache/dmcache.go
Outdated
return "Provide a native collection for dmsetup based statistics for dm-cache" | ||
} | ||
|
||
func dmSetupStatus() ([]string, error) { |
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 this should go into dmcache_linux since it is linux specific.
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 is fine, how does one accomplish this on Windows (not a Windows user/fan).
return nil, parseError | ||
} | ||
|
||
status["device"] = strings.TrimRight(values[0], ":") |
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.
Still want these in a struct, this separates the responsibilities. Call it something appropriate that represents the data, maybe CacheStatus. I get why you did this but I think it will clear up some weirdness in the code, such as taking device
from fields to add to tags. You won't need to do type conversions and summing will be more explicit. This open PR does a good job. #2031
"device": "cs-1", | ||
} | ||
fields1 := map[string]interface{}{ | ||
"device": "cs-1", |
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.
Device shouldn't be both a tag and a field.
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.
that is valid, in the actual output it's not a field but just a tag.
if err != nil { | ||
return nil, 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.
What about the values after dirty, why do we not collect them?
if err != nil { | ||
return nil, 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.
What about the values after dirty, do we need to collect them?
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.
Not sure what you're asking here, are you questioning why we're collecting the "dirty" blocks?
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 noticed in the sample output included in the tests that there are more values after the column containing "dirty". Are these metrics important, should we be collecting them, and why?
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.
Appreciate the feedback on this, it's been a long while getting this collector on-boarded.
Some of the changes you mention are fair, but some of the others I'd have to disagree. In particular, using the dmsetup stats
is separate altogether and requires its own configuration outside of a standard lvmcache entry. The data that comes back from the status command we've listed shows just the necessary data for the particular cache-disk.
https://www.kernel.org/doc/Documentation/device-mapper/cache.txt
plugins/inputs/dmcache/README.md
Outdated
[[inputs.dmcache]] | ||
## Whether to report per-device stats or not | ||
per_device = true | ||
``` |
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.
All of the fields are basically taken from the output of the dmsetup status --target=cache
output for the given disks. In this case, we want to split all the data into multiple lines per disk we set up.
plugins/inputs/dmcache/dmcache.go
Outdated
return "Provide a native collection for dmsetup based statistics for dm-cache" | ||
} | ||
|
||
func dmSetupStatus() ([]string, error) { |
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 is fine, how does one accomplish this on Windows (not a Windows user/fan).
"device": "cs-1", | ||
} | ||
fields1 := map[string]interface{}{ | ||
"device": "cs-1", |
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.
that is valid, in the actual output it's not a field but just a tag.
if err != nil { | ||
return nil, 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.
Not sure what you're asking here, are you questioning why we're collecting the "dirty" blocks?
Here is some output to (hopefully) provide some clarity...
|
@danielnelson, @sparrc we've made the proposed changes if you can please give it another look? |
plugins/inputs/dmcache/README.md
Outdated
``` | ||
$ ./telegraf --test --config /etc/telegraf/telegraf.conf --input-filter dmcache | ||
* Plugin: inputs.dmcache, Collection 1 | ||
> dmcache,bu=linux,cls=server,dc=colo,device=vg02-splunk_data_lv,env=production,host=hostname,sr=splunk_indexer,trd=false cache_free=0i,cache_used=1099511627776i,demotions=3265223720960i,dirty=0i,metadata_free=1060880384i,metadata_used=12861440i,promotions=3265223720960i,read_hits=995134034411520i,read_misses=916807089127424i,write_hits=195107267543040i,write_misses=563725346013184i 1491362011000000000 |
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.
Can you clean out the custom tags you have?
tags := map[string]string{"device": status.device} | ||
acc.AddFields(metricName, fields, tags) | ||
} | ||
aggregateStats(total, fields) |
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.
Switch to fields at the last moment. Overall flow should be get the cacheStatus, aggregate, then convert to fields and emit as needed.
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.
Just a minor style thing in the tests and then add this to the CHANGELOG.md
.
getCurrentStatus: output2Devices, | ||
} | ||
|
||
func TestDMCacheStats_1(t *testing.T) { |
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.
Can you move the DMCache initialization into the function and rename the tests like TestWhateverThisTests
.
I just noticed this, can you get it? |
I cannot get it. |
On the non linux builds. |
I was also working on a dmcache plugin for Telegraf, but put it aside whilst waiting for another plugin I wrote to be accepted, during what seems to have been quite a long moratorium on accepting new plugins. I took a slightly different approach, and implemented the ioctls so that it does not require os.Exec(). However, it does mean that the telegraf process must be able to open /dev/mapper/control with O_RDWR. If people are interested, I could merge my approach into this plugin. See my unanswered comment last November in #1581. |
@dswarbrick I'll reply over on the issue. |
Required for all PRs: