Skip to content

Commit

Permalink
tests/*: redirect metrics into file
Browse files Browse the repository at this point in the history
Redirect metrics data into file to reduce output.

Fix: etcd-io#16422

Signed-off-by: Wei Fu <fuweid89@gmail.com>
  • Loading branch information
fuweid committed Aug 18, 2023
1 parent 699861a commit e4e05c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
21 changes: 16 additions & 5 deletions tests/e2e/cmux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -126,7 +128,7 @@ func testConnectionMultiplexing(t *testing.T, ctx context.Context, member e2e.Et
}
t.Run(tname, func(t *testing.T) {
assert.NoError(t, fetchGrpcGateway(httpEndpoint, httpVersion, connType))
assert.NoError(t, fetchMetrics(httpEndpoint, httpVersion, connType))
assert.NoError(t, fetchMetrics(t, httpEndpoint, httpVersion, connType))
assert.NoError(t, fetchVersion(httpEndpoint, httpVersion, connType))
assert.NoError(t, fetchHealth(httpEndpoint, httpVersion, connType))
assert.NoError(t, fetchDebugVars(httpEndpoint, httpVersion, connType))
Expand Down Expand Up @@ -168,12 +170,21 @@ func validateGrpcgatewayRangeReponse(respData []byte) error {
return json.Unmarshal(respData, &resp)
}

func fetchMetrics(endpoint string, httpVersion string, connType e2e.ClientConnType) error {
req := e2e.CURLReq{Endpoint: "/metrics", Timeout: 5, HttpVersion: httpVersion}
respData, err := curl(endpoint, "GET", req, connType)
if err != nil {
func fetchMetrics(t *testing.T, endpoint string, httpVersion string, connType e2e.ClientConnType) error {
tmpDir := t.TempDir()
metricFile := filepath.Join(tmpDir, "metrics")

req := e2e.CURLReq{Endpoint: "/metrics", Timeout: 5, HttpVersion: httpVersion, OutputFile: metricFile}
if _, err := curl(endpoint, "GET", req, connType); err != nil {
return err
}

rawData, err := os.ReadFile(metricFile)
if err != nil {
return fmt.Errorf("failed to read the metric: %w", err)
}
respData := string(rawData)

var parser expfmt.TextParser
_, err = parser.TextToMetricFamilies(strings.NewReader(strings.ReplaceAll(respData, "\r\n", "\n")))
return err
Expand Down
6 changes: 6 additions & 0 deletions tests/framework/e2e/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type CURLReq struct {

Ciphers string
HttpVersion string

OutputFile string
}

func (r CURLReq) timeoutDuration() time.Duration {
Expand Down Expand Up @@ -93,6 +95,10 @@ func CURLPrefixArgs(clientURL string, cfg ClientConfig, CN bool, method string,
cmdArgs = append(cmdArgs, "--ciphers", req.Ciphers)
}

if req.OutputFile != "" {
cmdArgs = append(cmdArgs, "--output", req.OutputFile)
}

switch method {
case "POST", "PUT":
dt := req.Value
Expand Down

0 comments on commit e4e05c5

Please sign in to comment.