diff --git a/prometheus/prometheus.go b/prometheus/prometheus.go index e033d99..9cf6ff2 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" @@ -136,10 +136,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 } @@ -177,10 +173,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() } @@ -225,14 +220,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) +}