git » gofer » commit 5f71e10

raw: Log errors to reqlog

author Alberto Bertogli
2020-10-13 03:13:11 UTC
committer Alberto Bertogli
2020-10-13 03:13:11 UTC
parent 40eb3c8bf120113cfe9e97261d465bd31e398675

raw: Log errors to reqlog

This patch makes raw also write to reqlog when there is an error
connecting to the backend.

server/raw.go +11 -0
test/01-fe.yaml +3 -0
test/test.sh +7 -0
trace/trace.go +0 -12

diff --git a/server/raw.go b/server/raw.go
index aa8b022..0e6cea3 100644
--- a/server/raw.go
+++ b/server/raw.go
@@ -70,6 +70,17 @@ func forward(src net.Conn, dstAddr string, dstTLS bool, rlog *reqlog.Log) {
 
 	if err != nil {
 		tr.Errorf("%s error dialing %v : %v", src.LocalAddr(), dstAddr, err)
+		if rlog != nil {
+			rlog.Log(&reqlog.Event{
+				T: time.Now(),
+				R: &reqlog.RawRequest{
+					RemoteAddr: src.RemoteAddr(),
+					LocalAddr:  src.LocalAddr(),
+				},
+				Status:  500,
+				Latency: time.Since(start),
+			})
+		}
 		return
 	}
 	defer dst.Close()
diff --git a/test/01-fe.yaml b/test/01-fe.yaml
index 1721c11..d9294f1 100644
--- a/test/01-fe.yaml
+++ b/test/01-fe.yaml
@@ -50,3 +50,6 @@ raw:
     certs: ".certs"
     reqlog: "requests"
 
+  ":8447":
+    to: "localhost:0"
+    reqlog: "requests"
diff --git a/test/test.sh b/test/test.sh
index c0099b2..a50de17 100755
--- a/test/test.sh
+++ b/test/test.sh
@@ -196,5 +196,12 @@ echo "### Raw proxying"
 exp http://localhost:8445/file -body "ñaca\n"
 exp https://localhost:8446/file -body "ñaca\n"
 
+true < /dev/tcp/localhost/8447
+if ! waitgrep -q ":8447 = 500" .01-fe.requests.log; then
+	echo "raw connection to :8447: error entry not found"
+	exit 1
+fi
+
+
 echo "## Success"
 snoop
diff --git a/trace/trace.go b/trace/trace.go
index cac9837..ab387ba 100644
--- a/trace/trace.go
+++ b/trace/trace.go
@@ -64,18 +64,6 @@ func (t *Trace) Errorf(format string, a ...interface{}) error {
 	return err
 }
 
-// Error marks the trace as having seen an error, and also logs it to the
-// trace's log.
-func (t *Trace) Error(err error) error {
-	t.t.SetError()
-	t.t.LazyPrintf("error: %v", err)
-
-	log.Log(log.Info, 1, "%#p %s %s error: %s", t, t.family, t.title,
-		err.Error())
-
-	return err
-}
-
 // SetError marks the trace as having received an error, without emitting any
 // particular output.
 func (t *Trace) SetError() {