Loki: Adds a suggestions resource call that inject scopes for finding label/labelvalues (#96025)

Signed-off-by: bergquist <carl.bergquist@gmail.com>
Co-authored-by: Sven Grossmann <sven.grossmann@grafana.com>
This commit is contained in:
Carl Bergquist
2024-11-13 14:36:12 +01:00
committed by GitHub
co-authored by Sven Grossmann
parent 19c04168c3
commit 16330e4113
2 changed files with 95 additions and 6 deletions
+24 -6
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"regexp"
"strings"
"sync"
"time"
@@ -131,13 +132,30 @@ func callResource(ctx context.Context, req *backend.CallResourceRequest, sender
api := newLokiAPI(dsInfo.HTTPClient, dsInfo.URL, plog, tracer, false)
rawLokiResponse, err := api.RawQuery(ctx, lokiURL)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
plog.Error("Failed resource call from loki", "err", err, "url", lokiURL)
return err
var rawLokiResponse RawLokiResponse
var err error
// suggestions is a resource endpoint that will return label and label value suggestions based
// on queries and the existing scope. By moving this to the backend we can use the logql parser to
// rewrite queries safely.
if req.Method == http.MethodPost && strings.EqualFold(req.Path, "suggestions") {
rawLokiResponse, err = GetSuggestions(ctx, api, req)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
plog.FromContext(ctx).Error("Failed to get suggestions from loki", "err", err)
return err
}
} else {
rawLokiResponse, err = api.RawQuery(ctx, lokiURL)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
plog.Error("Failed resource call from loki", "err", err, "url", lokiURL)
return err
}
}
respHeaders := map[string][]string{
"content-type": {"application/json"},
}