github.com/wormhole-foundation/wormhole-explorer/common@v0.0.0-20240604151348-09585b5b97c5/client/alert/alert.go (about) 1 package alert 2 3 import ( 4 "fmt" 5 6 opsgenieAlert "github.com/opsgenie/opsgenie-go-sdk-v2/alert" 7 ) 8 9 // Respoder struct to define alert routing notifications. 10 type Responder struct { 11 Id string 12 Type string 13 Name string 14 Username string 15 } 16 17 // Priority is the alert priority. 18 type Priority string 19 20 const ( 21 CRITICAL Priority = "CRITICAL" 22 HIGH Priority = "HIGH" 23 MODERATE Priority = "MODERATE" 24 LOW Priority = "LOW" 25 INFORMATIONAL Priority = "INFORMATIONAL" 26 ) 27 28 // Alert is the alert struct. 29 type Alert struct { 30 Message string 31 Alias string 32 Description string 33 Actions []string 34 Tags []string 35 Entity string 36 Priority Priority 37 Responder []Responder 38 VisibleTo []Responder 39 context AlertContext 40 } 41 42 // AlertContext contains the alert execution context. 43 type AlertContext struct { 44 Details map[string]string 45 Error error 46 Note string 47 } 48 49 // toOpsgenieResponder converts a Responder to an Opsgenie Responder. 50 func (a *Responder) toOpsgenieResponder() opsgenieAlert.Responder { 51 52 var responderType opsgenieAlert.ResponderType 53 switch a.Type { 54 case "user": 55 responderType = opsgenieAlert.UserResponder 56 case "team": 57 responderType = opsgenieAlert.TeamResponder 58 case "escalation": 59 responderType = opsgenieAlert.EscalationResponder 60 case "schedule": 61 responderType = opsgenieAlert.ScheduleResponder 62 } 63 64 opsgenieResponder := opsgenieAlert.Responder{ 65 Id: a.Id, 66 Type: responderType, 67 Name: a.Name, 68 Username: a.Username, 69 } 70 return opsgenieResponder 71 } 72 73 // toOpsgeniePriority converts a Priority to an Opsgenie Priority. 74 func (p Priority) toOpsgeniePriority() opsgenieAlert.Priority { 75 switch p { 76 case CRITICAL: 77 return opsgenieAlert.P1 78 case HIGH: 79 return opsgenieAlert.P2 80 case MODERATE: 81 return opsgenieAlert.P3 82 case LOW: 83 return opsgenieAlert.P4 84 case INFORMATIONAL: 85 return opsgenieAlert.P5 86 default: 87 return opsgenieAlert.P5 88 } 89 } 90 91 // toOpsgenieRequest converts an Alert to an Opsgenie CreateAlertRequest. 92 func (a Alert) toOpsgenieRequest() opsgenieAlert.CreateAlertRequest { 93 // convert priority to opsgenie priority. 94 priotity := a.Priority.toOpsgeniePriority() 95 96 // convert responders to opsgenie responders. 97 var responders []opsgenieAlert.Responder 98 for _, responder := range a.Responder { 99 responders = append(responders, responder.toOpsgenieResponder()) 100 } 101 102 // convert visibleTo to opsgenie responders. 103 var visibleTo []opsgenieAlert.Responder 104 for _, responder := range a.VisibleTo { 105 visibleTo = append(visibleTo, responder.toOpsgenieResponder()) 106 } 107 108 // add error details to opsgenie alert details data. 109 description := a.Description 110 if a.context.Error != nil { 111 description = fmt.Sprintf("%s\n%s", a.Description, a.context.Error.Error()) 112 } 113 114 return opsgenieAlert.CreateAlertRequest{ 115 Message: a.Message, 116 Alias: a.Alias, 117 Description: description, 118 Actions: a.Actions, 119 Tags: a.Tags, 120 Details: a.context.Details, 121 Entity: a.Entity, 122 Priority: priotity, 123 Note: a.context.Note, 124 Responders: responders, 125 VisibleTo: visibleTo, 126 } 127 }