@@ -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,31 @@ 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+ if len (value ) < 2 {
719+ continue
720+ }
721+
722+ line , ok := value [1 ].(string )
723+ if ! ok {
724+ continue
725+ }
726+ lines = append (lines , line )
727+ }
728+ }
729+
730+ return lines , nil
731+ }
732+
693733type LokiQueryResponse struct {
694734 Status string `json:"status"`
695735 Data struct {
0 commit comments