-
Notifications
You must be signed in to change notification settings - Fork 56
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
*: add possibility to add fanout metadata #490
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ package model | |
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
|
@@ -14,6 +15,44 @@ import ( | |
"github.com/thanos-io/promql-engine/query" | ||
) | ||
|
||
type metadataKey string | ||
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. we have similar pattern in /execution/warning i think, should we move this to /execution/metadata so its similarly structured? |
||
|
||
const key metadataKey = "promql-metadata" | ||
|
||
func AddMetadataStorage(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, key, &FanoutMetadata{ | ||
storage: make(map[string]map[string]any), | ||
}) | ||
} | ||
|
||
func GetMetadataStorage(ctx context.Context) *FanoutMetadata { | ||
v := ctx.Value(key) | ||
if v == nil { | ||
return nil | ||
} | ||
return v.(*FanoutMetadata) | ||
} | ||
|
||
type FanoutMetadata struct { | ||
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. I think this could just be "Metadata" as its just a map; applications outside could build on top of this map since "fanout" is somewhat a thanos concept that this engine doesnt know about; its more an implementation detail of Select. Thanos could use this to just add fan out metadata in its Select implementation but no need that this library knows what this key contains really. |
||
m sync.Mutex | ||
|
||
storage map[string]map[string]any | ||
} | ||
|
||
func (m *FanoutMetadata) SetMetadata(endpoint string, data map[string]any) { | ||
m.m.Lock() | ||
defer m.m.Unlock() | ||
|
||
m.storage[endpoint] = data | ||
} | ||
|
||
func (m *FanoutMetadata) GetMetadata() map[string]map[string]any { | ||
m.m.Lock() | ||
defer m.m.Unlock() | ||
|
||
return m.storage | ||
} | ||
|
||
type OperatorTelemetry interface { | ||
fmt.Stringer | ||
|
||
|
@@ -22,18 +61,20 @@ type OperatorTelemetry interface { | |
IncrementSamplesAtTimestamp(samples int, t int64) | ||
Samples() *stats.QuerySamples | ||
SubQuery() bool | ||
|
||
FanoutMetadata() *FanoutMetadata | ||
} | ||
|
||
func NewTelemetry(operator fmt.Stringer, opts *query.Options) OperatorTelemetry { | ||
func NewTelemetry(ctx context.Context, operator fmt.Stringer, opts *query.Options) OperatorTelemetry { | ||
if opts.EnableAnalysis { | ||
return NewTrackedTelemetry(operator, opts, false) | ||
return NewTrackedTelemetry(ctx, operator, opts, false) | ||
} | ||
return NewNoopTelemetry(operator) | ||
} | ||
|
||
func NewSubqueryTelemetry(operator fmt.Stringer, opts *query.Options) OperatorTelemetry { | ||
func NewSubqueryTelemetry(ctx context.Context, operator fmt.Stringer, opts *query.Options) OperatorTelemetry { | ||
if opts.EnableAnalysis { | ||
return NewTrackedTelemetry(operator, opts, true) | ||
return NewTrackedTelemetry(ctx, operator, opts, true) | ||
} | ||
return NewNoopTelemetry(operator) | ||
} | ||
|
@@ -52,6 +93,10 @@ func (tm *NoopTelemetry) ExecutionTimeTaken() time.Duration { | |
return time.Duration(0) | ||
} | ||
|
||
func (tm *NoopTelemetry) FanoutMetadata() *FanoutMetadata { | ||
return nil | ||
} | ||
|
||
func (tm *NoopTelemetry) IncrementSamplesAtTimestamp(_ int, _ int64) {} | ||
|
||
func (tm *NoopTelemetry) Samples() *stats.QuerySamples { return nil } | ||
|
@@ -63,15 +108,17 @@ type TrackedTelemetry struct { | |
ExecutionTime time.Duration | ||
LoadedSamples *stats.QuerySamples | ||
subquery bool | ||
ctx context.Context | ||
} | ||
|
||
func NewTrackedTelemetry(operator fmt.Stringer, opts *query.Options, subquery bool) *TrackedTelemetry { | ||
func NewTrackedTelemetry(ctx context.Context, operator fmt.Stringer, opts *query.Options, subquery bool) *TrackedTelemetry { | ||
ss := stats.NewQuerySamples(opts.EnablePerStepStats) | ||
ss.InitStepTracking(opts.Start.UnixMilli(), opts.End.UnixMilli(), stepTrackingInterval(opts.Step)) | ||
return &TrackedTelemetry{ | ||
Stringer: operator, | ||
LoadedSamples: ss, | ||
subquery: subquery, | ||
ctx: ctx, | ||
} | ||
} | ||
|
||
|
@@ -88,6 +135,10 @@ func (ti *TrackedTelemetry) ExecutionTimeTaken() time.Duration { | |
return ti.ExecutionTime | ||
} | ||
|
||
func (ti *TrackedTelemetry) FanoutMetadata() *FanoutMetadata { | ||
return GetMetadataStorage(ti.ctx) | ||
} | ||
|
||
func (ti *TrackedTelemetry) IncrementSamplesAtTimestamp(samples int, t int64) { | ||
ti.updatePeak(samples) | ||
ti.LoadedSamples.IncrementSamplesAtTimestamp(t, int64(samples)) | ||
|
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.
Can we use context from constructor and pass through from engine? This is a bit more work but feels cleaner, we do same with warnings fwiw - if we want to use different kind of metadata someday