Skip to content

Commit

Permalink
Detail logging now added (request and response wire format)
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Jan 6, 2016
1 parent 2535881 commit 6b3a413
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 26 deletions.
3 changes: 2 additions & 1 deletion handler_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
b64 "encoding/base64"
"fmt"
"github.com/gorilla/context"
"net/http"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (e ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, err st
// Get the wire format representation
var wireFormatReq bytes.Buffer
requestCopy.Write(&wireFormatReq)
rawRequest = wireFormatReq.String()
rawRequest = b64.StdEncoding.EncodeToString(wireFormatReq.Bytes())
}
}

Expand Down
9 changes: 5 additions & 4 deletions handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
b64 "encoding/base64"
"github.com/gorilla/context"
"github.com/pmylund/go-cache"
"net/http"
Expand Down Expand Up @@ -183,13 +184,13 @@ func (s SuccessHandler) RecordHit(w http.ResponseWriter, r *http.Request, timing
// Get the wire format representation
var wireFormatReq bytes.Buffer
requestCopy.Write(&wireFormatReq)
rawRequest = wireFormatReq.String()
rawRequest = b64.StdEncoding.EncodeToString(wireFormatReq.Bytes())
}
if responseCopy != nil {
// Get the wire format representation
var wireFormatRes bytes.Buffer
responseCopy.Write(&wireFormatRes)
rawResponse = wireFormatRes.String()
rawResponse = b64.StdEncoding.EncodeToString(wireFormatRes.Bytes())
}
}

Expand Down Expand Up @@ -265,7 +266,7 @@ func (s SuccessHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) *http.

var copiedResponse *http.Response
if config.AnalyticsConfig.EnableDetailedRecording {
copiedResponse := CopyHttpResponse(resp)
copiedResponse = CopyHttpResponse(resp)
}

millisec := float64(t2.UnixNano()-t1.UnixNano()) * 0.000001
Expand Down Expand Up @@ -298,7 +299,7 @@ func (s SuccessHandler) ServeHTTPWithCache(w http.ResponseWriter, r *http.Reques

var copiedResponse *http.Response
if config.AnalyticsConfig.EnableDetailedRecording {
copiedResponse := CopyHttpResponse(inRes)
copiedResponse = CopyHttpResponse(inRes)
}

millisec := float64(t2.UnixNano()-t1.UnixNano()) * 0.000001
Expand Down
7 changes: 6 additions & 1 deletion middleware_redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
authHeaderValue = authVal.(string)
}

var copiedRequest *http.Request
if config.AnalyticsConfig.EnableDetailedRecording {
copiedRequest = CopyHttpRequest(r)
}

thisKey := m.CreateCheckSum(r, authHeaderValue)
retBlob, found := m.CacheStore.GetKey(thisKey)
if found != nil {
Expand Down Expand Up @@ -191,7 +196,7 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req

// Record analytics
if m.Spec.DoNotTrack == false {
go m.sh.RecordHit(w, r, 0, newRes.StatusCode)
go m.sh.RecordHit(w, r, 0, newRes.StatusCode, copiedRequest, nil)
}

// Stop any further execution
Expand Down
13 changes: 12 additions & 1 deletion middleware_virtual_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func (d *VirtualEndpoint) ServeHTTPForCache(w http.ResponseWriter, r *http.Reque
return nil
}

var copiedRequest *http.Request
if config.AnalyticsConfig.EnableDetailedRecording {
copiedRequest = CopyHttpRequest(r)
}

t1 := time.Now().UnixNano()
thisMeta := meta.(*tykcommon.VirtualMeta)

Expand Down Expand Up @@ -225,6 +230,12 @@ func (d *VirtualEndpoint) ServeHTTPForCache(w http.ResponseWriter, r *http.Reque
log.Error("Response chain failed! ", chainErr)
}

// deep logging
var copiedResponse *http.Response
if config.AnalyticsConfig.EnableDetailedRecording {
copiedResponse = CopyHttpResponse(newResponse)
}

// Clone the response so we can save it
copiedRes := new(http.Response)
*copiedRes = *newResponse // includes shallow copies of maps, but okay
Expand All @@ -245,7 +256,7 @@ func (d *VirtualEndpoint) ServeHTTPForCache(w http.ResponseWriter, r *http.Reque
d.HandleResponse(w, newResponse, &thisSessionState)

// Record analytics
go d.sh.RecordHit(w, r, 0, newResponse.StatusCode)
go d.sh.RecordHit(w, r, 0, newResponse.StatusCode, copiedRequest, copiedResponse)

return copiedRes

Expand Down
2 changes: 1 addition & 1 deletion tyk_reverse_proxy_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (p *ReverseProxy) New(c interface{}, spec *APISpec) (TykResponseHandler, er
}

func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) *http.Response {
return p.WrappedServeHTTP(rw, req, false)
return p.WrappedServeHTTP(rw, req, config.AnalyticsConfig.EnableDetailedRecording)
// return nil
}

Expand Down
41 changes: 23 additions & 18 deletions util_http_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,43 @@ func CopyHttpRequest(r *http.Request) *http.Request {
reqCopy := new(http.Request)
*reqCopy = *r

defer r.Body.Close()
if r.Body != nil {
defer r.Body.Close()

// Buffer body data
var bodyBuffer bytes.Buffer
bodyBuffer2 := new(bytes.Buffer)
// Buffer body data
var bodyBuffer bytes.Buffer
bodyBuffer2 := new(bytes.Buffer)

io.Copy(&bodyBuffer, r.Body)
*bodyBuffer2 = bodyBuffer
io.Copy(&bodyBuffer, r.Body)
*bodyBuffer2 = bodyBuffer

// Create new ReadClosers so we can split output
r.Body = ioutil.NopCloser(&bodyBuffer)
reqCopy.Body = ioutil.NopCloser(bodyBuffer2)
// Create new ReadClosers so we can split output
r.Body = ioutil.NopCloser(&bodyBuffer)
reqCopy.Body = ioutil.NopCloser(bodyBuffer2)
}

return reqCopy
}

func CopyHttpResponse(r *http.Response) *http.Response {

resCopy := new(http.Response)
*resCopy = *r

defer r.Body.Close()
if r.Body != nil {
defer r.Body.Close()

// Buffer body data
var bodyBuffer bytes.Buffer
bodyBuffer2 := new(bytes.Buffer)
// Buffer body data
var bodyBuffer bytes.Buffer
bodyBuffer2 := new(bytes.Buffer)

io.Copy(&bodyBuffer, r.Body)
*bodyBuffer2 = bodyBuffer
io.Copy(&bodyBuffer, r.Body)
*bodyBuffer2 = bodyBuffer

// Create new ReadClosers so we can split output
r.Body = ioutil.NopCloser(&bodyBuffer)
resCopy.Body = ioutil.NopCloser(bodyBuffer2)
// Create new ReadClosers so we can split output
r.Body = ioutil.NopCloser(&bodyBuffer)
resCopy.Body = ioutil.NopCloser(bodyBuffer2)
}

return resCopy
}

0 comments on commit 6b3a413

Please sign in to comment.