diff --git a/notification/flux/ast.go b/notification/flux/ast.go index b47b963a66..6aba966743 100644 --- a/notification/flux/ast.go +++ b/notification/flux/ast.go @@ -126,6 +126,17 @@ func Function(params []*ast.Property, b ast.Expression) *ast.FunctionExpression } } +// FuncBlock takes a series of statements and produces a function. +func FuncBlock(params []*ast.Property, stms ...ast.Statement) *ast.FunctionExpression { + b := &ast.Block{ + Body: stms, + } + return &ast.FunctionExpression{ + Params: params, + Body: b, + } +} + // String returns an *ast.StringLiteral of s. func String(s string) *ast.StringLiteral { return &ast.StringLiteral{ @@ -164,6 +175,13 @@ func Float(f float64) *ast.FloatLiteral { } } +// Integer returns an *ast.IntegerLiteral of i. +func Integer(i int64) *ast.IntegerLiteral { + return &ast.IntegerLiteral{ + Value: i, + } +} + // Negative returns *ast.UnaryExpression for -(e). func Negative(e ast.Expression) *ast.UnaryExpression { return &ast.UnaryExpression{ diff --git a/notification/rule/http.go b/notification/rule/http.go index 9582192f23..0ec5f58ff1 100644 --- a/notification/rule/http.go +++ b/notification/rule/http.go @@ -118,7 +118,12 @@ func (s *HTTP) generateFluxASTNotifyPipe() ast.Statement { headers, flux.Property("data", endpointBody), } - endpointFn := flux.Function(flux.FunctionParams("r"), flux.Object(endpointProps...)) + endpointFn := flux.FuncBlock(flux.FunctionParams("r"), + s.generateBody(), + &ast.ReturnStatement{ + Argument: flux.Object(endpointProps...), + }, + ) props := []*ast.Property{} props = append(props, flux.Property("data", flux.Identifier("notification"))) @@ -130,6 +135,48 @@ func (s *HTTP) generateFluxASTNotifyPipe() ast.Statement { return flux.ExpressionStatement(flux.Pipe(flux.Identifier("all_statuses"), call)) } +func (s *HTTP) generateBody() ast.Statement { + props := []*ast.Property{ + flux.Dictionary( + "version", flux.Integer(1), + ), + flux.Dictionary( + "rule_name", flux.Member("notification", "_notification_rule_name"), + ), + flux.Dictionary( + "rule_id", flux.Member("notification", "_notification_rule_id"), + ), + flux.Dictionary( + "endpoint_name", flux.Member("notification", "_notification_endpoint_name"), + ), + flux.Dictionary( + "endpoint_id", flux.Member("notification", "_notification_endpoint_id"), + ), + flux.Dictionary( + "check_name", flux.Member("r", "_check_name"), + ), + flux.Dictionary( + "check_id", flux.Member("r", "_check_id"), + ), + flux.Dictionary( + "check_type", flux.Member("r", "_type"), + ), + flux.Dictionary( + "source_measurement", flux.Member("r", "_source_measurement"), + ), + flux.Dictionary( + "source_timestamp", flux.Member("r", "_source_timestamp"), + ), + flux.Dictionary( + "level", flux.Member("r", "_level"), + ), + flux.Dictionary( + "message", flux.Member("r", "_message"), + ), + } + return flux.DefineVariable("body", flux.Object(props...)) +} + type httpAlias HTTP // MarshalJSON implement json.Marshaler interface. diff --git a/notification/rule/http_test.go b/notification/rule/http_test.go index 59730df6cd..17805c8c80 100644 --- a/notification/rule/http_test.go +++ b/notification/rule/http_test.go @@ -36,8 +36,24 @@ all_statuses = crit (r._time > experimental.subDuration(from: now(), d: 1h))) all_statuses - |> monitor.notify(data: notification, endpoint: endpoint(mapFn: (r) => - ({headers: headers, data: json.encode(v: r)})))` + |> monitor.notify(data: notification, endpoint: endpoint(mapFn: (r) => { + body = { + "version": 1, + "rule_name": notification._notification_rule_name, + "rule_id": notification._notification_rule_id, + "endpoint_name": notification._notification_endpoint_name, + "endpoint_id": notification._notification_endpoint_id, + "check_name": r._check_name, + "check_id": r._check_id, + "check_type": r._type, + "source_measurement": r._source_measurement, + "source_timestamp": r._source_timestamp, + "level": r._level, + "message": r._message, + } + + return {headers: headers, data: json.encode(v: r)} + }))` s := &rule.HTTP{ Base: rule.Base{ @@ -100,9 +116,24 @@ all_statuses = crit (r._time > experimental.subDuration(from: now(), d: 1h))) all_statuses - |> monitor.notify(data: notification, endpoint: endpoint(mapFn: (r) => - ({headers: headers, data: json.encode(v: r)})))` + |> monitor.notify(data: notification, endpoint: endpoint(mapFn: (r) => { + body = { + "version": 1, + "rule_name": notification._notification_rule_name, + "rule_id": notification._notification_rule_id, + "endpoint_name": notification._notification_endpoint_name, + "endpoint_id": notification._notification_endpoint_id, + "check_name": r._check_name, + "check_id": r._check_id, + "check_type": r._type, + "source_measurement": r._source_measurement, + "source_timestamp": r._source_timestamp, + "level": r._level, + "message": r._message, + } + return {headers: headers, data: json.encode(v: r)} + }))` s := &rule.HTTP{ Base: rule.Base{ ID: 1, @@ -171,8 +202,24 @@ all_statuses = crit (r._time > experimental.subDuration(from: now(), d: 1h))) all_statuses - |> monitor.notify(data: notification, endpoint: endpoint(mapFn: (r) => - ({headers: headers, data: json.encode(v: r)})))` + |> monitor.notify(data: notification, endpoint: endpoint(mapFn: (r) => { + body = { + "version": 1, + "rule_name": notification._notification_rule_name, + "rule_id": notification._notification_rule_id, + "endpoint_name": notification._notification_endpoint_name, + "endpoint_id": notification._notification_endpoint_id, + "check_name": r._check_name, + "check_id": r._check_id, + "check_type": r._type, + "source_measurement": r._source_measurement, + "source_timestamp": r._source_timestamp, + "level": r._level, + "message": r._message, + } + + return {headers: headers, data: json.encode(v: r)} + }))` s := &rule.HTTP{ Base: rule.Base{