diff --git a/CHANGELOG.md b/CHANGELOG.md index da2f5931c..7f84cedd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ changed to just run docker commands in the Makefile. See `make docker-run` and ### Features - [#325](https://github.com/influxdb/telegraf/pull/325): NSQ output. Thanks @jrxFive! +- [#318](https://github.com/influxdb/telegraf/pull/318): Prometheus output. Thanks @oldmantaiter! ### Bugfixes diff --git a/README.md b/README.md index 2793b4afb..aacf7a9ae 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,7 @@ found by running `telegraf -sample-config`. * amqp (rabbitmq) * mqtt * librato +* prometheus ## Contributing diff --git a/outputs/prometheus_client/prometheus_client.go b/outputs/prometheus_client/prometheus_client.go index 7bc301e05..64fadc4f6 100644 --- a/outputs/prometheus_client/prometheus_client.go +++ b/outputs/prometheus_client/prometheus_client.go @@ -2,15 +2,15 @@ package prometheus_client import ( "fmt" + "net/http" + "github.com/influxdb/influxdb/client/v2" "github.com/influxdb/telegraf/outputs" "github.com/prometheus/client_golang/prometheus" - "net/http" ) type PrometheusClient struct { Listen string - server *http.Server metrics map[string]*prometheus.UntypedVec } @@ -21,16 +21,16 @@ var sampleConfig = ` func (p *PrometheusClient) Start() error { if p.Listen == "" { - p.Listen = ":9126" + p.Listen = "localhost:9126" } + http.Handle("/metrics", prometheus.Handler()) server := &http.Server{ - Addr: p.Listen, - Handler: prometheus.Handler(), + Addr: p.Listen, } - p.server = server + p.metrics = make(map[string]*prometheus.UntypedVec) - go p.server.ListenAndServe() + go server.ListenAndServe() return nil } diff --git a/outputs/prometheus_client/prometheus_client_test.go b/outputs/prometheus_client/prometheus_client_test.go index f3de9e0f5..5d7fae7ec 100644 --- a/outputs/prometheus_client/prometheus_client_test.go +++ b/outputs/prometheus_client/prometheus_client_test.go @@ -1,26 +1,21 @@ package prometheus_client import ( + "testing" + "github.com/influxdb/influxdb/client/v2" "github.com/influxdb/telegraf/plugins/prometheus" "github.com/influxdb/telegraf/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "testing" ) -var pTesting *PrometheusClient = &PrometheusClient{} - -func TestPrometheusStart(t *testing.T) { - require.NoError(t, pTesting.Start()) -} +var pTesting *PrometheusClient func TestPrometheusWritePointEmptyTag(t *testing.T) { - if testing.Short() { - t.Skip("Skipping integration test in short mode") - } + p := &prometheus.Prometheus{ - Urls: []string{"http://" + testutil.GetLocalHost() + ":9126/metrics"}, + Urls: []string{"http://localhost:9126/metrics"}, } tags := make(map[string]string) var points = []*client.Point{ @@ -53,11 +48,9 @@ func TestPrometheusWritePointEmptyTag(t *testing.T) { } func TestPrometheusWritePointTag(t *testing.T) { - if testing.Short() { - t.Skip("Skipping integration test in short mode") - } + p := &prometheus.Prometheus{ - Urls: []string{"http://" + testutil.GetLocalHost() + ":9126/metrics"}, + Urls: []string{"http://localhost:9126/metrics"}, } tags := make(map[string]string) tags["testtag"] = "testvalue" @@ -88,3 +81,8 @@ func TestPrometheusWritePointTag(t *testing.T) { assert.True(t, acc.CheckTaggedValue(e.name, e.value, tags)) } } + +func init() { + pTesting = &PrometheusClient{Listen: "localhost:9126"} + pTesting.Start() +}