Skip to content
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

fix: properly return http status codes from ingester to querier for RPC function calls #13134

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions pkg/ingester/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
server_util "github.com/grafana/loki/v3/pkg/util/server"
"math/rand"
"net/http"
"os"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/concurrency"
"github.com/grafana/dskit/httpgrpc"
"github.com/grafana/dskit/modules"
"github.com/grafana/dskit/multierror"
"github.com/grafana/dskit/ring"
Expand Down Expand Up @@ -1041,6 +1043,15 @@ func (i *Ingester) asyncStoreMaxLookBack() time.Duration {

// GetChunkIDs is meant to be used only when using an async store like boltdb-shipper or tsdb.
func (i *Ingester) GetChunkIDs(ctx context.Context, req *logproto.GetChunkIDsRequest) (*logproto.GetChunkIDsResponse, error) {
var status int
cstyan marked this conversation as resolved.
Show resolved Hide resolved
gcr, err := i.getChunkIDs(ctx, req)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return gcr, err
}

// GetChunkIDs is meant to be used only when using an async store like boltdb-shipper or tsdb.
func (i *Ingester) getChunkIDs(ctx context.Context, req *logproto.GetChunkIDsRequest) (*logproto.GetChunkIDsResponse, error) {
orgID, err := tenant.TenantID(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1168,6 +1179,14 @@ func (i *Ingester) Label(ctx context.Context, req *logproto.LabelRequest) (*logp

// Series queries the ingester for log stream identifiers (label sets) matching a set of matchers
func (i *Ingester) Series(ctx context.Context, req *logproto.SeriesRequest) (*logproto.SeriesResponse, error) {
var status int
sr, err := i.series(ctx, req)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return sr, err
}

func (i *Ingester) series(ctx context.Context, req *logproto.SeriesRequest) (*logproto.SeriesResponse, error) {
instanceID, err := tenant.TenantID(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1331,6 +1350,13 @@ func (i *Ingester) getInstances() []*instance {

// Tail logs matching given query
func (i *Ingester) Tail(req *logproto.TailRequest, queryServer logproto.Querier_TailServer) error {
var status int
err := i.tail(req, queryServer)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return err
}
func (i *Ingester) tail(req *logproto.TailRequest, queryServer logproto.Querier_TailServer) error {
select {
case <-i.tailersQuit:
return errors.New("Ingester is stopping")
Expand Down Expand Up @@ -1376,6 +1402,14 @@ func (i *Ingester) Tail(req *logproto.TailRequest, queryServer logproto.Querier_

// TailersCount returns count of active tail requests from a user
func (i *Ingester) TailersCount(ctx context.Context, _ *logproto.TailersCountRequest) (*logproto.TailersCountResponse, error) {
var status int
tcr, err := i.tailersCount(ctx)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return tcr, err
}

func (i *Ingester) tailersCount(ctx context.Context) (*logproto.TailersCountResponse, error) {
instanceID, err := tenant.TenantID(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1431,6 +1465,14 @@ func (i *Ingester) GetDetectedFields(_ context.Context, r *logproto.DetectedFiel

// GetDetectedLabels returns map of detected labels and unique values from this ingester
func (i *Ingester) GetDetectedLabels(ctx context.Context, req *logproto.DetectedLabelsRequest) (*logproto.LabelToValuesResponse, error) {
var status int
lvr, err := i.getDetectedLabels(ctx, req)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return lvr, err
}

func (i *Ingester) getDetectedLabels(ctx context.Context, req *logproto.DetectedLabelsRequest) (*logproto.LabelToValuesResponse, error) {
userID, err := tenant.TenantID(ctx)
if err != nil {
return nil, err
Expand Down
41 changes: 41 additions & 0 deletions pkg/ingester/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/grafana/loki/v3/pkg/util/deletion"
util_log "github.com/grafana/loki/v3/pkg/util/log"
mathutil "github.com/grafana/loki/v3/pkg/util/math"
server_util "github.com/grafana/loki/v3/pkg/util/server"
"github.com/grafana/loki/v3/pkg/validation"
)

Expand Down Expand Up @@ -441,6 +442,14 @@ func (i *instance) getLabelsFromFingerprint(fp model.Fingerprint) labels.Labels
}

func (i *instance) Query(ctx context.Context, req logql.SelectLogParams) (iter.EntryIterator, error) {
var status int
it, err := i.query(ctx, req)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return it, err
}

func (i *instance) query(ctx context.Context, req logql.SelectLogParams) (iter.EntryIterator, error) {
expr, err := req.LogSelector()
if err != nil {
return nil, err
Expand Down Expand Up @@ -495,6 +504,14 @@ func (i *instance) Query(ctx context.Context, req logql.SelectLogParams) (iter.E
}

func (i *instance) QuerySample(ctx context.Context, req logql.SelectSampleParams) (iter.SampleIterator, error) {
var status int
it, err := i.querySample(ctx, req)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return it, err
}

func (i *instance) querySample(ctx context.Context, req logql.SelectSampleParams) (iter.SampleIterator, error) {
expr, err := req.Expr()
if err != nil {
return nil, err
Expand Down Expand Up @@ -556,6 +573,14 @@ func (i *instance) QuerySample(ctx context.Context, req logql.SelectSampleParams
// If label matchers are given only the matching streams are fetched from the index.
// The label names or values are then retrieved from those matching streams.
func (i *instance) Label(ctx context.Context, req *logproto.LabelRequest, matchers ...*labels.Matcher) (*logproto.LabelResponse, error) {
var status int
lr, err := i.label(ctx, req)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return lr, err
}

func (i *instance) label(ctx context.Context, req *logproto.LabelRequest, matchers ...*labels.Matcher) (*logproto.LabelResponse, error) {
if len(matchers) == 0 {
var labels []string
if req.Values {
Expand Down Expand Up @@ -709,6 +734,14 @@ func (i *instance) Series(ctx context.Context, req *logproto.SeriesRequest) (*lo
}

func (i *instance) GetStats(ctx context.Context, req *logproto.IndexStatsRequest) (*logproto.IndexStatsResponse, error) {
var status int
isr, err := i.getStats(ctx, req)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return isr, err
}

func (i *instance) getStats(ctx context.Context, req *logproto.IndexStatsRequest) (*logproto.IndexStatsResponse, error) {
matchers, err := syntax.ParseMatchers(req.Matchers, true)
if err != nil {
return nil, err
Expand Down Expand Up @@ -765,6 +798,14 @@ func (i *instance) GetStats(ctx context.Context, req *logproto.IndexStatsRequest
}

func (i *instance) GetVolume(ctx context.Context, req *logproto.VolumeRequest) (*logproto.VolumeResponse, error) {
var status int
vr, err := i.getVolume(ctx, req)
status, err = server_util.ClientHTTPStatusAndError(err)
err = httpgrpc.Errorf(status, "%s", err.Error())
return vr, err
}

func (i *instance) getVolume(ctx context.Context, req *logproto.VolumeRequest) (*logproto.VolumeResponse, error) {
matchers, err := syntax.ParseMatchers(req.Matchers, true)
if err != nil && req.Matchers != seriesvolume.MatchAny {
return nil, err
Expand Down
Loading