feat(go.d/prometheus): add relabeling engine#22660
Draft
ilyam8 wants to merge 2 commits into
Draft
Conversation
ScrapeStream now takes a context.Context and threads it into the HTTP request via req.WithContext, so a streaming consumer can cancel or deadline a scrape. The method had no callers, so the signature change is contained. Scrape() and ScrapeSeries() keep their signatures and pass context.Background() to the shared fetch helper, so their behavior is unchanged.
Add a per-sample transform hook for Prometheus-style metric relabeling and remove the unused ScrapeStream callback API it supersedes. - type SampleTransform func(Sample) (Sample, bool, error): keep (optionally mutated -- rewrite Name or mutate Labels in place), drop, or abort the scrape. - ScrapeWithTransform(ctx, transform) (MetricFamilies, error) on the Prometheus interface; Scrape() is now ScrapeWithTransform(ctx, nil), so both share one assembly path. - parseToMetricFamilies(text, transform): with a transform it sets ownLabels=true so the transform can mutate each sample's labels safely; with nil it keeps the no-allocation fast path. The transform runs before the assembler. - Remove ScrapeStream + parseToStream (no production callers); stream.go held only ScrapeStream, so it is removed; the exposition-order/deferral caveat moves to ScrapeWithTransform's doc. - Consolidate the parser tests into parse_test.go (stream_test.go no longer maps to a distinct surface); the parser benchmark measures the transform path.
|
Contributor
There was a problem hiding this comment.
1 issue found across 9 files
Confidence score: 4/5
- This PR appears safe to merge with minimal risk: the only reported issue is moderate severity (5/10) and does not indicate a likely functional break for normal scrape behavior.
- In
src/go/pkg/prometheus/client.go,fetch()ignores cancellation in the file-read branch, so a canceled context may continue a file scrape instead of stopping promptly, which can affect responsiveness under cancellation/timeouts. - Pay close attention to
src/go/pkg/prometheus/client.go- ensure context cancellation is honored in the file-read path to avoid non-interruptible scrapes.
Architecture diagram
sequenceDiagram
participant Client as Consumer Code
participant Prom as Prometheus
participant Fetch as HTTP/File Fetcher
participant Parser as PromTextParser
participant Xform as SampleTransform (callback)
participant Asm as Assembler
Note over Client,Asm: NEW: ScrapeWithTransform replaces ScrapeStream
alt Happy Path – No Transform
Client->>Prom: Scrape()
Prom->>Prom: delegates to ScrapeWithTransform(ctx.Background(), nil)
Note over Prom,Fetch: CHANGED: fetch now accepts context
Prom->>Fetch: fetch(ctx, buf)
alt HTTP URL
Fetch->>Fetch: req = req.WithContext(ctx)
Fetch->>Fetch: HTTP GET
else File Path
Fetch->>Fetch: file read
end
Fetch-->>Prom: raw bytes
Prom->>Parser: parseToMetricFamilies(text, nil)
Note over Parser: ownLabels = false
Parser->>Asm: applySample (copies labels)
Asm-->>Parser: MetricFamilies
Parser-->>Prom: MetricFamilies
Prom-->>Client: MetricFamilies
else Happy Path – With Transform
Client->>Prom: ScrapeWithTransform(ctx, transform)
Prom->>Fetch: fetch(ctx, buf)
Fetch-->>Prom: raw bytes
Prom->>Parser: parseToMetricFamilies(text, transform)
Note over Parser: ownLabels = true
loop Each Sample in Exposition Order
Parser->>Xform: transform(sample)
alt Keep
Xform-->>Parser: (mutated sample, true, nil)
Parser->>Asm: applySample(kept sample)
else Drop
Xform-->>Parser: (_, false, nil)
Note over Parser: sample dropped
else Error
Xform-->>Parser: (_, false, error)
Note over Parser: abort scrape
Parser->>Prom: error
Prom-->>Client: error
end
end
Asm-->>Parser: MetricFamilies (only kept samples)
Parser-->>Prom: MetricFamilies
Prom-->>Client: MetricFamilies
end
Note over Client,Asm: REMOVED: ScrapeStream(onHelp, onSample) – now inline in transform
opt Context Cancellation
Fetch->>Fetch: request cancelled by ctx
Fetch-->>Prom: HTTP error
Prom-->>Client: error
end
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Test Plan
Fixes: #22636
Additional Information
For users: How does this change affect me?
Summary by cubic
Adds a per-sample transform hook for Prometheus metric relabeling and replaces the old streaming API.
Scrape()behavior stays the same, whileScrapeWithTransform(ctx, transform)adds context cancellation and safe in-place label edits.New Features
ScrapeWithTransform(ctx, SampleTransform); runs before family assembly.niltransform behaves likeScrape().context.Contextinto HTTP fetch viareq.WithContext.ScrapeStream/parseToStream; tests consolidated and benchmarks updated.Migration
ScrapeStreamusage withScrapeWithTransform(ctx, transform). Return(sample, true, nil)to keep,(_, false, nil)to drop, or an error to abort.Written for commit 1bfb64b. Summary will update on new commits.