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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
package roll
import (
"bytes"
"encoding/json"
"fmt"
"hash/adler32"
"io"
"io/ioutil"
"net/http"
"os"
"reflect"
"runtime"
"strings"
"time"
)
const (
// By default, all Rollbar API requests are sent to this endpoint.
endpoint = "https://api.rollbar.com/api/1/item/"
// Identify this Rollbar client library to the Rollbar API.
clientName = "go-roll"
clientVersion = "0.2.0"
clientLanguage = "go"
)
var (
// Endpoint is the default HTTP(S) endpoint that all Rollbar API requests
// will be sent to. By default, this is Rollbar's "Items" API endpoint. If
// this is blank, no items will be sent to Rollbar.
Endpoint = endpoint
// Rollbar access token for the global client. If this is blank, no items
// will be sent to Rollbar.
Token = ""
// Environment for all items reported with the global client.
Environment = "development"
)
type rollbarSuccess struct {
Result map[string]string `json:"result"`
}
// Client reports items to a single Rollbar project.
type Client interface {
Critical(err error, custom map[string]string) (uuid string, e error)
CriticalStack(err error, ptrs []uintptr, custom map[string]string) (uuid string, e error)
Error(err error, custom map[string]string) (uuid string, e error)
ErrorStack(err error, ptrs []uintptr, custom map[string]string) (uuid string, e error)
Warning(err error, custom map[string]string) (uuid string, e error)
WarningStack(err error, ptrs []uintptr, custom map[string]string) (uuid string, e error)
Info(msg string, custom map[string]string) (uuid string, e error)
Debug(msg string, custom map[string]string) (uuid string, e error)
}
type rollbarClient struct {
token string
env string
}
// New creates a new Rollbar client that reports items to the given project
// token and with the given environment (eg. "production", "development", etc).
func New(token, env string) Client {
return &rollbarClient{token, env}
}
func Critical(err error, custom map[string]string) (uuid string, e error) {
return CriticalStack(err, getCallers(2), custom)
}
func CriticalStack(err error, ptrs []uintptr, custom map[string]string) (uuid string, e error) {
return New(Token, Environment).CriticalStack(err, ptrs, custom)
}
func Error(err error, custom map[string]string) (uuid string, e error) {
return ErrorStack(err, getCallers(2), custom)
}
func ErrorStack(err error, ptrs []uintptr, custom map[string]string) (uuid string, e error) {
return New(Token, Environment).ErrorStack(err, ptrs, custom)
}
func Warning(err error, custom map[string]string) (uuid string, e error) {
return WarningStack(err, getCallers(2), custom)
}
func WarningStack(err error, ptrs []uintptr, custom map[string]string) (uuid string, e error) {
return New(Token, Environment).WarningStack(err, ptrs, custom)
}
func Info(msg string, custom map[string]string) (uuid string, e error) {
return New(Token, Environment).Info(msg, custom)
}
func Debug(msg string, custom map[string]string) (uuid string, e error) {
return New(Token, Environment).Debug(msg, custom)
}
func (c *rollbarClient) Critical(err error, custom map[string]string) (uuid string, e error) {
return c.CriticalStack(err, getCallers(2), custom)
}
func (c *rollbarClient) CriticalStack(err error, callers []uintptr, custom map[string]string) (uuid string, e error) {
item := c.buildTraceItem("critical", err, callers, custom)
return c.send(item)
}
func (c *rollbarClient) Error(err error, custom map[string]string) (uuid string, e error) {
return c.ErrorStack(err, getCallers(2), custom)
}
func (c *rollbarClient) ErrorStack(err error, callers []uintptr, custom map[string]string) (uuid string, e error) {
item := c.buildTraceItem("error", err, callers, custom)
return c.send(item)
}
func (c *rollbarClient) Warning(err error, custom map[string]string) (uuid string, e error) {
return c.WarningStack(err, getCallers(2), custom)
}
func (c *rollbarClient) WarningStack(err error, callers []uintptr, custom map[string]string) (uuid string, e error) {
item := c.buildTraceItem("warning", err, callers, custom)
return c.send(item)
}
func (c *rollbarClient) Info(msg string, custom map[string]string) (uuid string, e error) {
item := c.buildMessageItem("info", msg, custom)
return c.send(item)
}
func (c *rollbarClient) Debug(msg string, custom map[string]string) (uuid string, e error) {
item := c.buildMessageItem("debug", msg, custom)
return c.send(item)
}
func (c *rollbarClient) buildTraceItem(level string, err error, callers []uintptr, custom map[string]string) (item map[string]interface{}) {
stack := buildRollbarFrames(callers)
item = c.buildItem(level, err.Error(), custom)
itemData := item["data"].(map[string]interface{})
itemData["fingerprint"] = stack.fingerprint()
itemData["body"] = map[string]interface{}{
"trace": map[string]interface{}{
"frames": stack,
"exception": map[string]interface{}{
"class": errorClass(err),
"message": err.Error(),
},
},
}
return item
}
func (c *rollbarClient) buildMessageItem(level string, msg string, custom map[string]string) (item map[string]interface{}) {
item = c.buildItem(level, msg, custom)
itemData := item["data"].(map[string]interface{})
itemData["body"] = map[string]interface{}{
"message": map[string]interface{}{
"body": msg,
},
}
return item
}
func (c *rollbarClient) buildItem(level, title string, custom map[string]string) map[string]interface{} {
hostname, _ := os.Hostname()
return map[string]interface{}{
"access_token": c.token,
"data": map[string]interface{}{
"environment": c.env,
"title": title,
"level": level,
"timestamp": time.Now().Unix(),
"platform": runtime.GOOS,
"language": clientLanguage,
"server": map[string]interface{}{
"host": hostname,
},
"notifier": map[string]interface{}{
"name": clientName,
"version": clientVersion,
},
"custom": custom,
},
}
}
// send reports the given item to Rollbar and returns either a UUID for the
// reported item or an error.
func (c *rollbarClient) send(item map[string]interface{}) (uuid string, err error) {
if len(c.token) == 0 || len(Endpoint) == 0 {
return "", nil
}
jsonBody, err := json.Marshal(item)
if err != nil {
return "", err
}
resp, err := http.Post(Endpoint, "application/json", bytes.NewReader(jsonBody))
if err != nil {
// If something goes wrong it really does not matter
return "", nil
}
defer func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
// If something goes wrong it really does not matter
return "", nil
}
// Extract UUID from JSON response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", nil
}
success := rollbarSuccess{}
json.Unmarshal(body, &success)
return success.Result["uuid"], nil
}
// errorClass returns a class name for an error (eg. "ErrUnexpectedEOF"). For
// string errors, it returns an Adler-32 checksum of the error string.
func errorClass(err error) string {
class := reflect.TypeOf(err).String()
if class == "" {
return "panic"
} else if class == "*errors.errorString" {
checksum := adler32.Checksum([]byte(err.Error()))
return fmt.Sprintf("{%x}", checksum)
} else {
return strings.TrimPrefix(class, "*")
}
}
|