Skip to content

Commit

Permalink
feat: change latency type to HistogramVec
Browse files Browse the repository at this point in the history
  • Loading branch information
kianaza committed Jul 1, 2024
1 parent 28db558 commit 276bb3a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
4 changes: 3 additions & 1 deletion internal/natsclient/jetstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ func (j *Jetstream) jetstreamSubscribe(h chan *Message, streamName string) {
return
}
latency := time.Since(publishTime).Seconds()
j.metrics.Latency.Observe(latency)
j.metrics.Latency.With(prometheus.Labels{
"stream": streamName,
}).Observe(latency)
j.metrics.SuccessCounter.With(prometheus.Labels{
"type": successfulSubscribe,
"stream": streamName,
Expand Down
14 changes: 7 additions & 7 deletions internal/natsclient/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ var latencyBuckets = []float64{
// Metrics has all the client metrics.
type Metrics struct {
Connection prometheus.CounterVec
Latency prometheus.Histogram
Latency prometheus.HistogramVec
SuccessCounter prometheus.CounterVec
}

// nolint: ireturn
func newHistogram(histogramOpts prometheus.HistogramOpts) prometheus.Histogram {
ev := prometheus.NewHistogram(histogramOpts)
func newHistogramVec(histogramOpts prometheus.HistogramOpts, labelNames []string) prometheus.HistogramVec {
ev := prometheus.NewHistogramVec(histogramOpts, labelNames)

if err := prometheus.Register(ev); err != nil {
var are prometheus.AlreadyRegisteredError
if ok := errors.As(err, &are); ok {
ev, ok = are.ExistingCollector.(prometheus.Histogram)
ev, ok = are.ExistingCollector.(*prometheus.HistogramVec)
if !ok {
panic("different metric type registration")
}
Expand All @@ -46,7 +46,7 @@ func newHistogram(histogramOpts prometheus.HistogramOpts) prometheus.Histogram {
}
}

return ev
return *ev
}

// nolint: ireturn
Expand Down Expand Up @@ -78,14 +78,14 @@ func NewMetrics(clinetName string) Metrics {
ConstLabels: nil,
}, []string{"type"}),
// nolint: exhaustruct
Latency: newHistogram(prometheus.HistogramOpts{
Latency: newHistogramVec(prometheus.HistogramOpts{
Namespace: Namespace,
Subsystem: clinetName,
Name: "latency",
Help: "from publish to consume duration in seconds",
ConstLabels: nil,
Buckets: latencyBuckets,
}),
}, []string{"stream"}),
SuccessCounter: newCounterVec(prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: clinetName,
Expand Down

0 comments on commit 276bb3a

Please sign in to comment.