Skip to content

Commit

Permalink
add grpc code to metrics replicator (#231)
Browse files Browse the repository at this point in the history
* add grpc code to metrics replicator

* fix style
  • Loading branch information
kevinye202 authored Dec 12, 2024
1 parent 07236be commit 078d9d7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/blobstore/replication/metrics_blob_replicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
Help: "Amount of time spent per operation on blob replicator, in seconds.",
Buckets: util.DecimalExponentialBuckets(-3, 6, 2),
},
[]string{"storage_type", "operation"})
[]string{"storage_type", "operation", "grpc_code"})

blobReplicatorOperationsBlobSizeBytes = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Expand Down
53 changes: 51 additions & 2 deletions pkg/blobstore/replication/metrics_blob_replicator_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package replication_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

remoteexecution "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
"github.com/buildbarn/bb-storage/internal/mock"
"github.com/buildbarn/bb-storage/pkg/blobstore/buffer"
"github.com/buildbarn/bb-storage/pkg/blobstore/replication"
"github.com/buildbarn/bb-storage/pkg/digest"
"github.com/stretchr/testify/require"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"go.uber.org/mock/gomock"
)

Expand All @@ -23,3 +31,44 @@ func TestNewMetricsBlobReplicator(t *testing.T) {
metricsReplicator := replication.NewMetricsBlobReplicator(mockReplicator, mockClock, storageTypeName)
require.NotNil(t, metricsReplicator)
}

func TestMetricsBlobReplicatorLabelCardinality(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockClock := mock.NewMockClock(ctrl)
mockReplicator := mock.NewMockBlobReplicator(ctrl)

// Set up fixed times for predictable testing
startTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
endTime := startTime.Add(1 * time.Second)
mockClock.EXPECT().Now().Return(startTime).AnyTimes()
mockClock.EXPECT().Now().Return(endTime).AnyTimes()

storageTypeName := "cas"
metricsReplicator := replication.NewMetricsBlobReplicator(
mockReplicator,
mockClock,
storageTypeName,
)

// Test ReplicateSingle
d := digest.MustNewDigest("hello", remoteexecution.DigestFunction_MD5, "8b1a9953c4611296a827abf8c47804d7", 5)
mockReplicator.EXPECT().
ReplicateSingle(gomock.Any(), d).
Return(buffer.NewBufferFromError(status.Error(codes.NotFound, "not found")))

// This should not panic due to label cardinality mismatch
b := metricsReplicator.ReplicateSingle(context.Background(), d)
b.Discard()

// Test ReplicateMultiple
digests := digest.NewSetBuilder().Add(d).Build()
mockReplicator.EXPECT().
ReplicateMultiple(gomock.Any(), digests).
Return(status.Error(codes.Internal, "internal error"))

// This should not panic due to label cardinality mismatch
err := metricsReplicator.ReplicateMultiple(context.Background(), digests)
require.Error(t, err)
}

0 comments on commit 078d9d7

Please sign in to comment.