forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_configurable_overrides.go
More file actions
349 lines (283 loc) · 12.2 KB
/
user_configurable_overrides.go
File metadata and controls
349 lines (283 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package overrides
import (
"context"
"errors"
"flag"
"fmt"
"io"
"net/http"
"sync"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/services"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v2"
userconfigurableoverrides "github.com/grafana/tempo/modules/overrides/userconfigurable/client"
filterconfig "github.com/grafana/tempo/pkg/spanfilter/config"
"github.com/grafana/tempo/pkg/util/listtomap"
tempo_log "github.com/grafana/tempo/pkg/util/log"
"github.com/grafana/tempo/pkg/util/tracing"
"github.com/grafana/tempo/tempodb/backend"
)
var metricUserConfigurableOverridesReloadFailed = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "tempo",
Name: "overrides_user_configurable_overrides_reload_failed_total",
Help: "How often reloading the user-configurable overrides has failed",
})
type UserConfigurableOverridesConfig struct {
Enabled bool `yaml:"enabled"`
// PollInterval controls how often the overrides will be refreshed by polling the backend
PollInterval time.Duration `yaml:"poll_interval"`
Client userconfigurableoverrides.Config `yaml:"client"`
}
func (cfg *UserConfigurableOverridesConfig) RegisterFlagsAndApplyDefaults(f *flag.FlagSet) {
cfg.PollInterval = time.Minute
cfg.Client.RegisterFlagsAndApplyDefaults(f)
}
type tenantLimits map[string]*userconfigurableoverrides.Limits
// userConfigurableOverridesManager can store user-configurable overrides on a bucket.
type userConfigurableOverridesManager struct {
services.Service
// wrap Interface and only overrides select functions
Interface
cfg *UserConfigurableOverridesConfig
subservices *services.Manager
subservicesWatcher *services.FailureWatcher
mtx sync.RWMutex
tenantLimits tenantLimits
client userconfigurableoverrides.Client
logger log.Logger
}
var _ Service = (*userConfigurableOverridesManager)(nil)
// newUserConfigOverrides wraps the given overrides with user-configurable overrides.
func newUserConfigOverrides(cfg *UserConfigurableOverridesConfig, subOverrides Service) (*userConfigurableOverridesManager, error) {
client, err := userconfigurableoverrides.New(&cfg.Client)
if err != nil {
return nil, fmt.Errorf("failed to initialize backend client for user-configurable overrides: %w", err)
}
mgr := userConfigurableOverridesManager{
Interface: subOverrides,
cfg: cfg,
tenantLimits: make(tenantLimits),
client: client,
logger: log.With(tempo_log.Logger, "component", "user-configurable overrides"),
}
mgr.subservices, err = services.NewManager(subOverrides)
if err != nil {
return nil, fmt.Errorf("failed to create subservices: %w", err)
}
mgr.subservicesWatcher = services.NewFailureWatcher()
mgr.subservicesWatcher.WatchManager(mgr.subservices)
mgr.Service = services.NewBasicService(mgr.starting, mgr.running, mgr.stopping)
return &mgr, nil
}
func (o *userConfigurableOverridesManager) starting(ctx context.Context) error {
if err := services.StartManagerAndAwaitHealthy(ctx, o.subservices); err != nil {
return fmt.Errorf("unable to start overrides subservices: %w", err)
}
return o.reloadAllTenantLimits(ctx)
}
func (o *userConfigurableOverridesManager) running(ctx context.Context) error {
ticker := time.NewTicker(o.cfg.PollInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
err := o.reloadAllTenantLimits(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
metricUserConfigurableOverridesReloadFailed.Inc()
level.Error(o.logger).Log("msg", "failed to refresh user-configurable config", "err", err)
}
continue
case err := <-o.subservicesWatcher.Chan():
return fmt.Errorf("overrides subservice failed: %w", err)
}
}
}
func (o *userConfigurableOverridesManager) stopping(error) error {
return services.StopManagerAndAwaitStopped(context.Background(), o.subservices)
}
func (o *userConfigurableOverridesManager) reloadAllTenantLimits(ctx context.Context) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "userConfigurableOverridesManager.reloadAllTenantLimits")
defer span.Finish()
traceID, _ := tracing.ExtractTraceID(ctx)
level.Info(o.logger).Log("msg", "reloading all tenant limits", "traceID", traceID)
// List tenants with user-configurable overrides
tenants, err := o.client.List(ctx)
if err != nil {
return err
}
// Clean up cached tenants that have been removed from the backend
for cachedTenant := range o.tenantLimits {
if !slices.Contains(tenants, cachedTenant) {
o.setTenantLimit(cachedTenant, nil)
}
}
// For every tenant with user-configurable overrides, download and cache them
for _, tenant := range tenants {
limits, _, err := o.client.Get(ctx, tenant)
if errors.Is(err, backend.ErrDoesNotExist) {
o.setTenantLimit(tenant, nil)
continue
}
if err != nil {
return fmt.Errorf("failed to load tenant limits for tenant %v: %w", tenant, err)
}
o.setTenantLimit(tenant, limits)
}
return nil
}
// getTenantLimits returns the tenant limits for the given tenant, can be nil.
func (o *userConfigurableOverridesManager) getTenantLimits(userID string) *userconfigurableoverrides.Limits {
o.mtx.RLock()
defer o.mtx.RUnlock()
return o.tenantLimits[userID]
}
func (o *userConfigurableOverridesManager) getAllTenantLimits() tenantLimits {
o.mtx.RLock()
defer o.mtx.RUnlock()
return o.tenantLimits
}
func (o *userConfigurableOverridesManager) setTenantLimit(userID string, limits *userconfigurableoverrides.Limits) {
o.mtx.Lock()
defer o.mtx.Unlock()
if limits == nil {
delete(o.tenantLimits, userID)
} else {
o.tenantLimits[userID] = limits
}
}
func (o *userConfigurableOverridesManager) Forwarders(userID string) []string {
if forwarders, ok := o.getTenantLimits(userID).GetForwarders(); ok {
return forwarders
}
return o.Interface.Forwarders(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessors(userID string) map[string]struct{} {
// We merge settings from both layers meaning if a processor is enabled on any layer it will be always enabled (OR logic)
processorsUserConfigurable, _ := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessors()
processorsRuntime := o.Interface.MetricsGeneratorProcessors(userID)
return listtomap.Merge(processorsUserConfigurable, processorsRuntime)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorDisableCollection(userID string) bool {
if disableCollection, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetDisableCollection(); ok {
return disableCollection
}
return o.Interface.MetricsGeneratorDisableCollection(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorCollectionInterval(userID string) time.Duration {
if collectionInterval, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetCollectionInterval(); ok {
return collectionInterval
}
return o.Interface.MetricsGeneratorCollectionInterval(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessorServiceGraphsDimensions(userID string) []string {
if dimensions, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessor().GetServiceGraphs().GetDimensions(); ok {
return dimensions
}
return o.Interface.MetricsGeneratorProcessorServiceGraphsDimensions(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessorServiceGraphsEnableClientServerPrefix(userID string) bool {
if enableClientServerPrefix, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessor().GetServiceGraphs().GetEnableClientServerPrefix(); ok {
return enableClientServerPrefix
}
return o.Interface.MetricsGeneratorProcessorServiceGraphsEnableClientServerPrefix(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessorServiceGraphsPeerAttributes(userID string) []string {
if peerAttributes, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessor().GetServiceGraphs().GetPeerAttributes(); ok {
return peerAttributes
}
return o.Interface.MetricsGeneratorProcessorServiceGraphsPeerAttributes(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessorServiceGraphsHistogramBuckets(userID string) []float64 {
if histogramBuckets, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessor().GetServiceGraphs().GetHistogramBuckets(); ok {
return histogramBuckets
}
return o.Interface.MetricsGeneratorProcessorServiceGraphsHistogramBuckets(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessorSpanMetricsDimensions(userID string) []string {
if dimensions, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessor().GetSpanMetrics().GetDimensions(); ok {
return dimensions
}
return o.Interface.MetricsGeneratorProcessorSpanMetricsDimensions(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessorSpanMetricsEnableTargetInfo(userID string) bool {
if enableTargetInfo, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessor().GetSpanMetrics().GetEnableTargetInfo(); ok {
return enableTargetInfo
}
return o.Interface.MetricsGeneratorProcessorSpanMetricsEnableTargetInfo(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessorSpanMetricsFilterPolicies(userID string) []filterconfig.FilterPolicy {
if filterPolicies, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessor().GetSpanMetrics().GetFilterPolicies(); ok {
return filterPolicies
}
return o.Interface.MetricsGeneratorProcessorSpanMetricsFilterPolicies(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessorSpanMetricsHistogramBuckets(userID string) []float64 {
if histogramBuckets, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessor().GetSpanMetrics().GetHistogramBuckets(); ok {
return histogramBuckets
}
return o.Interface.MetricsGeneratorProcessorSpanMetricsHistogramBuckets(userID)
}
func (o *userConfigurableOverridesManager) MetricsGeneratorProcessorSpanMetricsTargetInfoExcludedDimensions(userID string) []string {
if targetInfoExcludedDimensions, ok := o.getTenantLimits(userID).GetMetricsGenerator().GetProcessor().GetSpanMetrics().GetTargetInfoExcludedDimensions(); ok {
return targetInfoExcludedDimensions
}
return o.Interface.MetricsGeneratorProcessorSpanMetricsTargetInfoExcludedDimensions(userID)
}
// statusUserConfigurableOverrides used to marshal userconfigurableoverrides.Limits for tenants
type statusUserConfigurableOverrides struct {
TenantLimits tenantLimits `yaml:"user_configurable_overrides" json:"user_configurable_overrides"`
}
func (o *userConfigurableOverridesManager) WriteStatusRuntimeConfig(w io.Writer, r *http.Request) error {
// fetch runtimeConfig and Runtime per tenant Overrides
err := o.Interface.WriteStatusRuntimeConfig(w, r)
if err != nil {
return err
}
// now write per tenant user configured overrides
// wrap in userConfigOverrides struct to return correct yaml
l := o.getAllTenantLimits()
ucl := statusUserConfigurableOverrides{TenantLimits: l}
out, err := yaml.Marshal(ucl)
if err != nil {
return err
}
_, err = w.Write(out)
if err != nil {
return err
}
return nil
}
type statusTenantOverrides struct {
UserConfigurableLimits *userconfigurableoverrides.Limits `yaml:"user_configurable_limits"`
RuntimeOverrides *Overrides `yaml:"runtime_overrides"`
}
func (o *userConfigurableOverridesManager) WriteTenantOverrides(w io.Writer, _ *http.Request, userID string) error {
overrides := statusTenantOverrides{
UserConfigurableLimits: o.getTenantLimits(userID),
RuntimeOverrides: o.GetRuntimeOverridesFor(userID),
}
out, err := yaml.Marshal(overrides)
if err != nil {
return err
}
_, err = w.Write(out)
return err
}
func (o *userConfigurableOverridesManager) Describe(ch chan<- *prometheus.Desc) {
// TODO for now just pass along to the underlying overrides, in the future we should export
// the user-config overrides as well
o.Interface.Describe(ch)
}
func (o *userConfigurableOverridesManager) Collect(ch chan<- prometheus.Metric) {
// TODO for now just pass along to the underlying overrides, in the future we should export
// the user-config overrides as well
o.Interface.Collect(ch)
}