Add target to context (#10473)

Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com>
This commit is contained in:
Goutham Veeramachaneni 2022-03-24 16:53:04 +01:00 committed by GitHub
parent 1291ec7185
commit 4d8bbfd416
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -305,6 +305,7 @@ func newScrapePool(cfg *config.ScrapeConfig, app storage.Appendable, jitterSeed
// Store the cache in the context. // Store the cache in the context.
loopCtx := ContextWithMetricMetadataStore(ctx, cache) loopCtx := ContextWithMetricMetadataStore(ctx, cache)
loopCtx = ContextWithTarget(loopCtx, opts.target)
return newScrapeLoop( return newScrapeLoop(
loopCtx, loopCtx,
@ -1812,6 +1813,7 @@ type ctxKey int
// Valid CtxKey values. // Valid CtxKey values.
const ( const (
ctxKeyMetadata ctxKey = iota + 1 ctxKeyMetadata ctxKey = iota + 1
ctxKeyTarget
) )
func ContextWithMetricMetadataStore(ctx context.Context, s MetricMetadataStore) context.Context { func ContextWithMetricMetadataStore(ctx context.Context, s MetricMetadataStore) context.Context {
@ -1822,3 +1824,12 @@ func MetricMetadataStoreFromContext(ctx context.Context) (MetricMetadataStore, b
s, ok := ctx.Value(ctxKeyMetadata).(MetricMetadataStore) s, ok := ctx.Value(ctxKeyMetadata).(MetricMetadataStore)
return s, ok return s, ok
} }
func ContextWithTarget(ctx context.Context, t *Target) context.Context {
return context.WithValue(ctx, ctxKeyTarget, t)
}
func TargetFromContext(ctx context.Context) (*Target, bool) {
t, ok := ctx.Value(ctxKeyTarget).(*Target)
return t, ok
}