2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2022-10-11 22:00:32 +00:00
|
|
|
package middleware
|
2020-07-22 23:57:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
2020-11-13 02:12:12 +00:00
|
|
|
"github.com/armon/go-metrics/prometheus"
|
2020-07-22 23:57:29 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/stats"
|
|
|
|
)
|
|
|
|
|
2020-11-13 02:12:12 +00:00
|
|
|
var StatsGauges = []prometheus.GaugeDefinition{
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"grpc", "server", "connections"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Measures the number of active gRPC connections open on the server.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"grpc", "client", "connections"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Measures the number of active gRPC connections open from the client agent to any Consul servers.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"grpc", "server", "streams"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Measures the number of active gRPC streams handled by the server.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var StatsCounters = []prometheus.CounterDefinition{
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"grpc", "client", "request", "count"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Counts the number of gRPC requests made by the client agent to a Consul server.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"grpc", "server", "request", "count"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Counts the number of gRPC requests received by the server.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"grpc", "client", "connection", "count"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Counts the number of new gRPC connections opened by the client agent to a Consul server.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"grpc", "server", "connection", "count"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Counts the number of new gRPC connections received by the server.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"grpc", "server", "stream", "count"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Counts the number of new gRPC streams received by the server.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
}
|
2020-09-14 17:16:13 +00:00
|
|
|
|
2020-07-22 23:57:29 +00:00
|
|
|
// statsHandler is a grpc/stats.StatsHandler which emits connection and
|
|
|
|
// request metrics to go-metrics.
|
|
|
|
type statsHandler struct {
|
2021-06-28 22:19:44 +00:00
|
|
|
// activeConns is used with sync/atomic and MUST be 64-bit aligned. To ensure
|
|
|
|
// alignment on 32-bit platforms this field must remain the first field in
|
|
|
|
// the struct. See https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
|
|
|
|
activeConns uint64
|
2020-09-14 17:16:13 +00:00
|
|
|
metrics *metrics.Metrics
|
2022-10-11 22:00:32 +00:00
|
|
|
labels []metrics.Label
|
2020-07-22 23:57:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 22:00:32 +00:00
|
|
|
func NewStatsHandler(m *metrics.Metrics, labels []metrics.Label) *statsHandler {
|
|
|
|
return &statsHandler{metrics: m, labels: labels}
|
2020-09-14 17:16:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 23:57:29 +00:00
|
|
|
// TagRPC implements grpcStats.StatsHandler
|
|
|
|
func (c *statsHandler) TagRPC(ctx context.Context, _ *stats.RPCTagInfo) context.Context {
|
|
|
|
// No-op
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleRPC implements grpcStats.StatsHandler
|
|
|
|
func (c *statsHandler) HandleRPC(_ context.Context, s stats.RPCStats) {
|
|
|
|
label := "server"
|
|
|
|
if s.IsClient() {
|
|
|
|
label = "client"
|
|
|
|
}
|
|
|
|
switch s.(type) {
|
|
|
|
case *stats.InHeader:
|
2022-10-11 22:00:32 +00:00
|
|
|
c.metrics.IncrCounterWithLabels([]string{"grpc", label, "request", "count"}, 1, c.labels)
|
2020-07-22 23:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TagConn implements grpcStats.StatsHandler
|
|
|
|
func (c *statsHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
|
|
|
|
// No-op
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleConn implements grpcStats.StatsHandler
|
|
|
|
func (c *statsHandler) HandleConn(_ context.Context, s stats.ConnStats) {
|
|
|
|
label := "server"
|
|
|
|
if s.IsClient() {
|
|
|
|
label = "client"
|
|
|
|
}
|
|
|
|
var count uint64
|
|
|
|
switch s.(type) {
|
|
|
|
case *stats.ConnBegin:
|
|
|
|
count = atomic.AddUint64(&c.activeConns, 1)
|
2022-10-11 22:00:32 +00:00
|
|
|
c.metrics.IncrCounterWithLabels([]string{"grpc", label, "connection", "count"}, 1, c.labels)
|
2020-07-22 23:57:29 +00:00
|
|
|
case *stats.ConnEnd:
|
|
|
|
// Decrement!
|
|
|
|
count = atomic.AddUint64(&c.activeConns, ^uint64(0))
|
|
|
|
}
|
2022-10-11 22:00:32 +00:00
|
|
|
c.metrics.SetGaugeWithLabels([]string{"grpc", label, "connections"}, float32(count), c.labels)
|
2020-07-22 23:57:29 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 16:54:07 +00:00
|
|
|
// Intercept matches the Unary interceptor function signature. This unary interceptor will count RPC requests
|
|
|
|
// but does not handle any connection processing or perform RPC "tagging"
|
|
|
|
func (c *statsHandler) Intercept(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
|
|
c.metrics.IncrCounterWithLabels([]string{"grpc", "server", "request", "count"}, 1, c.labels)
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:57:29 +00:00
|
|
|
type activeStreamCounter struct {
|
2021-06-28 22:19:44 +00:00
|
|
|
// count is used with sync/atomic and MUST be 64-bit aligned. To ensure
|
|
|
|
// alignment on 32-bit platforms this field must remain the first field in
|
|
|
|
// the struct. See https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
|
|
|
|
count uint64
|
2020-10-14 19:43:48 +00:00
|
|
|
metrics *metrics.Metrics
|
2022-10-11 22:00:32 +00:00
|
|
|
labels []metrics.Label
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewActiveStreamCounter(m *metrics.Metrics, labels []metrics.Label) *activeStreamCounter {
|
|
|
|
return &activeStreamCounter{metrics: m, labels: labels}
|
2020-07-22 23:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GRPCCountingStreamInterceptor is a grpc.ServerStreamInterceptor that emits a
|
|
|
|
// a metric of the count of open streams.
|
|
|
|
func (i *activeStreamCounter) Intercept(
|
|
|
|
srv interface{},
|
|
|
|
ss grpc.ServerStream,
|
|
|
|
_ *grpc.StreamServerInfo,
|
|
|
|
handler grpc.StreamHandler,
|
|
|
|
) error {
|
|
|
|
count := atomic.AddUint64(&i.count, 1)
|
2022-10-11 22:00:32 +00:00
|
|
|
i.metrics.SetGaugeWithLabels([]string{"grpc", "server", "streams"}, float32(count), i.labels)
|
|
|
|
i.metrics.IncrCounterWithLabels([]string{"grpc", "server", "stream", "count"}, 1, i.labels)
|
2020-07-22 23:57:29 +00:00
|
|
|
defer func() {
|
|
|
|
count := atomic.AddUint64(&i.count, ^uint64(0))
|
2022-10-11 22:00:32 +00:00
|
|
|
i.metrics.SetGaugeWithLabels([]string{"grpc", "server", "streams"}, float32(count), i.labels)
|
2020-07-22 23:57:29 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return handler(srv, ss)
|
|
|
|
}
|