keel/integration/gotsrpc/net/middleware/telemetry.go
2023-08-11 14:59:05 +02:00

132 lines
3.8 KiB
Go

package keelgotsrpcmiddleware
import (
"net/http"
"strconv"
"time"
"github.com/foomo/gotsrpc/v2"
httplog "github.com/foomo/keel/net/http/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"github.com/foomo/keel/net/http/middleware"
)
// Prometheus Metrics
const (
defaultGOTSRPCFunctionLabel = "gotsrpc_func"
defaultGOTSRPCServiceLabel = "gotsrpc_service"
defaultGOTSRPCPackageLabel = "gotsrpc_package"
defaultGOTSRPCPackageOperation = "gotsrpc_operation"
defaultGOTSRPCError = "gotsrpc_error"
defaultGOTSRPCErrorCode = "gotsrpc_error_code"
defaultGOTSRPCErrorType = "gotsrpc_error_type"
defaultGOTSRPCErrorMessage = "gotsrpc_error_message"
)
type (
TelemetryOptions struct {
Exemplars bool
}
TelemetryOption func(*TelemetryOptions)
)
var (
gotsrpcRequestDurationSummary = promauto.NewSummaryVec(prometheus.SummaryOpts{
Namespace: "gotsrpc",
Name: "request_duration_seconds",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
Help: "Specifies the duration of gotsrpc request in seconds",
}, []string{defaultGOTSRPCFunctionLabel, defaultGOTSRPCServiceLabel, defaultGOTSRPCPackageLabel, defaultGOTSRPCPackageOperation, defaultGOTSRPCError})
)
// DefaultTelemetryOptions returns the default options
func DefaultTelemetryOptions() TelemetryOptions {
return TelemetryOptions{
Exemplars: false,
}
}
// TelemetryWithExemplars middleware option
func TelemetryWithExemplars(v bool) TelemetryOption {
return func(o *TelemetryOptions) {
o.Exemplars = v
}
}
// Telemetry middleware
func Telemetry(opts ...TelemetryOption) middleware.Middleware {
options := DefaultTelemetryOptions()
for _, opt := range opts {
if opt != nil {
opt(&options)
}
}
return TelemetryWithOptions(options)
}
// TelemetryWithOptions middleware
func TelemetryWithOptions(opts TelemetryOptions) middleware.Middleware {
return func(l *zap.Logger, name string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
*r = *gotsrpc.RequestWithStatsContext(r)
next.ServeHTTP(w, r)
if stats, ok := gotsrpc.GetStatsForRequest(r); ok {
// create custom metics
observe := func(operation string, duration time.Duration) {
observer := gotsrpcRequestDurationSummary.WithLabelValues(
stats.Func,
stats.Service,
stats.Package,
operation,
strconv.FormatBool(stats.ErrorCode != 0),
)
spanCtx := trace.SpanContextFromContext(r.Context())
if v, ok := observer.(prometheus.ExemplarObserver); ok && opts.Exemplars && spanCtx.HasTraceID() {
v.ObserveWithExemplar(duration.Seconds(), prometheus.Labels{
"TraceID": spanCtx.TraceID().String(),
})
} else {
if !ok {
l.Info("=> not ok")
}
if !opts.Exemplars {
l.Info("=> no exemplars")
}
if !spanCtx.HasTraceID() {
l.Info("=> no trace id")
}
observer.Observe(duration.Seconds())
}
}
observe("marshalling", stats.Marshalling)
observe("unmarshalling", stats.Unmarshalling)
observe("execution", stats.Execution)
// enrich logger
if labeler, ok := httplog.LabelerFromRequest(r); ok {
labeler.Add(
zap.String(defaultGOTSRPCFunctionLabel, stats.Func),
zap.String(defaultGOTSRPCServiceLabel, stats.Service),
zap.String(defaultGOTSRPCPackageLabel, stats.Package),
)
if stats.ErrorType != "" {
labeler.Add(zap.String(defaultGOTSRPCErrorType, stats.ErrorType))
}
if stats.ErrorCode != 0 {
labeler.Add(zap.Int(defaultGOTSRPCErrorCode, stats.ErrorCode))
if stats.ErrorMessage != "" {
labeler.Add(zap.String(defaultGOTSRPCErrorMessage, stats.ErrorMessage))
}
}
}
}
})
}
}