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

Replace some FreeBSD collectors with pure Go versions #385

Merged
merged 4 commits into from
Dec 31, 2016
Merged

Replace some FreeBSD collectors with pure Go versions #385

merged 4 commits into from
Dec 31, 2016

Conversation

dominikh
Copy link
Contributor

@dominikh dominikh commented Dec 26, 2016

This PR replaces the loadavg and filesystem collectors for FreeBSD with pure Go versions, using syscalls and sysctl directly.

It is possible that these may also work for other BSDs without changes, but I don't have any machines to test that on.

I use unsafe in two places, once for endianness reasons (loadavg) and once to avoid some memory allocations (file systems). I'm happy to remove either of those.

The code may also work for other BSDs, but I don't have access to those
for testing.
The code may also work for other BSDs, but I don't have access to those
for testing.
@SuperQ
Copy link
Member

SuperQ commented Dec 26, 2016

Very nice.

@discordianfish
Copy link
Member

Oh that's awesome!

I've tested this on darwin by using those collectors for all BSDs (see https://github.com/discordianfish/node_exporter/tree/used-dominikh-all-bsd is anyone wants to try it).

@matthiasr You happen to have some BSDs around and can quickly test my branch?
And in general: Do you have some ideas how we can such things in the future? It doesn't look like there is a cloud provider with all BSDs (including darwin) and I'd rather avoid using multiple providers.. Was thinking maybe just running a bunch of BSD VMs, but not sure how feasible that would be. Have you used vagrant to spin of BSD for example? That would probably be best option.

This removes some error handling, which should be fine. If the calls
fail, we will get the zeroes, which is a safe enough fallback.
Additionally, if the first sysctl (page_size) succeeded it is unlikely
that other ones will fail.
if err != nil {
return nil, err
}
load := *(*loadavg)(unsafe.Pointer((&b[0])))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, unsafe.Pointer((&b[0])) can be simplified to unsafe.Pointer(&b[0]).

}

var ro float64
if (fs.Flags & MNT_RDONLY) != 0 {
Copy link

@dmitshur dmitshur Dec 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent to if fs.Flags&MNT_RDONLY != 0 {, which is simpler and equally readable I think, since gofmt removes the spaces around & to help visualize the order of operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggested alternative looks like a blob of ink to me, precisely because of how gofmt formats it. Parentheses also help visualize the order of operations, but render much more readable in the case of bitmasks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, keeping it like it is is readable IMO

@dominikh
Copy link
Contributor Author

I've pushed another commit, rewriting the BSD meminfo collector to be cgo-free.

@dominikh
Copy link
Contributor Author

Now also for the CPU collector.

@discordianfish
Copy link
Member

LGTM! If you want to work on other collectors, better create a new PR so we can get this reviewed quicker.

@dominikh
Copy link
Contributor Author

@discordianfish I'm pretty much done; the initial two collectors and the two I pushed yesterday. There remain two collectors that use cgo but are not trivial to rewrite.

@dominikh
Copy link
Contributor Author

f0adcd1 fixes #339

@juliusv juliusv merged commit 269ee7a into prometheus:master Dec 31, 2016
@SuperQ SuperQ mentioned this pull request Jan 15, 2017
tklauser added a commit to tklauser/node_exporter that referenced this pull request Jan 8, 2020
Reuse the Go-only implementation already in place for FreeBSD (prometheus#385) on
Darwin, DragonflyBSD, NetBSD and OpenBSD.

Tested on all affected platforms.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
SuperQ pushed a commit that referenced this pull request Feb 18, 2020
Reuse the Go-only implementation already in place for FreeBSD (#385) on
Darwin, DragonflyBSD, NetBSD and OpenBSD.

Tested on all affected platforms.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
oblitorum pushed a commit to shatteredsilicon/node_exporter that referenced this pull request Apr 9, 2024
Reuse the Go-only implementation already in place for FreeBSD (prometheus#385) on
Darwin, DragonflyBSD, NetBSD and OpenBSD.

Tested on all affected platforms.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
oblitorum pushed a commit to shatteredsilicon/node_exporter that referenced this pull request Apr 9, 2024
Reuse the Go-only implementation already in place for FreeBSD (prometheus#385) on
Darwin, DragonflyBSD, NetBSD and OpenBSD.

Tested on all affected platforms.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
tamcore pushed a commit to gitgrave/node_exporter that referenced this pull request Oct 22, 2024
Synchronize common files from prometheus/prometheus
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.

5 participants