Skip to content

Commit

Permalink
Remove Server metrics from client
Browse files Browse the repository at this point in the history
  • Loading branch information
helder-junior committed Jul 25, 2024
1 parent e345b16 commit 4302807
Showing 1 changed file with 0 additions and 121 deletions.
121 changes: 0 additions & 121 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,10 @@ package metrics

import (
"errors"
"github.com/spf13/viper"
"net/http"
"time"

log "github.com/sirupsen/logrus"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
// APIResponseTime summary, observes the API response time as perceived by the server
APIResponseTime *prometheus.HistogramVec

// APIPayloadSize summary, observes the payload size of requests arriving at the server
APIPayloadSize *prometheus.HistogramVec

// APIRequestsSuccessCounter counter
APIRequestsSuccessCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "eventsgateway",
Subsystem: "api",
Name: "requests_success_counter",
Help: "A counter of succeeded api requests",
},
[]string{"route", "topic", "retry"},
)

// APIRequestsFailureCounter counter
APIRequestsFailureCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "eventsgateway",
Subsystem: "api",
Name: "requests_failure_counter",
Help: "A counter of failed api requests",
},
[]string{"route", "topic", "retry", "reason"},
)

// ClientRequestsResponseTime summary, observes the API response time as perceived by the client
ClientRequestsResponseTime *prometheus.HistogramVec

Expand Down Expand Up @@ -97,33 +61,8 @@ var (
},
[]string{"topic"},
)

APITopicsSubmission = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "eventsgateway",
Subsystem: "api",
Name: "topics_submission_total",
Help: "Topic submissions sent to kafka",
},
[]string{"topic", "success"},
)
)

func defaultLatencyBuckets(config *viper.Viper) []float64 {
// in milliseconds
const configKey = "prometheus.buckets.latency"
config.SetDefault(configKey, []float64{3, 5, 10, 50, 100, 300, 500, 1000, 5000})

return config.Get(configKey).([]float64)
}

func defaultPayloadSizeBuckets(config *viper.Viper) []float64 {
// in bytes
configKey := "prometheus.buckets.payloadSize"
config.SetDefault(configKey, []float64{100, 1000, 5000, 10000, 50000, 100000, 500000, 1000000, 5000000})

return config.Get(configKey).([]float64)
}

// 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.
Expand All @@ -140,63 +79,3 @@ func RegisterMetrics(collectors []prometheus.Collector) error {

return nil
}

// StartServer runs a metrics server inside a goroutine
// that reports default application metrics in prometheus format.
// Any errors that may occur will stop the server and log.Fatal the error.
func StartServer(config *viper.Viper) {
APIPayloadSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "eventsgateway",
Subsystem: "api",
Name: "payload_size",
Help: "payload size of API routes, in bytes",
Buckets: defaultPayloadSizeBuckets(config),
},
[]string{"route", "topic"},
)

APIResponseTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "eventsgateway",
Subsystem: "api",
Name: "response_time_ms",
Help: "the response time in ms of api routes",
Buckets: defaultLatencyBuckets(config),
},
[]string{"route", "topic", "retry"},
)

collectors := []prometheus.Collector{
APIResponseTime,
APIPayloadSize,
APIRequestsFailureCounter,
APIRequestsSuccessCounter,
APITopicsSubmission,
}

err := RegisterMetrics(collectors)
if err != nil {
log.Fatal(err)
}

go func() {
envEnabled := config.GetString("prometheus.enabled")
if envEnabled != "true" {
log.Warn("Prometheus web server disabled")
return
}

r := mux.NewRouter()
r.Handle("/metrics", promhttp.Handler())

s := &http.Server{
Addr: config.GetString("prometheus.port"),
ReadTimeout: 8 * time.Second,
WriteTimeout: 8 * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: r,
}
log.Fatal(s.ListenAndServe())
}()
}

0 comments on commit 4302807

Please sign in to comment.