(ctx context.Context, req Request, resps []*PrometheusResponse)
| 137 | } |
| 138 | |
| 139 | func vectorMerge(ctx context.Context, req Request, resps []*PrometheusResponse) (*Vector, error) { |
| 140 | output := map[string]Sample{} |
| 141 | metrics := []string{} // Used to preserve the order for topk and bottomk. |
| 142 | sortPlan, err := sortPlanForQuery(req.GetQuery()) |
| 143 | if err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | buf := make([]byte, 0, 1024) |
| 147 | for _, resp := range resps { |
| 148 | if err = ctx.Err(); err != nil { |
| 149 | return nil, err |
| 150 | } |
| 151 | if resp == nil { |
| 152 | continue |
| 153 | } |
| 154 | // Merge vector result samples only. Skip other types such as |
| 155 | // string, scalar as those are not sharable. |
| 156 | if resp.Data.Result.GetVector() == nil { |
| 157 | continue |
| 158 | } |
| 159 | for _, sample := range resp.Data.Result.GetVector().Samples { |
| 160 | s := sample |
| 161 | metric := string(cortexpb.FromLabelAdaptersToLabels(sample.Labels).Bytes(buf)) |
| 162 | if existingSample, ok := output[metric]; !ok { |
| 163 | output[metric] = s |
| 164 | metrics = append(metrics, metric) // Preserve the order of metric. |
| 165 | } else if existingSample.GetTimestampMs() < s.GetTimestampMs() { |
| 166 | // Choose the latest sample if we see overlap. |
| 167 | output[metric] = s |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | result := &Vector{ |
| 173 | Samples: make([]Sample, 0, len(output)), |
| 174 | } |
| 175 | |
| 176 | if len(output) == 0 { |
| 177 | return result, nil |
| 178 | } |
| 179 | |
| 180 | if sortPlan == mergeOnly { |
| 181 | for _, k := range metrics { |
| 182 | result.Samples = append(result.Samples, output[k]) |
| 183 | } |
| 184 | return result, nil |
| 185 | } |
| 186 | |
| 187 | samples := make([]*pair, 0, len(output)) |
| 188 | for k, v := range output { |
| 189 | samples = append(samples, &pair{ |
| 190 | metric: k, |
| 191 | s: v, |
| 192 | }) |
| 193 | } |
| 194 | |
| 195 | // TODO: What if we have mixed float and histogram samples in the response? |
| 196 | // Then the sorting behavior is undefined. Prometheus doesn't handle it. |
no test coverage detected