-
Notifications
You must be signed in to change notification settings - Fork 692
user-configurable overrides: handle versioning, add PATCH endpoint #2681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yvrhdn
merged 13 commits into
grafana:main
from
yvrhdn:kvrhdn/user-configurable-overrides-versioning-patch
Jul 26, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ed5ebd3
user-configurable overrides: handle versioning, add PATCH endpoint
e17e6d8
Add DeleteVersioned; make fmt
870618a
make fmt - file must have missed previous CI checks
c5f95e6
linting
6543024
Update CHANGELOG.md
c63453c
Fix integration tests, smash some bugs
5a8c3aa
Merge branch 'main' into kvrhdn/user-configurable-overrides-versionin…
f5880f0
make fmt
e6aef82
Address review comments: log warning on S3/Azure, return structs
f6d419e
Merge branch 'main' into kvrhdn/user-configurable-overrides-versionin…
a8b7a1f
Fix merge, fix logging
89789d6
linting
a518688
Export FakeVersionedReaderWriter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,13 @@ import ( | |
| "os" | ||
| "path" | ||
|
|
||
| "github.com/go-kit/log/level" | ||
| jsoniter "github.com/json-iterator/go" | ||
| "github.com/opentracing/opentracing-go" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
|
|
||
| "github.com/grafana/tempo/pkg/util/log" | ||
| "github.com/grafana/tempo/tempodb/backend" | ||
| "github.com/grafana/tempo/tempodb/backend/azure" | ||
| "github.com/grafana/tempo/tempodb/backend/gcs" | ||
|
|
@@ -27,6 +29,11 @@ const ( | |
| ) | ||
|
|
||
| var ( | ||
| metricList = promauto.NewCounter(prometheus.CounterOpts{ | ||
| Namespace: "tempo", | ||
| Name: "overrides_user_configurable_overrides_list_total", | ||
| Help: "How often the user-configurable overrides was listed", | ||
| }) | ||
| metricFetch = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
| Namespace: "tempo", | ||
| Name: "overrides_user_configurable_overrides_fetch_total", | ||
|
|
@@ -42,79 +49,104 @@ var ( | |
| type UserConfigurableOverridesClientConfig struct { | ||
| Backend string `yaml:"backend"` | ||
|
|
||
| // ConfirmVersioning is enabled when creating the backend client. If versioning is disabled no | ||
| // checks against concurrent writes will be performed. | ||
| ConfirmVersioning bool `yaml:"confirm_versioning"` | ||
|
|
||
| Local *local.Config `yaml:"local"` | ||
| GCS *gcs.Config `yaml:"gcs"` | ||
| S3 *s3.Config `yaml:"s3"` | ||
| Azure *azure.Config `yaml:"azure"` | ||
| } | ||
|
|
||
| func (c *UserConfigurableOverridesClientConfig) RegisterFlagsAndApplyDefaults(*flag.FlagSet) { | ||
| c.ConfirmVersioning = true | ||
|
|
||
| c.Local = &local.Config{} | ||
| c.GCS = &gcs.Config{} | ||
| c.S3 = &s3.Config{} | ||
| c.Azure = &azure.Config{} | ||
| } | ||
|
|
||
| // Client is a collection of methods to manage overrides on a backend. | ||
| type Client interface { | ||
| // List tenants that have user-configurable overrides. | ||
| List(ctx context.Context) ([]string, error) | ||
| Get(context.Context, string) (*UserConfigurableLimits, error) | ||
| Set(context.Context, string, *UserConfigurableLimits) error | ||
| Delete(context.Context, string) error | ||
| // Get the user-configurable overrides. Returns backend.ErrDoesNotExist if no limits are set. | ||
| Get(context.Context, string) (*UserConfigurableLimits, backend.Version, error) | ||
| // Set the user-configurable overrides. Returns backend.ErrVersionDoesNotMatch if the backend | ||
| // has a newer version. | ||
| Set(context.Context, string, *UserConfigurableLimits, backend.Version) (backend.Version, error) | ||
| // Delete the user-configurable overrides. | ||
| Delete(context.Context, string, backend.Version) error | ||
| // Shutdown the client. | ||
| Shutdown() | ||
| } | ||
|
|
||
| type userConfigOverridesClient struct { | ||
| r backend.RawReader | ||
| w backend.RawWriter | ||
| rw backend.VersionedReaderWriter | ||
| } | ||
|
|
||
| var _ Client = (*userConfigOverridesClient)(nil) | ||
|
|
||
| func NewUserConfigOverridesClient(cfg *UserConfigurableOverridesClientConfig) (Client, error) { | ||
| r, w, err := initBackend(cfg) | ||
| rw, err := initBackend(cfg) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &userConfigOverridesClient{r, w}, nil | ||
| return &userConfigOverridesClient{rw}, nil | ||
| } | ||
|
|
||
| func (o *userConfigOverridesClient) Shutdown() { | ||
| o.r.Shutdown() | ||
| o.rw.Shutdown() | ||
| } | ||
|
|
||
| func initBackend(cfg *UserConfigurableOverridesClientConfig) (reader backend.RawReader, writer backend.RawWriter, err error) { | ||
| func initBackend(cfg *UserConfigurableOverridesClientConfig) (rw backend.VersionedReaderWriter, err error) { | ||
| switch cfg.Backend { | ||
| case backend.Local: | ||
| reader, writer, _, err = local.New(cfg.Local) | ||
| r, w, _, err := local.New(cfg.Local) | ||
| if err != nil { | ||
| return | ||
| return nil, err | ||
| } | ||
| // Create overrides directory with necessary permissions | ||
| err = os.MkdirAll(path.Join(cfg.Local.Path, OverridesKeyPath), os.ModePerm) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| rw = backend.NewFakeVersionedReaderWriter(r, w) | ||
| case backend.GCS: | ||
| reader, writer, _, err = gcs.New(cfg.GCS) | ||
| rw, err = gcs.NewVersionedReaderWriter(cfg.GCS, cfg.ConfirmVersioning) | ||
| case backend.S3: | ||
| reader, writer, _, err = s3.New(cfg.S3) | ||
| rw, err = s3.NewVersionedReaderWriter(cfg.S3) | ||
|
yvrhdn marked this conversation as resolved.
|
||
| case backend.Azure: | ||
| reader, writer, _, err = azure.New(cfg.Azure) | ||
| rw, err = azure.NewVersionedReaderWriter(cfg.Azure) | ||
| default: | ||
| err = fmt.Errorf("unknown backend %s", cfg.Backend) | ||
| } | ||
| return | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if cfg.Backend == backend.Local || cfg.Backend == backend.S3 || cfg.Backend == backend.Azure { | ||
| level.Warn(log.Logger).Log( | ||
| "msg", "versioned backend requests are best-effort for the configured backend, concurrent requests modifying user-configurable overrides might cause data races", | ||
| "backend", cfg.Backend, | ||
| ) | ||
| } | ||
| return rw, nil | ||
| } | ||
|
|
||
| func (o *userConfigOverridesClient) List(ctx context.Context) ([]string, error) { | ||
| span, ctx := opentracing.StartSpanFromContext(ctx, "userConfigOverridesClient.List") | ||
| defer span.Finish() | ||
|
|
||
| return o.r.List(ctx, []string{OverridesKeyPath}) | ||
| metricList.Inc() | ||
|
|
||
| return o.rw.List(ctx, []string{OverridesKeyPath}) | ||
| } | ||
|
|
||
| // Get downloads the tenant limits from the backend. Returns nil, nil if no limits are set. | ||
| func (o *userConfigOverridesClient) Get(ctx context.Context, userID string) (tenantLimits *UserConfigurableLimits, err error) { | ||
| span, ctx := opentracing.StartSpanFromContext(ctx, "userConfigOverridesClient.Get") | ||
| func (o *userConfigOverridesClient) Get(ctx context.Context, userID string) (tenantLimits *UserConfigurableLimits, version backend.Version, err error) { | ||
| span, ctx := opentracing.StartSpanFromContext(ctx, "userConfigOverridesClient.Get", opentracing.Tag{Key: "tenant", Value: userID}) | ||
| defer span.Finish() | ||
| span.SetTag("tenant", userID) | ||
|
|
||
| metricFetch.WithLabelValues(userID).Inc() | ||
| defer func() { | ||
|
|
@@ -123,12 +155,9 @@ func (o *userConfigOverridesClient) Get(ctx context.Context, userID string) (ten | |
| } | ||
| }() | ||
|
|
||
| reader, _, err := o.r.Read(ctx, OverridesFileName, []string{OverridesKeyPath, userID}, false) | ||
| if err == backend.ErrDoesNotExist { | ||
| return nil, nil | ||
| } | ||
| reader, version, err := o.rw.ReadVersioned(ctx, OverridesFileName, []string{OverridesKeyPath, userID}) | ||
| if err != nil { | ||
| return | ||
| return nil, "", err | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a change in behaviour: instead of returning |
||
| defer reader.Close() | ||
|
|
||
|
|
@@ -137,26 +166,21 @@ func (o *userConfigOverridesClient) Get(ctx context.Context, userID string) (ten | |
| return | ||
| } | ||
|
|
||
| // Set stores the user-configurable limits on the backend. | ||
| func (o *userConfigOverridesClient) Set(ctx context.Context, userID string, limits *UserConfigurableLimits) error { | ||
| span, ctx := opentracing.StartSpanFromContext(ctx, "userConfigOverridesClient.Set") | ||
| func (o *userConfigOverridesClient) Set(ctx context.Context, userID string, limits *UserConfigurableLimits, version backend.Version) (backend.Version, error) { | ||
| span, ctx := opentracing.StartSpanFromContext(ctx, "userConfigOverridesClient.Set", opentracing.Tag{Key: "tenant", Value: userID}) | ||
| defer span.Finish() | ||
| span.SetTag("tenant", userID) | ||
|
|
||
| data, err := jsoniter.Marshal(limits) | ||
| if err != nil { | ||
| return err | ||
| return "", err | ||
| } | ||
|
|
||
| // Store on the bucket | ||
| return o.w.Write(ctx, OverridesFileName, []string{OverridesKeyPath, userID}, bytes.NewReader(data), -1, false) | ||
| return o.rw.WriteVersioned(ctx, OverridesFileName, []string{OverridesKeyPath, userID}, bytes.NewReader(data), version) | ||
| } | ||
|
|
||
| // Delete will clear all user-configurable limits for the given tenant. | ||
| func (o *userConfigOverridesClient) Delete(ctx context.Context, userID string) error { | ||
| span, ctx := opentracing.StartSpanFromContext(ctx, "userConfigOverridesClient.Delete") | ||
| func (o *userConfigOverridesClient) Delete(ctx context.Context, userID string, version backend.Version) error { | ||
| span, ctx := opentracing.StartSpanFromContext(ctx, "userConfigOverridesClient.Delete", opentracing.Tag{Key: "tenant", Value: userID}) | ||
| defer span.Finish() | ||
| span.SetTag("tenant", userID) | ||
|
|
||
| return o.w.Delete(ctx, OverridesFileName, []string{OverridesKeyPath, userID}, false) | ||
| return o.rw.DeleteVersioned(ctx, OverridesFileName, []string{OverridesKeyPath, userID}, version) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered placing this on
gcs.Configinstead but I'm worried this would conflict with our existing bucket configuration. For storing traces you don't need versioning, so setting this to default true would cause a lot of issues.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with the reasoning here 👍