func evaluationResult()

in sharedlibraries/configurablemetrics/configurablemetrics.go [236:263]


func evaluationResult(ctx context.Context, res *cmpb.EvalResult, output Output) string {
	source := outputSource(output, res.GetOutputSource())

	switch res.GetEvalResultTypes().(type) {
	case *cmpb.EvalResult_ValueFromLiteral:
		return res.GetValueFromLiteral()
	case *cmpb.EvalResult_ValueFromOutput:
		return source
	case *cmpb.EvalResult_ValueFromRegex:
		pattern, err := regexp.Compile(res.GetValueFromRegex())
		if err != nil {
			log.CtxLogger(ctx).Warnw("Regular Expression failed to compile", "regexp", res.GetValueFromRegex(), "error", err)
			return ""
		}
		// Return the first capture group found in a regular expression match,
		// or the full match string if no capture groups are specified.
		match := pattern.FindStringSubmatch(source)
		if len(match) > 1 {
			return match[1]
		} else if len(match) > 0 {
			return match[0]
		}
		return ""
	default:
		log.CtxLogger(ctx).Debug("No evaluation result detected, defaulting to empty string.")
		return ""
	}
}