Skip to content

Commit

Permalink
fixup! Expose stats' relevant threshold expression symbols publicly
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Feb 3, 2022
1 parent d5be0cd commit 2aeebfc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
12 changes: 1 addition & 11 deletions stats/thresholds.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,9 @@ func newThreshold(src string, abortOnFail bool, gracePeriod types.NullDuration)
}

func (t *Threshold) runNoTaint(sinks map[string]float64) (bool, error) {
// Because aggregation method can either be a static keyword ("count", "rate", etc...),
// or a parametric expression ("p(somefloatingpointvalue)"), we need to handle this
// case specifically. If we encounter the percentile aggregation method token,
// we recompute the whole "p(value)" expression in order to look for it in the
// sinks.
sinkKey := t.Parsed.AggregationMethod
if t.Parsed.AggregationMethod == TokenPercentile {
sinkKey = fmt.Sprintf("%s(%g)", TokenPercentile, t.Parsed.AggregationValue.Float64)
}

// Extract the sink value for the aggregation method used in the threshold
// expression
lhs, ok := sinks[sinkKey]
lhs, ok := sinks[t.Parsed.SinkKey()]
if !ok {
return false, fmt.Errorf("unable to apply threshold %s over metrics; reason: "+
"no metric supporting the %s aggregation method found",
Expand Down
22 changes: 22 additions & 0 deletions stats/thresholds_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ func NewThresholdExpressionFrom(input string) (*ThresholdExpression, error) {
return parseThresholdExpression(input)
}

// SinkKey computes the key used to index a ThresholdExpression in the engine's sinks.
//
// During execution, the engine "sinks" metrics into a internal mapping, so that
// thresholds can be tried against them. This method is a helper to normalize the
// sink the threshold expression should be applied to.
//
// Because a theshold expression's aggregation method can either be
// a static keyword ("count", "rate", etc...), or a parametric
// expression ("p(somefloatingpointvalue)"), we need to handle this
// case specifically. If we encounter the percentile aggregation method token,
// we recompute the whole "p(value)" expression in order to look for it in the
// sinks.
func (te *ThresholdExpression) SinkKey() string {
//
sinkKey := te.AggregationMethod
if te.AggregationMethod == TokenPercentile {
sinkKey = fmt.Sprintf("%s(%g)", TokenPercentile, te.AggregationValue.Float64)
}

return sinkKey
}

// parseThresholdAssertion parses a threshold condition expression,
// as defined in a JS script (for instance p(95)<1000), into a thresholdExpression
// instance.
Expand Down

0 comments on commit 2aeebfc

Please sign in to comment.