2023-05-30 09:47:04 +00:00
|
|
|
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"
|
2023-10-17 15:48:36 +00:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
2023-05-30 09:47:04 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2023-10-17 15:48:36 +00:00
|
|
|
var serverMetrics = grpcprom.NewServerMetrics(
|
2023-05-30 09:47:04 +00:00
|
|
|
grpcprom.WithServerHandlingTimeHistogram(
|
|
|
|
grpcprom.WithHistogramBuckets(prometheus.DefBuckets),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-10-17 15:48:36 +00:00
|
|
|
// 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...)
|
2023-05-30 09:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|