mirror of https://github.com/grafana/loki
Storage: Allow setting a constant prefix for all created keys (#10096)
**What this PR does / why we need it**:
Adds a new option under the aws stanza named key_prefix. This is useful
if you have many Loki installations and do not want to create a million
buckets. This is different than `compactor.shared_store_key_prefix`
because it also affects eg the `loki_cluster_seed.json` file.
**Which issue(s) this PR fixes**:
Fixes #5889
**Special notes for your reviewer**:
**Checklist**
- [x] Reviewed the
[`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md)
guide (**required**)
- [x] Documentation added
- [x] Tests updated
- [x] `CHANGELOG.md` updated
- [ ] If the change is worth mentioning in the release notes, add
`add-to-release-notes` label
- [x] Changes that require user attention or interaction to upgrade are
documented in `docs/sources/setup/upgrade/_index.md`
- [ ] For Helm chart changes bump the Helm chart version in
`production/helm/loki/Chart.yaml` and update
`production/helm/loki/CHANGELOG.md` and
`production/helm/loki/README.md`. [Example
PR](d10549e3ec
)
---------
Signed-off-by: Edward Welch <edward.welch@grafana.com>
Co-authored-by: Ed Welch <edward.welch@grafana.com>
pull/11075/head^2
parent
9be3c0863e
commit
4b750cc0cc
@ -0,0 +1,69 @@ |
||||
package client |
||||
|
||||
import ( |
||||
"context" |
||||
"io" |
||||
"strings" |
||||
) |
||||
|
||||
type PrefixedObjectClient struct { |
||||
downstreamClient ObjectClient |
||||
prefix string |
||||
} |
||||
|
||||
func NewPrefixedObjectClient(downstreamClient ObjectClient, prefix string) ObjectClient { |
||||
return PrefixedObjectClient{downstreamClient: downstreamClient, prefix: prefix} |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) PutObject(ctx context.Context, objectKey string, object io.ReadSeeker) error { |
||||
return p.downstreamClient.PutObject(ctx, p.prefix+objectKey, object) |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) ObjectExists(ctx context.Context, objectKey string) (bool, error) { |
||||
return p.downstreamClient.ObjectExists(ctx, p.prefix+objectKey) |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) GetObject(ctx context.Context, objectKey string) (io.ReadCloser, int64, error) { |
||||
return p.downstreamClient.GetObject(ctx, p.prefix+objectKey) |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) List(ctx context.Context, prefix, delimiter string) ([]StorageObject, []StorageCommonPrefix, error) { |
||||
objects, commonPrefixes, err := p.downstreamClient.List(ctx, p.prefix+prefix, delimiter) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
for i := range objects { |
||||
objects[i].Key = strings.TrimPrefix(objects[i].Key, p.prefix) |
||||
} |
||||
|
||||
for i := range commonPrefixes { |
||||
commonPrefixes[i] = StorageCommonPrefix(strings.TrimPrefix(string(commonPrefixes[i]), p.prefix)) |
||||
} |
||||
|
||||
return objects, commonPrefixes, nil |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) DeleteObject(ctx context.Context, objectKey string) error { |
||||
return p.downstreamClient.DeleteObject(ctx, p.prefix+objectKey) |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) IsObjectNotFoundErr(err error) bool { |
||||
return p.downstreamClient.IsObjectNotFoundErr(err) |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) IsRetryableErr(err error) bool { |
||||
return p.downstreamClient.IsRetryableErr(err) |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) Stop() { |
||||
p.downstreamClient.Stop() |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) GetDownstream() ObjectClient { |
||||
return p.downstreamClient |
||||
} |
||||
|
||||
func (p PrefixedObjectClient) GetPrefix() string { |
||||
return p.prefix |
||||
} |
@ -1,63 +0,0 @@ |
||||
package storage |
||||
|
||||
import ( |
||||
"context" |
||||
"io" |
||||
"strings" |
||||
|
||||
"github.com/grafana/loki/pkg/storage/chunk/client" |
||||
) |
||||
|
||||
type prefixedObjectClient struct { |
||||
downstreamClient client.ObjectClient |
||||
prefix string |
||||
} |
||||
|
||||
func newPrefixedObjectClient(downstreamClient client.ObjectClient, prefix string) client.ObjectClient { |
||||
return prefixedObjectClient{downstreamClient: downstreamClient, prefix: prefix} |
||||
} |
||||
|
||||
func (p prefixedObjectClient) PutObject(ctx context.Context, objectKey string, object io.ReadSeeker) error { |
||||
return p.downstreamClient.PutObject(ctx, p.prefix+objectKey, object) |
||||
} |
||||
|
||||
func (p prefixedObjectClient) ObjectExists(ctx context.Context, objectKey string) (bool, error) { |
||||
return p.downstreamClient.ObjectExists(ctx, p.prefix+objectKey) |
||||
} |
||||
|
||||
func (p prefixedObjectClient) GetObject(ctx context.Context, objectKey string) (io.ReadCloser, int64, error) { |
||||
return p.downstreamClient.GetObject(ctx, p.prefix+objectKey) |
||||
} |
||||
|
||||
func (p prefixedObjectClient) List(ctx context.Context, prefix, delimiter string) ([]client.StorageObject, []client.StorageCommonPrefix, error) { |
||||
objects, commonPrefixes, err := p.downstreamClient.List(ctx, p.prefix+prefix, delimiter) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
for i := range objects { |
||||
objects[i].Key = strings.TrimPrefix(objects[i].Key, p.prefix) |
||||
} |
||||
|
||||
for i := range commonPrefixes { |
||||
commonPrefixes[i] = client.StorageCommonPrefix(strings.TrimPrefix(string(commonPrefixes[i]), p.prefix)) |
||||
} |
||||
|
||||
return objects, commonPrefixes, nil |
||||
} |
||||
|
||||
func (p prefixedObjectClient) DeleteObject(ctx context.Context, objectKey string) error { |
||||
return p.downstreamClient.DeleteObject(ctx, p.prefix+objectKey) |
||||
} |
||||
|
||||
func (p prefixedObjectClient) IsObjectNotFoundErr(err error) bool { |
||||
return p.downstreamClient.IsObjectNotFoundErr(err) |
||||
} |
||||
|
||||
func (p prefixedObjectClient) IsRetryableErr(err error) bool { |
||||
return p.downstreamClient.IsRetryableErr(err) |
||||
} |
||||
|
||||
func (p prefixedObjectClient) Stop() { |
||||
p.downstreamClient.Stop() |
||||
} |
Loading…
Reference in new issue