-
Notifications
You must be signed in to change notification settings - Fork 316
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
Adding support for /proc/net/xfrm_stat for xfrm statistics #49
Conversation
xfrm is an IP framework for transforming packets. This framework is used to implement the ipsec protocol. This is part of the iproute2 utilities More info can be found * http://kernelspec.blogspot.com/2014/10/ipsec-implementation-in-linux-kernel.h/tml * http://man7.org/linux/man-pages/man8/ip-xfrm.8.html
xfrm.go
Outdated
return x, err | ||
} | ||
|
||
ioFormat := "XfrmInError %d\nXfrmInBufferError %d\nXfrmInHdrError %d\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.
I'll leave it up to the maintainers, but IMO it would be much more maintainable to use a bufio.Scanner
and then map each field name to a struct field, discarding unknown fields as iteration continues.
This could look something like: https://play.golang.org/p/9UiIl8_j13.
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.
Sounds good to me, as it will be more resilient to different file formats, e.g. having additional / less / reordered lines.
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.
Thanks, looks great on first look.
Besides the concerns about using scanf, please include our Apache header at the top of all files (I need to fix some other files). Another small nit, our prometheus project style requirement is to write all comments as full sentence including a final period.
*Added apache license headers *Ended comments with period in accordance with documentation style
I see some PR's people add themselves to the AUTHORS.MD file. Is that something I should do as part of this PR? |
No, we do not use an AUTHORS file anymore. |
xfrm.go
Outdated
// limitations under the License. | ||
|
||
// Package procfs provides functions to retrieve system, kernel and process | ||
// metrics from the pseudo-filesystem proc. |
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 package should only get documented once, I believe we do this already in proc.go. Most projects use a doc.go for that.
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.
Looks good on first glance, will have a closer look tonight.
} | ||
|
||
// NewXfrmStat reads the xfrm_stat statistics. | ||
func NewXfrmStat() (XfrmStat, 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.
The current consensus goes towards not offering such helper methods. Let me know if you feel very strongly about this, otherwise let's remove 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.
I guess I am not seeing the point of diverging from how most of the other plugins are written. I was following the example that was set from other plugins in the repo. Is there an example of what you would prefer the format to be now?
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 see your point, let's leave it like this for now. We can change everything together at a later point.
xfrm.go
Outdated
return fs.NewXfrmStat() | ||
} | ||
|
||
// ParseXfrmStat reads the xfrm_stat statistics from the 'proc' filesystem. |
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.
Doc string doesn't match method name. @mdlayher also proposed to drop the New
prefix and just use XfrmStat()
.
xfrm.go
Outdated
|
||
for s.Scan() { | ||
fields := strings.Fields(s.Text()) | ||
name := fields[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.
Note that you have to check if len(fields) != 2
before accessing slice elements or else it may panic.
xfrm.go
Outdated
|
||
switch name { | ||
case "XfrmInError": | ||
x.XfrmInError, err = strconv.Atoi(value) |
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 move the strconv.Atoi(value)
outside of the switch to avoid all the repetitive lines.
Note that my example also removed the switch statement from the body of this function to keep it more concise.
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 move Atoi outside of switch statement, I will still be repeating whatever function i capture this logic under. I dont see the benefit.
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.
Doing the check once so you can do:
x.Field = value
... is much cleaner than:
x.Field, err = strconv.Atoi(value)
corrected docstring added check for fields length to prevent panic
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.
Thank you, looks good! Some very minor comments (the Prometheus project has a reputation of being a bit picky about style consistency ;-) ).
} | ||
|
||
// NewXfrmStat reads the xfrm_stat statistics. | ||
func NewXfrmStat() (XfrmStat, 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 see your point, let's leave it like this for now. We can change everything together at a later point.
xfrm.go
Outdated
|
||
// NewXfrmStat reads the xfrm_stat statistics from the 'proc' filesystem. | ||
func (fs FS) NewXfrmStat() (XfrmStat, 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.
Nit: For style consistency with the other methods, let's remove this newline here and on top of if err != nil
below.
xfrm.go
Outdated
|
||
if len(fields) != 2 { | ||
return XfrmStat{}, fmt.Errorf( | ||
"Couldnt parse %s line %s", file.Name(), s.Text()) |
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's style guide suggests to not use capitalized error messages https://github.com/golang/go/wiki/CodeReviewComments#error-strings.
xfrm.go
Outdated
x.XfrmAcquireError = value | ||
} | ||
|
||
if err != 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.
Let's move this above right below the Atoi call.
xfrm_test.go
Outdated
|
||
func TestXfrmStats(t *testing.T) { | ||
xfrmStats, err := FS("fixtures").NewXfrmStat() | ||
|
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.
Nit: remove.
XfrmOutPolDead int | ||
// Policy Error | ||
XfrmOutPolError int | ||
XfrmFwdHdrError 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.
Is there a reason these three fields don't have a 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.
I was unable to find documentation as to what they mean. XFRM is notoriously bad at documentation. The fields with comments are taken from the proc man page
Thanks a lot for your contribution! |
…s#49) * Adding support for /proc/net/xfrm_stat for xfrm statistics xfrm is an IP framework for transforming packets. This framework is used to implement the ipsec protocol. This is part of the iproute2 utilities More info can be found * http://kernelspec.blogspot.com/2014/10/ipsec-implementation-in-linux-kernel.h/tml * http://man7.org/linux/man-pages/man8/ip-xfrm.8.html * converted to bufio.Scanner / switch statement. *Added apache license headers *Ended comments with period in accordance with documentation style * removing package documentation as its redundant corrected docstring added check for fields length to prevent panic * move atoi outside of switch statement * fixing requested formatting/style changes
fix loginuid test and documentation
xfrm is an IP framework for transforming packets. This framework is used
to implement the ipsec protocol. This is part of the iproute2 utilities
More info can be found