in verify-evaluate-cloud-metrics/main.go [174:234]
func errorConditionTriggered(ctx context.Context, client *monitoring.QueryClient, refreshCount int, query string) (bool, error) {
req := &monitoringpb.QueryTimeSeriesRequest{
Name: fmt.Sprintf("projects/%s", project),
Query: query,
}
it := client.QueryTimeSeries(ctx, req)
fmt.Printf("querying the time series, refresh count: %d\n", refreshCount)
for {
resp, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return false, fmt.Errorf("could not read time series value: %w", err)
}
// The sliding window calculation are based on the points of a singular time series.
startTimeOfErrorCondition := time.Time{}
endTimeOfErrorCondition := time.Time{}
var dataPoints []*monitoringpb.TimeSeriesData_PointData
for _, p := range resp.GetPointData() {
errorRatio := p.GetValues()[0].GetDoubleValue() * 100
fmt.Printf("error ratio: %f\n", errorRatio)
fmt.Printf("Start time: %v\n", p.GetTimeInterval().StartTime.AsTime())
fmt.Printf("End time: %v\n", p.GetTimeInterval().EndTime.AsTime())
if calculateDuration(startTimeOfErrorCondition, endTimeOfErrorCondition) >= triggerDuration {
// We check to see if the sliding windows that we have set from previous iterations exceed the trigger duration.
// If it has, then we stop reading point data.
break
}
// Time series list data points from newest data to oldest data.
if len(p.GetValues()) != 1 {
// Assuming that the point data is a ratio.
return false, fmt.Errorf("expected 1 rate value for the total interval, instead got: %d", len(p.GetValues()))
}
if errorRatio := p.GetValues()[0].GetDoubleValue() * 100; errorRatio >= maxErrorPercentage {
if endTimeOfErrorCondition.IsZero() {
// initialization
endTimeOfErrorCondition = p.GetTimeInterval().EndTime.AsTime()
}
// Always replace the start as we iterate; it gets earlier and earlier.
dataPoints = append([]*monitoringpb.TimeSeriesData_PointData{p}, dataPoints...)
startTimeOfErrorCondition = p.GetTimeInterval().StartTime.AsTime()
} else {
// We found a sliding window which does not violate percentage.
startTimeOfErrorCondition = time.Time{}
endTimeOfErrorCondition = time.Time{}
dataPoints = nil // reset the points
}
}
// We check to see if the sliding windows that we have set from previous iterations exceed the trigger duration.
if errorDuration := calculateDuration(startTimeOfErrorCondition, endTimeOfErrorCondition); errorDuration >= triggerDuration {
fmt.Printf("found duration in which max error percentage %f exceeded trigger duration, duration condition triggered for: %v\n", maxErrorPercentage, errorDuration)
fmt.Printf("data: %v\n", dataPoints)
return true, nil
}
}
return false, nil
}