-
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
Changes from 1 commit
67952f1
7143b60
f5e762f
e5943bb
5eadca5
dc4c419
b8d6d96
039d558
2091671
edd631a
525fc05
c7a7ab6
b696320
9044638
6ab3f04
95e01e0
7981757
0fc7149
bc220fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# DMCache Input Plugin | ||
|
||
This plugin provide a native collection for dmsetup based statistics for dm-cache | ||
|
||
## Configuration | ||
|
||
``` | ||
[[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 commentThe 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://github.com/raw/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 commentThe reason will be displayed to describe this comment to others. Learn more. All of the fields are basically taken from the output of the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dmcache | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
) | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I feel like the name |
||
} | ||
|
||
var sampleConfig = ` | ||
## Whether to report per-device stats or not | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indent by 2 spaces instead of tabs |
||
per_device = true | ||
` | ||
|
||
func (c *DMCache) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
func (c *DMCache) Description() string { | ||
return "Provide a native collection for dmsetup based statistics for dm-cache" | ||
} | ||
|
||
func init() { | ||
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 commentThe 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. |
||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Yes. It's required for sudo. |
||
if err != nil { | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Return an empty slice instead of nil |
||
} | ||
|
||
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 commentThe 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. |
||
|
||
return status, nil | ||
}, | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// +build linux | ||
|
||
package dmcache | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/influxdata/telegraf" | ||
) | ||
|
||
const metricName = "dmcache" | ||
|
||
var fieldNames = [...]string{ | ||
"metadata_used", | ||
"metadata_free", | ||
"cache_used", | ||
"cache_free", | ||
"read_hits", | ||
"read_misses", | ||
"write_hits", | ||
"write_misses", | ||
"demotions", | ||
"promotions", | ||
"dirty", | ||
} | ||
|
||
func (c *DMCache) Gather(acc telegraf.Accumulator) error { | ||
outputLines, err := c.rawStatus() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var total map[string]interface{} | ||
if !c.PerDevice { | ||
total = make(map[string]interface{}) | ||
} | ||
|
||
for _, s := range outputLines { | ||
fields := make(map[string]interface{}) | ||
data, err := parseDMSetupStatus(s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, f := range fieldNames { | ||
fields[f] = calculateSize(data, f) | ||
} | ||
|
||
if c.PerDevice { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
tags := map[string]string{"device": data["device"].(string)} | ||
acc.AddFields(metricName, fields, tags) | ||
} else { | ||
aggregateStats(total, fields) | ||
} | ||
} | ||
|
||
if !c.PerDevice { | ||
acc.AddFields(metricName, total, nil) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
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 commentThe 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() |
||
if r := recover(); r != nil { | ||
status = nil | ||
err = r.(error) | ||
} | ||
}() | ||
|
||
values := strings.Split(line, " ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you might want to use |
||
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 commentThe 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 commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is trimming the colon on the right, use TrimRight |
||
status["length"] = toInt(values[2]) | ||
status["target"] = values[3] | ||
status["metadata_blocksize"] = toInt(values[4]) | ||
status["metadata_used"] = toInt(strings.Split(values[5], "/")[0]) | ||
status["metadata_total"] = toInt(strings.Split(values[5], "/")[1]) | ||
status["cache_blocksize"] = toInt(values[6]) | ||
status["cache_used"] = toInt(strings.Split(values[7], "/")[0]) | ||
status["cache_total"] = toInt(strings.Split(values[7], "/")[1]) | ||
status["read_hits"] = toInt(values[8]) | ||
status["read_misses"] = toInt(values[9]) | ||
status["write_hits"] = toInt(values[10]) | ||
status["write_misses"] = toInt(values[11]) | ||
status["demotions"] = toInt(values[12]) | ||
status["promotions"] = toInt(values[13]) | ||
status["dirty"] = toInt(values[14]) | ||
status["blocksize"] = 512 | ||
|
||
return status, nil | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. don't panic, return error |
||
} | ||
return i | ||
} | ||
|
||
func calculateSize(data map[string]interface{}, key string) (value int) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. #1581 - this is the source of this solution. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
if key == "metadata_free" { | ||
value = data["metadata_total"].(int) - data["metadata_used"].(int) | ||
} else if key == "cache_free" { | ||
value = data["cache_total"].(int) - data["cache_used"].(int) - data["dirty"].(int) | ||
} else { | ||
value = data[key].(int) | ||
} | ||
|
||
if key == "metadata_free" || key == "metadata_used" { | ||
value = value * data["blocksize"].(int) * data["metadata_blocksize"].(int) | ||
} else { | ||
value = value * data["blocksize"].(int) * data["cache_blocksize"].(int) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the values after dirty, why do we not collect them? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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? |
||
return | ||
} | ||
|
||
func aggregateStats(total, fields map[string]interface{}) { | ||
for key, value := range fields { | ||
if _, ok := total[key]; ok { | ||
total[key] = total[key].(int) + value.(int) | ||
} else { | ||
total[key] = value.(int) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// +build !linux | ||
|
||
package dmcache | ||
|
||
import ( | ||
"github.com/influxdata/telegraf" | ||
) | ||
|
||
func (c *DMCache) Gather(acc telegraf.Accumulator) error { | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// +build linux | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
package dmcache | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/influxdata/telegraf/testutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func output2Devices() ([]string, error) { | ||
return []string{ | ||
"cs-1: 0 4883791872 cache 8 1018/1501122 512 7/464962 139 352643 15 46 0 7 0 1 writeback 2 migration_threshold 2048 mq 10 random_threshold 4 sequential_threshold 512 discard_promote_adjustment 1 read_promote_adjustment 4 write_promote_adjustment 8", | ||
"cs-2: 0 4294967296 cache 8 72352/1310720 128 26/24327168 2409 286 265 524682 0 0 0 1 writethrough 2 migration_threshold 2048 mq 10 random_threshold 4 sequential_threshold 512 discard_promote_adjustment 1 read_promote_adjustment 4 write_promote_adjustment 8", | ||
}, nil | ||
} | ||
|
||
var dmc1 = &DMCache{ | ||
PerDevice: true, | ||
rawStatus: output2Devices, | ||
} | ||
|
||
func TestDMCacheStats_1(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
var acc testutil.Accumulator | ||
|
||
err := dmc1.Gather(&acc) | ||
require.NoError(t, err) | ||
|
||
tags1 := map[string]string{ | ||
"device": "cs-1", | ||
} | ||
fields1 := map[string]interface{}{ | ||
"metadata_used": 4169728, | ||
"metadata_free": 6144425984, | ||
"cache_used": 1835008, | ||
"cache_free": 121885163520, | ||
"read_hits": 36438016, | ||
"read_misses": 92443246592, | ||
"write_hits": 3932160, | ||
"write_misses": 12058624, | ||
"demotions": 0, | ||
"promotions": 1835008, | ||
"dirty": 0, | ||
} | ||
acc.AssertContainsTaggedFields(t, "dmcache", fields1, tags1) | ||
|
||
tags2 := map[string]string{ | ||
"device": "cs-2", | ||
} | ||
fields2 := map[string]interface{}{ | ||
"metadata_used": 296353792, | ||
"metadata_free": 5072355328, | ||
"cache_used": 1703936, | ||
"cache_free": 1594303578112, | ||
"read_hits": 157876224, | ||
"read_misses": 18743296, | ||
"write_hits": 17367040, | ||
"write_misses": 34385559552, | ||
"demotions": 0, | ||
"promotions": 0, | ||
"dirty": 0, | ||
} | ||
acc.AssertContainsTaggedFields(t, "dmcache", fields2, tags2) | ||
} | ||
|
||
var dmc2 = &DMCache{ | ||
PerDevice: false, | ||
rawStatus: output2Devices, | ||
} | ||
|
||
func TestDMCacheStats_2(t *testing.T) { | ||
var acc testutil.Accumulator | ||
|
||
err := dmc2.Gather(&acc) | ||
require.NoError(t, err) | ||
|
||
tags := map[string]string{} | ||
|
||
fields := map[string]interface{}{ | ||
"metadata_used": 300523520, | ||
"metadata_free": 11216781312, | ||
"cache_used": 3538944, | ||
"cache_free": 1716188741632, | ||
"read_hits": 194314240, | ||
"read_misses": 92461989888, | ||
"write_hits": 21299200, | ||
"write_misses": 34397618176, | ||
"demotions": 0, | ||
"promotions": 1835008, | ||
"dirty": 0, | ||
} | ||
acc.AssertContainsTaggedFields(t, "dmcache", fields, tags) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
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