RemoteRead runs a remote read query.
(matchers []*labels.Matcher, start, end time.Time, step time.Duration)
| 564 | |
| 565 | // RemoteRead runs a remote read query. |
| 566 | func (c *Client) RemoteRead(matchers []*labels.Matcher, start, end time.Time, step time.Duration) (*prompb.ReadResponse, error) { |
| 567 | startMs := start.UnixMilli() |
| 568 | endMs := end.UnixMilli() |
| 569 | stepMs := step.Milliseconds() |
| 570 | |
| 571 | q, err := remote.ToQuery(startMs, endMs, matchers, &storage.SelectHints{ |
| 572 | Step: stepMs, |
| 573 | Start: startMs, |
| 574 | End: endMs, |
| 575 | }) |
| 576 | if err != nil { |
| 577 | return nil, err |
| 578 | } |
| 579 | |
| 580 | req := &prompb.ReadRequest{ |
| 581 | Queries: []*prompb.Query{q}, |
| 582 | AcceptedResponseTypes: []prompb.ReadRequest_ResponseType{prompb.ReadRequest_STREAMED_XOR_CHUNKS}, |
| 583 | } |
| 584 | |
| 585 | data, err := proto.Marshal(req) |
| 586 | if err != nil { |
| 587 | return nil, err |
| 588 | } |
| 589 | compressed := snappy.Encode(nil, data) |
| 590 | |
| 591 | // Call the remote read API endpoint with a timeout. |
| 592 | httpReqCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 593 | defer cancel() |
| 594 | |
| 595 | httpReq, err := http.NewRequestWithContext(httpReqCtx, "POST", "http://"+c.querierAddress+"/prometheus/api/v1/read", bytes.NewReader(compressed)) |
| 596 | if err != nil { |
| 597 | return nil, err |
| 598 | } |
| 599 | httpReq.Header.Set("X-Scope-OrgID", "user-1") |
| 600 | httpReq.Header.Add("Content-Encoding", "snappy") |
| 601 | httpReq.Header.Add("Accept-Encoding", "snappy") |
| 602 | httpReq.Header.Set("Content-Type", "application/x-protobuf") |
| 603 | httpReq.Header.Set("User-Agent", "Prometheus/1.8.2") |
| 604 | httpReq.Header.Set("X-Prometheus-Remote-Read-Version", "0.1.0") |
| 605 | |
| 606 | httpResp, err := c.httpClient.Do(httpReq) |
| 607 | if err != nil { |
| 608 | return nil, err |
| 609 | } |
| 610 | if httpResp.StatusCode != http.StatusOK { |
| 611 | return nil, fmt.Errorf("unexpected status code %d", httpResp.StatusCode) |
| 612 | } |
| 613 | |
| 614 | compressed, err = io.ReadAll(httpResp.Body) |
| 615 | if err != nil { |
| 616 | return nil, err |
| 617 | } |
| 618 | |
| 619 | uncompressed, err := snappy.Decode(nil, compressed) |
| 620 | if err != nil { |
| 621 | return nil, err |
| 622 | } |
| 623 |