Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions _examples/basic/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ go 1.18

replace github.com/goware/alerter => ../../

require github.com/goware/alerter v0.0.0-00010101000000-000000000000
require (
github.com/go-chi/chi/v5 v5.0.10
github.com/goware/alerter v0.0.0-00010101000000-000000000000
)

require (
github.com/goware/cachestore v0.3.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/rs/zerolog v1.28.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/goware/cachestore v0.7.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/redis/go-redis/v9 v9.0.3 // indirect
github.com/rs/zerolog v1.29.1 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
golang.org/x/sys v0.10.0 // indirect
)
47 changes: 32 additions & 15 deletions _examples/basic/go.sum
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao=
github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/goware/cachestore v0.3.1 h1:c17LIMGVM/Y4HBzLIHNuR3FFkySYr3eVoYgT53U4Fl4=
github.com/goware/cachestore v0.3.1/go.mod h1:ifyKCgoWJDpJneKC8YUryjgiRy7JnEgbkpe/FpgR6BA=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/goware/cachestore v0.7.0 h1:9aLkGOA0qNaXzXFHP6OzIpmdtkunp0WROTn8XD23RPk=
github.com/goware/cachestore v0.7.0/go.mod h1:/AoSqLZfLN1nowTOnk4ButuUiDsXC0/Jfjpe/sPX7lE=
github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4=
github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
20 changes: 18 additions & 2 deletions _examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"context"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/goware/alerter"
)

Expand All @@ -11,10 +13,24 @@ func main() {
WebhookURL: "https://discord.com/api/webhooks/0000000000/abcdefghijklmnopqrstuvwxyz",
// Username: "Alerter",
// AvatarURL: "https://cdn.discordapp.com/embed/avatars/0.png",
RoleIDToPing: 849690281536389230,
MentionRoleID: 849690281536389230,
Env: "production",
})
if err != nil {
panic(err)
}
alerter.Alert(context.Background(), "hello world %v", "error 1 2 3")
a.Alert(context.Background(), "hello world %v", "error 1 2 3")

r := chi.NewRouter()
r.Use(a.Recoverer())

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})

r.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
panic("oh no")
})

http.ListenAndServe(":5555", r)
}
6 changes: 5 additions & 1 deletion alerter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package alerter

import "context"
import (
"context"
"net/http"
)

type Alerter interface {
Alert(ctx context.Context, format string, v ...interface{})
Recoverer() func(next http.Handler) http.Handler
}
6 changes: 6 additions & 0 deletions default_alerter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package alerter

import (
"context"
"net/http"

"github.com/go-chi/chi/v5/middleware"
"github.com/rs/zerolog/log"
)

Expand All @@ -22,3 +24,7 @@ func (a *defaultAlerter) Alert(ctx context.Context, format string, v ...interfac
log.Error().Str("alert", "alert").Msgf(format, v...)
}
}

func (a *defaultAlerter) Recoverer() func(next http.Handler) http.Handler {
return middleware.Recoverer
}
13 changes: 10 additions & 3 deletions discord_alerter.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,21 @@ func (a *discordAlerter) doRequest(ctx context.Context, cacheKey string, payload
}
}

type field struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline"`
}

type embed struct {
Author struct {
Name string `json:"name"`
IconURL string `json:"icon_url"`
}
Title string `json:"title"`
Description string `json:"description"`
Color int `json:"color"`
Title string `json:"title"`
Description string `json:"description"`
Color int `json:"color"`
Fields []field `json:"fields"`
}

type payload struct {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/goware/alerter
go 1.18

require (
github.com/go-chi/chi/v5 v5.0.10
github.com/goware/cachestore v0.7.0
github.com/rs/zerolog v1.29.1
github.com/zeebo/xxh3 v1.0.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/goware/cachestore v0.7.0 h1:9aLkGOA0qNaXzXFHP6OzIpmdtkunp0WROTn8XD23RPk=
github.com/goware/cachestore v0.7.0/go.mod h1:/AoSqLZfLN1nowTOnk4ButuUiDsXC0/Jfjpe/sPX7lE=
Expand Down
122 changes: 122 additions & 0 deletions recoverer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package alerter

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"runtime/debug"
"strings"

"github.com/go-chi/chi/v5/middleware"
)

func (a *discordAlerter) Recoverer() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil {
if rvr == http.ErrAbortHandler {
// we don't recover http.ErrAbortHandler so the response
// to the client is aborted, this should not be logged
panic(rvr)
}

if !a.SkipLogEntry {
middleware.PrintPrettyStack(rvr)
}

p, err := a.alertWithPanicPayload(r, rvr)
if err != nil {
return
}
cacheKey := fmt.Sprintf("%d", xxh64FromString(p))
a.doRequest(r.Context(), cacheKey, p)

if r.Header.Get("Connection") != "Upgrade" {
w.WriteHeader(http.StatusInternalServerError)
}
}
}()

next.ServeHTTP(w, r)
}

return http.HandlerFunc(fn)
}
}

// parseStack is a modified version of parse from go-chi/chi/middleware/recoverer.go
func (a *discordAlerter) parseStack(debugStack []byte, rvr interface{}) (string, error) {
var err error

buf := &bytes.Buffer{}

fmt.Fprintf(buf, "panic: %v\n", rvr)

stack := strings.Split(string(debugStack), "\n")
lines := []string{}

// locate panic line, as we may have nested panics
for i := len(stack) - 1; i > 0; i-- {
lines = append(lines, stack[i])
if strings.HasPrefix(stack[i], "panic(") {
lines = lines[0 : len(lines)-2] // remove boilerplate
break
}
}

// reverse
for i := len(lines)/2 - 1; i >= 0; i-- {
opp := len(lines) - 1 - i
lines[i], lines[opp] = lines[opp], lines[i]
}

for _, l := range lines {
fmt.Fprintf(buf, "\t %s", l)
}

return buf.String(), err
}

func (a *discordAlerter) alertWithPanicPayload(r *http.Request, rvr interface{}) (string, error) {
debugStack, err := a.parseStack(debug.Stack(), rvr)
if err != nil {
return "", err
}

p := payload{
Username: a.Username,
AvatarURL: a.AvatarURL,
Embeds: []embed{
{
Author: struct {
Name string `json:"name"`
IconURL string `json:"icon_url"`
}{Name: a.Username, IconURL: a.AvatarURL},
Title: "Panic Stack Trace",
Description: fmt.Sprintf("```\n %v \n ```", debugStack),
Fields: []field{
{
Name: "ENV:",
Value: a.Env,
},
{
Name: "Request",
Value: fmt.Sprintf("%s %s", r.Method, r.URL.String()),
},
},
Color: 0xCC0000,
},
},
}
if a.MentionRoleID > 0 {
p.Content = fmt.Sprintf("<@&%d>", a.MentionRoleID)
}

b, err := json.Marshal(p)
if err != nil {
return "", err
}
return string(b), nil
}