diff options
| author | Gabriel A. Giovanini <g.giovanini@gridx.de> | 2025-09-12 23:30:01 +0200 | 
|---|---|---|
| committer | Gabriel A. Giovanini <g.giovanini@gridx.de> | 2025-09-12 23:30:01 +0200 | 
| commit | 1fee46153555f3fe6b80aa7ddd975d78d62f2351 (patch) | |
| tree | 2c375c048b78c7255aca2a0f69cc35b3f17530a2 | |
| parent | 2b5bfb4d20581ae6931416b30265195d83dd1ac8 (diff) | |
| download | jnfilter-master.tar.gz jnfilter-master.tar.bz2 jnfilter-master.zip | |
Also add metric for request that panic.
| -rw-r--r-- | main.go | 25 | 
1 files changed, 18 insertions, 7 deletions
| @@ -69,6 +69,10 @@ var (  		Name: "serie_count",  		Help: "How often a serie is called",  	}, []string{"serie"}) +	panicCount = promauto.NewCounter(prometheus.CounterOpts{ +		Name: "panic", +		Help: "How many times the application panic", +	})  )  func getSeries(r *http.Request) []string { @@ -125,8 +129,10 @@ func fetchXML(_ context.Context) ([]byte, error) {  }  func appendTag(tag *etree.Element, ap string) { -	text := tag.Text() -	tag.SetText(text + ap) +	if tag != nil { +		text := tag.Text() +		tag.SetText(text + ap) +	}  }  func filterBySeries(series []string, xml []byte, temper bool) ([]byte, error) { @@ -163,6 +169,14 @@ func filterBySeries(series []string, xml []byte, temper bool) ([]byte, error) {  func handleError(next errorRequestHandler) http.HandlerFunc {  	return func(w http.ResponseWriter, r *http.Request) { +		defer func() { +			if perr := recover(); perr != nil { +				w.WriteHeader(http.StatusInternalServerError) +				slog.Error("Request panic", "error", perr) +				panicCount.Inc() +			} +		}() +  		if err := next(w, r); err != nil {  			slog.ErrorContext(r.Context(), "Error", "error", err.Error()) @@ -242,10 +256,8 @@ func view(w http.ResponseWriter, r *http.Request) error {  }  func podcast(w http.ResponseWriter, r *http.Request) error { -  	if r.URL.Path != "/" {  		return errNotFound -  	}  	xml, err := fetchXML(r.Context()) @@ -306,9 +318,7 @@ func main() {  		return  	} -	var ( -		addr = flag.String("addr", ":8080", "Server address") -	) +	addr := flag.String("addr", ":8080", "Server address")  	flag.Parse() @@ -323,6 +333,7 @@ func main() {  		Addr:    *addr,  	} +	slog.Info("Starting server", "addr", *addr)  	err := server.ListenAndServe()  	if err != nil {  		fmt.Printf("Server error: %s", err.Error()) | 
