Skip to content

Commit 6eee2d5

Browse files
committed
debugging
Signed-off-by: Huabing (Robin) Zhao <zhaohuabing@gmail.com>
1 parent f9c5ad2 commit 6eee2d5

2 files changed

Lines changed: 93 additions & 17 deletions

File tree

test/e2e/tests/dynamic_resolver_original_host_accesslog.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
package tests
99

1010
import (
11+
"context"
12+
"encoding/json"
1113
"testing"
14+
"time"
1215

16+
"github.com/stretchr/testify/require"
1317
"k8s.io/apimachinery/pkg/types"
18+
"k8s.io/apimachinery/pkg/util/wait"
1419
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
1520
"sigs.k8s.io/gateway-api/conformance/utils/http"
1621
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
1722
"sigs.k8s.io/gateway-api/conformance/utils/suite"
23+
"sigs.k8s.io/gateway-api/conformance/utils/tlog"
1824

1925
"github.com/envoyproxy/gateway/internal/gatewayapi"
2026
"github.com/envoyproxy/gateway/internal/gatewayapi/resource"
@@ -70,8 +76,37 @@ var DynamicResolverOriginalHostAccessLogTest = suite.ConformanceTest{
7076
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expectedResponse)
7177

7278
labels := getOTELLabels(ns)
73-
match := `\"x-request-id\":\"` + requestID +
74-
`\".*\"http.host\":\"test-service-foo.gateway-conformance-infra.svc.cluster.local\".*\"http.original_host\":\"www.example.com\"`
75-
runLogTest(t, suite, gwAddr, &expectedResponse, labels, match, 1)
79+
match := `\"x-request-id\":\"` + requestID + `\"`
80+
81+
var matchedLog string
82+
err := wait.PollUntilContextTimeout(context.Background(), time.Second, time.Minute, true, func(_ context.Context) (bool, error) {
83+
lines, err := QueryLogLinesFromLoki(t, suite.Client, labels, match)
84+
if err != nil {
85+
tlog.Logf(t, "failed to query log lines from loki: %v", err)
86+
return false, nil
87+
}
88+
89+
for _, line := range lines {
90+
tlog.Logf(t, "candidate access log line: %s", line)
91+
entry := map[string]any{}
92+
if err := json.Unmarshal([]byte(line), &entry); err != nil {
93+
tlog.Logf(t, "failed to unmarshal access log line: %v", err)
94+
continue
95+
}
96+
97+
if gotRequestID, ok := entry["x-request-id"].(string); ok && gotRequestID == requestID {
98+
matchedLog = line
99+
return true, nil
100+
}
101+
}
102+
103+
return false, nil
104+
})
105+
require.NoError(t, err, "timed out waiting for access log line for request id %s", requestID)
106+
107+
entry := map[string]string{}
108+
require.NoError(t, json.Unmarshal([]byte(matchedLog), &entry))
109+
require.Equal(t, "test-service-foo.gateway-conformance-infra.svc.cluster.local", entry["http.host"])
110+
require.Equal(t, "www.example.com", entry["http.original_host"])
76111
},
77112
}

test/e2e/tests/utils.go

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -629,14 +629,27 @@ func OverLimitCount(suite *suite.ConformanceTestSuite) (int, error) {
629629
return total, nil
630630
}
631631

632-
// QueryLogCountFromLoki queries log count from loki
633-
func QueryLogCountFromLoki(t *testing.T, c client.Client, keyValues map[string]string, match string) (int, error) {
632+
func buildLokiQuery(keyValues map[string]string, match string) string {
633+
qParams := make([]string, 0, len(keyValues))
634+
for k, v := range keyValues {
635+
qParams = append(qParams, fmt.Sprintf("%s=\"%s\"", k, v))
636+
}
637+
638+
q := "{" + strings.Join(qParams, ",") + "}"
639+
if match != "" {
640+
q = q + "|~\"" + match + "\""
641+
}
642+
643+
return q
644+
}
645+
646+
func queryLoki(t *testing.T, c client.Client, keyValues map[string]string, match string) (*LokiQueryResponse, string, error) {
634647
svc := corev1.Service{}
635648
if err := c.Get(context.Background(), types.NamespacedName{
636649
Namespace: "monitoring",
637650
Name: "loki",
638651
}, &svc); err != nil {
639-
return -1, err
652+
return nil, "", err
640653
}
641654
lokiHost := ""
642655
for _, ing := range svc.Status.LoadBalancer.Ingress {
@@ -646,22 +659,14 @@ func QueryLogCountFromLoki(t *testing.T, c client.Client, keyValues map[string]s
646659
}
647660
}
648661

649-
qParams := make([]string, 0, len(keyValues))
650-
for k, v := range keyValues {
651-
qParams = append(qParams, fmt.Sprintf("%s=\"%s\"", k, v))
652-
}
653-
654-
q := "{" + strings.Join(qParams, ",") + "}"
655-
if match != "" {
656-
q = q + "|~\"" + match + "\""
657-
}
662+
q := buildLokiQuery(keyValues, match)
658663
params := url.Values{}
659664
params.Add("query", q)
660665
params.Add("start", fmt.Sprintf("%d", time.Now().Add(-10*time.Minute).Unix())) // query logs from last 10 minutes
661666
lokiQueryURL := fmt.Sprintf("http://%s/loki/api/v1/query_range?%s", net.JoinHostPort(lokiHost, "3100"), params.Encode())
662667
res, err := http.DefaultClient.Get(lokiQueryURL)
663668
if err != nil {
664-
return -1, err
669+
return nil, q, err
665670
}
666671
defer func() {
667672
_ = res.Body.Close()
@@ -670,11 +675,21 @@ func QueryLogCountFromLoki(t *testing.T, c client.Client, keyValues map[string]s
670675

671676
b, err := io.ReadAll(res.Body)
672677
if err != nil {
673-
return -1, err
678+
return nil, q, err
674679
}
675680

676681
lokiResponse := &LokiQueryResponse{}
677682
if err := json.Unmarshal(b, lokiResponse); err != nil {
683+
return nil, q, err
684+
}
685+
686+
return lokiResponse, q, nil
687+
}
688+
689+
// QueryLogCountFromLoki queries log count from loki
690+
func QueryLogCountFromLoki(t *testing.T, c client.Client, keyValues map[string]string, match string) (int, error) {
691+
lokiResponse, q, err := queryLoki(t, c, keyValues, match)
692+
if err != nil {
678693
return -1, err
679694
}
680695

@@ -690,6 +705,32 @@ func QueryLogCountFromLoki(t *testing.T, c client.Client, keyValues map[string]s
690705
return total, nil
691706
}
692707

708+
// QueryLogLinesFromLoki queries matching log lines from loki.
709+
func QueryLogLinesFromLoki(t *testing.T, c client.Client, keyValues map[string]string, match string) ([]string, error) {
710+
lokiResponse, _, err := queryLoki(t, c, keyValues, match)
711+
if err != nil {
712+
return nil, err
713+
}
714+
715+
lines := make([]string, 0)
716+
for _, res := range lokiResponse.Data.Result {
717+
for _, value := range res.Values {
718+
pair, ok := value.([]interface{})
719+
if !ok || len(pair) < 2 {
720+
continue
721+
}
722+
723+
line, ok := pair[1].(string)
724+
if !ok {
725+
continue
726+
}
727+
lines = append(lines, line)
728+
}
729+
}
730+
731+
return lines, nil
732+
}
733+
693734
type LokiQueryResponse struct {
694735
Status string `json:"status"`
695736
Data struct {

0 commit comments

Comments
 (0)