Skip to content

Commit

Permalink
move function to promclient
Browse files Browse the repository at this point in the history
  • Loading branch information
Titasp committed Sep 27, 2022
1 parent 8dff352 commit 2b06a47
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 57 deletions.
30 changes: 30 additions & 0 deletions pkg/promclient/promclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,3 +838,33 @@ func (c *Client) TargetsInGRPC(ctx context.Context, base *url.URL, stateTargets
}
return v.Data, c.get2xxResultWithGRPCErrors(ctx, "/prom_targets HTTP[client]", &u, &v)
}

func (c *Client) SeriesMatchCount(ctx context.Context, base *url.URL, matchers []*labels.Matcher, start, end int64) (int, error) {
u := *base
u.Path = path.Join(u.Path, "/api/v1/series")
q := u.Query()

q.Add("match[]", storepb.PromMatchersToString(matchers...))
q.Add("start", formatTime(timestamp.Time(start)))
q.Add("end", formatTime(timestamp.Time(end)))
q.Add("only_count", "1")
u.RawQuery = q.Encode()

body, _, err := c.req2xx(ctx, &u, http.MethodGet)
if err != nil {
return -1, errors.Wrap(err, "read query instant response")
}

var m struct {
Status string `json:"status"`
Data struct {
MetricsCount int `json:"metrics_count"`
} `json:"data"`
}

if err = json.Unmarshal(body, &m); err != nil {
return -1, errors.Wrap(err, "unmarshal query instant response")
}

return m.Data.MetricsCount, nil
}
59 changes: 2 additions & 57 deletions pkg/store/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ package store
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/timestamp"
Expand Down Expand Up @@ -166,9 +163,9 @@ func (p *PrometheusStore) Series(r *storepb.SeriesRequest, s storepb.Store_Serie
}

if p.limitMaxMatchedSeries > 0 {
matchedSeriesCount, err := p.getMatchedSeriesCount(matchers, r.MinTime, r.MaxTime)
matchedSeriesCount, err := p.client.SeriesMatchCount(s.Context(), p.base, matchers, r.MinTime, r.MaxTime)
if err != nil {
return errors.Wrap(err, "get matched series count")
return errors.Wrap(err, "get series match count")
}

if matchedSeriesCount > p.limitMaxMatchedSeries {
Expand Down Expand Up @@ -720,55 +717,3 @@ func (p *PrometheusStore) LabelSet() []labelpb.ZLabelSet {
func (p *PrometheusStore) Timestamps() (mint int64, maxt int64) {
return p.timestamps()
}

func (p *PrometheusStore) getMatchedSeriesCount(matchers []*labels.Matcher, start, end int64) (int, error) {
u := *p.base
u.Path = path.Join(u.Path, "/api/v1/series")
q := u.Query()

q.Add("match[]", storepb.PromMatchersToString(matchers...))
q.Add("start", timeToPromTimestamp(timestamp.Time(start)))
q.Add("end", timeToPromTimestamp(timestamp.Time(end)))
q.Add("only_count", "1")
u.RawQuery = q.Encode()

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return -1, errors.Wrap(err, "new series count request")
}

c := http.Client{}
res, err := c.Do(req)
if err != nil {
return -1, errors.Wrap(err, "execute series count request")
}

if res.StatusCode != 200 {
return -1, fmt.Errorf("returned status code: %v", res.StatusCode)
}

defer res.Body.Close()

type respModel struct {
Status string `json:"status"`
Data struct {
MetricsCount int `json:"metrics_count"`
} `json:"data"`
}

bodyData, err := io.ReadAll(res.Body)
if err != nil {
return -1, fmt.Errorf("read resp body: %w", err)
}

var resp respModel
if err := json.Unmarshal(bodyData, &resp); err != nil {
return -1, errors.Wrap(err, "decode resp body")
}

return resp.Data.MetricsCount, nil
}

func timeToPromTimestamp(t time.Time) string {
return strconv.FormatFloat(float64(t.Unix())+float64(t.Nanosecond())/1e9, 'f', -1, 64)
}

0 comments on commit 2b06a47

Please sign in to comment.