Skip to content

Commit d2fb065

Browse files
committed
Prometheus client test refactor
closes #318
1 parent 4449f7f commit d2fb065

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ changed to just run docker commands in the Makefile. See `make docker-run` and
88

99
### Features
1010
- [#325](https://github.com/influxdb/telegraf/pull/325): NSQ output. Thanks @jrxFive!
11+
- [#318](https://github.com/influxdb/telegraf/pull/318): Prometheus output. Thanks @oldmantaiter!
1112

1213
### Bugfixes
1314

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ found by running `telegraf -sample-config`.
221221
* amqp (rabbitmq)
222222
* mqtt
223223
* librato
224+
* prometheus
224225

225226
## Contributing
226227

outputs/prometheus_client/prometheus_client.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package prometheus_client
22

33
import (
44
"fmt"
5+
"net/http"
6+
57
"github.com/influxdb/influxdb/client/v2"
68
"github.com/influxdb/telegraf/outputs"
79
"github.com/prometheus/client_golang/prometheus"
8-
"net/http"
910
)
1011

1112
type PrometheusClient struct {
1213
Listen string
13-
server *http.Server
1414
metrics map[string]*prometheus.UntypedVec
1515
}
1616

@@ -21,16 +21,16 @@ var sampleConfig = `
2121

2222
func (p *PrometheusClient) Start() error {
2323
if p.Listen == "" {
24-
p.Listen = ":9126"
24+
p.Listen = "localhost:9126"
2525
}
26+
2627
http.Handle("/metrics", prometheus.Handler())
2728
server := &http.Server{
28-
Addr: p.Listen,
29-
Handler: prometheus.Handler(),
29+
Addr: p.Listen,
3030
}
31-
p.server = server
31+
3232
p.metrics = make(map[string]*prometheus.UntypedVec)
33-
go p.server.ListenAndServe()
33+
go server.ListenAndServe()
3434
return nil
3535
}
3636

outputs/prometheus_client/prometheus_client_test.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
package prometheus_client
22

33
import (
4+
"testing"
5+
46
"github.com/influxdb/influxdb/client/v2"
57
"github.com/influxdb/telegraf/plugins/prometheus"
68
"github.com/influxdb/telegraf/testutil"
79
"github.com/stretchr/testify/assert"
810
"github.com/stretchr/testify/require"
9-
"testing"
1011
)
1112

12-
var pTesting *PrometheusClient = &PrometheusClient{}
13-
14-
func TestPrometheusStart(t *testing.T) {
15-
require.NoError(t, pTesting.Start())
16-
}
13+
var pTesting *PrometheusClient
1714

1815
func TestPrometheusWritePointEmptyTag(t *testing.T) {
19-
if testing.Short() {
20-
t.Skip("Skipping integration test in short mode")
21-
}
16+
2217
p := &prometheus.Prometheus{
23-
Urls: []string{"http://" + testutil.GetLocalHost() + ":9126/metrics"},
18+
Urls: []string{"http://localhost:9126/metrics"},
2419
}
2520
tags := make(map[string]string)
2621
var points = []*client.Point{
@@ -53,11 +48,9 @@ func TestPrometheusWritePointEmptyTag(t *testing.T) {
5348
}
5449

5550
func TestPrometheusWritePointTag(t *testing.T) {
56-
if testing.Short() {
57-
t.Skip("Skipping integration test in short mode")
58-
}
51+
5952
p := &prometheus.Prometheus{
60-
Urls: []string{"http://" + testutil.GetLocalHost() + ":9126/metrics"},
53+
Urls: []string{"http://localhost:9126/metrics"},
6154
}
6255
tags := make(map[string]string)
6356
tags["testtag"] = "testvalue"
@@ -88,3 +81,8 @@ func TestPrometheusWritePointTag(t *testing.T) {
8881
assert.True(t, acc.CheckTaggedValue(e.name, e.value, tags))
8982
}
9083
}
84+
85+
func init() {
86+
pTesting = &PrometheusClient{Listen: "localhost:9126"}
87+
pTesting.Start()
88+
}

0 commit comments

Comments
 (0)