-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use series API for query/series check
- Loading branch information
Showing
5 changed files
with
273 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package promapi | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"github.com/prometheus/common/model" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type SeriesResult struct { | ||
Series model.Vector | ||
} | ||
|
||
func (p *Prometheus) Series(ctx context.Context, matches []string) ([]model.LabelSet, error) { | ||
log.Debug().Str("uri", p.uri).Strs("matches", matches).Msg("Scheduling prometheus series query") | ||
|
||
lockKey := strings.Join(matches, ",") | ||
p.lock.lock(lockKey) | ||
defer p.lock.unlock((lockKey)) | ||
|
||
if v, ok := p.cache.Get(lockKey); ok { | ||
log.Debug().Str("key", lockKey).Str("uri", p.uri).Msg("Query cache hit") | ||
r := v.([]model.LabelSet) | ||
return r, nil | ||
} | ||
|
||
log.Debug().Str("uri", p.uri).Strs("matches", matches).Msg("Query started") | ||
|
||
ctx, cancel := context.WithTimeout(ctx, p.timeout) | ||
defer cancel() | ||
|
||
start := time.Now() | ||
result, _, err := p.api.Series(ctx, matches, start.Add(time.Minute*-5), start) | ||
duration := time.Since(start) | ||
log.Debug(). | ||
Str("uri", p.uri). | ||
Strs("matches", matches). | ||
Str("duration", HumanizeDuration(duration)). | ||
Msg("Query completed") | ||
if err != nil { | ||
log.Error().Err(err). | ||
Str("uri", p.uri). | ||
Strs("matches", matches). | ||
Msg("Query failed") | ||
return nil, err | ||
} | ||
|
||
log.Debug().Str("uri", p.uri).Strs("matches", matches).Int("series", len(result)).Msg("Parsed response") | ||
|
||
log.Debug().Str("key", lockKey).Str("uri", p.uri).Msg("Query cache miss") | ||
p.cache.Add(lockKey, result) | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package promapi_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/common/model" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/cloudflare/pint/internal/promapi" | ||
) | ||
|
||
func TestSeries(t *testing.T) { | ||
done := sync.Map{} | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
match := r.URL.Query()[("match[]")] | ||
|
||
switch strings.Join(match, ", ") { | ||
case "empty": | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, _ = w.Write([]byte(`{ | ||
"status": "success", | ||
"data": [] | ||
}`)) | ||
case "single": | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, _ = w.Write([]byte(`{ | ||
"status": "success", | ||
"data": [{"__name__":"single", "foo": "bar"}] | ||
}`)) | ||
case "two": | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, _ = w.Write([]byte(`{ | ||
"status": "success", | ||
"data": [ | ||
{"__name__":"two", "foo": "bar"}, | ||
{"__name__":"two", "foo": "baz"} | ||
] | ||
}`)) | ||
case "single, two": | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, _ = w.Write([]byte(`{ | ||
"status": "success", | ||
"data": [ | ||
{"__name__":"single", "foo": "bar"}, | ||
{"__name__":"two", "foo": "bar"}, | ||
{"__name__":"two", "foo": "baz"} | ||
] | ||
}`)) | ||
case "timeout": | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "application/json") | ||
time.Sleep(time.Second) | ||
_, _ = w.Write([]byte(`{ | ||
"status":"success", | ||
"data":{ | ||
"resultType":"vector", | ||
"result":[] | ||
} | ||
}`)) | ||
case "once": | ||
if _, wasDone := done.Load(match); wasDone { | ||
w.WriteHeader(500) | ||
_, _ = w.Write([]byte("query already requested\n")) | ||
return | ||
} | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, _ = w.Write([]byte(`{ | ||
"status": "success", | ||
"data": [ | ||
{"__name__":"once", "foo": "bar"}, | ||
{"__name__":"once", "foo": "baz"} | ||
] | ||
}`)) | ||
done.Store(match, true) | ||
default: | ||
w.WriteHeader(400) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, _ = w.Write([]byte(`{ | ||
"status":"error", | ||
"errorType":"bad_data", | ||
"error":"unhandled query" | ||
}`)) | ||
} | ||
})) | ||
defer srv.Close() | ||
|
||
type testCaseT struct { | ||
matches []string | ||
timeout time.Duration | ||
series []model.LabelSet | ||
err *regexp.Regexp | ||
runs int | ||
} | ||
|
||
testCases := []testCaseT{ | ||
{ | ||
matches: []string{"empty"}, | ||
timeout: time.Second, | ||
series: []model.LabelSet{}, | ||
runs: 5, | ||
}, | ||
{ | ||
matches: []string{"single"}, | ||
timeout: time.Second, | ||
series: []model.LabelSet{ | ||
{model.MetricNameLabel: "single", "foo": "bar"}, | ||
}, | ||
runs: 5, | ||
}, | ||
{ | ||
matches: []string{"two"}, | ||
timeout: time.Second, | ||
series: []model.LabelSet{ | ||
{model.MetricNameLabel: "two", "foo": "bar"}, | ||
{model.MetricNameLabel: "two", "foo": "baz"}, | ||
}, | ||
runs: 5, | ||
}, | ||
{ | ||
matches: []string{"single", "two"}, | ||
timeout: time.Second, | ||
series: []model.LabelSet{ | ||
{model.MetricNameLabel: "single", "foo": "bar"}, | ||
{model.MetricNameLabel: "two", "foo": "bar"}, | ||
{model.MetricNameLabel: "two", "foo": "baz"}, | ||
}, | ||
runs: 5, | ||
}, | ||
{ | ||
matches: []string{"timeout"}, | ||
timeout: time.Millisecond * 50, | ||
series: []model.LabelSet{}, | ||
runs: 5, | ||
err: regexp.MustCompile(`Get "http.+\/api\/v1\/series\?end=.+&match%5B%5D=timeout&start=.+": context deadline exceeded`), | ||
}, | ||
{ | ||
matches: []string{"error"}, | ||
timeout: time.Second, | ||
series: []model.LabelSet{}, | ||
runs: 5, | ||
err: regexp.MustCompile("unhandled query"), | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(strings.Join(tc.matches, ","), func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
prom := promapi.NewPrometheus("test", srv.URL, tc.timeout) | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(tc.runs) | ||
for i := 1; i <= tc.runs; i++ { | ||
go func() { | ||
m, err := prom.Series(context.Background(), tc.matches) | ||
if tc.err != nil { | ||
assert.Error(err) | ||
assert.True(tc.err.MatchString(err.Error())) | ||
} else { | ||
assert.NoError(err) | ||
} | ||
if m != nil { | ||
assert.Equal(tc.series, m) | ||
} | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
}) | ||
} | ||
} |