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 d51bbd5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
10 changes: 10 additions & 0 deletions docs/sources/shared/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,10 @@ storage:
# CLI flag: -common.storage.thanos.s3.insecure
[insecure: <boolean> | default = false]
# Disable forcing S3 dualstack endpoint usage.
# CLI flag: -common.storage.thanos.s3.disable-dualstack
[disable_dualstack: <boolean> | default = false]
# The signature version to use for authenticating against S3. Supported
# values are: v4.
# CLI flag: -common.storage.thanos.s3.signature-version
Expand Down Expand Up @@ -4916,6 +4920,8 @@ storage:
[insecure: <boolean>]
[disable_dualstack: <boolean>]
[signature_version: <string> | default = ""]
[storage_class: <string> | default = ""]
Expand Down Expand Up @@ -6211,6 +6217,10 @@ objstore_config:
# CLI flag: -thanos.s3.insecure
[insecure: <boolean> | default = false]

# Disable forcing S3 dualstack endpoint usage.
# CLI flag: -thanos.s3.disable-dualstack
[disable_dualstack: <boolean> | default = false]

# The signature version to use for authenticating against S3. Supported
# values are: v4.
# CLI flag: -thanos.s3.signature-version
Expand Down
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 d51bbd5

Please sign in to comment.