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

Bring back RED metrics for querier when processing scheduler requests. #11097

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion pkg/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ type Loki struct {

HTTPAuthMiddleware middleware.Interface

Codec Codec
Codec Codec
Metrics *server.Metrics
}

// New makes a new Loki.
Expand Down
11 changes: 4 additions & 7 deletions pkg/loki/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ func (t *Loki) initServer() (services.Service, error) {

// Loki handles signals on its own.
DisableSignalHandling(&t.Cfg.Server)
serv, err := server.New(t.Cfg.Server)

t.Metrics = server.NewServerMetrics(t.Cfg.Server)
serv, err := server.NewWithMetrics(t.Cfg.Server, t.Metrics)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -515,12 +517,7 @@ func (t *Loki) initQuerier() (services.Service, error) {

internalHandler := queryrangebase.MergeMiddlewares(
serverutil.RecoveryMiddleware,
queryrange.Instrument{
QueryHandlerMetrics: queryrange.NewQueryHandlerMetrics(
prometheus.DefaultRegisterer,
t.Cfg.MetricsNamespace,
),
},
queryrange.Instrument{Metrics: t.Metrics},
).Wrap(handler)

svc, err := querier.InitWorkerService(
Expand Down
23 changes: 23 additions & 0 deletions pkg/querier/queryrange/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ func (Codec) DecodeHTTPGrpcRequest(ctx context.Context, r *httpgrpc.HTTPRequest)

// DecodeHTTPGrpcResponse decodes an httpgrp.HTTPResponse to queryrangebase.Response.
func (Codec) DecodeHTTPGrpcResponse(r *httpgrpc.HTTPResponse, req queryrangebase.Request) (queryrangebase.Response, error) {
if r.Code/100 != 2 {
return nil, httpgrpc.Errorf(int(r.Code), string(r.Body))
}

headers := make(http.Header)
for _, header := range r.Headers {
headers[header.Key] = header.Values
Expand Down Expand Up @@ -702,6 +706,25 @@ func (c Codec) EncodeRequest(ctx context.Context, r queryrangebase.Request) (*ht
}
}

func (c Codec) Path(r queryrangebase.Request) string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this could be used by EncodePath as well.

switch request := r.(type) {
case *LokiRequest:
return "loki/api/v1/query_range"
case *LokiSeriesRequest:
return "loki/api/v1/series"
case *LabelRequest:
return request.Path() // NOTE: this could be either /label or /label/{name}/values endpoint. So forward the original path as it is.
case *LokiInstantRequest:
return "/loki/api/v1/query"
case *logproto.IndexStatsRequest:
return "/loki/api/v1/index/stats"
case *logproto.VolumeRequest:
return "/loki/api/v1/index/volume_range"
}

return "other"
}

func (p RequestProtobufCodec) EncodeRequest(ctx context.Context, r queryrangebase.Request) (*http.Request, error) {
req, err := p.Codec.EncodeRequest(ctx, r)
if err != nil {
Expand Down
51 changes: 29 additions & 22 deletions pkg/querier/queryrange/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,53 @@ package queryrange

import (
"context"
"fmt"
"strconv"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/grafana/dskit/httpgrpc"
"github.com/grafana/dskit/instrument"
"github.com/grafana/dskit/middleware"

"github.com/grafana/dskit/server"

"github.com/grafana/loki/pkg/querier/queryrange/queryrangebase"
)

const (
gRPC = "gRPC"
method = "GET"
)

type QueryHandlerMetrics struct {
InflightRequests *prometheus.GaugeVec
}

func NewQueryHandlerMetrics(registerer prometheus.Registerer, metricsNamespace string) *QueryHandlerMetrics {
return &QueryHandlerMetrics{
InflightRequests: promauto.With(registerer).NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "inflight_requests",
Help: "Current number of inflight requests.",
}, []string{"method", "route"}),
}
}

type Instrument struct {
*QueryHandlerMetrics
*server.Metrics
}

var _ queryrangebase.Middleware = Instrument{}

// Wrap implements the queryrangebase.Middleware
func (i Instrument) Wrap(next queryrangebase.Handler) queryrangebase.Handler {
return queryrangebase.HandlerFunc(func(ctx context.Context, r queryrangebase.Request) (queryrangebase.Response, error) {
route := fmt.Sprintf("%T", r)
inflight := i.InflightRequests.WithLabelValues(gRPC, route)
route := DefaultCodec.Path(r)
route = middleware.MakeLabelValue(route)
inflight := i.InflightRequests.WithLabelValues(method, route)
inflight.Inc()
defer inflight.Dec()

return next.Do(ctx, r)
begin := time.Now()
result, err := next.Do(ctx, r)
i.observe(ctx, route, err, time.Since(begin))

return result, err
})
}

func (i Instrument) observe(ctx context.Context, route string, err error, duration time.Duration) {
respStatus := "200"
if err != nil {
if errResp, ok := httpgrpc.HTTPResponseFromError(err); ok {
respStatus = strconv.Itoa(int(errResp.Code))
} else {
respStatus = "500"
}
}
instrument.ObserveWithExemplar(ctx, i.RequestDuration.WithLabelValues(method, route, respStatus, "false"), duration.Seconds())
}
Loading