Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
loki/pkg/limits/frontend/http.go

63 lines
1.6 KiB

package frontend
import (
"encoding/json"
"net/http"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/user"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/util"
)
type httpExceedsLimitsRequest struct {
TenantID string `json:"tenantID"`
StreamHashes []uint64 `json:"streamHashes"`
}
type httpExceedsLimitsResponse struct {
Results []*logproto.ExceedsLimitsResult `json:"results,omitempty"`
}
// ServeHTTP implements http.Handler.
func (f *Frontend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var req httpExceedsLimitsRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "JSON is invalid or does not match expected schema", http.StatusBadRequest)
return
}
if req.TenantID == "" {
http.Error(w, "tenantID is required", http.StatusBadRequest)
return
}
streams := make([]*logproto.StreamMetadata, 0, len(req.StreamHashes))
for _, streamHash := range req.StreamHashes {
streams = append(streams, &logproto.StreamMetadata{
StreamHash: streamHash,
})
}
protoReq := &logproto.ExceedsLimitsRequest{
Tenant: req.TenantID,
Streams: streams,
}
ctx, err := user.InjectIntoGRPCRequest(user.InjectOrgID(r.Context(), req.TenantID))
if err != nil {
http.Error(w, "failed to inject org ID", http.StatusInternalServerError)
return
}
resp, err := f.ExceedsLimits(ctx, protoReq)
if err != nil {
level.Error(f.logger).Log("msg", "failed to check limits", "err", err)
http.Error(w, "an unexpected error occurred while checking limits", http.StatusInternalServerError)
return
}
util.WriteJSONResponse(w, httpExceedsLimitsResponse{
Results: resp.Results,
})
}