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

Adding support for /proc/net/xfrm_stat for xfrm statistics #49

Merged
merged 5 commits into from
May 19, 2017

Conversation

pid1co
Copy link
Contributor

@pid1co pid1co commented May 15, 2017

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

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" +
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Member

@grobie grobie left a 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
@pid1co
Copy link
Contributor Author

pid1co commented May 17, 2017

I see some PR's people add themselves to the AUTHORS.MD file. Is that something I should do as part of this PR?

@SuperQ
Copy link
Member

SuperQ commented May 18, 2017

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.
Copy link
Member

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.

Copy link
Member

@grobie grobie left a 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) {
Copy link
Member

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.

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 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?

Copy link
Member

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.
Copy link
Member

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]
Copy link
Contributor

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)
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 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.

Copy link
Contributor Author

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.

Copy link
Contributor

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)

ian_anderson added 2 commits May 18, 2017 11:05
corrected docstring
added check for fields length to prevent panic
Copy link
Member

@grobie grobie left a 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) {
Copy link
Member

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) {

Copy link
Member

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())
Copy link
Member

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 {
Copy link
Member

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()

Copy link
Member

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
Copy link
Member

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?

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 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

@grobie
Copy link
Member

grobie commented May 19, 2017

Thanks a lot for your contribution!

@grobie grobie merged commit 65c1f6f into prometheus:master May 19, 2017
remijouannet pushed a commit to remijouannet/procfs that referenced this pull request Oct 20, 2022
…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
bobrik pushed a commit to bobrik/procfs that referenced this pull request Jan 14, 2023
fix loginuid test and documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants