Skip to content

Commit 61e3e52

Browse files
Handle duplicate registration of deletion metrics (#6446)
* Handle duplicate registration of deletion metrics Signed-off-by: Michel Hollands <[email protected]> * Create metrics struct at Loki level Signed-off-by: Michel Hollands <[email protected]> * Fix test Signed-off-by: Michel Hollands <[email protected]>
1 parent 243b0a6 commit 61e3e52

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

pkg/loki/loki.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/grafana/dskit/runtimeconfig"
2121
"github.com/grafana/dskit/services"
2222
"github.com/pkg/errors"
23+
"github.com/prometheus/client_golang/prometheus"
2324
"github.com/weaveworks/common/middleware"
2425
"github.com/weaveworks/common/server"
2526
"github.com/weaveworks/common/signals"
@@ -43,6 +44,7 @@ import (
4344
"github.com/grafana/loki/pkg/storage/config"
4445
"github.com/grafana/loki/pkg/storage/stores/series/index"
4546
"github.com/grafana/loki/pkg/storage/stores/shipper/compactor"
47+
"github.com/grafana/loki/pkg/storage/stores/shipper/compactor/deletion"
4648
"github.com/grafana/loki/pkg/storage/stores/shipper/indexgateway"
4749
"github.com/grafana/loki/pkg/tracing"
4850
"github.com/grafana/loki/pkg/usagestats"
@@ -250,16 +252,18 @@ type Loki struct {
250252
usageReport *usagestats.Reporter
251253
indexGatewayRingManager *indexgateway.RingManager
252254

253-
clientMetrics storage.ClientMetrics
255+
clientMetrics storage.ClientMetrics
256+
deleteClientMetrics *deletion.DeleteRequestClientMetrics
254257

255258
HTTPAuthMiddleware middleware.Interface
256259
}
257260

258261
// New makes a new Loki.
259262
func New(cfg Config) (*Loki, error) {
260263
loki := &Loki{
261-
Cfg: cfg,
262-
clientMetrics: storage.NewClientMetrics(),
264+
Cfg: cfg,
265+
clientMetrics: storage.NewClientMetrics(),
266+
deleteClientMetrics: deletion.NewDeleteRequestClientMetrics(prometheus.DefaultRegisterer),
263267
}
264268
usagestats.Edition("oss")
265269
loki.setupAuthMiddleware()

pkg/loki/modules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ func (t *Loki) deleteRequestsClient() (deletion.DeleteRequestsClient, error) {
10021002
return nil, err
10031003
}
10041004

1005-
return deletion.NewDeleteRequestsClient(compactorAddress, &http.Client{Timeout: 5 * time.Second}, prometheus.DefaultRegisterer)
1005+
return deletion.NewDeleteRequestsClient(compactorAddress, &http.Client{Timeout: 5 * time.Second}, t.deleteClientMetrics)
10061006
}
10071007

10081008
func calculateMaxLookBack(pc config.PeriodConfig, maxLookBackConfig, minDuration time.Duration) (time.Duration, error) {

pkg/storage/stores/shipper/compactor/deletion/delete_requests_client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/go-kit/log/level"
13-
"github.com/prometheus/client_golang/prometheus"
1413

1514
"github.com/grafana/loki/pkg/util/log"
1615
)
@@ -33,7 +32,7 @@ type deleteRequestsClient struct {
3332
cache map[string][]DeleteRequest
3433
cacheDuration time.Duration
3534

36-
metrics *deleteRequestClientMetrics
35+
metrics *DeleteRequestClientMetrics
3736

3837
stopChan chan struct{}
3938
}
@@ -50,7 +49,7 @@ func WithRequestClientCacheDuration(d time.Duration) DeleteRequestsStoreOption {
5049
}
5150
}
5251

53-
func NewDeleteRequestsClient(addr string, c httpClient, r prometheus.Registerer, opts ...DeleteRequestsStoreOption) (DeleteRequestsClient, error) {
52+
func NewDeleteRequestsClient(addr string, c httpClient, deleteClientMetrics *DeleteRequestClientMetrics, opts ...DeleteRequestsStoreOption) (DeleteRequestsClient, error) {
5453
u, err := url.Parse(addr)
5554
if err != nil {
5655
level.Error(log.Logger).Log("msg", "error parsing url", "err", err)
@@ -63,7 +62,7 @@ func NewDeleteRequestsClient(addr string, c httpClient, r prometheus.Registerer,
6362
httpClient: c,
6463
cacheDuration: 5 * time.Minute,
6564
cache: make(map[string][]DeleteRequest),
66-
metrics: newDeleteRequestClientMetrics(r),
65+
metrics: deleteClientMetrics,
6766
stopChan: make(chan struct{}),
6867
}
6968

pkg/storage/stores/shipper/compactor/deletion/delete_requests_client_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/prometheus/client_golang/prometheus"
1112
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestGetCacheGenNumberForUser(t *testing.T) {
16+
deleteClientMetrics := NewDeleteRequestClientMetrics(prometheus.DefaultRegisterer)
17+
1518
t.Run("it requests results from the api", func(t *testing.T) {
1619
httpClient := &mockHTTPClient{ret: `[{"request_id":"test-request"}]`}
17-
client, err := NewDeleteRequestsClient("http://test-server", httpClient, nil)
20+
client, err := NewDeleteRequestsClient("http://test-server", httpClient, deleteClientMetrics)
1821
require.Nil(t, err)
1922

2023
deleteRequests, err := client.GetAllDeleteRequestsForUser(context.Background(), "userID")
@@ -30,7 +33,7 @@ func TestGetCacheGenNumberForUser(t *testing.T) {
3033

3134
t.Run("it caches the results", func(t *testing.T) {
3235
httpClient := &mockHTTPClient{ret: `[{"request_id":"test-request"}]`}
33-
client, err := NewDeleteRequestsClient("http://test-server", httpClient, nil, WithRequestClientCacheDuration(100*time.Millisecond))
36+
client, err := NewDeleteRequestsClient("http://test-server", httpClient, deleteClientMetrics, WithRequestClientCacheDuration(100*time.Millisecond))
3437
require.Nil(t, err)
3538

3639
deleteRequests, err := client.GetAllDeleteRequestsForUser(context.Background(), "userID")

pkg/storage/stores/shipper/compactor/deletion/metrics.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"github.com/prometheus/client_golang/prometheus/promauto"
66
)
77

8-
type deleteRequestClientMetrics struct {
8+
type DeleteRequestClientMetrics struct {
99
deleteRequestsLookupsTotal prometheus.Counter
1010
deleteRequestsLookupsFailedTotal prometheus.Counter
1111
}
1212

13-
func newDeleteRequestClientMetrics(r prometheus.Registerer) *deleteRequestClientMetrics {
14-
m := deleteRequestClientMetrics{}
13+
func NewDeleteRequestClientMetrics(r prometheus.Registerer) *DeleteRequestClientMetrics {
14+
m := DeleteRequestClientMetrics{}
1515

1616
m.deleteRequestsLookupsTotal = promauto.With(r).NewCounter(prometheus.CounterOpts{
1717
Namespace: "loki",

0 commit comments

Comments
 (0)