Skip to content

Commit

Permalink
chore: add ObjectExistsWithSize to respect the interface
Browse files Browse the repository at this point in the history
new method was added in grafana#14268
  • Loading branch information
JoaoBraveCoding committed Sep 30, 2024
1 parent 3fef0bf commit ec00fec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
29 changes: 11 additions & 18 deletions pkg/storage/bucket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,7 @@ func NewClient(ctx context.Context, cfg Config, name string, logger log.Logger,
client = NewPrefixedBucketClient(client, cfg.StoragePrefix)
}

if metrics.BucketMetrics != nil {
client = bucketWrapWith(client, metrics.BucketMetrics)
} else {
bucketMetrics := bucketMetrics(name, metrics.Registerer)
client = bucketWrapWith(client, bucketMetrics)
// Save metrics to be assigned to other buckets created with the same component name
metrics.BucketMetrics = bucketMetrics
}

instrumentedClient := objstoretracing.WrapWithTraces(client)
instrumentedClient := objstoretracing.WrapWithTraces(bucketWithMetrics(client, name, metrics))

// Wrap the client with any provided middleware
for _, wrap := range cfg.Middlewares {
Expand All @@ -205,16 +196,18 @@ func NewClient(ctx context.Context, cfg Config, name string, logger log.Logger,
return instrumentedClient, nil
}

func bucketMetrics(name string, reg prometheus.Registerer) *objstore.Metrics {
reg = prometheus.WrapRegistererWithPrefix("loki_", reg)
reg = prometheus.WrapRegistererWith(prometheus.Labels{"component": name}, reg)
return objstore.BucketMetrics(reg, "")
}

func bucketWrapWith(bucketClient objstore.Bucket, metrics *objstore.Metrics) objstore.Bucket {
func bucketWithMetrics(bucketClient objstore.Bucket, name string, metrics *Metrics) objstore.Bucket {
if metrics == nil {
return bucketClient
}

return objstore.WrapWith(bucketClient, metrics)
if metrics.BucketMetrics == nil {
reg := metrics.Registerer
reg = prometheus.WrapRegistererWithPrefix("loki_", reg)
reg = prometheus.WrapRegistererWith(prometheus.Labels{"component": name}, reg)
// Save metrics to be assigned to other buckets created with the same component name
metrics.BucketMetrics = objstore.BucketMetrics(reg, "")
}

return objstore.WrapWith(bucketClient, metrics.BucketMetrics)
}
5 changes: 3 additions & 2 deletions pkg/storage/chunk/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"errors"

"github.com/grafana/loki/v3/pkg/storage/chunk"
"github.com/grafana/loki/v3/pkg/storage/stores/series/index"
"github.com/prometheus/client_golang/prometheus"
"github.com/thanos-io/objstore"

"github.com/grafana/loki/v3/pkg/storage/chunk"
"github.com/grafana/loki/v3/pkg/storage/stores/series/index"
)

var (
Expand Down
15 changes: 15 additions & 0 deletions pkg/storage/chunk/client/gcp/gcs_thanos_object_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ func (s *GCSThanosObjectClient) ObjectExists(ctx context.Context, objectKey stri
return s.client.Exists(ctx, objectKey)
}

// ObjectExistsWithSize checks if a given objectKey exists and it's size in the GCS bucket
func (s *GCSThanosObjectClient) ObjectExistsWithSize(ctx context.Context, objectKey string) (bool, int64, error) {
_, err := s.client.Get(ctx, objectKey)
if err != nil {
return false, 0, err
}

attr, err := s.client.Attributes(ctx, objectKey)
if err != nil {
return true, 0, nil
}

return true, attr.Size, nil
}

// PutObject puts the specified bytes into the configured GCS bucket at the provided key
func (s *GCSThanosObjectClient) PutObject(ctx context.Context, objectKey string, object io.Reader) error {
return s.client.Upload(ctx, objectKey, object)
Expand Down

0 comments on commit ec00fec

Please sign in to comment.