Skip to content

Commit

Permalink
Fix overlapping metric registrations
Browse files Browse the repository at this point in the history
This fixes a problem when multiple metrics bucket are regsitered with
different names. Also adds a test so this won't be happening in the
future.

Signed-off-by: Christian Simon <[email protected]>
  • Loading branch information
simonswine committed Jul 27, 2023
1 parent d0c4344 commit c8b2d20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
14 changes: 7 additions & 7 deletions objstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,11 @@ func WrapWithMetrics(b Bucket, reg prometheus.Registerer, name string) *metricBu
Buckets: []float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120},
}, []string{"operation"}),

lastSuccessfulUploadTime: promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{
Name: "objstore_bucket_last_successful_upload_time",
Help: "Second timestamp of the last successful upload to the bucket.",
}, []string{"bucket"}),
lastSuccessfulUploadTime: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Name: "objstore_bucket_last_successful_upload_time",
Help: "Second timestamp of the last successful upload to the bucket.",
ConstLabels: prometheus.Labels{"bucket": name},
}),
}
for _, op := range []string{
OpIter,
Expand All @@ -465,7 +466,6 @@ func WrapWithMetrics(b Bucket, reg prometheus.Registerer, name string) *metricBu
} {
bkt.opsTransferredBytes.WithLabelValues(op)
}
bkt.lastSuccessfulUploadTime.WithLabelValues(b.Name())
return bkt
}

Expand All @@ -479,7 +479,7 @@ type metricBucket struct {
opsFetchedBytes *prometheus.CounterVec
opsTransferredBytes *prometheus.HistogramVec
opsDuration *prometheus.HistogramVec
lastSuccessfulUploadTime *prometheus.GaugeVec
lastSuccessfulUploadTime prometheus.Gauge
}

func (b *metricBucket) WithExpectedErrs(fn IsOpFailureExpectedFunc) Bucket {
Expand Down Expand Up @@ -599,7 +599,7 @@ func (b *metricBucket) Upload(ctx context.Context, name string, r io.Reader) err
}
return err
}
b.lastSuccessfulUploadTime.WithLabelValues(b.bkt.Name()).SetToCurrentTime()
b.lastSuccessfulUploadTime.SetToCurrentTime()
b.opsDuration.WithLabelValues(op).Observe(time.Since(start).Seconds())
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions objstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func TestMetricBucket_Close(t *testing.T) {
testutil.Assert(t, promtest.ToFloat64(bkt.lastSuccessfulUploadTime) > lastUpload)
}

// TestMetricBucket_MultipleClients tests that the metrics from two different buckets clients are not conflicting with each other.
func TestMetricBucket_Multiple_Clients(t *testing.T) {
reg := prometheus.NewPedanticRegistry()

WrapWithMetrics(NewInMemBucket(), reg, "abc")
WrapWithMetrics(NewInMemBucket(), reg, "def")
}

func TestDownloadUploadDirConcurrency(t *testing.T) {
r := prometheus.NewRegistry()
m := WrapWithMetrics(NewInMemBucket(), r, "")
Expand Down

0 comments on commit c8b2d20

Please sign in to comment.