github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/sys/it/impl_metrics_test.go (about) 1 /* 2 * Copyright (c) 2022-present unTill Pro, Ltd. 3 */ 4 5 package sys_it 6 7 import ( 8 "fmt" 9 "io" 10 "log" 11 "net/http" 12 "testing" 13 14 "github.com/stretchr/testify/require" 15 16 "github.com/voedger/voedger/pkg/istructs" 17 commandprocessor "github.com/voedger/voedger/pkg/processors/command" 18 coreutils "github.com/voedger/voedger/pkg/utils" 19 it "github.com/voedger/voedger/pkg/vit" 20 ) 21 22 func TestBasicUsage_Metrics(t *testing.T) { 23 vit := it.NewVIT(t, &it.SharedConfig_App1) 24 defer vit.TearDown() 25 require := require.New(t) 26 27 url := fmt.Sprintf("http://127.0.0.1:%d/metrics", vit.MetricsServicePort()) 28 resp, err := http.Get(url) 29 require.NoError(err) 30 31 bb, err := io.ReadAll(resp.Body) 32 require.NoError(err) 33 resp.Body.Close() 34 35 require.Contains(string(bb), "{app=") 36 } 37 38 func TestMetricsService(t *testing.T) { 39 vit := it.NewVIT(t, &it.SharedConfig_App1) 40 defer vit.TearDown() 41 client, cleanup := coreutils.NewIHTTPClient() 42 defer cleanup() 43 44 t.Run("service check", func(t *testing.T) { 45 log.Println(vit.MetricsRequest(client, coreutils.WithRelativeURL("/metrics/check"))) 46 }) 47 48 t.Run("404 on wrong url", func(t *testing.T) { 49 log.Println(vit.MetricsRequest(client, coreutils.WithRelativeURL("/unknown"), coreutils.Expect404())) 50 }) 51 52 t.Run("404 on wrong method", func(t *testing.T) { 53 log.Println(vit.MetricsRequest(client, coreutils.WithMethod(http.MethodPost), coreutils.Expect404())) 54 }) 55 } 56 57 func TestCommandProcessorMetrics(t *testing.T) { 58 vit := it.NewVIT(t, &it.SharedConfig_App1) 59 defer vit.TearDown() 60 require := require.New(t) 61 client, cleanup := coreutils.NewIHTTPClient() 62 defer cleanup() 63 64 ws := vit.WS(istructs.AppQName_test1_app1, "test_ws") 65 body := `{"cuds": [{"fields": {"sys.ID": 1,"sys.QName": "app1pkg.articles","name": "cola","article_manual": 1,"article_hash": 2,"hideonhold": 3,"time_active": 4,"control_active": 5}}]}` 66 vit.PostWS(ws, "c.sys.CUD", body) 67 68 metrics := vit.MetricsRequest(client) 69 70 require.Contains(metrics, commandprocessor.CommandsTotal) 71 require.Contains(metrics, commandprocessor.CommandsSeconds) 72 require.Contains(metrics, commandprocessor.ExecSeconds) 73 require.Contains(metrics, commandprocessor.ProjectorsSeconds) 74 }