diff --git a/scrape/scrape.go b/scrape/scrape.go index 68c392587..098d5573e 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -305,6 +305,7 @@ func newScrapePool(cfg *config.ScrapeConfig, app storage.Appendable, jitterSeed // Store the cache in the context. loopCtx := ContextWithMetricMetadataStore(ctx, cache) + loopCtx = ContextWithTarget(loopCtx, opts.target) return newScrapeLoop( loopCtx, @@ -1812,6 +1813,7 @@ type ctxKey int // Valid CtxKey values. const ( ctxKeyMetadata ctxKey = iota + 1 + ctxKeyTarget ) 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) 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 +}