Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #620 from mihaitodor/prometheus-push-gateway-no-pu…
…sh-interval Allow Prometheus PushGateway to work if PushInterval not set
- Loading branch information
Showing
2 changed files
with
101 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPrometheusNoPushGateway(t *testing.T) { | ||
config := NewConfig() | ||
|
||
p, err := NewPrometheus(config) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, p) | ||
assert.Nil(t, p.(*Prometheus).pusher) | ||
} | ||
|
||
func TestPrometheusWithPushGateway(t *testing.T) { | ||
pusherChan := make(chan struct{}) | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
pusherChan <- struct{}{} | ||
})) | ||
defer server.Close() | ||
|
||
config := NewConfig() | ||
config.Prometheus.PushURL = server.URL | ||
|
||
p, err := NewPrometheus(config) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, p) | ||
assert.NotNil(t, p.(*Prometheus).pusher) | ||
|
||
go func() { | ||
err = p.Close() | ||
assert.NoError(t, err) | ||
}() | ||
|
||
// Wait for message for the PushGateway after close | ||
select { | ||
case <-pusherChan: | ||
case <-time.After(100 * time.Millisecond): | ||
assert.Fail(t, "PushGateway did not receive expected messages") | ||
} | ||
} | ||
|
||
func TestPrometheusWithPushGatewayAndPushInterval(t *testing.T) { | ||
pusherChan := make(chan struct{}) | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
pusherChan <- struct{}{} | ||
})) | ||
defer server.Close() | ||
|
||
pushInterval := 1 * time.Millisecond | ||
config := NewConfig() | ||
config.Prometheus.PushURL = server.URL | ||
config.Prometheus.PushInterval = pushInterval.String() | ||
|
||
p, err := NewPrometheus(config) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, p) | ||
assert.NotNil(t, p.(*Prometheus).pusher) | ||
|
||
// Wait for first message for the PushGateway | ||
select { | ||
case <-pusherChan: | ||
case <-time.After(100 * time.Millisecond): | ||
assert.Fail(t, "PushGateway did not receive expected messages") | ||
} | ||
|
||
go func() { | ||
err = p.Close() | ||
assert.NoError(t, err) | ||
}() | ||
|
||
// Wait for another message for the PushGateway (might not be the one sent on close) | ||
select { | ||
case <-pusherChan: | ||
case <-time.After(100 * time.Millisecond): | ||
assert.Fail(t, "PushGateway did not receive expected messages after close") | ||
} | ||
} |