From 61bf99a67135ae7e12ec79e7fdbf224b7143e6b9 Mon Sep 17 00:00:00 2001 From: Dimitri Capitaine Date: Wed, 12 Feb 2020 15:36:17 +0100 Subject: [PATCH] implement fetch metrics using prometheus common/expfmt instead of local http fetch --- go.mod | 1 + prometheus/prometheus.go | 24 +++++++++--------------- prometheus/prometheus_test.go | 11 +++++++++++ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 000d5b5..3a31639 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/opentracing/opentracing-go v1.1.0 github.com/pkg/errors v0.8.1 // indirect github.com/prometheus/client_golang v1.1.0 + github.com/prometheus/common v0.6.0 github.com/stretchr/testify v1.4.0 github.com/uber-go/atomic v1.4.0 // indirect github.com/uber/jaeger-client-go v2.19.1-0.20191002155754-0be28c34dabf+incompatible diff --git a/prometheus/prometheus.go b/prometheus/prometheus.go index cb6eb42..4469144 100644 --- a/prometheus/prometheus.go +++ b/prometheus/prometheus.go @@ -22,7 +22,6 @@ package prometheus import ( "bytes" - "io/ioutil" "net/http" "os" "strconv" @@ -33,6 +32,7 @@ import ( "github.com/labstack/gommon/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/prometheus/common/expfmt" ) var defaultMetricPath = "/metrics" @@ -133,10 +133,6 @@ type PushGateway struct { // where JOBNAME can be any string of your choice PushGatewayURL string - // Local metrics URL where metrics are fetched from, this could be ommited in the future - // if implemented using prometheus common/expfmt instead - MetricsURL string - // pushgateway job name, defaults to "echo" Job string } @@ -174,10 +170,9 @@ func NewPrometheus(subsystem string, skipper middleware.Skipper, customMetricsLi } // SetPushGateway sends metrics to a remote pushgateway exposed on pushGatewayURL -// every pushIntervalSeconds. Metrics are fetched from metricsURL -func (p *Prometheus) SetPushGateway(pushGatewayURL, metricsURL string, pushIntervalSeconds time.Duration) { +// every pushIntervalSeconds. Metrics are fetched from +func (p *Prometheus) SetPushGateway(pushGatewayURL string, pushIntervalSeconds time.Duration) { p.Ppg.PushGatewayURL = pushGatewayURL - p.Ppg.MetricsURL = metricsURL p.Ppg.PushIntervalSeconds = pushIntervalSeconds p.startPushTicker() } @@ -222,14 +217,13 @@ func (p *Prometheus) runServer() { } func (p *Prometheus) getMetrics() []byte { - response, err := http.Get(p.Ppg.MetricsURL) - if err != nil { - log.Errorf("Error getting metrics: %v", err) - } - defer response.Body.Close() - body, _ := ioutil.ReadAll(response.Body) + out := &bytes.Buffer{} + metricFamilies, _ := prometheus.DefaultGatherer.Gather() + for i := range metricFamilies { + expfmt.MetricFamilyToText(out, metricFamilies[i]) - return body + } + return out.Bytes() } func (p *Prometheus) getPushGatewayURL() string { diff --git a/prometheus/prometheus_test.go b/prometheus/prometheus_test.go index 62232f0..1cf7696 100644 --- a/prometheus/prometheus_test.go +++ b/prometheus/prometheus_test.go @@ -121,3 +121,14 @@ func TestMetricsPathIgnored(t *testing.T) { }) unregister(p) } + +func TestMetricsPushGateway(t *testing.T) { + e := echo.New() + p := NewPrometheus("echo", nil) + p.Use(e) + + result := p.getMetrics() + + assert.Contains(t, string(result), fmt.Sprintf("%s_request_duration", p.Subsystem)) + unregister(p) +}