k3s/vendor/github.com/grpc-ecosystem/go-grpc-prometheus/client_reporter.go

47 lines
1.4 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
// Copyright 2016 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package grpc_prometheus
import (
"time"
"google.golang.org/grpc/codes"
)
type clientReporter struct {
2019-09-27 21:51:53 +00:00
metrics *ClientMetrics
2019-01-12 04:58:27 +00:00
rpcType grpcType
serviceName string
methodName string
startTime time.Time
}
2019-09-27 21:51:53 +00:00
func newClientReporter(m *ClientMetrics, rpcType grpcType, fullMethod string) *clientReporter {
r := &clientReporter{
metrics: m,
rpcType: rpcType,
}
if r.metrics.clientHandledHistogramEnabled {
2019-01-12 04:58:27 +00:00
r.startTime = time.Now()
}
r.serviceName, r.methodName = splitMethodName(fullMethod)
2019-09-27 21:51:53 +00:00
r.metrics.clientStartedCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
2019-01-12 04:58:27 +00:00
return r
}
func (r *clientReporter) ReceivedMessage() {
2019-09-27 21:51:53 +00:00
r.metrics.clientStreamMsgReceived.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
2019-01-12 04:58:27 +00:00
}
func (r *clientReporter) SentMessage() {
2019-09-27 21:51:53 +00:00
r.metrics.clientStreamMsgSent.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
2019-01-12 04:58:27 +00:00
}
func (r *clientReporter) Handled(code codes.Code) {
2019-09-27 21:51:53 +00:00
r.metrics.clientHandledCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName, code.String()).Inc()
if r.metrics.clientHandledHistogramEnabled {
r.metrics.clientHandledHistogram.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Observe(time.Since(r.startTime).Seconds())
2019-01-12 04:58:27 +00:00
}
}