2022-03-22 12:40:24 +00:00
|
|
|
package private
|
2020-08-28 23:23:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2020-10-13 23:38:13 +00:00
|
|
|
"sort"
|
2020-10-08 23:43:49 +00:00
|
|
|
"sync"
|
2020-08-28 23:23:40 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
2020-10-13 23:38:13 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-08-28 23:23:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"google.golang.org/grpc"
|
2020-10-08 23:43:49 +00:00
|
|
|
|
2022-03-22 12:40:24 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc/private/internal/testservice"
|
2022-03-30 16:51:56 +00:00
|
|
|
"github.com/hashicorp/consul/proto/prototest"
|
2021-08-22 18:06:26 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-08-28 23:23:40 +00:00
|
|
|
)
|
|
|
|
|
2020-09-08 21:31:47 +00:00
|
|
|
func noopRegister(*grpc.Server) {}
|
|
|
|
|
2020-08-28 23:23:40 +00:00
|
|
|
func TestHandler_EmitsStats(t *testing.T) {
|
2020-10-14 19:43:48 +00:00
|
|
|
sink, reset := patchGlobalMetrics(t)
|
2020-08-28 23:23:40 +00:00
|
|
|
|
|
|
|
addr := &net.IPAddr{IP: net.ParseIP("127.0.0.1")}
|
2021-08-22 18:06:26 +00:00
|
|
|
handler := NewHandler(hclog.Default(), addr, noopRegister)
|
2020-10-14 19:43:48 +00:00
|
|
|
reset()
|
2020-09-08 21:31:47 +00:00
|
|
|
|
2020-08-28 23:23:40 +00:00
|
|
|
testservice.RegisterSimpleServer(handler.srv, &simple{})
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
g.Go(func() error {
|
|
|
|
return handler.srv.Serve(lis)
|
|
|
|
})
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := handler.Shutdown(); err != nil {
|
|
|
|
t.Logf("grpc server shutdown: %v", err)
|
|
|
|
}
|
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
t.Logf("grpc server error: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
conn, err := grpc.DialContext(ctx, lis.Addr().String(), grpc.WithInsecure())
|
|
|
|
require.NoError(t, err)
|
2020-10-14 17:21:51 +00:00
|
|
|
t.Cleanup(func() { conn.Close() })
|
2020-08-28 23:23:40 +00:00
|
|
|
|
|
|
|
client := testservice.NewSimpleClient(conn)
|
|
|
|
fClient, err := client.Flow(ctx, &testservice.Req{Datacenter: "mine"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Wait for the first event so that we know the stream is sending.
|
|
|
|
_, err = fClient.Recv()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-14 17:16:13 +00:00
|
|
|
cancel()
|
2020-10-08 23:43:49 +00:00
|
|
|
conn.Close()
|
|
|
|
handler.srv.GracefulStop()
|
2020-09-14 17:16:13 +00:00
|
|
|
// Wait for the server to stop so that active_streams is predictable.
|
2020-10-08 23:43:49 +00:00
|
|
|
require.NoError(t, g.Wait())
|
|
|
|
|
2020-10-13 23:38:13 +00:00
|
|
|
// Occasionally the active_stream=0 metric may be emitted before the
|
|
|
|
// active_conns=0 metric. The order of those metrics is not really important
|
|
|
|
// so we sort the calls to match the expected.
|
|
|
|
sort.Slice(sink.gaugeCalls, func(i, j int) bool {
|
|
|
|
if i < 2 || j < 2 {
|
|
|
|
return i < j
|
|
|
|
}
|
|
|
|
if len(sink.gaugeCalls[i].key) < 4 || len(sink.gaugeCalls[j].key) < 4 {
|
|
|
|
return i < j
|
|
|
|
}
|
|
|
|
return sink.gaugeCalls[i].key[3] < sink.gaugeCalls[j].key[3]
|
|
|
|
})
|
|
|
|
|
|
|
|
cmpMetricCalls := cmp.AllowUnexported(metricCall{})
|
2020-10-08 23:43:49 +00:00
|
|
|
expectedGauge := []metricCall{
|
2020-10-15 20:29:09 +00:00
|
|
|
{key: []string{"testing", "grpc", "server", "connections"}, val: 1},
|
|
|
|
{key: []string{"testing", "grpc", "server", "streams"}, val: 1},
|
|
|
|
{key: []string{"testing", "grpc", "server", "connections"}, val: 0},
|
|
|
|
{key: []string{"testing", "grpc", "server", "streams"}, val: 0},
|
2020-10-08 23:43:49 +00:00
|
|
|
}
|
2022-03-30 16:51:56 +00:00
|
|
|
prototest.AssertDeepEqual(t, expectedGauge, sink.gaugeCalls, cmpMetricCalls)
|
2020-09-14 17:16:13 +00:00
|
|
|
|
2020-08-28 23:23:40 +00:00
|
|
|
expectedCounter := []metricCall{
|
2020-10-15 20:40:33 +00:00
|
|
|
{key: []string{"testing", "grpc", "server", "connection", "count"}, val: 1},
|
|
|
|
{key: []string{"testing", "grpc", "server", "request", "count"}, val: 1},
|
|
|
|
{key: []string{"testing", "grpc", "server", "stream", "count"}, val: 1},
|
2020-08-28 23:23:40 +00:00
|
|
|
}
|
2022-03-30 16:51:56 +00:00
|
|
|
prototest.AssertDeepEqual(t, expectedCounter, sink.incrCounterCalls, cmpMetricCalls)
|
2020-10-13 23:38:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 19:43:48 +00:00
|
|
|
func patchGlobalMetrics(t *testing.T) (*fakeMetricsSink, func()) {
|
2020-08-28 23:23:40 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
sink := &fakeMetricsSink{}
|
|
|
|
cfg := &metrics.Config{
|
|
|
|
ServiceName: "testing",
|
|
|
|
TimerGranularity: time.Millisecond, // Timers are in milliseconds
|
|
|
|
ProfileInterval: time.Second, // Poll runtime every second
|
|
|
|
FilterDefault: true,
|
|
|
|
}
|
2020-09-14 17:16:13 +00:00
|
|
|
var err error
|
2020-11-11 19:27:07 +00:00
|
|
|
defaultMetrics = func() *metrics.Metrics {
|
|
|
|
m, _ := metrics.New(cfg, sink)
|
|
|
|
return m
|
|
|
|
}
|
2020-08-28 23:23:40 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-14 19:43:48 +00:00
|
|
|
reset := func() {
|
|
|
|
t.Helper()
|
2020-11-11 19:27:07 +00:00
|
|
|
defaultMetrics = metrics.Default
|
2020-08-28 23:23:40 +00:00
|
|
|
require.NoError(t, err, "failed to reset global metrics")
|
2020-10-14 19:43:48 +00:00
|
|
|
}
|
|
|
|
return sink, reset
|
2020-08-28 23:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type fakeMetricsSink struct {
|
2020-10-08 23:43:49 +00:00
|
|
|
lock sync.Mutex
|
2020-08-28 23:23:40 +00:00
|
|
|
metrics.BlackholeSink
|
|
|
|
gaugeCalls []metricCall
|
|
|
|
incrCounterCalls []metricCall
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeMetricsSink) SetGaugeWithLabels(key []string, val float32, labels []metrics.Label) {
|
2020-10-08 23:43:49 +00:00
|
|
|
f.lock.Lock()
|
2020-08-28 23:23:40 +00:00
|
|
|
f.gaugeCalls = append(f.gaugeCalls, metricCall{key: key, val: val, labels: labels})
|
2020-10-08 23:43:49 +00:00
|
|
|
f.lock.Unlock()
|
2020-08-28 23:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeMetricsSink) IncrCounterWithLabels(key []string, val float32, labels []metrics.Label) {
|
2020-10-08 23:43:49 +00:00
|
|
|
f.lock.Lock()
|
2020-08-28 23:23:40 +00:00
|
|
|
f.incrCounterCalls = append(f.incrCounterCalls, metricCall{key: key, val: val, labels: labels})
|
2020-10-08 23:43:49 +00:00
|
|
|
f.lock.Unlock()
|
2020-08-28 23:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type metricCall struct {
|
|
|
|
key []string
|
|
|
|
val float32
|
|
|
|
labels []metrics.Label
|
|
|
|
}
|
2020-09-14 17:16:13 +00:00
|
|
|
|
|
|
|
func logError(t *testing.T, f func() error) func() {
|
|
|
|
return func() {
|
|
|
|
if err := f(); err != nil {
|
|
|
|
t.Logf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|