-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement filter for remote write #1891
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
Open
pipiland2612
wants to merge
1
commit into
prometheus:main
Choose a base branch
from
pipiland2612:add_filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,4 +295,147 @@ func TestRemoteAPI_Write_WithHandler(t *testing.T) { | |
t.Fatal("retry callback should not be invoked on successful request") | ||
} | ||
}) | ||
|
||
t.Run("filter invoked on each attempt", func(t *testing.T) { | ||
tLogger := slog.Default() | ||
mockCode := http.StatusInternalServerError | ||
mStore := &mockStorage{ | ||
mockErr: errors.New("storage error"), | ||
mockCode: &mockCode, | ||
} | ||
srv := httptest.NewServer(NewWriteHandler(mStore, MessageTypes{WriteV2MessageType}, WithWriteHandlerLogger(tLogger))) | ||
t.Cleanup(srv.Close) | ||
|
||
var filterInvocations []int | ||
client, err := NewAPI(srv.URL, | ||
WithAPIHTTPClient(srv.Client()), | ||
WithAPILogger(tLogger), | ||
WithAPIPath("api/v1/write"), | ||
WithAPIBackoff(backoff.Config{ | ||
Min: 1 * time.Millisecond, | ||
Max: 1 * time.Millisecond, | ||
MaxRetries: 2, | ||
}), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
req := testV2() | ||
_, err = client.Write(context.Background(), WriteV2MessageType, req, | ||
WithWriteFilter(func(attempt int, msg any) (any, error) { | ||
filterInvocations = append(filterInvocations, attempt) | ||
return msg, nil | ||
}), | ||
) | ||
if err == nil { | ||
t.Fatal("expected error, got nil") | ||
} | ||
|
||
// Filter should be invoked for initial attempt (0) and 2 retries (1, 2). | ||
expectedInvocations := []int{0, 1, 2} | ||
if diff := cmp.Diff(expectedInvocations, filterInvocations); diff != "" { | ||
t.Fatalf("unexpected filter invocations (-want +got):\n%s", diff) | ||
} | ||
}) | ||
|
||
t.Run("filter can modify message on retries", func(t *testing.T) { | ||
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 might be misunderstanding, but I'm wondering if the test name fully matches what we're testing here? It seems like we're only attempting once, rather than simulating retry behavior. Does that sound right, or am I missing something? |
||
tLogger := slog.Default() | ||
mStore := &mockStorage{} | ||
srv := httptest.NewServer(NewWriteHandler(mStore, MessageTypes{WriteV2MessageType}, WithWriteHandlerLogger(tLogger))) | ||
t.Cleanup(srv.Close) | ||
|
||
client, err := NewAPI(srv.URL, | ||
WithAPIHTTPClient(srv.Client()), | ||
WithAPILogger(tLogger), | ||
WithAPIPath("api/v1/write"), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
req := testV2() | ||
originalTimeseriesCount := len(req.Timeseries) | ||
|
||
_, err = client.Write(context.Background(), WriteV2MessageType, req, | ||
WithWriteFilter(func(attempt int, msg any) (any, error) { | ||
r, ok := msg.(*writev2.Request) | ||
if !ok { | ||
t.Fatal("expected *writev2.Request") | ||
} | ||
|
||
// On retries (attempt > 0), filter out the first timeseries. | ||
if attempt > 0 { | ||
filtered := &writev2.Request{ | ||
Timeseries: r.Timeseries[1:], | ||
Symbols: r.Symbols, | ||
} | ||
return filtered, nil | ||
} | ||
return msg, nil | ||
}), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Verify original message was sent on first attempt. | ||
if len(mStore.v2Reqs) != 1 { | ||
t.Fatalf("expected 1 request stored, got %d", len(mStore.v2Reqs)) | ||
} | ||
if len(mStore.v2Reqs[0].Timeseries) != originalTimeseriesCount { | ||
t.Fatalf("expected %d timeseries in stored request, got %d", | ||
originalTimeseriesCount, len(mStore.v2Reqs[0].Timeseries)) | ||
} | ||
}) | ||
|
||
t.Run("filter error stops retries", func(t *testing.T) { | ||
tLogger := slog.Default() | ||
mockCode := http.StatusInternalServerError | ||
mStore := &mockStorage{ | ||
mockErr: errors.New("storage error"), | ||
mockCode: &mockCode, | ||
} | ||
srv := httptest.NewServer(NewWriteHandler(mStore, MessageTypes{WriteV2MessageType}, WithWriteHandlerLogger(tLogger))) | ||
t.Cleanup(srv.Close) | ||
|
||
var attemptCount int | ||
client, err := NewAPI(srv.URL, | ||
WithAPIHTTPClient(srv.Client()), | ||
WithAPILogger(tLogger), | ||
WithAPIPath("api/v1/write"), | ||
WithAPIBackoff(backoff.Config{ | ||
Min: 1 * time.Millisecond, | ||
Max: 1 * time.Millisecond, | ||
MaxRetries: 5, | ||
}), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
req := testV2() | ||
_, err = client.Write(context.Background(), WriteV2MessageType, req, | ||
WithWriteFilter(func(attempt int, msg any) (any, error) { | ||
attemptCount++ | ||
// Return error on second retry (attempt 2). | ||
if attempt >= 2 { | ||
return nil, errors.New("filter rejected message") | ||
} | ||
return msg, nil | ||
}), | ||
) | ||
|
||
if err == nil { | ||
t.Fatal("expected error, got nil") | ||
} | ||
if !strings.Contains(err.Error(), "filter rejected message") { | ||
t.Fatalf("expected error to contain 'filter rejected message', got %v", err) | ||
} | ||
|
||
// Should only reach attempt 2 (0, 1, 2) before filter stops it. | ||
if attemptCount != 3 { | ||
t.Fatalf("expected 3 filter invocations (attempts 0,1,2), got %d", attemptCount) | ||
} | ||
}) | ||
} |
Oops, something went wrong.
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.
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.
Here's an idea for more realistic tests: we could implement a custom test server or modify
mockStorage
to handle retry logic. This would allow us to validate code behavior against scenarios such as 'fails, retries, fails, then succeeds.' Do you feel that this make sense?