| 91 | } |
| 92 | |
| 93 | func (tc *simpleTestCase) Query(ctx context.Context, client v1.API, selectors string, start time.Time, duration time.Duration) ([]model.SamplePair, error) { |
| 94 | log, ctx := spanlogger.New(ctx, "simpleTestCase.Query") |
| 95 | defer log.Finish() |
| 96 | |
| 97 | metricName := prometheus.BuildFQName(namespace, subsystem, tc.name) |
| 98 | query := fmt.Sprintf("%s{%s}[%dm]", metricName, selectors, duration/time.Minute) |
| 99 | level.Info(log).Log("query", query) |
| 100 | |
| 101 | value, wrngs, err := client.Query(ctx, query, start) |
| 102 | if err != nil { |
| 103 | return nil, err |
| 104 | } |
| 105 | if wrngs != nil { |
| 106 | level.Warn(log).Log( |
| 107 | "query", query, |
| 108 | "start", start, |
| 109 | "warnings", wrngs, |
| 110 | ) |
| 111 | } |
| 112 | if value.Type() != model.ValMatrix { |
| 113 | return nil, fmt.Errorf("didn't get matrix from Prom") |
| 114 | } |
| 115 | |
| 116 | ms, ok := value.(model.Matrix) |
| 117 | if !ok { |
| 118 | return nil, fmt.Errorf("didn't get matrix from Prom") |
| 119 | } |
| 120 | |
| 121 | // sort samples belonging to different series by first timestamp of the batch |
| 122 | sort.Slice(ms, func(i, j int) bool { |
| 123 | if len(ms[i].Values) == 0 { |
| 124 | return true |
| 125 | } |
| 126 | if len(ms[j].Values) == 0 { |
| 127 | return true |
| 128 | } |
| 129 | |
| 130 | return ms[i].Values[0].Timestamp.Before(ms[j].Values[0].Timestamp) |
| 131 | }) |
| 132 | |
| 133 | var result []model.SamplePair |
| 134 | for _, stream := range ms { |
| 135 | result = append(result, stream.Values...) |
| 136 | } |
| 137 | return result, nil |
| 138 | } |
| 139 | |
| 140 | func (tc *simpleTestCase) Test(ctx context.Context, client v1.API, selectors string, start time.Time, duration time.Duration) (bool, error) { |
| 141 | log := spanlogger.FromContext(ctx) |