frostfs-observability/metrics/grpc/server.go
Anton Nikiforov 7960099809 [] Export gRPC client and server metrics
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
2023-10-19 10:51:18 +03:00

63 lines
2.5 KiB
Go

package grpc
import (
"git.frostfs.info/TrueCloudLab/frostfs-observability/metrics"
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"google.golang.org/grpc"
)
var serverMetrics = grpcprom.NewServerMetrics(
grpcprom.WithServerHandlingTimeHistogram(
grpcprom.WithHistogramBuckets(prometheus.DefBuckets),
),
)
func init() {
// Description copied from grpc-ecosystem:
// https://github.com/grpc-ecosystem/go-grpc-middleware/blob/71d7422112b1d7fadd4b8bf12a6f33ba6d22e98e/providers/prometheus/server_metrics.go#L26
descs := []metrics.Description{
{
Name: "grpc_server_started_total",
Type: dto.MetricType_COUNTER.String(),
Help: "Total number of RPCs started on the server.",
VariableLabels: []string{"grpc_type", "grpc_service", "grpc_method"},
},
{
Name: "grpc_server_handled_total",
Type: dto.MetricType_COUNTER.String(),
Help: "Total number of RPCs completed on the server, regardless of success or failure.",
VariableLabels: []string{"grpc_type", "grpc_service", "grpc_method", "grpc_code"},
},
{
Name: "grpc_server_msg_received_total",
Type: dto.MetricType_COUNTER.String(),
Help: "Total number of RPC stream messages received on the server.",
VariableLabels: []string{"grpc_type", "grpc_service", "grpc_method"},
},
{
Name: "grpc_server_msg_sent_total",
Type: dto.MetricType_COUNTER.String(),
Help: "Total number of gRPC stream messages sent by the server.",
VariableLabels: []string{"grpc_type", "grpc_service", "grpc_method"},
},
{
Name: "grpc_server_handling_seconds",
Type: dto.MetricType_HISTOGRAM.String(),
Help: "Histogram of response latency (seconds) of gRPC that had been application-level handled by the server.",
VariableLabels: []string{"grpc_type", "grpc_service", "grpc_method"},
},
}
metrics.MustRegister(serverMetrics, descs...)
}
// NewUnaryServerInterceptor returns server interceptor to collect metrics from unary RPCs.
func NewUnaryServerInterceptor() grpc.UnaryServerInterceptor {
return serverMetrics.UnaryServerInterceptor()
}
// NewStreamServerInterceptor returns server interceptor to collect metrics from stream RPCs.
func NewStreamServerInterceptor() grpc.StreamServerInterceptor {
return serverMetrics.StreamServerInterceptor()
}