Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package prometheus

import (
"bytes"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}