https://github.com/rbague/panic-notifier
HTTP middleware that notifies panics on a webserver
https://github.com/rbague/panic-notifier
go golang notification-service panic slack webserver
Last synced: about 2 months ago
JSON representation
HTTP middleware that notifies panics on a webserver
- Host: GitHub
- URL: https://github.com/rbague/panic-notifier
- Owner: rbague
- License: mit
- Created: 2019-01-09T21:06:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-10-06T19:12:06.000Z (over 6 years ago)
- Last Synced: 2024-07-30T20:15:09.727Z (over 1 year ago)
- Topics: go, golang, notification-service, panic, slack, webserver
- Language: Go
- Size: 9.77 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PANIC-NOTIFIER
[](https://godoc.org/github.com/rbague/panic-notifier/integration)
[](https://goreportcard.com/report/github.com/rbague/panic-notifier)
Panic notifier is based in the [Exception Notification](https://github.com/smartinez87/exception_notification/) ruby gem. You should definitely give it a look.
---
The purpose of this library is to receive a notification into an integration everytime a webserver panics.
Right now, the built-in notifiers can deliver notifications to Slack, email and via custom WebHooks.
## Getting Started
```go
go get github.com/rbague/panic-notifier
```
### Usage
To start using it with [go-chi/chi](https://github.com/go-chi/chi), [gorilla/mux](https://github.com/gorilla/mux) or similar:
```go
package main
import "github.com/rbague/panic-notifier"
func main() {
r := chi.NewRouter() // github.com/go-chi/chi
r := mux.NewRouter() // github.com/gorilla/mux
s := integration.NewSlack("INCOMING_WEBHOOK_URL")
r.Use(notifier.Middleware(s))
...
}
```
## Integrations
panic-notifier relies on integrations to deliver the notifications to the different services, here are the default ones:
- [Slack](#slack)
- [Email](#email)
- [WebHooks](#webhooks)
But you could easily implement your [custom integration](#custom-integration).
### Slack
This integration delivers the notification to a slack channel using the [slack-webhook](https://github.com/rbague/slack-webhook) library.
#### Usage
To start using it you only need to provide the webhook url.
```go
package main
import "github.com/rbague/panic-notifier"
func main() {
r := chi.NewRouter() // github.com/go-chi/chi
slack := integration.NewSlack("INCOMING_WEBHOOK_URL")
r.Use(notifier.Middleware(slack))
...
}
```
For production uses, recommend calling the following method with a `*http.Client` so it does not use the default client to deliver the notification.
```go
slack.SetHTTPClient(*http.Client)
```
### Email
This integration delivers the notification via email using SMTP.
#### Usage
To start using it with the default configuration you only need to provide a GMail email and password:
```go
package main
import "github.com/rbague/panic-notifier"
func main() {
r := chi.NewRouter() // github.com/go-chi/chi
email := integration.NewDefaultEmail("YOUR_EMAIL@gmail.com", "YOUR_PASSWORD")
r.Use(notifier.Middleware(email))
...
}
```
The default configuration uses gmail's SMTP server (smtp.gmail.com) with SSL enabled (port: 465).
But this canbe easily overridden by providing your on configuration:
```go
package main
import "github.com/rbague/panic-notifier"
func main() {
r := chi.NewRouter() // github.com/go-chi/chi
config := &SMTPConfig{
Addr: "smtp.example.org",
Port: 587,
User: "YOUR_EMAIL@gmail.com",
Password: "YOUR_PASSWORD",
}
email := integration.NewEmail(config)
r.Use(notifier.Middleware(email))
...
}
```
### WebHooks
This integration delivers the notification via the HTTP protocol.
Right now each request is sent using the `POST` method.
#### Usage
To start using it you only need to provide the webhook url.
```go
package main
import "github.com/rbague/panic-notifier"
func main() {
r := chi.NewRouter() // github.com/go-chi/chi
wh := integration.NewWebHook("WEBHOOK_URL")
r.Use(notifier.Middleware(wh))
...
}
```
The WebHook type exposes its `Client` field, so the http.Client can be changed for production uses.
### Custom integration
Creating a custom integration is as easy as implementing the integration.Integration interface.
```go
// Integration is the interface used to deliver a notification of a panic
type Integration interface {
// StackTraceLines return the number of stack trace lines
// to be sent in each notification
StackTraceLines() int
// Deliver delivers the given notification and returns an error if any.
Deliver(*Notification) error
}
```
## TODO
Add integrations for:
- [x] Slack
- [x] Email
- [x] WebHooks
Other:
- [ ] Add option to send custom data
- [ ] Add tests
- [ ] Allow custom webhooks to use other methods than POST
## License
Copyright (c) 2018 Roger Bagué Martí, released under the [MIT license](http://www.opensource.org/licenses/MIT).