Add TwirpRequestStatusReporter twirp server hook to count requests (#1309)
This commit is contained in:
+83
-4
@@ -18,18 +18,27 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/twitchtv/twirp"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
loggerKey = struct{}{}
|
||||
loggerKey = struct{}{}
|
||||
statusReporterKey = struct{ a int }{42}
|
||||
)
|
||||
|
||||
type twirpRequestFields struct {
|
||||
service string
|
||||
method string
|
||||
error twirp.Error
|
||||
}
|
||||
|
||||
// logging handling inspired by https://github.com/bakins/twirpzap
|
||||
// License: Apache-2.0
|
||||
func TwirpLogger(logger logger.Logger) *twirp.ServerHooks {
|
||||
@@ -54,10 +63,9 @@ func TwirpLogger(logger logger.Logger) *twirp.ServerHooks {
|
||||
}
|
||||
|
||||
type requestLogger struct {
|
||||
twirpRequestFields
|
||||
|
||||
logger logger.Logger
|
||||
service string
|
||||
method string
|
||||
error twirp.Error
|
||||
fieldsOrig []interface{}
|
||||
fields []interface{}
|
||||
startedAt time.Time
|
||||
@@ -135,3 +143,74 @@ func errorReceived(ctx context.Context, e twirp.Error) context.Context {
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TwirpRequestStatusReporter() *twirp.ServerHooks {
|
||||
return &twirp.ServerHooks{
|
||||
RequestReceived: statusReporterRequestReceived,
|
||||
RequestRouted: statusReporterResponseRouted,
|
||||
Error: statusReporterErrorReceived,
|
||||
ResponseSent: statusReporterResponseSent,
|
||||
}
|
||||
}
|
||||
|
||||
func statusReporterRequestReceived(ctx context.Context) (context.Context, error) {
|
||||
r := &twirpRequestFields{}
|
||||
|
||||
if svc, ok := twirp.ServiceName(ctx); ok {
|
||||
r.service = svc
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, statusReporterKey, r)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func statusReporterResponseRouted(ctx context.Context) (context.Context, error) {
|
||||
if meth, ok := twirp.MethodName(ctx); ok {
|
||||
l, ok := ctx.Value(statusReporterKey).(*twirpRequestFields)
|
||||
if !ok || l == nil {
|
||||
return ctx, nil
|
||||
}
|
||||
l.method = meth
|
||||
}
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func statusReporterResponseSent(ctx context.Context) {
|
||||
r, ok := ctx.Value(statusReporterKey).(*twirpRequestFields)
|
||||
if !ok || r == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var statusFamily string
|
||||
if statusCode, ok := twirp.StatusCode(ctx); ok {
|
||||
if status, err := strconv.Atoi(statusCode); err != nil {
|
||||
switch {
|
||||
case status >= 400 && status <= 499:
|
||||
statusFamily = "4xx"
|
||||
case status >= 500 && status <= 599:
|
||||
statusFamily = "5xx"
|
||||
default:
|
||||
statusFamily = strconv.Itoa(status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var code twirp.ErrorCode
|
||||
if r.error != nil {
|
||||
code = r.error.Code()
|
||||
}
|
||||
|
||||
prometheus.TwirpRequestStatusCounter.WithLabelValues(r.service, r.method, statusFamily, string(code)).Add(1)
|
||||
}
|
||||
|
||||
func statusReporterErrorReceived(ctx context.Context, e twirp.Error) context.Context {
|
||||
r, ok := ctx.Value(statusReporterKey).(*twirpRequestFields)
|
||||
if !ok || r == nil {
|
||||
return ctx
|
||||
}
|
||||
|
||||
r.error = e
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user