Skip to content

Commit

Permalink
Merge pull request #64 from OpenSLO/sumo-agaurav
Browse files Browse the repository at this point in the history
[sumologic] add support to override email msg body & connection payload
  • Loading branch information
agaurav authored Apr 5, 2023
2 parents 2b3e27f + 6b4330b commit 690e70f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 31 deletions.
16 changes: 9 additions & 7 deletions libs/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 32 additions & 19 deletions libs/sumologic/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions libs/sumologic/templates/slo-monitors.tf.gotf
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -67,6 +69,16 @@ resource "sumologic_monitor" "{{ $m.TFResourceName }}" {
notification {
connection_type = "{{$n.Type}}"
connection_id = "{{$n.ID}}"
{{- if ne $n.PayloadOverride ""}}
payload_override = <<JSON
{{$n.PayloadOverride}}
JSON
{{- end}}
{{- if ne $n.ResolutionPayloadOverride ""}}
resolution_payload_override = <<JSON
{{$n.ResolutionPayloadOverride}}
JSON
{{- end}}
}
run_for_trigger_types = [
{{- range $t := $n.RunForTriggers}}
Expand Down
22 changes: 19 additions & 3 deletions libs/templates/terraform/sumologic/monitor.tf.gotf
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,27 @@ QUERY
{{end}}
]
subject = "SLO breach alert: {{$.Service}} - {{$.Name}}"
time_zone = {{if ne $n.TimeZone "" }}"{{$n.TimeZone}}"{{else}}"PST"{{end}}
time_zone = {{ if ne $n.TimeZone "" }} "{{$n.TimeZone}}" {{ else }} {{"PST"}} {{ end }}
{{- if ne $n.MessageBody "" }}
message_body = <<MESSAGE
{{$n.MessageBody}}
MESSAGE
{{ else }}
message_body = "{{`{{Description}} \n Result : {{ResultsJson}}\n Alert: {{AlertResponseURL}}`}}"
{{else}}
{{- end }}
{{ else }}
connection_id = "{{$n.ConnectionID}}"
{{end}}
{{- if ne $n.PayloadOverride ""}}
payload_override = <<JSON
{{$n.PayloadOverride}}
JSON
{{- end}}
{{- if ne $n.ResolutionPayloadOverride ""}}
resolution_payload_override = <<JSON
{{$n.ResolutionPayloadOverride}}
JSON
{{- end}}
{{- end}}
}
run_for_trigger_types = [
{{range $t := $n.TriggerFor}}
Expand Down
22 changes: 22 additions & 0 deletions samples/sumologic/logs/drift-calculation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,31 @@ alerts:
longLimit: 14
notifications:
- connectionType: 'Email'
messageBody: "Triggered {{TriggerType}} Alert on {{Name}}: {{QueryURL}}"
recipients:
- '[email protected]'
timeZone: 'PST'
triggerFor:
- Warning
- ResolvedWarning
- connectionType: 'PagerDuty'
connectionID: '000000000001ABCD' # id of pagerduty connection created in Sumo Logic
triggerFor:
- Warning
- ResolvedWarning
payload_override: |
{
"service_key": "your_pagerduty_api_integration_key",
"event_type": "trigger",
"description": "Alert: Triggered {{TriggerType}} for Monitor {{Name}}",
"client": "Sumo Logic",
"client_url": "{{QueryUrl}}"
}
resolution_payload_override: |
{
"service_key": "your_pagerduty_api_integration_key",
"event_type": "resolve",
"description": "Alert: Resolved {{TriggerType}} for Monitor {{Name}}",
"client": "Sumo Logic",
"client_url": "{{QueryUrl}}"
}
1 change: 0 additions & 1 deletion samples/sumologic/v1/logs-req-calendar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
displayName: DriftGen SLO Calendar
annotations:
sumologic/signal-type: Latency
sumologic/slo-folder-id: 0000000000000ABC
spec:
description: test slo created from slogen
service: tsat
Expand Down
2 changes: 1 addition & 1 deletion samples/sumologic/v1/notification-target/email.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ metadata:
annotations:
recipients: "[email protected]"
subject: "Monitor Alert: {{TriggerType}} on {{Name}}"
message_body: "Triggered {{TriggerType}} Alert on {{Name}}: {{QueryURL}}"
message_body: "Triggered {{TriggerType}} Alert on the {{Name}}: {{QueryURL}}"
time_zone: "PST"
run_for_triggers: "Critical,ResolvedCritical"
16 changes: 16 additions & 0 deletions samples/sumologic/v1/notification-target/pagerduty.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@ metadata:
connection_type : "PagerDuty"
connection_id: "000000000000ABCD"
run_for_triggers: "Critical,ResolvedCritical"
payload_override: |
{
"service_key": "your_pagerduty_api_integration_key",
"event_type": "trigger",
"description": "Alert: Triggered {{TriggerType}} for Monitor {{Name}}",
"client": "Sumo Logic",
"client_url": "{{QueryUrl}}"
}
resolution_payload_override: |
{
"service_key": "your_pagerduty_api_integration_key",
"event_type": "resolve",
"description": "Alert: Resolved {{TriggerType}} for Monitor {{Name}}",
"client": "Sumo Logic",
"client_url": "{{QueryUrl}}"
}

0 comments on commit 690e70f

Please sign in to comment.