Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flaky metrics test #174

Merged
merged 2 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
http.HandleFunc("/status", Status)

handler.InitHandler()
metrics.SetupPrometheus()
metrics.SetupPrometheus(9090)
log.Print("Listening on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
16 changes: 10 additions & 6 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metrics

import (
"fmt"
"net"
"net/http"
"net/http/pprof"
Expand Down Expand Up @@ -81,7 +82,7 @@ func init() {
}

// SetupPrometheus sets up and runs a webserver to export prometheus metrics.
func SetupPrometheus() *http.Server {
func SetupPrometheus(port int) *http.Server {
// Define a custom serve mux for prometheus to listen on a separate port.
// We listen on a separate port so we can forward this port on the host VM.
// We cannot forward port 8080 because it is used by AppEngine.
Expand All @@ -96,15 +97,18 @@ func SetupPrometheus() *http.Server {
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)

// Don't ListenAndServe because we want to be able to GET as soon as this function returns.
// Listen synchronously.
addr := fmt.Sprintf(":%d", port)
listener, err := net.Listen("tcp", addr)
rtx.Must(err, "Could not open listening socket for Prometheus metrics")
port = listener.Addr().(*net.TCPAddr).Port

server := &http.Server{
Addr: ":9090",
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
}

// Don't ListenAndServe because we want to be able to GET as soon as this function returns.
// Listen synchronously.
listener, err := net.Listen("tcp", server.Addr)
rtx.Must(err, "Could not open listening socket for Prometheus metrics")
// Serve asynchronously.
go server.Serve(listener.(*net.TCPListener))

Expand Down
10 changes: 6 additions & 4 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics_test
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"testing"

Expand All @@ -11,16 +12,17 @@ import (
)

func TestPrometheusMetrics(t *testing.T) {
server := metrics.SetupPrometheus()
server := metrics.SetupPrometheus(0)
defer server.Shutdown(nil)
log.Println(server.Addr)

metricReader, err := http.Get("http://localhost:9090/metrics")
metricReader, err := http.Get("http://localhost" + server.Addr + "/metrics")
if err != nil || metricReader == nil {
t.Errorf("Could not GET metrics: %v", err)
t.Fatalf("Could not GET metrics: %v", err)
}
metricBytes, err := ioutil.ReadAll(metricReader.Body)
if err != nil {
t.Errorf("Could not read metrics: %v", err)
t.Fatalf("Could not read metrics: %v", err)
}
metricsLinter := promlint.New(bytes.NewBuffer(metricBytes))
problems, err := metricsLinter.Lint()
Expand Down