-
Notifications
You must be signed in to change notification settings - Fork 100
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
WIP: Implement Prometheus statser #355
Changes from all commits
01c6b02
6e54078
110e0c5
fa95d00
d629631
a05c8e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package stats | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/atlassian/gostatsd" | ||
) | ||
|
||
// NOTE: is using a collector/vec of gauges/counters reasonable? Or does | ||
// using only a single gauge/counter suffice? | ||
|
||
// this depends on the metrics being collected, if they are the same thing | ||
// but can be partitioned into different types/groups, e.g. http requests partitioned | ||
// by user age and demographics, then using a collector makes sense. (is this correct?) | ||
|
||
// PrometheusStatser is a Statser that monitors gostasd's internal metrics from | ||
// Prometheus, it is useful when there is a large number of ephemeral hosts. | ||
type PrometheusStatser struct { | ||
flushNotifier | ||
|
||
// collector of gauges that stores the internal gauge metrics of gostatsd | ||
gaugeVec prometheus.GaugeVec | ||
// collector of counters that stores the internal count metrics of gostatsd | ||
counterVec prometheus.CounterVec | ||
} | ||
|
||
// NewPrometheusStatser creates a new Statser which | ||
// sends internal metrics to prometheus | ||
func NewPrometheusStatser(gaugeVec prometheus.GaugeVec, counterVec prometheus.CounterVec) *PrometheusStatser { | ||
return &PrometheusStatser{ | ||
gaugeVec: gaugeVec, | ||
counterVec: counterVec, | ||
} | ||
} | ||
|
||
func (ps *PrometheusStatser) NotifyFlush(ctx context.Context, d time.Duration) { | ||
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. You don't need to implement 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. How come 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. My bad, that comments from my personal account. The It shouldn't use a That being said, it could and probably should use an embedded 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. Oh, I see that now, what was the design process behind, is it because that they have different notification targets, so one such as I am using the GoLand IDE, and I am trying to find the usage of 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. There was no special design considerations behind
The I'm not sure why GoLand doesn't detect usage - it does in my setup. You could try rebuilding the index through File -> Invalidatae Caches / Restart, but I don't know if it will help. The only place it's called is at https://github.com/atlassian/gostatsd/blob/master/pkg/statsd/flusher.go#L69 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. My problem was I was not aware that 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. Ahhh, that would do it :) |
||
ps.flushNotifier.NotifyFlush(ctx, d) | ||
} | ||
|
||
func (ps *PrometheusStatser) RegisterFlush() (<-chan time.Duration, func()) { | ||
return ps.flushNotifier.RegisterFlush() | ||
} | ||
|
||
// TODO: how do I use tags here, same for the Count and the TimingMS methods | ||
func (ps *PrometheusStatser) Gauge(name string, value float64, tags gostatsd.Tags) { | ||
ps.gaugeVec.WithLabelValues(name).Add(value) | ||
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. The tags map to labels |
||
} | ||
|
||
func (ps *PrometheusStatser) Count(name string, amount float64, tags gostatsd.Tags) { | ||
ps.counterVec.WithLabelValues(name).Add(amount) | ||
} | ||
|
||
func (ps *PrometheusStatser) Increment(name string, tags gostatsd.Tags) { | ||
ps.Count(name, 1, tags) | ||
} | ||
|
||
// TimingMS sends a timing metric from a millisecond value | ||
func (ps *PrometheusStatser) TimingMS(name string, ms float64, tags gostatsd.Tags) { | ||
ps.counterVec.WithLabelValues(name).Add(ms) | ||
} | ||
|
||
// TimingDuration sends a timing metric from a time.Duration | ||
func (ps *PrometheusStatser) TimingDuration(name string, d time.Duration, tags gostatsd.Tags) { | ||
ps.TimingMS(name, float64(d)/float64(time.Millisecond), tags) | ||
} | ||
|
||
// NewTimer returns a new timer with time set to now | ||
func (ps *PrometheusStatser) NewTimer(name string, tags gostatsd.Tags) *Timer { | ||
return newTimer(ps, name, tags) | ||
} | ||
|
||
// WithTags creates a new Statser with additional tags | ||
func (ps *PrometheusStatser) WithTags(tags gostatsd.Tags) Statser { | ||
return NewTaggedStatser(ps, tags) | ||
} |
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.
Each
GaugeVec
andCounterVec
is a single metric name, they need to be lazily created.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.
Does it make sense to use the
Gauges
andCounter
structsgostatsd
already provides or would using the structs provided from theprometheus
package be better?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.
You'd need to use the ones from the prometheus packages, because they're ultimately added to the prometheus registry.