feat(metric): adds accepted/closed connections counters for controller and worker cluster connections (#2656)

same contents as PR https://github.com/hashicorp/boundary/pull/2593 but this time actually push to main
This commit is contained in:
Haotian authored and GitHub committed 2022-11-28 17:35:16 -08:00
1 parent 7c2d0692f6
commit 80f72b8511
7 files changed
+349 -13

No files matched your search

@@ -2,6 +2,7 @@ package metric
import (
"context"
"net"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/daemon/metric"
@@ -24,12 +25,34 @@ var grpcServerRequestLatency prometheus.ObserverVec = prometheus.NewHistogramVec
Namespace: globals.MetricNamespace,
Subsystem: workerClusterSubsystem,
Name: "grpc_request_duration_seconds",
Help: "Histogram of latencies for gRPC requests between the a worker server and a worker client.",
Help: "Histogram of latencies for gRPC requests between a worker server and a worker client.",
Buckets: prometheus.DefBuckets,
},
metric.ListGrpcLabels,
)
// acceptedConnsTotal keeps a count of the total accepted connections to a worker.
var acceptedConnsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: globals.MetricNamespace,
Subsystem: workerClusterSubsystem,
Name: "accepted_connections_total",
Help: "Count of total accepted network connections to this worker.",
},
[]string{metric.LabelConnectionPurpose},
)
// closedConnsTotal keeps a count of the total closed connections to a worker.
var closedConnsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: globals.MetricNamespace,
Subsystem: workerClusterSubsystem,
Name: "closed_connections_total",
Help: "Count of total closed network connections to this worker.",
},
[]string{metric.LabelConnectionPurpose},
)
// All the codes expected to be returned by boundary or the grpc framework to
// requests to the cluster server.
var expectedGrpcCodes = []codes.Code{
@@ -42,6 +65,11 @@ var expectedGrpcCodes = []codes.Code{
codes.Unavailable, codes.Unauthenticated,
}
func InstrumentWorkerClusterTrackingListener(l net.Listener, purpose string) net.Listener {
p := prometheus.Labels{metric.LabelConnectionPurpose: purpose}
return metric.NewConnectionTrackingListener(l, acceptedConnsTotal.With(p), closedConnsTotal.With(p))
}
// InstrumentClusterStatsHandler returns a gRPC stats.Handler which observes
// cluster-specific metrics for a gRPC server.
func InstrumentClusterStatsHandler(ctx context.Context) (stats.Handler, error) {
@@ -54,3 +82,7 @@ func InstrumentClusterStatsHandler(ctx context.Context) (stats.Handler, error) {
func InitializeClusterServerCollectors(r prometheus.Registerer, server *grpc.Server) {
metric.InitializeGrpcCollectorsFromServer(r, grpcServerRequestLatency, server, expectedGrpcCodes)
}
func InitializeConnectionCounters(r prometheus.Registerer) {
metric.InitializeConnectionCounters(r, []prometheus.CounterVec{*acceptedConnsTotal, *closedConnsTotal})
}
+4 -3
View File
@@ -196,6 +196,7 @@ func (w *Worker) configureForWorker(ln *base.ServerListener, logger *log.Logger,
}
}
metric.InitializeConnectionCounters(w.conf.PrometheusRegisterer)
metric.InitializeClusterServerCollectors(w.conf.PrometheusRegisterer, downstreamServer)
ln.GrpcServer = downstreamServer
@@ -214,9 +215,9 @@ func (w *Worker) configureForWorker(ln *base.ServerListener, logger *log.Logger,
return func() {
go w.workerAuthSplitListener.Start()
go httpServer.Serve(proxyListener)
go ln.GrpcServer.Serve(pkiWorkerTrackingListener)
go handleSecondaryConnection(cancelCtx, revPkiWorkerTrackingListener, w.downstreamRoutes, -1)
go httpServer.Serve(metric.InstrumentWorkerClusterTrackingListener(proxyListener, "proxy"))
go ln.GrpcServer.Serve(metric.InstrumentWorkerClusterTrackingListener(pkiWorkerTrackingListener, "grpc"))
go handleSecondaryConnection(cancelCtx, metric.InstrumentWorkerClusterTrackingListener(revPkiWorkerTrackingListener, "reverse-grpc"), w.downstreamRoutes, -1)
}, nil
}