mirror of https://github.com/grafana/loki
Add per-tenant request counter metric to index gateway server (#9797)
This commit add a counter metric `loki_index_gateway_requests_total` with labels `operation`, `tenant`, `status` for gRPC requests that are served by the index gateway. **What for?** The per-tenant RPS on the index gateway is used to derive the per-tenant shard factor. **Why tracking on the server?** Unlike tracking index gateway RPS on the client side, tracking on the server side does not yield that many series, even in multi-tenant installations with a lot of tenants, because the amount of index gateway instances is relatively small compared to the amount of queriers and frontends. **Special notes for your reviewer**: The previous approach of tracking requests on the client https://github.com/grafana/loki/pull/9781 has been abandoned. Signed-off-by: Christian Haudum <christian.haudum@gmail.com>pull/9809/head
parent
8ca035ffbf
commit
a65c99d9bf
@ -0,0 +1,45 @@ |
||||
package indexgateway |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/dskit/tenant" |
||||
"github.com/prometheus/client_golang/prometheus" |
||||
"github.com/prometheus/client_golang/prometheus/promauto" |
||||
"google.golang.org/grpc" |
||||
) |
||||
|
||||
type ServerInterceptors struct { |
||||
reqCount *prometheus.CounterVec |
||||
PerTenantRequestCount grpc.UnaryServerInterceptor |
||||
} |
||||
|
||||
func NewServerInterceptors(r prometheus.Registerer) *ServerInterceptors { |
||||
requestCount := promauto.With(r).NewCounterVec(prometheus.CounterOpts{ |
||||
Namespace: "loki", |
||||
Subsystem: "index_gateway", |
||||
Name: "requests_total", |
||||
Help: "Total amount of requests served by the index gateway", |
||||
}, []string{"operation", "status", "tenant"}) |
||||
|
||||
perTenantRequestCount := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { |
||||
tenantID, err := tenant.TenantID(ctx) |
||||
if err != nil { |
||||
// ignore requests without tenantID
|
||||
return handler(ctx, req) |
||||
} |
||||
|
||||
resp, err = handler(ctx, req) |
||||
status := "success" |
||||
if err != nil { |
||||
status = "error" |
||||
} |
||||
requestCount.WithLabelValues(info.FullMethod, status, tenantID).Inc() |
||||
return |
||||
} |
||||
|
||||
return &ServerInterceptors{ |
||||
reqCount: requestCount, |
||||
PerTenantRequestCount: perTenantRequestCount, |
||||
} |
||||
} |
Loading…
Reference in new issue