Skip to content

Commit

Permalink
Move all client metrics registration to metrics.go file
Browse files Browse the repository at this point in the history
  • Loading branch information
helder-junior committed Aug 12, 2024
1 parent 5592d06 commit ed1091d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 48 deletions.
2 changes: 1 addition & 1 deletion client/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (a *gRPCClientAsync) sendEvents(req *pb.SendEventsRequest, retryCount int)
if retryCount > a.maxRetries {
l.Info("dropped events due to max retries")
for _, e := range req.Events {
metrics.ClientRequestsDroppedCounter.WithLabelValues(
metrics.AsyncClientRequestsDroppedCounter.WithLabelValues(
e.Topic,
).Inc()
}
Expand Down
35 changes: 5 additions & 30 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/topfreegames/eventsgateway/v4/metrics"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -115,41 +114,17 @@ func (c *Client) newGRPCClient(
"async": async,
})

c.registerMetrics(configPrefix)
err := metrics.RegisterMetrics(configPrefix, c.config)
if err != nil {
return nil, err
}

if async {
return newGRPCClientAsync(configPrefix, c.config, c.logger, c.serverAddress, client, opts...)
}
return newGRPCClientSync(configPrefix, c.config, c.logger, c.serverAddress, client, opts...)
}

func (c *Client) registerMetrics(configPrefix string) {
latencyBucketsConf := fmt.Sprintf("%sclient.prometheus.buckets.latency", configPrefix)
c.config.SetDefault(latencyBucketsConf, []float64{3, 5, 10, 50, 100, 300, 500, 1000, 5000})
latencyBuckets := c.config.Get(latencyBucketsConf).([]float64)

metrics.ClientRequestsResponseTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "eventsgateway",
Subsystem: "client",
Name: "response_time_ms",
Help: "the response time in ms of calls to server",
Buckets: latencyBuckets,
},
[]string{"route", "topic", "retry"},
)

collectors := []prometheus.Collector{
metrics.ClientRequestsResponseTime,
metrics.ClientRequestsSuccessCounter,
metrics.ClientRequestsFailureCounter,
metrics.ClientRequestsDroppedCounter,
}
err := metrics.RegisterMetrics(collectors)
if err != nil {
c.logger.WithError(err).Error("failed to register metric")
}
}

// Send sends an event to another server via grpc using the client's configured topic
func (c *Client) Send(
ctx context.Context,
Expand Down
2 changes: 1 addition & 1 deletion client/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ func (s *gRPCClientSync) metricsReporterInterceptor(
event.Topic,
"0",
).Observe(elapsedTime)

l.WithFields(map[string]interface{}{
"elapsedTime": elapsedTime,
"reply": reply.(*pb.SendEventResponse),
}).Debug("request processed")
}(time.Now())

if err := invoker(ctx, method, req, reply, cc, opts...); err != nil {
l.WithError(err).Error("error processing request")
metrics.ClientRequestsFailureCounter.WithLabelValues(
Expand Down
4 changes: 2 additions & 2 deletions config/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ client:
serverAddress: eventsgateway-api:5000 #eventsgateway-api:5000
timeout: 500ms
loadtestclient:
duration: 20s
threads: 20
duration: 10s
threads: 10
randSleepCeilingMs: 500
randPropsSize: small # small, medium, large, jumbo
prometheus:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ services:
command:
- sh
- -c
- 'go run main.go load-test'
- 'go run main.go load-test -d'
networks:
- eventsgateway

Expand Down
47 changes: 34 additions & 13 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,30 @@ package metrics
import (
"errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/viper"
)

const metricsNamespace = "eventsgateway"
const metricsSubsystem = "client"

var (

// ClientRequestsResponseTime summary, observes the API response time as perceived by the client
ClientRequestsResponseTime *prometheus.HistogramVec
ClientRequestsResponseTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: metricsNamespace,
Subsystem: metricsSubsystem,
Name: "response_time_ms",
Help: "the response time in ms of calls to server",
Buckets: []float64{3, 5, 10, 50, 100, 300, 500, 1000, 5000},
},
[]string{"route", "topic", "retry"},
)

// ClientRequestsSuccessCounter is the count of successfull calls to the server
ClientRequestsSuccessCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "eventsgateway",
Subsystem: "client",
Namespace: metricsNamespace,
Subsystem: metricsSubsystem,
Name: "requests_success_counter",
Help: "the count of successfull client requests to the server",
},
Expand All @@ -43,21 +57,21 @@ var (

// ClientRequestsFailureCounter is the count of failed calls to the server
ClientRequestsFailureCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "eventsgateway",
Subsystem: "client",
Namespace: metricsNamespace,
Subsystem: metricsSubsystem,
Name: "requests_failure_counter",
Help: "the count of failed client requests to the server",
},
[]string{"route", "topic", "retry", "reason"},
)

// ClientRequestsDroppedCounter is the count of requests that were dropped due
// to req.Retry > maxRetries
ClientRequestsDroppedCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "eventsgateway",
Subsystem: "client",
Name: "requests_dropped_counter",
Help: "the count of dropped client requests to the server",
// AsyncClientRequestsDroppedCounter is the count of requests that were dropped due
// to req.Retry > maxRetries. Only available for Async mode
AsyncClientRequestsDroppedCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Subsystem: metricsSubsystem,
Name: "async_requests_dropped_counter",
Help: "the count of dropped client async requests to the server",
},
[]string{"topic"},
)
Expand All @@ -66,7 +80,14 @@ var (
// RegisterMetrics is a wrapper to handle prometheus.AlreadyRegisteredError;
// it only returns an error if the metric wasn't already registered and there was an
// actual error registering it.
func RegisterMetrics(collectors []prometheus.Collector) error {
func RegisterMetrics(configPrefix string, config *viper.Viper) error {
collectors := []prometheus.Collector{
ClientRequestsResponseTime,
ClientRequestsSuccessCounter,
ClientRequestsFailureCounter,
AsyncClientRequestsDroppedCounter,
}

for _, collector := range collectors {
err := prometheus.Register(collector)
if err != nil {
Expand Down

0 comments on commit ed1091d

Please sign in to comment.