Skip to content

Commit

Permalink
fix register
Browse files Browse the repository at this point in the history
  • Loading branch information
glebnaz committed Nov 13, 2023
1 parent 7e98d61 commit 8a198d4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
35 changes: 28 additions & 7 deletions metrics/grpc_mw.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

"github.com/prometheus/client_golang/prometheus"

"google.golang.org/grpc/status"

"google.golang.org/grpc"
Expand All @@ -25,14 +27,33 @@ func ServerMetricsUnaryInterceptor(namespace string) func(ctx context.Context,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {

var serverRequestCounter = MustRegisterCounterVec("server_grpc_request_count",
namespace,
"server_request_count", []string{"method", "code"})
const nameCounter = "server_grpc_request_count"
const nameHistogram = "server_grpc_response_time"

keyCounter := nameCounter + namespace
keyHistogram := nameHistogram + namespace

var serverRequestCounter *prometheus.CounterVec
var serverResponseTime *prometheus.HistogramVec

var serverResponseTime = MustRegisterHistogramVec("server_grpc_response_time",
namespace,
"server response time in seconds",
TimeBucketsMedium, []string{"method"})
if _, ok := metricsCollector[keyCounter]; !ok {
serverRequestCounter = MustRegisterCounterVec("server_grpc_request_count",
namespace,
"server_request_count", []string{"method", "code"})
metricsCollector[keyCounter] = serverRequestCounter
} else {
serverRequestCounter = metricsCollector[keyCounter].(*prometheus.CounterVec)
}

if _, ok := metricsCollector[keyHistogram]; !ok {
serverResponseTime = MustRegisterHistogramVec("server_grpc_response_time",
namespace,
"server response time in seconds",
TimeBucketsMedium, []string{"method"})
metricsCollector[keyHistogram] = serverResponseTime
} else {
serverResponseTime = metricsCollector[keyHistogram].(*prometheus.HistogramVec)
}

return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
startTime := time.Now()
Expand Down
40 changes: 32 additions & 8 deletions metrics/grpc_mw_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"context"
"time"

"github.com/prometheus/client_golang/prometheus"

"google.golang.org/grpc"
"google.golang.org/grpc/status"
)

var metricsCollector = make(map[string]any)

// ClientMetricsUnaryInterceptor returns a new unary client interceptor for
// metrics from what to
// namespace here is a prefix for metrics name
Expand All @@ -17,14 +21,34 @@ import (
// - client_grpc_response_time{method,from,to}
// namespace here is a prefix for metrics name
func ClientMetricsUnaryInterceptor(from, to string) grpc.UnaryClientInterceptor {
var clientRequestCounter = MustRegisterCounterVec("client_grpc_request_count",
from,
"client_request_count", []string{"method", "code", "from", "to"})

var clientResponseTime = MustRegisterHistogramVec("client_grpc_response_time",
from,
"client response time in seconds",
TimeBucketsMedium, []string{"method", "from", "to"})
const nameCounter = "client_grpc_request_count"
const nameHistogram = "client_grpc_response_time"

keyCounter := nameCounter + from
keyHistogram := nameHistogram + from

var clientRequestCounter *prometheus.CounterVec
var clientResponseTime *prometheus.HistogramVec

if _, ok := metricsCollector[keyCounter]; !ok {
clientRequestCounter = MustRegisterCounterVec("client_grpc_request_count",
from,
"client_request_count", []string{"method", "code", "from", "to"})

metricsCollector[keyCounter] = clientRequestCounter
} else {
clientRequestCounter = metricsCollector[keyCounter].(*prometheus.CounterVec)
}

if _, ok := metricsCollector[keyHistogram]; !ok {
clientResponseTime = MustRegisterHistogramVec("client_grpc_response_time",
from,
"client response time in seconds",
TimeBucketsMedium, []string{"method", "from", "to"})
metricsCollector[keyHistogram] = clientResponseTime
} else {
clientResponseTime = metricsCollector[keyHistogram].(*prometheus.HistogramVec)
}

return func(ctx context.Context,
method string,
Expand Down

0 comments on commit 8a198d4

Please sign in to comment.