-
Notifications
You must be signed in to change notification settings - Fork 20
GIE-226: Add LLM-friendly errors and handle empty response cases #6
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
saswatamcode
wants to merge
3
commits into
rhobs:main
Choose a base branch
from
saswatamcode:checkempty
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| package prometheus | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/common/model" | ||
| ) | ||
|
|
||
| func TestMakeLLMFriendlyError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| originalError error | ||
| query string | ||
| expectedSubstr []string // substrings that should be in the error message | ||
| }{ | ||
| { | ||
| name: "parse error", | ||
| originalError: errors.New("parse error: unexpected character"), | ||
| query: "up{invalid", | ||
| expectedSubstr: []string{ | ||
| "syntax error", | ||
| "up{invalid", | ||
| "check the query syntax", | ||
| }, | ||
| }, | ||
| { | ||
| name: "bad_data error", | ||
| originalError: errors.New("bad_data: invalid expression"), | ||
| query: "rate(http[5m])", | ||
| expectedSubstr: []string{ | ||
| "syntax error", | ||
| "rate(http[5m])", | ||
| "correctly formatted", | ||
| }, | ||
| }, | ||
| { | ||
| name: "unknown function", | ||
| originalError: errors.New("unknown function: foobar"), | ||
| query: "foobar(up)", | ||
| expectedSubstr: []string{ | ||
| "unknown function", | ||
| "foobar(up)", | ||
| "function name is correct", | ||
| }, | ||
| }, | ||
| { | ||
| name: "timeout error", | ||
| originalError: errors.New("query timeout exceeded"), | ||
| query: "sum(rate(http_requests_total[5m])) by (job)", | ||
| expectedSubstr: []string{ | ||
| "timed out", | ||
| "sum(rate(http_requests_total[5m])) by (job)", | ||
| "time range", | ||
| "step size", | ||
| }, | ||
| }, | ||
| { | ||
| name: "deadline exceeded", | ||
| originalError: errors.New("context deadline exceeded"), | ||
| query: "up", | ||
| expectedSubstr: []string{ | ||
| "timed out", | ||
| "up", | ||
| "reducing the time range", | ||
| }, | ||
| }, | ||
| { | ||
| name: "connection refused", | ||
| originalError: errors.New("connection refused"), | ||
| query: "up", | ||
| expectedSubstr: []string{ | ||
| "cannot connect", | ||
| "Prometheus server is running", | ||
| }, | ||
| }, | ||
| { | ||
| name: "no such host", | ||
| originalError: errors.New("no such host: prometheus.example.com"), | ||
| query: "up", | ||
| expectedSubstr: []string{ | ||
| "cannot connect", | ||
| "Prometheus server is running", | ||
| }, | ||
| }, | ||
| { | ||
| name: "generic error", | ||
| originalError: errors.New("some other error"), | ||
| query: "up", | ||
| expectedSubstr: []string{ | ||
| "up", | ||
| "some other error", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := makeLLMFriendlyError(tt.originalError, tt.query) | ||
| if result == nil { | ||
| t.Fatalf("expected error, got nil") | ||
| } | ||
|
|
||
| resultMsg := result.Error() | ||
| for _, substr := range tt.expectedSubstr { | ||
| if !strings.Contains(resultMsg, substr) { | ||
| t.Errorf("expected error to contain %q, got: %s", substr, resultMsg) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestMakeLLMFriendlyError_NilError(t *testing.T) { | ||
| result := makeLLMFriendlyError(nil, "up") | ||
| if result != nil { | ||
| t.Errorf("expected nil error for nil input, got: %v", result) | ||
| } | ||
| } | ||
|
|
||
| func TestCheckEmptyResult(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| result any | ||
| query string | ||
| expectWarning bool | ||
| expectedSubstr []string | ||
| }{ | ||
| { | ||
| name: "empty matrix", | ||
| result: model.Matrix{}, | ||
| query: "nonexistent_metric", | ||
| expectWarning: true, | ||
| expectedSubstr: []string{ | ||
| "nonexistent_metric", | ||
| "returned no data", | ||
| "metric does not exist", | ||
| "no data for the specified time range", | ||
| "list_metrics", | ||
| }, | ||
| }, | ||
| { | ||
| name: "empty vector", | ||
| result: model.Vector{}, | ||
| query: "up{job=\"missing\"}", | ||
| expectWarning: true, | ||
| expectedSubstr: []string{ | ||
| "up{job=\"missing\"}", | ||
| "returned no data", | ||
| "label filters are too restrictive", | ||
| }, | ||
| }, | ||
| { | ||
| name: "non-empty matrix", | ||
| result: model.Matrix{ | ||
| &model.SampleStream{ | ||
| Metric: model.Metric{"__name__": "up"}, | ||
| Values: []model.SamplePair{{Timestamp: 0, Value: 1}}, | ||
| }, | ||
| }, | ||
| query: "up", | ||
| expectWarning: false, | ||
| }, | ||
| { | ||
| name: "non-empty vector", | ||
| result: model.Vector{ | ||
| &model.Sample{ | ||
| Metric: model.Metric{"__name__": "up"}, | ||
| Timestamp: 0, | ||
| Value: 1, | ||
| }, | ||
| }, | ||
| query: "up", | ||
| expectWarning: false, | ||
| }, | ||
| { | ||
| name: "nil scalar", | ||
| result: (*model.Scalar)(nil), | ||
| query: "scalar(nonexistent)", | ||
| expectWarning: true, | ||
| }, | ||
| { | ||
| name: "valid scalar", | ||
| result: &model.Scalar{Value: 1, Timestamp: 0}, | ||
| query: "scalar(up)", | ||
| expectWarning: false, | ||
| }, | ||
| { | ||
| name: "nil string", | ||
| result: (*model.String)(nil), | ||
| query: "string_metric", | ||
| expectWarning: true, | ||
| }, | ||
| { | ||
| name: "valid string", | ||
| result: &model.String{Value: "test", Timestamp: 0}, | ||
| query: "string_metric", | ||
| expectWarning: false, | ||
| }, | ||
| { | ||
| name: "unknown type", | ||
| result: "unknown", | ||
| query: "up", | ||
| expectWarning: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| warning := checkEmptyResult(tt.result, tt.query) | ||
|
|
||
| if tt.expectWarning { | ||
| if warning == "" { | ||
| t.Errorf("expected warning for empty result, got none") | ||
| } | ||
| for _, substr := range tt.expectedSubstr { | ||
| if !strings.Contains(warning, substr) { | ||
| t.Errorf("expected warning to contain %q, got: %s", substr, warning) | ||
| } | ||
| } | ||
| } else if warning != "" { | ||
| t.Errorf("expected no warning, got: %s", warning) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
I wonder if we could better target the case when the metric is purely hallucinated and not present in the system at all. Rather than giving the guidance to the LLM to check the list_metrics, we could then right away return query as error, if the targeted time-series doesn't exist, both saving tokens and getting more deterministic behavior.
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.
There's no real way of doing this here directly when we are executing the query, but...I forgot we already do check for this in the guardrails method. I can move those errors into isSafeQuery method then
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.
Yes, that's what I was thinking of.
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.
Tried to address this in b923bf5. This is a bit more complex now, but essentially we check parsing, metric and label name existence in guardrails (as an always-on check). And then we check if query response is still empty post execution and guide accordingly