forked from de3/golib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
153 lines (135 loc) · 2.94 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package golib
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
)
// Attachment model
type Attachment struct {
Attachments []Payload `json:"attachments"`
}
// Payload model
type Payload struct {
Text string `json:"text"`
Color string `json:"color"`
Fields []Field `json:"fields"`
}
// Field model
type Field struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
const (
successColor = "#36a64f"
errorColor = "#f44b42"
)
func getCaller() string {
var name, file string
var line int
var pc [16]uintptr
n := runtime.Callers(4, pc[:])
for _, pc := range pc[:n] {
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
file, line = fn.FileLine(pc)
name = fn.Name()
if !strings.HasPrefix(name, "runtime.") {
break
}
}
var source string
switch {
case name != "":
source = fmt.Sprintf("%v:%v", name, line)
case file != "":
source = fmt.Sprintf("%v:%v", file, line)
default:
source = fmt.Sprintf("pc:%x", pc)
}
return source
}
// SendNotification to slack channel
func SendNotification(title, body, ctx string, err error) {
isActive, _ := strconv.ParseBool(os.Getenv("SLACK_NOTIFIER"))
if !isActive {
return
}
stackTrace := getCaller()
go func() {
defer func() {
if r := recover(); r != nil {
LogError(fmt.Errorf("%v", r), "send_notification_slack", title)
}
}()
message := fmt.Sprintf("*%s*\n\n%s", title, body)
var slackPayload Payload
slackPayload.Text = message
slackPayload.Color = successColor
if err != nil {
slackPayload.Color = errorColor
slackPayload.Text = fmt.Sprintf("%s\n*Error*: ```%s```", message, err.Error())
}
hostName, _ := os.Hostname()
now := time.Now().Format(time.RFC3339)
slackPayload.Fields = []Field{
Field{
Title: "Server",
Value: hostName,
Short: true,
},
Field{
Title: "Environment",
Value: os.Getenv("SERVER_ENV"),
Short: true,
},
Field{
Title: "Context",
Value: ctx,
Short: true,
},
Field{
Title: "Time",
Value: now,
Short: true,
},
}
if err != nil {
slackPayload.Fields = append(slackPayload.Fields, Field{
Title: "Error Line Stack",
Value: fmt.Sprintf("`%s`", stackTrace),
Short: true,
})
}
var slackAttachment Attachment
slackAttachment.Attachments = append(slackAttachment.Attachments, slackPayload)
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(true)
encoder.Encode(slackAttachment)
url := os.Getenv("SLACK_URL")
req, _ := http.NewRequest("POST", url, buffer)
defer req.Body.Close()
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
Log(ErrorLevel, string(body), "slack.SendNotification", "send_to_slack")
}()
}