diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 5dcba82..a66a4f4 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -16,12 +16,11 @@ import ( "github.com/ultravioletrs/agent/agent/api" agentgrpc "github.com/ultravioletrs/agent/agent/api/grpc" agenthttpapi "github.com/ultravioletrs/agent/agent/api/http" + "github.com/ultravioletrs/agent/internal" "github.com/ultravioletrs/agent/internal/env" "google.golang.org/grpc" - kitprometheus "github.com/go-kit/kit/metrics/prometheus" opentracing "github.com/opentracing/opentracing-go" - stdprometheus "github.com/prometheus/client_golang/prometheus" jconfig "github.com/uber/jaeger-client-go/config" ) @@ -95,21 +94,8 @@ func newService(secret string, logger logger.Logger) agent.Service { svc := agent.New(secret) svc = api.LoggingMiddleware(svc, logger) - svc = api.MetricsMiddleware( - svc, - kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ - Namespace: "agent", - Subsystem: "api", - Name: "request_count", - Help: "Number of requests received.", - }, []string{"method"}), - kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{ - Namespace: "agent", - Subsystem: "api", - Name: "request_latency_microseconds", - Help: "Total duration of requests in microseconds.", - }, []string{"method"}), - ) + counter, latency := internal.MakeMetrics(svcName, "api") + svc = api.MetricsMiddleware(svc, counter, latency) return svc } diff --git a/internal/metrics.go b/internal/metrics.go new file mode 100644 index 0000000..a183ccb --- /dev/null +++ b/internal/metrics.go @@ -0,0 +1,25 @@ +package internal + +import ( + kitprometheus "github.com/go-kit/kit/metrics/prometheus" + stdprometheus "github.com/prometheus/client_golang/prometheus" +) + +// MakeMetrics returns an instance of metrics. +func MakeMetrics(namespace, subsystem string) (*kitprometheus.Counter, *kitprometheus.Summary) { + counter := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ + Namespace: namespace, + Subsystem: subsystem, + Name: "request_count", + Help: "Number of requests received.", + }, []string{"method"}) + latency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{ + Namespace: namespace, + Subsystem: subsystem, + Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, + Name: "request_latency_microseconds", + Help: "Total duration of requests in microseconds.", + }, []string{"method"}) + + return counter, latency +}