From 6b4330bb9cbd5ab3d1f0e8f6f8733089d52601c2 Mon Sep 17 00:00:00 2001 From: Ashish Gaurav Date: Wed, 5 Apr 2023 10:26:51 +0530 Subject: [PATCH] add support to override email msg body & connection payload Took 2 hours 20 minutes --- libs/monitor.go | 16 +++--- libs/sumologic/native.go | 51 ++++++++++++------- libs/sumologic/templates/slo-monitors.tf.gotf | 12 +++++ .../terraform/sumologic/monitor.tf.gotf | 22 ++++++-- samples/sumologic/logs/drift-calculation.yaml | 22 ++++++++ samples/sumologic/v1/logs-req-calendar.yaml | 1 - .../v1/notification-target/email.yaml | 2 +- .../v1/notification-target/pagerduty.yaml | 16 ++++++ 8 files changed, 111 insertions(+), 31 deletions(-) diff --git a/libs/monitor.go b/libs/monitor.go index 7f0706b..eaae5f1 100644 --- a/libs/monitor.go +++ b/libs/monitor.go @@ -27,13 +27,15 @@ func giveDefaultTriggers() []string { } type Notification struct { - TriggerFor []string `yaml:"triggerFor,omitempty"` - ConnectionType string `yaml:"connectionType"` - ConnectionID string `yaml:"connectionID,omitempty"` - Subject string `yaml:"subject,omitempty"` - Recipients []string `yaml:"recipients,omitempty"` - MessageBody string `yaml:"messageBody,omitempty"` - TimeZone string `yaml:"timeZone,omitempty"` + TriggerFor []string `yaml:"triggerFor,omitempty"` + ConnectionType string `yaml:"connectionType"` + ConnectionID string `yaml:"connectionID,omitempty"` + Subject string `yaml:"subject,omitempty"` + Recipients []string `yaml:"recipients,omitempty"` + MessageBody string `yaml:"messageBody,omitempty"` + TimeZone string `yaml:"timeZone,omitempty"` + PayloadOverride string `yaml:"payload_override,omitempty"` + ResolutionPayloadOverride string `yaml:"resolution_payload_override,omitempty"` } type SLOObjective struct { diff --git a/libs/sumologic/native.go b/libs/sumologic/native.go index 89c7506..7792e91 100644 --- a/libs/sumologic/native.go +++ b/libs/sumologic/native.go @@ -18,19 +18,21 @@ const ( ) const ( - AnnotationMonitorFolderID = "sumologic/monitor-folder-id" - AnnotationSLOFolderID = "sumologic/slo-folder-id" - AnnotationTFResourceName = "sumologic/tf-resource-name" - AnnotationSignalType = "sumologic/signal-type" - AnnotationEmailRecipients = "recipients" - AnnotationEmailSubject = "subject" - AnnotationEmailBody = "body" - AnnotationEmailTimeZone = "timezone" - AnnotationRunForTriggers = "run_for_triggers" - AnnotationConnectionID = "connection_id" - AnnotationConnectionType = "connection_type" - AlertConditionTypeBurnRate = "burnrate" - AlertConditionTypeSLI = "sli" + AnnotationMonitorFolderID = "sumologic/monitor-folder-id" + AnnotationSLOFolderID = "sumologic/slo-folder-id" + AnnotationTFResourceName = "sumologic/tf-resource-name" + AnnotationSignalType = "sumologic/signal-type" + AnnotationEmailRecipients = "recipients" + AnnotationEmailSubject = "subject" + AnnotationEmailBody = "message_body" + AnnotationEmailTimeZone = "timezone" + AnnotationRunForTriggers = "run_for_triggers" + AnnotationConnectionID = "connection_id" + AnnotationConnectionType = "connection_type" + AnnotationPayloadOverride = "payload_override" + AnnotationResolutionPayloadOverride = "resolution_payload_override" + AlertConditionTypeBurnRate = "burnrate" + AlertConditionTypeSLI = "sli" ) const ( @@ -66,9 +68,11 @@ type NotifyEmail struct { } type NotifyConnection struct { - ID string - Type string - RunForTriggers []string + ID string + Type string + RunForTriggers []string + PayloadOverride string + ResolutionPayloadOverride string } type SLO struct { @@ -325,15 +329,24 @@ func giveNotifyTargets(ap oslo.AlertPolicy, notifyMap map[string]oslo.AlertNotif TimeZone: annotations[AnnotationEmailTimeZone], RunForTriggers: strings.Split(annotations[AnnotationRunForTriggers], ","), } + if notifyMail.TimeZone == "" { + notifyMail.TimeZone = "PST" + } + + if notifyMail.Body == "" { + notifyMail.Body = "Triggered {{TriggerType}} Alert on {{Name}}: {{QueryURL}}" + } emailTargets = append(emailTargets, notifyMail) } if strings.ToLower(target.Spec.Target) == "connection" { notifyConn := NotifyConnection{ - Type: annotations[AnnotationConnectionType], - ID: annotations[AnnotationConnectionID], - RunForTriggers: strings.Split(annotations[AnnotationRunForTriggers], ","), + Type: annotations[AnnotationConnectionType], + ID: annotations[AnnotationConnectionID], + RunForTriggers: strings.Split(annotations[AnnotationRunForTriggers], ","), + PayloadOverride: annotations[AnnotationPayloadOverride], + ResolutionPayloadOverride: annotations[AnnotationResolutionPayloadOverride], } connTargets = append(connTargets, notifyConn) } diff --git a/libs/sumologic/templates/slo-monitors.tf.gotf b/libs/sumologic/templates/slo-monitors.tf.gotf index 6486c4e..d6d0876 100644 --- a/libs/sumologic/templates/slo-monitors.tf.gotf +++ b/libs/sumologic/templates/slo-monitors.tf.gotf @@ -52,7 +52,9 @@ resource "sumologic_monitor" "{{ $m.TFResourceName }}" { ] subject = "{{$n.Subject}}" time_zone = "{{$n.TimeZone}}" + {{- if ne $n.Body ""}} message_body = "{{$n.Body}}" + {{- end}} } run_for_trigger_types = [ {{- range $t := $n.RunForTriggers}} @@ -67,6 +69,16 @@ resource "sumologic_monitor" "{{ $m.TFResourceName }}" { notification { connection_type = "{{$n.Type}}" connection_id = "{{$n.ID}}" + {{- if ne $n.PayloadOverride ""}} + payload_override = <