Skip to content

Commit

Permalink
Improved error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed May 24, 2016
1 parent 9542aed commit de0fc58
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
42 changes: 37 additions & 5 deletions handler_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
b64 "encoding/base64"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"net"
"net/http"
"runtime/pprof"
"strings"
Expand All @@ -28,13 +30,15 @@ func (e ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, err st
return
}

keyName := ""
// Track the key ID if it exists
authHeaderValue := context.Get(r, AuthHeaderValue)
var alias string

if config.StoreAnalytics(r) {

t := time.Now()

// Track the key ID if it exists
authHeaderValue := context.Get(r, AuthHeaderValue)
keyName := ""
if authHeaderValue != nil {
keyName = authHeaderValue.(string)
}
Expand All @@ -50,15 +54,17 @@ func (e ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, err st

// This is an odd bugfix, will need further testing
r.URL.Path = "/" + r.URL.Path
if strings.HasPrefix(r.URL.Path, "//") {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
}

OauthClientID := ""
var alias string
tags := make([]string, 0)
thisSessionState := context.Get(r, SessionData)

if thisSessionState != nil {
OauthClientID = thisSessionState.(SessionState).OauthClientID
alias = thisSessionState.(SessionState).Alias
alias = thisSessionState.(SessionState).Alias
tags = thisSessionState.(SessionState).Tags
}

Expand Down Expand Up @@ -137,6 +143,32 @@ func (e ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, err st
w.Header().Add("Connection", "close")
}

var obfuscated string
log.Info(keyName)
if len(keyName) > 4 {
obfuscated = "****" + keyName[len(keyName)-4:]
}

var thisIP string
if clientIP, _, derr := net.SplitHostPort(r.RemoteAddr); derr == nil {
// If we aren't the first proxy retain prior
// X-Forwarded-For information as a comma+space
// separated list and fold multiple headers into one.
if prior, ok := r.Header["X-Forwarded-For"]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
thisIP = clientIP
}
log.WithFields(logrus.Fields{
"prefix": "gateway",
"user_ip": thisIP,
"server_name": e.Spec.APIDefinition.Proxy.TargetURL,
"user_id": obfuscated,
"org_id": e.Spec.APIDefinition.OrgID,
"api_id": e.Spec.APIDefinition.APIID,
"path": r.URL.Path,
}).Error("request error: ", err)

log.Debug("Returning error header")
w.WriteHeader(errCode)
thisError := APIError{fmt.Sprintf("%s", err)}
Expand Down
26 changes: 25 additions & 1 deletion tyk_reverse_proxy_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"bytes"
"github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"github.com/pmylund/go-cache"
"io"
Expand Down Expand Up @@ -406,6 +407,7 @@ func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Reques
}
}

var thisIP string
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
// If we aren't the first proxy retain prior
// X-Forwarded-For information as a comma+space
Expand All @@ -414,6 +416,7 @@ func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Reques
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
outreq.Header.Set("X-Forwarded-For", clientIP)
thisIP = clientIP
}

// Circuit breaker
Expand Down Expand Up @@ -445,7 +448,28 @@ func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Reques
}

if err != nil {
log.Error("http: proxy error: ", err)

authHeaderValue := context.Get(req, AuthHeaderValue).(string)
var obfuscated string
if len(authHeaderValue) > 4 {
obfuscated = "****" + authHeaderValue[len(authHeaderValue)-4:]
}

var alias string
if sessVal != nil {
alias = sessVal.(SessionState).Alias
}

log.WithFields(logrus.Fields{
"prefix": "proxy",
"user_ip": thisIP,
"server_name": outreq.Host,
"user_id": obfuscated,
"user_name": alias,
"org_id": p.TykAPISpec.APIDefinition.OrgID,
"api_id": p.TykAPISpec.APIDefinition.APIID,
}).Error("http: proxy error: ", err)

if strings.Contains(err.Error(), "timeout awaiting response headers") {
p.ErrorHandler.HandleError(rw, logreq, "Upstream service reached hard timeout.", 408)

Expand Down
4 changes: 4 additions & 0 deletions util_http_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func CopyHttpRequest(r *http.Request) *http.Request {
func CopyHttpResponse(r *http.Response) *http.Response {

resCopy := new(http.Response)
if r == nil {
return resCopy
}

*resCopy = *r

if r.Body != nil {
Expand Down

0 comments on commit de0fc58

Please sign in to comment.