mirror of
https://codeberg.org/git-pages/git-pages.git
synced 2026-08-18 18:06:04 +00:00
Add Sentry support.
This commit is contained in:
@@ -72,6 +72,7 @@ func serve(listener net.Listener, handler http.Handler) {
|
||||
|
||||
func main() {
|
||||
InitObservability()
|
||||
defer FiniObservability()
|
||||
|
||||
printConfigEnvVars := flag.Bool("print-config-env-vars", false,
|
||||
"print every recognized configuration environment variable and exit")
|
||||
|
||||
+39
-1
@@ -1,27 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"github.com/honeybadger-io/honeybadger-go"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
sentryhttp "github.com/getsentry/sentry-go/http"
|
||||
)
|
||||
|
||||
func hasHoneybadger() bool {
|
||||
return os.Getenv("HONEYBADGER_API_KEY") != ""
|
||||
}
|
||||
|
||||
func hasSentry() bool {
|
||||
return os.Getenv("SENTRY_DSN") != ""
|
||||
}
|
||||
|
||||
func InitObservability() {
|
||||
environment := "development"
|
||||
if value, ok := os.LookupEnv("ENVIRONMENT"); ok {
|
||||
environment = value
|
||||
}
|
||||
|
||||
if hasHoneybadger() {
|
||||
honeybadger.Configure(honeybadger.Configuration{})
|
||||
honeybadger.Configure(honeybadger.Configuration{
|
||||
Env: environment,
|
||||
})
|
||||
debug.SetPanicOnFault(true)
|
||||
}
|
||||
|
||||
if hasSentry() {
|
||||
options := sentry.ClientOptions{}
|
||||
options.Environment = environment
|
||||
options.Dsn = os.Getenv("SENTRY_DSN")
|
||||
if err := sentry.Init(options); err != nil {
|
||||
log.Fatalf("sentry: %s\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func FiniObservability() {
|
||||
if hasSentry() {
|
||||
sentry.Flush(2 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func ObserveHTTPHandler(handler http.Handler) http.Handler {
|
||||
if hasHoneybadger() {
|
||||
handler = honeybadger.Handler(handler)
|
||||
}
|
||||
|
||||
if hasSentry() {
|
||||
handler = sentryhttp.New(sentryhttp.Options{
|
||||
Repanic: true,
|
||||
}).Handle(handler)
|
||||
}
|
||||
|
||||
return handler
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user