Chore: Add more logs and tracing to hysteresis flows (#90369)

This commit is contained in:
Yuri Tseretyan 2024-07-15 13:38:20 -04:00 committed by GitHub
parent 3c95f0d2a5
commit 9c05b30489
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
"go.opentelemetry.io/otel/attribute"
"github.com/grafana/grafana/pkg/expr/mathexp"
"github.com/grafana/grafana/pkg/infra/tracing"
@ -34,12 +35,18 @@ func (h *HysteresisCommand) NeedsVars() []string {
func (h *HysteresisCommand) Execute(ctx context.Context, now time.Time, vars mathexp.Vars, tracer tracing.Tracer) (mathexp.Results, error) {
results := vars[h.ReferenceVar]
logger := logger.FromContext(ctx)
traceCtx, span := tracer.Start(ctx, "SSE.ExecuteHysteresis")
span.SetAttributes(attribute.Int("previousLoadedDimensions", len(h.LoadedDimensions)))
span.SetAttributes(attribute.Int("totalDimensions", len(results.Values)))
defer span.End()
// shortcut for NoData
if results.IsNoData() {
return mathexp.Results{Values: mathexp.Values{mathexp.NewNoData()}}, nil
}
if h.LoadedDimensions == nil || len(h.LoadedDimensions) == 0 {
return h.LoadingThresholdFunc.Execute(ctx, now, vars, tracer)
return h.LoadingThresholdFunc.Execute(traceCtx, now, vars, tracer)
}
var loadedVals, unloadedVals mathexp.Values
for _, value := range results.Values {
@ -51,11 +58,14 @@ func (h *HysteresisCommand) Execute(ctx context.Context, now time.Time, vars mat
}
}
span.SetAttributes(attribute.Int("matchedLoadedDimensions", len(loadedVals)))
logger.Debug("Evaluating thresholds", "unloadingThresholdDimensions", len(loadedVals), "loadingThresholdDimensions", len(unloadedVals))
if len(loadedVals) == 0 { // if all values are unloaded
return h.LoadingThresholdFunc.Execute(ctx, now, vars, tracer)
return h.LoadingThresholdFunc.Execute(traceCtx, now, vars, tracer)
}
if len(unloadedVals) == 0 { // if all values are loaded
return h.UnloadingThresholdFunc.Execute(ctx, now, vars, tracer)
return h.UnloadingThresholdFunc.Execute(traceCtx, now, vars, tracer)
}
defer func() {
@ -64,12 +74,12 @@ func (h *HysteresisCommand) Execute(ctx context.Context, now time.Time, vars mat
}()
vars[h.ReferenceVar] = mathexp.Results{Values: unloadedVals}
loadingResults, err := h.LoadingThresholdFunc.Execute(ctx, now, vars, tracer)
loadingResults, err := h.LoadingThresholdFunc.Execute(traceCtx, now, vars, tracer)
if err != nil {
return mathexp.Results{}, fmt.Errorf("failed to execute loading threshold: %w", err)
}
vars[h.ReferenceVar] = mathexp.Results{Values: loadedVals}
unloadingResults, err := h.UnloadingThresholdFunc.Execute(ctx, now, vars, tracer)
unloadingResults, err := h.UnloadingThresholdFunc.Execute(traceCtx, now, vars, tracer)
if err != nil {
return mathexp.Results{}, fmt.Errorf("failed to execute unloading threshold: %w", err)
}

View File

@ -387,8 +387,9 @@ func getExprRequest(ctx EvaluationContext, condition models.Condition, dsCacheSe
return nil, fmt.Errorf("recovery threshold '%s' is only allowed to be the alert condition", q.RefID)
}
if reader != nil {
logger.FromContext(ctx.Ctx).Debug("Detected hysteresis threshold command. Populating with the results")
err = q.PatchHysteresisExpression(reader.Read())
active := reader.Read()
logger.FromContext(ctx.Ctx).Debug("Detected hysteresis threshold command. Populating with the results", "items", len(active))
err = q.PatchHysteresisExpression(active)
if err != nil {
return nil, fmt.Errorf("failed to amend hysteresis command '%s': %w", q.RefID, err)
}