-
Notifications
You must be signed in to change notification settings - Fork 894
feat: expose agent metrics via Prometheus endpoint #7011
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
Changes from 1 commit
8d4e67d
da729e6
9ad09b2
440657c
8764f89
663b5d5
63aff5e
3905481
f8d6f46
d487a77
7acbaf0
7418779
3a8e4e6
b5d0581
e4d708b
e0669f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,80 @@ | ||||||
package prometheusmetrics | ||||||
|
||||||
import ( | ||||||
"sync" | ||||||
|
||||||
"github.com/prometheus/client_golang/prometheus" | ||||||
) | ||||||
|
||||||
type CachedGaugeVec struct { | ||||||
m sync.Mutex | ||||||
|
||||||
gaugeVec *prometheus.GaugeVec | ||||||
records []vectorRecord | ||||||
} | ||||||
|
||||||
var _ prometheus.Collector = new(CachedGaugeVec) | ||||||
|
||||||
type VectorOperation int | ||||||
|
||||||
const ( | ||||||
VectorOperationAdd VectorOperation = iota | ||||||
VectorOperationSet | ||||||
) | ||||||
|
||||||
type vectorRecord struct { | ||||||
operation VectorOperation | ||||||
value float64 | ||||||
labelValues []string | ||||||
} | ||||||
|
||||||
func NewCachedGaugeVec(gaugeVec *prometheus.GaugeVec) *CachedGaugeVec { | ||||||
return &CachedGaugeVec{ | ||||||
gaugeVec: gaugeVec, | ||||||
} | ||||||
} | ||||||
|
||||||
func (v *CachedGaugeVec) Describe(desc chan<- *prometheus.Desc) { | ||||||
v.m.Lock() | ||||||
defer v.m.Unlock() | ||||||
|
||||||
v.gaugeVec.Describe(desc) | ||||||
} | ||||||
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 actually does not need the mutex.
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 wasn't sure if |
||||||
|
||||||
func (v *CachedGaugeVec) Collect(ch chan<- prometheus.Metric) { | ||||||
v.m.Lock() | ||||||
defer v.m.Unlock() | ||||||
|
||||||
v.gaugeVec.Collect(ch) | ||||||
} | ||||||
|
||||||
func (v *CachedGaugeVec) WithLabelValues(operation VectorOperation, value float64, labelValues ...string) { | ||||||
v.m.Lock() | ||||||
defer v.m.Unlock() | ||||||
|
||||||
v.records = append(v.records, vectorRecord{ | ||||||
operation: operation, | ||||||
value: value, | ||||||
labelValues: labelValues, | ||||||
}) | ||||||
} | ||||||
|
||||||
func (v *CachedGaugeVec) Commit() { | ||||||
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. // Commit will set the internal value as the cached value to return from 'Collect'.
Suggested change
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. Comment added. |
||||||
v.m.Lock() | ||||||
defer v.m.Unlock() | ||||||
|
||||||
v.gaugeVec.Reset() | ||||||
for _, record := range v.records { | ||||||
g := v.gaugeVec.WithLabelValues(record.labelValues...) | ||||||
switch record.operation { | ||||||
case VectorOperationAdd: | ||||||
g.Add(record.value) | ||||||
case VectorOperationSet: | ||||||
g.Set(record.value) | ||||||
default: | ||||||
panic("unsupported vector operation") | ||||||
} | ||||||
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. Might want this switch statement on 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. This switch is also useful to pick the right operation, so I will add another switch-case-panic to |
||||||
} | ||||||
|
||||||
v.records = 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.
Can you doc the usage? And the why?
Eg:
Or something...
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.
Added 👍