Skip to content

Commit

Permalink
Log Cel expressions on failures
Browse files Browse the repository at this point in the history
Signed-off-by: Juho Majasaari <[email protected]>
  • Loading branch information
juho9000 committed Dec 17, 2024
1 parent 4f7ae55 commit 704e04a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ func (sc *SafeConfig) ReloadConfig(confFile string, logger *slog.Logger) (err er
// CelProgram encapsulates a cel.Program and makes it YAML marshalable.
type CelProgram struct {
cel.Program
expression string
Expression string
}

// NewCelProgram creates a new CEL Program and returns an error if the
// passed-in CEL expression does not compile.
func NewCelProgram(s string) (CelProgram, error) {
program := CelProgram{
expression: s,
Expression: s,
}

env, err := cel.NewEnv(
Expand Down Expand Up @@ -199,8 +199,8 @@ func (c *CelProgram) UnmarshalYAML(unmarshal func(interface{}) error) error {

// MarshalYAML implements the yaml.Marshaler interface.
func (c CelProgram) MarshalYAML() (interface{}, error) {
if c.expression != "" {
return c.expression, nil
if c.Expression != "" {
return c.Expression, nil
}
return nil, nil
}
Expand Down
18 changes: 9 additions & 9 deletions prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ func matchRegularExpressions(reader io.Reader, httpConfig config.HTTPProbe, logg
return true
}

func matchCelExpressions(reader io.Reader, httpConfig config.HTTPProbe, logger *slog.Logger) bool {
func matchCelExpressions(ctx context.Context, reader io.Reader, httpConfig config.HTTPProbe, logger *slog.Logger) bool {
body, err := io.ReadAll(reader)
if err != nil {
logger.Error("msg", "Error reading HTTP body", "err", err)
logger.Error("Error reading HTTP body", "err", err)
return false
}

bodyJSON := make(map[string]interface{})
if err := json.Unmarshal(body, &bodyJSON); err != nil {
logger.Error("msg", "Error unmarshalling HTTP body", "err", err)
logger.Error("Error unmarshalling HTTP body", "err", err)
return false
}

Expand All @@ -86,31 +86,31 @@ func matchCelExpressions(reader io.Reader, httpConfig config.HTTPProbe, logger *
if httpConfig.FailIfBodyJSONMatchesCel != nil {
result, details, err := httpConfig.FailIfBodyJSONMatchesCel.ContextEval(ctx, evalPayload)
if err != nil {
logger.Error("msg", "Error evaluating CEL expression", "err", err)
logger.Error("Error evaluating CEL expression", "err", err)
return false
}
if result.Type() != cel.BoolType {
logger.Error("msg", "CEL evaluation result is not a boolean", "details", details)
logger.Error("CEL evaluation result is not a boolean", "details", details)
return false
}
if result.Type() == cel.BoolType && result.Value().(bool) {
logger.Error("msg", "Body matched CEL expression", "expression", httpConfig.FailIfBodyJSONMatchesCel)
logger.Error("Body matched CEL expression", "expression", httpConfig.FailIfBodyJSONMatchesCel.Expression)
return false
}
}

if httpConfig.FailIfBodyJSONNotMatchesCel != nil {
result, details, err := httpConfig.FailIfBodyJSONNotMatchesCel.ContextEval(ctx, evalPayload)
if err != nil {
logger.Error("msg", "Error evaluating CEL expression", "err", err)
logger.Error("Error evaluating CEL expression", "err", err)
return false
}
if result.Type() != cel.BoolType {
logger.Error("msg", "CEL evaluation result is not a boolean", "details", details)
logger.Error("CEL evaluation result is not a boolean", "details", details)
return false
}
if result.Type() == cel.BoolType && !result.Value().(bool) {
logger.Error("msg", "Body did not match CEL expression", "expression", httpConfig.FailIfBodyJSONNotMatchesCel)
logger.Error("Body did not match CEL expression", "expression", httpConfig.FailIfBodyJSONNotMatchesCel.Expression)
return false
}
}
Expand Down
4 changes: 2 additions & 2 deletions prober/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ func TestFailIfBodyMatchesCel(t *testing.T) {
registry := prometheus.NewRegistry()
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, FailIfBodyJSONMatchesCel: &testcase.cel}}, registry, log.NewNopLogger())
result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, FailIfBodyJSONMatchesCel: &testcase.cel}}, registry, promslog.NewNopLogger())
if testcase.expectedResult && !result {
t.Fatalf("CEL test failed unexpectedly, got %s", recorder.Body.String())
} else if !testcase.expectedResult && result {
Expand Down Expand Up @@ -1050,7 +1050,7 @@ func TestFailIfBodyNotMatchesCel(t *testing.T) {
registry := prometheus.NewRegistry()
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, FailIfBodyJSONNotMatchesCel: &testcase.cel}}, registry, log.NewNopLogger())
result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, FailIfBodyJSONNotMatchesCel: &testcase.cel}}, registry, promslog.NewNopLogger())
if testcase.expectedResult && !result {
t.Fatalf("CEL test failed unexpectedly, got %s", recorder.Body.String())
} else if !testcase.expectedResult && result {
Expand Down

0 comments on commit 704e04a

Please sign in to comment.