diff --git a/notification/flux/ast.go b/notification/flux/ast.go index 2bd9b7808f..4427014fdc 100644 --- a/notification/flux/ast.go +++ b/notification/flux/ast.go @@ -113,6 +113,16 @@ func Call(fn ast.Expression, args *ast.ObjectExpression) *ast.CallExpression { } } +// DirectCall returns a *ast.CallExpression that is a function call of fn with args. +func DirectCall(fn ast.Expression, args ast.Expression) *ast.CallExpression { + return &ast.CallExpression{ + Callee: fn, + Arguments: []ast.Expression{ + args, + }, + } +} + // ExpressionStatement returns an *ast.ExpressionStagement of e. func ExpressionStatement(e ast.Expression) *ast.ExpressionStatement { return &ast.ExpressionStatement{Expression: e} diff --git a/notification/rule/pagerduty.go b/notification/rule/pagerduty.go index 8a3a84ba08..1bcba1052c 100644 --- a/notification/rule/pagerduty.go +++ b/notification/rule/pagerduty.go @@ -114,7 +114,7 @@ func (s *PagerDuty) generateFluxASTNotifyPipe(url string) ast.Statement { // optional // string // name of the client sending the alert. - endpointProps = append(endpointProps, flux.Property("client", flux.Identifier("r._check_name"))) + endpointProps = append(endpointProps, flux.Property("client", flux.String("influxdata"))) // clientURL // optional @@ -132,33 +132,37 @@ func (s *PagerDuty) generateFluxASTNotifyPipe(url string) ast.Statement { // optional // string // Logical grouping of components of a service, for example app-stack - endpointProps = append(endpointProps, flux.Property("group", flux.Identifier("r._check_name"))) + endpointProps = append(endpointProps, flux.Property("group", flux.Member("r", "_source_measurement"))) // severity: // required // string // The perceived severity of the status the event is describing with respect to the affected system. This can be critical, error, warning or info. - // TODO: transfrom the influx names to pagerduty names - endpointProps = append(endpointProps, flux.Property("severity", flux.Identifier("r._level"))) + endpointProps = append(endpointProps, flux.Property("severity", severityFromLevel())) + + // event_action: + // required + // string trigger + // The type of event. Can be trigger, acknowledge or resolve. See Event Action. + endpointProps = append(endpointProps, flux.Property("eventAction", actionFromLevel())) // source: // required // string // The unique location of the affected system, preferably a hostname or FQDN - endpointProps = append(endpointProps, flux.Property("source", flux.Identifier("r._source_measurement"))) + endpointProps = append(endpointProps, flux.Property("source", flux.Member("r", "_notification_rule_name"))) // summary: // required // string // A brief text summary of the event, used to generate the summaries/titles of any associated alerts. The maximum permitted length of this property is 1024 characters. - endpointProps = append(endpointProps, flux.Property("summary", flux.Identifier("r._message"))) + endpointProps = append(endpointProps, flux.Property("summary", flux.Member("r", "_message"))) // timestamp: // optional // timestamp (rfc3339 milliseconds) // The time at which the emitting tool detected or generated the event. - // TODO: this should be r._status_timestamp - endpointProps = append(endpointProps, flux.Property("timestamp", flux.Identifier("r._status_timestamp"))) + endpointProps = append(endpointProps, flux.Property("timestamp", generateTime())) endpointFn := flux.Function(flux.FunctionParams("r"), flux.Object(endpointProps...)) @@ -171,3 +175,24 @@ func (s *PagerDuty) generateFluxASTNotifyPipe(url string) ast.Statement { return flux.ExpressionStatement(flux.Pipe(flux.Identifier("statuses"), call)) } + +func severityFromLevel() *ast.CallExpression { + return flux.DirectCall( + flux.Member("pagerduty", "severityFromLevel"), + flux.Member("r", "_level"), + ) +} + +func actionFromLevel() *ast.CallExpression { + return flux.DirectCall( + flux.Member("pagerduty", "actionFromLevel"), + flux.Member("r", "_level"), + ) +} + +func generateTime() *ast.CallExpression { + props := []*ast.Property{ + flux.Property("v", flux.Member("r", "_source_timestamp")), + } + return flux.Call(flux.Identifier("time"), flux.Object(props...)) +} diff --git a/notification/rule/pagerduty_test.go b/notification/rule/pagerduty_test.go index d99ec8e147..e490a32171 100644 --- a/notification/rule/pagerduty_test.go +++ b/notification/rule/pagerduty_test.go @@ -33,14 +33,15 @@ statuses |> monitor.notify(data: notification, endpoint: pagerduty_endpoint(mapFn: (r) => ({ routingKey: pagerduty_secret, - client: r._check_name, + client: "influxdata", clientURL: "http://localhost:7777", class: r._check_name, - group: r._check_name, - severity: r._level, - source: r._source_measurement, + group: r._source_measurement, + severity: pagerduty.severityFromLevel(r._level), + eventAction: pagerduty.actionFromLevel(r._level), + source: r._notification_rule_name, summary: r._message, - timestamp: r._status_timestamp, + timestamp: time(v: r._source_timestamp), })))` s := &rule.PagerDuty{