/usr/share/gocode/src/github.com/tobi/airbrake-go/airbrake.go is in golang-github-tobi-airbrake-go-dev 0.0~git20150109-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | package airbrake
import (
"bytes"
"errors"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"regexp"
"runtime"
"sync"
"text/template"
)
var (
ApiKey = ""
Endpoint = "https://api.airbrake.io/notifier_api/v2/notices"
Environment = "development"
Verbose = false
// PrettyParams allows including request query/form parameters on the Environment tab
// which is more readable than the raw text of the Parameters tab (in Errbit).
// The param keys will be rendered as "?<param>" so they will sort together at the top of the tab.
PrettyParams = false
sensitive = regexp.MustCompile(`password|token|secret|key`)
badResponse = errors.New("Bad response")
apiKeyMissing = errors.New("Please set the airbrake.ApiKey before doing calls")
dunno = []byte("???")
centerDot = []byte("·")
dot = []byte(".")
tmpl = template.Must(template.New("error").Parse(source))
)
type Line struct {
Function string
File string
Line int
}
// stack implements Stack, skipping N frames
func stacktrace(skip int) (lines []Line) {
for i := skip; ; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
item := Line{string(function(pc)), string(file), line}
// ignore panic method
if item.Function != "panic" {
lines = append(lines, item)
}
}
return
}
var channel chan map[string]interface{}
var once sync.Once
// function returns, if possible, the name of the function containing the PC.
func function(pc uintptr) []byte {
fn := runtime.FuncForPC(pc)
if fn == nil {
return dunno
}
name := []byte(fn.Name())
// The name includes the path name to the package, which is unnecessary
// since the file name is already included. Plus, it has center dots.
// That is, we see
// runtime/debug.*T·ptrmethod
// and want
// *T.ptrmethod
if period := bytes.Index(name, dot); period >= 0 {
name = name[period+1:]
}
name = bytes.Replace(name, centerDot, dot, -1)
return name
}
func initChannel() {
channel = make(chan map[string]interface{}, 100)
go func() {
for params := range channel {
post(params)
}
}()
}
func post(params map[string]interface{}) error {
buffer := bytes.NewBufferString("")
if err := tmpl.Execute(buffer, params); err != nil {
log.Printf("Airbrake error: %s", err)
return err
}
if Verbose {
log.Printf("Airbrake payload for endpoint %s: %s", Endpoint, buffer)
}
response, err := http.Post(Endpoint, "text/xml", buffer)
if err != nil {
log.Printf("Airbrake error: %s", err)
return err
}
if Verbose {
body, _ := ioutil.ReadAll(response.Body)
log.Printf("response: %s", body)
}
response.Body.Close()
if Verbose {
log.Printf("Airbrake post: %s status code: %d", params["Error"], response.StatusCode)
}
return nil
}
func Error(e error, request *http.Request) error {
once.Do(initChannel)
if ApiKey == "" {
return apiKeyMissing
}
return post(params(e, request))
}
func Notify(e error) error {
once.Do(initChannel)
if ApiKey == "" {
return apiKeyMissing
}
return post(params(e, nil))
}
func params(e error, request *http.Request) map[string]interface{} {
params := map[string]interface{}{
"Class": reflect.TypeOf(e).String(),
"Error": e,
"ApiKey": ApiKey,
"ErrorName": e.Error(),
"Environment": Environment,
}
if params["Class"] == "" {
params["Class"] = "Panic"
}
pwd, err := os.Getwd()
if err == nil {
params["Pwd"] = pwd
}
hostname, err := os.Hostname()
if err == nil {
params["Hostname"] = hostname
}
params["Backtrace"] = stacktrace(3)
if request == nil || request.ParseForm() != nil {
return params
}
// Compile relevant request parameters into a map.
req := make(map[string]interface{})
params["Request"] = req
req["Component"] = ""
req["Action"] = ""
// Nested http Muxes muck with the URL, prefer RequestURI.
if request.RequestURI != "" {
req["URL"] = request.RequestURI
} else {
req["URL"] = request.URL
}
// Compile header parameters.
header := make(map[string]string)
req["Header"] = header
header["Method"] = request.Method
header["Protocol"] = request.Proto
for k, v := range request.Header {
if !omit(k, v) {
header[k] = v[0]
}
}
// Compile query/form parameters.
form := make(map[string]string)
req["Form"] = form
for k, v := range request.Form {
if !omit(k, v) {
form[k] = v[0]
if PrettyParams {
header["?"+k] = v[0]
}
}
}
return params
}
// omit checks the key, values for emptiness or sensitivity.
func omit(key string, values []string) bool {
return len(key) == 0 || len(values) == 0 || len(values[0]) == 0 || sensitive.FindString(key) != ""
}
func CapturePanic(r *http.Request) {
if rec := recover(); rec != nil {
if err, ok := rec.(error); ok {
log.Printf("Recording err %s", err)
Error(err, r)
} else if err, ok := rec.(string); ok {
log.Printf("Recording string %s", err)
Error(errors.New(err), r)
}
panic(rec)
}
}
const source = `<?xml version="1.0" encoding="UTF-8"?>
<notice version="2.0">
<api-key>{{ .ApiKey }}</api-key>
<notifier>
<name>Airbrake Golang</name>
<version>0.0.1</version>
<url>http://airbrake.io</url>
</notifier>
<error>
<class>{{ html .Class }}</class>
<message>{{ html .ErrorName }}</message>
<backtrace>{{ range .Backtrace }}
<line method="{{ html .Function}}" file="{{ html .File}}" number="{{.Line}}"/>{{ end }}
</backtrace>
</error>{{ with .Request }}
<request>
<url>{{html .URL}}</url>
<component>{{ .Component }}</component>
<action>{{ .Action }}</action>
<params>{{ range $key, $value := .Form }}
<var key="{{ $key }}">{{ $value }}</var>{{ end }}
</params>
<cgi-data>{{ range $key, $value := .Header }}
<var key="{{ $key }}">{{ $value }}</var>{{ end }}
</cgi-data>
</request>{{ end }}
<server-environment>
<project-root>{{ html .Pwd }}</project-root>
<environment-name>{{ .Environment }}</environment-name>
<hostname>{{ html .Hostname }}</hostname>
</server-environment>
</notice>`
|