github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/querier/queryrange/prometheus_test.go (about)

     1  package queryrange
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/grafana/loki/pkg/loghttp"
    11  	"github.com/grafana/loki/pkg/logproto"
    12  	"github.com/grafana/loki/pkg/querier/queryrange/queryrangebase"
    13  )
    14  
    15  var emptyStats = `"stats": {
    16  	"ingester" : {
    17  		"store": {
    18  			"chunksDownloadTime": 0,
    19  			"totalChunksRef": 0,
    20  			"totalChunksDownloaded": 0,
    21  			"chunk" :{
    22  				"compressedBytes": 0,
    23  				"decompressedBytes": 0,
    24  				"decompressedLines": 0,
    25  				"headChunkBytes": 0,
    26  				"headChunkLines": 0,
    27  				"totalDuplicates": 0
    28  			}
    29  		},
    30  		"totalBatches": 0,
    31  		"totalChunksMatched": 0,
    32  		"totalLinesSent": 0,
    33  		"totalReached": 0
    34  	},
    35  	"querier": {
    36  		"store": {
    37  			"chunksDownloadTime": 0,
    38  			"totalChunksRef": 0,
    39  			"totalChunksDownloaded": 0,
    40  			"chunk" :{
    41  				"compressedBytes": 0,
    42  				"decompressedBytes": 0,
    43  				"decompressedLines": 0,
    44  				"headChunkBytes": 0,
    45  				"headChunkLines": 0,
    46  				"totalDuplicates": 0
    47  			}
    48  		}
    49  	},
    50  	"cache": {
    51  		"chunk": {
    52  			"entriesFound": 0,
    53  			"entriesRequested": 0,
    54  			"entriesStored": 0,
    55  			"bytesReceived": 0,
    56  			"bytesSent": 0,
    57  			"requests": 0
    58  		},
    59  		"index": {
    60  			"entriesFound": 0,
    61  			"entriesRequested": 0,
    62  			"entriesStored": 0,
    63  			"bytesReceived": 0,
    64  			"bytesSent": 0,
    65  			"requests": 0
    66  		},
    67  		"result": {
    68  			"entriesFound": 0,
    69  			"entriesRequested": 0,
    70  			"entriesStored": 0,
    71  			"bytesReceived": 0,
    72  			"bytesSent": 0,
    73  			"requests": 0
    74  		}
    75  	},
    76  	"summary": {
    77  		"bytesProcessedPerSecond": 0,
    78  		"execTime": 0,
    79  		"linesProcessedPerSecond": 0,
    80  		"queueTime": 0,
    81  		"subqueries": 0,
    82  		"totalBytesProcessed":0,
    83                  "totalEntriesReturned":0,
    84  		"totalLinesProcessed":0
    85  	}
    86  }`
    87  
    88  func Test_encodePromResponse(t *testing.T) {
    89  	for _, tt := range []struct {
    90  		name string
    91  		resp *LokiPromResponse
    92  		want string
    93  	}{
    94  		{
    95  			"matrix",
    96  			&LokiPromResponse{
    97  				Response: &queryrangebase.PrometheusResponse{
    98  					Status: string(queryrangebase.StatusSuccess),
    99  					Data: queryrangebase.PrometheusData{
   100  						ResultType: loghttp.ResultTypeMatrix,
   101  						Result: []queryrangebase.SampleStream{
   102  							{
   103  								Labels: []logproto.LabelAdapter{
   104  									{Name: "foo", Value: "bar"},
   105  								},
   106  								Samples: []logproto.LegacySample{
   107  									{Value: 1, TimestampMs: 1000},
   108  									{Value: 1, TimestampMs: 2000},
   109  								},
   110  							},
   111  							{
   112  								Labels: []logproto.LabelAdapter{
   113  									{Name: "foo", Value: "buzz"},
   114  								},
   115  								Samples: []logproto.LegacySample{
   116  									{Value: 4, TimestampMs: 1000},
   117  									{Value: 5, TimestampMs: 2000},
   118  								},
   119  							},
   120  						},
   121  					},
   122  				},
   123  			},
   124  			`{
   125  				"status": "success",
   126  				"data": {
   127  					"resultType": "matrix",
   128  					"result": [
   129  						{
   130  							"metric": {"foo": "bar"},
   131  							"values": [[1, "1"],[2, "1"]]
   132  						},
   133  						{
   134  							"metric": {"foo": "buzz"},
   135  							"values": [[1, "4"],[2, "5"]]
   136  						}
   137  					],
   138  					` + emptyStats + `
   139  				}
   140  			}`,
   141  		},
   142  		{
   143  			"vector",
   144  			&LokiPromResponse{
   145  				Response: &queryrangebase.PrometheusResponse{
   146  					Status: string(queryrangebase.StatusSuccess),
   147  					Data: queryrangebase.PrometheusData{
   148  						ResultType: loghttp.ResultTypeVector,
   149  						Result: []queryrangebase.SampleStream{
   150  							{
   151  								Labels: []logproto.LabelAdapter{
   152  									{Name: "foo", Value: "bar"},
   153  								},
   154  								Samples: []logproto.LegacySample{
   155  									{Value: 1, TimestampMs: 1000},
   156  								},
   157  							},
   158  							{
   159  								Labels: []logproto.LabelAdapter{
   160  									{Name: "foo", Value: "buzz"},
   161  								},
   162  								Samples: []logproto.LegacySample{
   163  									{Value: 4, TimestampMs: 1000},
   164  								},
   165  							},
   166  						},
   167  					},
   168  				},
   169  			},
   170  			`{
   171  				"status": "success",
   172  				"data": {
   173  					"resultType": "vector",
   174  					"result": [
   175  						{
   176  							"metric": {"foo": "bar"},
   177  							"value": [1, "1"]
   178  						},
   179  						{
   180  							"metric": {"foo": "buzz"},
   181  							"value": [1, "4"]
   182  						}
   183  					],
   184  					` + emptyStats + `
   185  				}
   186  			}`,
   187  		},
   188  	} {
   189  		tt := tt
   190  		t.Run(tt.name, func(t *testing.T) {
   191  			r, err := tt.resp.encode(context.Background())
   192  			require.NoError(t, err)
   193  			b, err := io.ReadAll(r.Body)
   194  			require.NoError(t, err)
   195  			got := string(b)
   196  			require.JSONEq(t, tt.want, got)
   197  		})
   198  	}
   199  }