// run starts an analysis which runs the query at the given interval, until the context is done.
// It returns an error when the number of failures exceeds the the failureLimit.
run starts an analysis which runs the query at the given interval, until the context is done.
It returns an error when the number of failures exceeds the the failureLimit.
// run starts an analysis which runs the query at the given interval until the context is done . // It returns an error when the number of failures exceeds the the failureLimit .
func (a *analyzer) run(ctx context.Context) error {
	ticker := time.NewTicker(a.interval)
	defer ticker.Stop()
	failureCount := 0
	for {
		select {
		case <-ticker.C:
			expected, reason, err := a.evaluate(ctx, a.query)
			if errors.Is(err, context.DeadlineExceeded) && ctx.Err() == context.DeadlineExceeded {
				return nil
			}
			if errors.Is(err, metrics.ErrNoDataFound) && a.skipOnNoData {
				a.logPersister.Infof("[%s] The query result evaluation was skipped because \"skipOnNoData\" is true even though no data returned. Reason: %v. Performed query: %q", a.id, err, a.query)
				continue
			}
			if err != nil {
				reason = fmt.Sprintf("failed to run query: %s", err.Error())
			}
			if expected {
				a.logPersister.Successf("[%s] The query result is expected one. Reason: %s. Performed query: %q", a.id, reason, a.query)
				continue
			}
			a.logPersister.Errorf("[%s] The query result is unexpected. Reason: %s. Performed query: %q", a.id, reason, a.query)
			failureCount++
			if failureCount > a.failureLimit {
				return fmt.Errorf("analysis '%s' failed because the failure number exceeded the failure limit (%d)", a.id, a.failureLimit)
			}
		case <-ctx.Done():
			return nil
		}
	}
}
func ( a * analyzer ) run ( ctx context . Context ) error { ticker := time . NewTicker ( a . interval ) defer ticker . Stop ( ) failureCount := 0 for { select { case <- ticker . C : expected , reason , err := a . evaluate ( ctx , a . query ) if errors . Is ( err , context . DeadlineExceeded ) && ctx . Err ( ) == context . DeadlineExceeded { return nil } if errors . Is ( err , metrics . ErrNoDataFound ) && a . skipOnNoData { a . logPersister . Infof ( " \" \" " , a . id , err , a . query ) continue } if err != nil { reason = fmt . Sprintf ( " " , err . Error ( ) ) } if expected { a . logPersister . Successf ( " " , a . id , reason , a . query ) continue } a . logPersister . Errorf ( " " , a . id , reason , a . query ) failureCount ++ if failureCount > a . failureLimit { return fmt . Errorf ( " " , a . id , a . failureLimit ) } case <- ctx . Done ( ) : return nil } } }
run starts an analysis which runs the query at the given interval, until the context is done.
run starts an analysis which runs the query at the given interval until the context is done .
// Ignore parent's context deadline exceeded error, and return immediately.