You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/09/12 15:52:14 UTC

[trafficcontrol] branch master updated: Add TO Go panic logging

This is an automated email from the ASF dual-hosted git repository.

dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new c680af6  Add TO Go panic logging
c680af6 is described below

commit c680af65e2cb5c9bb3cfb5d3824729efb732b57b
Author: Robert Butts <ro...@apache.org>
AuthorDate: Tue Sep 11 16:18:59 2018 -0600

    Add TO Go panic logging
---
 traffic_ops/traffic_ops_golang/routing.go  |  2 +-
 traffic_ops/traffic_ops_golang/wrappers.go | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/traffic_ops/traffic_ops_golang/routing.go b/traffic_ops/traffic_ops_golang/routing.go
index 424686a..7f4fda8 100644
--- a/traffic_ops/traffic_ops_golang/routing.go
+++ b/traffic_ops/traffic_ops_golang/routing.go
@@ -66,7 +66,7 @@ type RawRoute struct {
 }
 
 func getDefaultMiddleware(secret string, requestTimeout time.Duration) []Middleware {
-	return []Middleware{getWrapAccessLog(secret), timeOutWrapper(requestTimeout), wrapHeaders}
+	return []Middleware{getWrapAccessLog(secret), timeOutWrapper(requestTimeout), wrapHeaders, wrapPanicRecover}
 }
 
 // ServerData ...
diff --git a/traffic_ops/traffic_ops_golang/wrappers.go b/traffic_ops/traffic_ops_golang/wrappers.go
index c19309a..914f706 100644
--- a/traffic_ops/traffic_ops_golang/wrappers.go
+++ b/traffic_ops/traffic_ops_golang/wrappers.go
@@ -29,6 +29,7 @@ import (
 	"errors"
 	"fmt"
 	"net/http"
+	"runtime"
 	"strings"
 	"time"
 	"unicode"
@@ -163,6 +164,29 @@ func wrapHeaders(h http.HandlerFunc) http.HandlerFunc {
 	}
 }
 
+func wrapPanicRecover(h http.HandlerFunc) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		defer func() {
+			if err := recover(); err != nil {
+				log.Errorf("panic: (err: %v) stacktrace:\n%s\n", err, stacktrace())
+			}
+		}()
+		h(w, r)
+	}
+}
+
+func stacktrace() []byte {
+	initialBufSize := 1024
+	buf := make([]byte, initialBufSize)
+	for {
+		n := runtime.Stack(buf, true)
+		if n < len(buf) {
+			return buf[:n]
+		}
+		buf = make([]byte, len(buf)*2)
+	}
+}
+
 // AccessLogTimeFormat ...
 const AccessLogTimeFormat = "02/Jan/2006:15:04:05 -0700"