Skip to content

Commit b1f9424

Browse files
[coordinator] Fix inverted interpretation of Prometheus remote write shadow sampling config (#4210)
1 parent 6d5f3aa commit b1f9424

2 files changed

Lines changed: 43 additions & 37 deletions

File tree

src/query/api/v1/handler/prometheus/remote/write.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,13 +650,15 @@ func (h *PromWriteHandler) buildForwardShadowRequestBody(
650650

651651
// Use a range of 10k to allow for setting 0.01% having an effect
652652
// when shadow percent is set (i.e. with percent=0.0001)
653-
if hash(buffer)%10000 <= uint64(shadowOpts.Percent*10000) {
654-
// Keep this series, it falls below the volume target of shards.
655-
h.metrics.forwardShadowKeep.Inc(1)
653+
if hash(buffer)%10000 >= uint64(shadowOpts.Percent*10000) {
654+
// Skip forwarding this series, not in shadow volume of shards.
655+
// Swap it with the tail and continue.
656+
h.metrics.forwardShadowDrop.Inc(1)
656657
continue
657658
}
658659

659-
h.metrics.forwardShadowDrop.Inc(1)
660+
// Keep this series, it falls below the volume target of shards.
661+
h.metrics.forwardShadowKeep.Inc(1)
660662

661663
// Skip forwarding this series, not in shadow volume of shards.
662664
// Swap it with the tail and continue.

src/query/api/v1/handler/prometheus/remote/write_test.go

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -451,40 +451,38 @@ func TestPromWriteLiteralIsTooLongError(t *testing.T) {
451451
}
452452
}
453453

454-
func TestPromWriteForwardWithShadowDefaultHash(t *testing.T) {
455-
testPromWriteForwardWithShadow(t, testPromWriteForwardWithShadowOptions{
456-
numSeries: 10000,
457-
percent: 0.5,
458-
expectedFwded: 5000,
459-
expectedFwdedAllowedVariance: 0.05,
460-
})
461-
}
462-
463-
func TestPromWriteForwardWithShadowXXHash(t *testing.T) {
464-
testPromWriteForwardWithShadow(t, testPromWriteForwardWithShadowOptions{
465-
numSeries: 10000,
466-
percent: 0.5,
467-
hash: "xxhash",
468-
expectedFwded: 5000,
469-
expectedFwdedAllowedVariance: 0.05,
470-
})
471-
}
472-
473-
func TestPromWriteForwardWithShadowMurmur3(t *testing.T) {
474-
testPromWriteForwardWithShadow(t, testPromWriteForwardWithShadowOptions{
475-
numSeries: 10000,
476-
percent: 0.5,
477-
hash: "murmur3",
478-
expectedFwded: 5000,
479-
expectedFwdedAllowedVariance: 0.05,
480-
})
454+
func TestPromWriteForwardWithShadow(t *testing.T) {
455+
for _, tt := range []struct {
456+
percent float64
457+
numSeries int
458+
allowedVariance float64
459+
}{
460+
{0, 10000, 0},
461+
{0.25, 10000, 0.05},
462+
{0.5, 10000, 0.05},
463+
{0.75, 10000, 0.05},
464+
{1, 10000, 0},
465+
} {
466+
for _, h := range []string{"", "murmur3", "xxhash"} {
467+
h := h
468+
t.Run(fmt.Sprintf("hash='%s', params=%+v", h, tt), func(t *testing.T) {
469+
testPromWriteForwardWithShadow(t, testPromWriteForwardWithShadowOptions{
470+
hash: h,
471+
numSeries: tt.numSeries,
472+
percent: tt.percent,
473+
expectedFwded: int(float64(tt.numSeries) * tt.percent),
474+
expectedFwdedAllowedVariance: tt.allowedVariance,
475+
})
476+
})
477+
}
478+
}
481479
}
482480

483481
type testPromWriteForwardWithShadowOptions struct {
484482
numSeries int
485483
percent float64
486484
hash string
487-
expectedFwded int64
485+
expectedFwded int
488486
expectedFwdedAllowedVariance float64
489487
}
490488

@@ -569,11 +567,17 @@ func testPromWriteForwardWithShadow(
569567

570568
select {
571569
case fwdReq := <-forwardRecvReqCh:
572-
assert.InEpsilon(t, testOpts.expectedFwded, len(fwdReq.Timeseries),
573-
testOpts.expectedFwdedAllowedVariance,
574-
fmt.Sprintf("expected=%v, actual=%v, allowed_variance=%v",
575-
testOpts.expectedFwded, len(fwdReq.Timeseries),
576-
testOpts.expectedFwdedAllowedVariance))
570+
if testOpts.expectedFwdedAllowedVariance > 0 {
571+
assert.InEpsilon(t, testOpts.expectedFwded, len(fwdReq.Timeseries),
572+
testOpts.expectedFwdedAllowedVariance,
573+
fmt.Sprintf("expected=%v, actual=%v, allowed_variance=%v",
574+
testOpts.expectedFwded, len(fwdReq.Timeseries),
575+
testOpts.expectedFwdedAllowedVariance))
576+
} else {
577+
assert.Equal(t, testOpts.expectedFwded, len(fwdReq.Timeseries),
578+
fmt.Sprintf("expected=%v, actual=%v",
579+
testOpts.expectedFwded, len(fwdReq.Timeseries)))
580+
}
577581
case <-time.After(10 * time.Second):
578582
require.FailNow(t, "timeout waiting for fwd request")
579583
}

0 commit comments

Comments
 (0)