-
Notifications
You must be signed in to change notification settings - Fork 242
feat(metrics): adding prometheus metrics for dc power extension #556
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package kvm | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| var ( | ||
| dcCurrentGauge = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
| Name: "jetkvm_dc_current_amperes", | ||
| Help: "Current DC power consumption in amperes", | ||
| }) | ||
|
|
||
| dcPowerGauge = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
| Name: "jetkvm_dc_power_watts", | ||
| Help: "DC power consumption in watts", | ||
| }) | ||
|
|
||
| dcVoltageGauge = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
| Name: "jetkvm_dc_voltage_volts", | ||
| Help: "DC voltage in volts", | ||
| }) | ||
|
|
||
| dcStateGauge = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
| Name: "jetkvm_dc_power_state", | ||
| Help: "DC power state (1 = on, 0 = off)", | ||
| }) | ||
|
|
||
| dcMetricsRegistered sync.Once | ||
| ) | ||
|
|
||
| // registerDCMetrics registers the DC power metrics with Prometheus (called once when DC control is mounted) | ||
| func registerDCMetrics() { | ||
| dcMetricsRegistered.Do(func() { | ||
| prometheus.MustRegister(dcCurrentGauge) | ||
| prometheus.MustRegister(dcPowerGauge) | ||
| prometheus.MustRegister(dcVoltageGauge) | ||
| prometheus.MustRegister(dcStateGauge) | ||
| }) | ||
| } | ||
|
|
||
| // updateDCMetrics updates the Prometheus metrics with current DC power state values | ||
| func updateDCMetrics(state DCPowerState) { | ||
| dcCurrentGauge.Set(state.Current) | ||
| dcPowerGauge.Set(state.Power) | ||
| dcVoltageGauge.Set(state.Voltage) | ||
| if state.IsOn { | ||
| dcStateGauge.Set(1) | ||
| } else { | ||
| dcStateGauge.Set(0) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
Please use
promautoto register metrics.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.
There is a mixed bag of recommendations over at prometheus in recent PRs
If we DO want to do swap to
promauto, we should be using.With(reg)should be done to ensure it uses the correctprometheus.Registerer.The biggest win for
promautois that it ensures you don't forget the registering, but this PR does seem to handle it correctly already.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.
Interesting, as a Prometheus developer I don't know why they are recommending against using promauto. It's been a standard recommendation for quite a while now for exactly the "don't forget to register". We've been migrating to promauto for quite a while now.
We don't need to switch to
.With(reg)until we actually use a non-default registry.Uh oh!
There was an error while loading. Please reload this page.
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.
In reading over the linked PR comments, I think that discussion is only related to the fact that there is a bunch of default and non-default registry stuff going on in that specific codebase that needs cleaning up.
In this codebase, we have a very simple/clean use of the default registry, which is perfectly normal for this kind of code.
This codebase is already using promauto for all the existing metrics.
Uh oh!
There was an error while loading. Please reload this page.
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.
Perfect! Thanks for the correction. I appreciate the internal guidance.
Uh oh!
There was an error while loading. Please reload this page.
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 @SuperQ it's been so long since I used the underlying raw Prometheus libraries, I wasn't even aware of promauto.
Question, do we want to always register the metrics if the extension is optional and not always present? My original thinking was that it would only be present if the extension was loaded otherwise it will register everything with a 0 value.
I guess if we do want to register them all with 0 values we should try to have a metric that shows when the DC extension is enabled? So we know if metrics should be non-zero.
Uh oh!
There was an error while loading. Please reload this page.
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.
Usually what I do for optional metrics is put the vars into the setup function, rather then have them as package vars. You'll of course need a metrics struct like this:
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 other thing that could be done is to add a feature flag metric like
jetkvm_dc_power_present.