Prometheus client test refactor

closes #318
This commit is contained in:
Cameron Sparr 2015-10-28 16:19:13 -06:00
parent 4449f7f2fb
commit d2fb065d0d
4 changed files with 21 additions and 21 deletions

View File

@ -8,6 +8,7 @@ changed to just run docker commands in the Makefile. See `make docker-run` and
### Features ### Features
- [#325](https://github.com/influxdb/telegraf/pull/325): NSQ output. Thanks @jrxFive! - [#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 ### Bugfixes

View File

@ -221,6 +221,7 @@ found by running `telegraf -sample-config`.
* amqp (rabbitmq) * amqp (rabbitmq)
* mqtt * mqtt
* librato * librato
* prometheus
## Contributing ## Contributing

View File

@ -2,15 +2,15 @@ package prometheus_client
import ( import (
"fmt" "fmt"
"net/http"
"github.com/influxdb/influxdb/client/v2" "github.com/influxdb/influxdb/client/v2"
"github.com/influxdb/telegraf/outputs" "github.com/influxdb/telegraf/outputs"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"net/http"
) )
type PrometheusClient struct { type PrometheusClient struct {
Listen string Listen string
server *http.Server
metrics map[string]*prometheus.UntypedVec metrics map[string]*prometheus.UntypedVec
} }
@ -21,16 +21,16 @@ var sampleConfig = `
func (p *PrometheusClient) Start() error { func (p *PrometheusClient) Start() error {
if p.Listen == "" { if p.Listen == "" {
p.Listen = ":9126" p.Listen = "localhost:9126"
} }
http.Handle("/metrics", prometheus.Handler()) http.Handle("/metrics", prometheus.Handler())
server := &http.Server{ server := &http.Server{
Addr: p.Listen, Addr: p.Listen,
Handler: prometheus.Handler(),
} }
p.server = server
p.metrics = make(map[string]*prometheus.UntypedVec) p.metrics = make(map[string]*prometheus.UntypedVec)
go p.server.ListenAndServe() go server.ListenAndServe()
return nil return nil
} }

View File

@ -1,26 +1,21 @@
package prometheus_client package prometheus_client
import ( import (
"testing"
"github.com/influxdb/influxdb/client/v2" "github.com/influxdb/influxdb/client/v2"
"github.com/influxdb/telegraf/plugins/prometheus" "github.com/influxdb/telegraf/plugins/prometheus"
"github.com/influxdb/telegraf/testutil" "github.com/influxdb/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"testing"
) )
var pTesting *PrometheusClient = &PrometheusClient{} var pTesting *PrometheusClient
func TestPrometheusStart(t *testing.T) {
require.NoError(t, pTesting.Start())
}
func TestPrometheusWritePointEmptyTag(t *testing.T) { func TestPrometheusWritePointEmptyTag(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
p := &prometheus.Prometheus{ p := &prometheus.Prometheus{
Urls: []string{"http://" + testutil.GetLocalHost() + ":9126/metrics"}, Urls: []string{"http://localhost:9126/metrics"},
} }
tags := make(map[string]string) tags := make(map[string]string)
var points = []*client.Point{ var points = []*client.Point{
@ -53,11 +48,9 @@ func TestPrometheusWritePointEmptyTag(t *testing.T) {
} }
func TestPrometheusWritePointTag(t *testing.T) { func TestPrometheusWritePointTag(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
p := &prometheus.Prometheus{ p := &prometheus.Prometheus{
Urls: []string{"http://" + testutil.GetLocalHost() + ":9126/metrics"}, Urls: []string{"http://localhost:9126/metrics"},
} }
tags := make(map[string]string) tags := make(map[string]string)
tags["testtag"] = "testvalue" tags["testtag"] = "testvalue"
@ -88,3 +81,8 @@ func TestPrometheusWritePointTag(t *testing.T) {
assert.True(t, acc.CheckTaggedValue(e.name, e.value, tags)) assert.True(t, acc.CheckTaggedValue(e.name, e.value, tags))
} }
} }
func init() {
pTesting = &PrometheusClient{Listen: "localhost:9126"}
pTesting.Start()
}