Skip to content

Commit

Permalink
added metrics_test
Browse files Browse the repository at this point in the history
  • Loading branch information
neufeldtech committed Mar 1, 2022
1 parent 0d65baf commit 6bb118c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
76 changes: 76 additions & 0 deletions metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package toxiproxy

import (
"bufio"
"bytes"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

"github.com/Shopify/toxiproxy/v2/collectors"
"github.com/Shopify/toxiproxy/v2/stream"
"github.com/prometheus/client_golang/prometheus"
)

func TestProxyMetricsReceivedSentBytes(t *testing.T) {
expectedMetrics := []string{
`toxiproxy_proxy_received_bytes_total{direction="upstream",listener="localhost:0",proxy="test",upstream="upstream"} 5`,
`toxiproxy_proxy_sent_bytes_total{direction="upstream",listener="localhost:0",proxy="test",upstream="upstream"} 5`,
}
srv := NewServer(NewMetricsContainer(prometheus.NewRegistry()))
srv.Metrics.ProxyMetrics = collectors.NewProxyMetricCollectors()
proxy := NewProxy(srv)
proxy.Name = "test"
proxy.Listen = "localhost:0"
proxy.Upstream = "upstream"
r := bufio.NewReader(bytes.NewBufferString("hello"))
w := &testWriteCloser{
bufio.NewWriter(bytes.NewBuffer([]byte{})),
}
proxy.Toxics.StartLink(srv, "testupstream", r, w, stream.Upstream)
gotMetrics := prometheusOutput(t, srv, "toxiproxy")
if !reflect.DeepEqual(gotMetrics, expectedMetrics) {
t.Fatalf("expected: %v got: %v", expectedMetrics, gotMetrics)
}
}
func TestRuntimeMetricsBuildInfo(t *testing.T) {
expectedMetrics := []string{
`go_build_info{checksum="unknown",path="unknown",version="unknown"} 1`,
}
srv := NewServer(NewMetricsContainer(prometheus.NewRegistry()))
srv.Metrics.RuntimeMetrics = collectors.NewRuntimeMetricCollectors()

gotMetrics := prometheusOutput(t, srv, "go_build_info")
if !reflect.DeepEqual(gotMetrics, expectedMetrics) {
t.Fatalf("expected: %v got: %v", expectedMetrics, gotMetrics)
}
}

type testWriteCloser struct {
*bufio.Writer
}

func (t *testWriteCloser) Close() error {
return t.Flush()
}

func prometheusOutput(t *testing.T, apiServer *ApiServer, prefix string) []string {
t.Helper()

testServer := httptest.NewServer(apiServer.Metrics.handler())
defer testServer.Close()
resp, err := http.Get(testServer.URL)
if err != nil {
t.Fatal(err)
}
var selected []string
s := bufio.NewScanner(resp.Body)
for s.Scan() {
if strings.HasPrefix(s.Text(), prefix) {
selected = append(selected, s.Text())
}
}
return selected
}
1 change: 0 additions & 1 deletion toxics/toxic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func init() {
func NewTestProxy(name, upstream string) *toxiproxy.Proxy {
srv := toxiproxy.NewServer(toxiproxy.NewMetricsContainer(prometheus.NewRegistry()))
srv.Metrics.ProxyMetrics = collectors.NewProxyMetricCollectors()
srv.Metrics.RuntimeMetrics = collectors.NewRuntimeMetricCollectors()
proxy := toxiproxy.NewProxy(srv)

proxy.Name = name
Expand Down
1 change: 0 additions & 1 deletion toxiproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
func NewTestProxy(name, upstream string) *toxiproxy.Proxy {
srv := toxiproxy.NewServer(toxiproxy.NewMetricsContainer(prometheus.NewRegistry()))
srv.Metrics.ProxyMetrics = collectors.NewProxyMetricCollectors()
srv.Metrics.RuntimeMetrics = collectors.NewRuntimeMetricCollectors()
proxy := toxiproxy.NewProxy(srv)

proxy.Name = name
Expand Down

0 comments on commit 6bb118c

Please sign in to comment.