Skip to content

Commit

Permalink
Split warnings and info annotations in API response (prometheus#14327)
Browse files Browse the repository at this point in the history
* split warnings and info annotations in API response

Signed-off-by: Jeanette Tan <[email protected]>

* update according to code review

Signed-off-by: Jeanette Tan <[email protected]>

* minimal UI change: show infos in different colour

Signed-off-by: Jeanette Tan <[email protected]>

* Update web/ui/react-app/src/pages/graph/Panel.tsx

Co-authored-by: Julius Volz <[email protected]>
Signed-off-by: zenador <[email protected]>

---------

Signed-off-by: Jeanette Tan <[email protected]>
Signed-off-by: zenador <[email protected]>
Co-authored-by: Julius Volz <[email protected]>
  • Loading branch information
zenador and juliusv authored Jul 6, 2024
1 parent 89608c6 commit 480fefd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
6 changes: 4 additions & 2 deletions storage/fanout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ func TestFanoutErrors(t *testing.T) {
require.NotEmpty(t, ss.Warnings(), "warnings expected")
w := ss.Warnings()
require.Error(t, w.AsErrors()[0])
require.Equal(t, tc.warning.Error(), w.AsStrings("", 0)[0])
warn, _ := w.AsStrings("", 0, 0)
require.Equal(t, tc.warning.Error(), warn[0])
}
})
t.Run("chunks", func(t *testing.T) {
Expand All @@ -207,7 +208,8 @@ func TestFanoutErrors(t *testing.T) {
require.NotEmpty(t, ss.Warnings(), "warnings expected")
w := ss.Warnings()
require.Error(t, w.AsErrors()[0])
require.Equal(t, tc.warning.Error(), w.AsStrings("", 0)[0])
warn, _ := w.AsStrings("", 0, 0)
require.Equal(t, tc.warning.Error(), warn[0])
}
})
}
Expand Down
50 changes: 34 additions & 16 deletions util/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,49 @@ func (a Annotations) AsErrors() []error {
return arr
}

// AsStrings is a convenience function to return the annotations map as a slice
// of strings. The query string is used to get the line number and character offset
// positioning info of the elements which trigger an annotation. We limit the number
// of annotations returned here with maxAnnos (0 for no limit).
func (a Annotations) AsStrings(query string, maxAnnos int) []string {
arr := make([]string, 0, len(a))
// AsStrings is a convenience function to return the annotations map as 2 slices
// of strings, separated into warnings and infos. The query string is used to get the
// line number and character offset positioning info of the elements which trigger an
// annotation. We limit the number of warnings and infos returned here with maxWarnings
// and maxInfos respectively (0 for no limit).
func (a Annotations) AsStrings(query string, maxWarnings, maxInfos int) (warnings, infos []string) {
warnings = make([]string, 0, maxWarnings+1)
infos = make([]string, 0, maxInfos+1)
warnSkipped := 0
infoSkipped := 0
for _, err := range a {
if maxAnnos > 0 && len(arr) >= maxAnnos {
break
}
var anErr annoErr
if errors.As(err, &anErr) {
anErr.Query = query
err = anErr
}
arr = append(arr, err.Error())
switch {
case errors.Is(err, PromQLInfo):
if maxInfos == 0 || len(infos) < maxInfos {
infos = append(infos, err.Error())
} else {
infoSkipped++
}
default:
if maxWarnings == 0 || len(warnings) < maxWarnings {
warnings = append(warnings, err.Error())
} else {
warnSkipped++
}
}
}
if maxAnnos > 0 && len(a) > maxAnnos {
arr = append(arr, fmt.Sprintf("%d more annotations omitted", len(a)-maxAnnos))
if warnSkipped > 0 {
warnings = append(warnings, fmt.Sprintf("%d more warning annotations omitted", warnSkipped))
}
return arr
if infoSkipped > 0 {
infos = append(infos, fmt.Sprintf("%d more info annotations omitted", infoSkipped))
}
return
}

func (a Annotations) CountWarningsAndInfo() (int, int) {
var countWarnings, countInfo int
// CountWarningsAndInfo counts and returns the number of warnings and infos in the
// annotations wrapper.
func (a Annotations) CountWarningsAndInfo() (countWarnings, countInfo int) {
for _, err := range a {
if errors.Is(err, PromQLWarning) {
countWarnings++
Expand All @@ -104,7 +122,7 @@ func (a Annotations) CountWarningsAndInfo() (int, int) {
countInfo++
}
}
return countWarnings, countInfo
return
}

//nolint:revive // error-naming.
Expand Down
5 changes: 4 additions & 1 deletion web/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ type Response struct {
ErrorType errorType `json:"errorType,omitempty"`
Error string `json:"error,omitempty"`
Warnings []string `json:"warnings,omitempty"`
Infos []string `json:"infos,omitempty"`
}

type apiFuncResult struct {
Expand Down Expand Up @@ -1747,11 +1748,13 @@ func (api *API) cleanTombstones(*http.Request) apiFuncResult {
// can be empty if the position information isn't needed.
func (api *API) respond(w http.ResponseWriter, req *http.Request, data interface{}, warnings annotations.Annotations, query string) {
statusMessage := statusSuccess
warn, info := warnings.AsStrings(query, 10, 10)

resp := &Response{
Status: statusMessage,
Data: data,
Warnings: warnings.AsStrings(query, 10),
Warnings: warn,
Infos: info,
}

codec, err := api.negotiateCodec(req, resp)
Expand Down
1 change: 1 addition & 0 deletions web/ui/module/codemirror-promql/src/client/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ interface APIResponse<T> {
data?: T;
error?: string;
warnings?: string[];
infos?: string[];
}

// These are status codes where the Prometheus API still returns a valid JSON body,
Expand Down
8 changes: 8 additions & 0 deletions web/ui/react-app/src/pages/graph/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface PanelState {
lastQueryParams: QueryParams | null;
loading: boolean;
warnings: string[] | null;
infos: string[] | null;
error: string | null;
stats: QueryStats | null;
exprInputValue: string;
Expand Down Expand Up @@ -87,6 +88,7 @@ class Panel extends Component<PanelProps, PanelState> {
lastQueryParams: null,
loading: false,
warnings: null,
infos: null,
error: null,
stats: null,
exprInputValue: props.options.expr,
Expand Down Expand Up @@ -204,6 +206,7 @@ class Panel extends Component<PanelProps, PanelState> {
data: query.data,
exemplars: exemplars?.data,
warnings: query.warnings,
infos: query.infos,
lastQueryParams: {
startTime,
endTime,
Expand Down Expand Up @@ -307,6 +310,11 @@ class Panel extends Component<PanelProps, PanelState> {
<Col>{warning && <Alert color="warning">{warning}</Alert>}</Col>
</Row>
))}
{this.state.infos?.map((info, index) => (
<Row key={index}>
<Col>{info && <Alert color="info">{info}</Alert>}</Col>
</Row>
))}
<Row>
<Col>
<Nav tabs>
Expand Down

0 comments on commit 480fefd

Please sign in to comment.