github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/server/graphite_test.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package server 12 13 import ( 14 "context" 15 "fmt" 16 "net" 17 "testing" 18 "time" 19 20 "github.com/cockroachdb/cockroach/pkg/base" 21 "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" 22 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" 23 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 24 "github.com/cockroachdb/cockroach/pkg/util/log" 25 ) 26 27 // TestGraphite tests that a server pushes metrics data to Graphite endpoint, 28 // if configured. In addition, it verifies that things don't fall apart when 29 // the endpoint goes away. 30 func TestGraphite(t *testing.T) { 31 defer leaktest.AfterTest(t)() 32 s, rawDB, _ := serverutils.StartServer(t, base.TestServerArgs{}) 33 defer s.Stopper().Stop(context.Background()) 34 ctx := context.Background() 35 36 const setQ = `SET CLUSTER SETTING "%s" = "%s"` 37 const interval = 3 * time.Millisecond 38 db := sqlutils.MakeSQLRunner(rawDB) 39 db.Exec(t, fmt.Sprintf(setQ, graphiteIntervalKey, interval)) 40 41 listen := func() { 42 lis, err := net.Listen("tcp", "127.0.0.1:0") 43 if err != nil { 44 t.Fatal("failed to open port", err) 45 } 46 p := lis.Addr().String() 47 log.Infof(ctx, "Open port %s and listening", p) 48 49 defer func() { 50 log.Infof(ctx, "Close port %s", p) 51 if err := lis.Close(); err != nil { 52 t.Fatal("failed to close port", err) 53 } 54 }() 55 56 db.Exec(t, fmt.Sprintf(setQ, "external.graphite.endpoint", p)) 57 if _, e := lis.Accept(); e != nil { 58 t.Fatal("failed to receive connection", e) 59 } else { 60 log.Info(ctx, "received connection") 61 } 62 } 63 64 listen() 65 log.Info(ctx, "Make sure things don't fall apart when endpoint goes away.") 66 time.Sleep(5 * interval) 67 listen() 68 }