Skip to content

Commit

Permalink
fix(logql-compliance-tester): wait until instance gets some data
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Apr 18, 2024
1 parent 0e9c2a1 commit 445e781
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
39 changes: 33 additions & 6 deletions cmd/logql-compliance-tester/lokiapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,44 @@ func newLokiAPI(ctx context.Context, cfg lokicompliance.TargetConfig) (lokicompl
}

func waitForLoki(ctx context.Context, c *lokiapi.Client, cfg lokicompliance.TargetConfig) error {
check := func(ctx context.Context) error {
q := cfg.ReadyQuery
if q == "" {
q = `{job="varlogs"}`
}

resp, err := c.Query(ctx, lokiapi.QueryParams{
Query: q,
})
if err != nil {
if cerr := ctx.Err(); cerr != nil {
return backoff.Permanent(cerr)
}
return err
}

streams, ok := resp.Data.GetStreamsResult()
if !ok {
// Ready query should be exactly a log query.
err := errors.Errorf("unexpected result type %q", resp.Data.Type)
return backoff.Permanent(err)
}

for _, s := range streams.Result {
if len(s.Values) > 0 {
return nil
}
}
return errors.New("empty result")
}

var (
b = backoff.NewConstantBackOff(5 * time.Second)
log = zctx.From(ctx)
)
if err := backoff.RetryNotify(
func() error {
_, err := c.Labels(ctx, lokiapi.LabelsParams{})
if cerr := ctx.Err(); cerr != nil {
return backoff.Permanent(cerr)
}
return err
return check(ctx)
},
b,
func(err error, d time.Duration) {
Expand All @@ -46,6 +73,6 @@ func waitForLoki(ctx context.Context, c *lokiapi.Client, cfg lokicompliance.Targ
); err != nil {
return err
}
log.Info("Loki is ready", zap.String("target", cfg.QueryURL))
log.Info("Target is ready", zap.String("target", cfg.QueryURL))
return nil
}
28 changes: 27 additions & 1 deletion cmd/logql-compliance-tester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/cheggaaa/pb/v3"
"github.com/fatih/color"
"github.com/go-faster/errors"
"github.com/go-faster/sdk/zctx"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

"github.com/go-faster/oteldb/internal/lokicompliance"
Expand Down Expand Up @@ -113,17 +115,28 @@ func run(ctx context.Context) error {
rangeDuration = flag.Duration("range", 10*time.Minute, "The duration of the query range.")
stepDuration = flag.Duration("step", 10*time.Second, "The step of the query.")

wait = flag.Duration("wait", time.Minute, "The amonunt of time to wait until storages are filled up.")

outputFile = flag.String("output-file", "", "Path to output file")
outputFormat = flag.String("output-format", "text", "The comparison output format. Valid values: [json]")
outputPassing = flag.Bool("output-passing", false, "Whether to also include passing test cases in the output.")
minimumPercentage = flag.Float64("target", math.NaN(), "Minimum compliance percentage")
)
flag.Parse()

flag.Parse()
if format := *outputFormat; format != "json" {
return errors.Errorf("unknown output format %q", format)
}

log, err := zap.NewDevelopment()
if err != nil {
return errors.Wrap(err, "create logger")
}
defer func() {
_ = log.Sync()
}()
ctx = zctx.Base(ctx, log)

cfg, err := lokicompliance.LoadFromFiles(configFile)
if err != nil {
return errors.Wrap(err, "loading configuration file")
Expand Down Expand Up @@ -153,6 +166,19 @@ func run(ctx context.Context) error {
return errors.Wrap(err, "expand test cases")
}

{
log.Info("Waiting for data", zap.Duration("wait", *wait))

waitTimer := time.NewTimer(*wait)
defer waitTimer.Stop()

select {
case <-waitTimer.C:
case <-ctx.Done():
return ctx.Err()
}
}

var (
results = make([]*lokicompliance.Result, len(testCases))
progressBar = pb.StartNew(len(results))
Expand Down
4 changes: 3 additions & 1 deletion internal/lokicompliance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type QueryParameters struct {

// TargetConfig represents the configuration of a single Prometheus API endpoint.
type TargetConfig struct {
QueryURL string `yaml:"query_url"`
// ReadyQuery is a log query to check instance readiness.
ReadyQuery string `yaml:"ready_query"`
QueryURL string `yaml:"query_url"`
}

// LoadFromFiles parses the given YAML files into a Config.
Expand Down

0 comments on commit 445e781

Please sign in to comment.