Skip to content

Commit

Permalink
Merge 0fcdfcf into 79fd301
Browse files Browse the repository at this point in the history
  • Loading branch information
dencoded committed Nov 28, 2018
2 parents 79fd301 + 0fcdfcf commit d3e0e8f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
95 changes: 95 additions & 0 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,101 @@ func TestAnalytics(t *testing.T) {
t.Error("Analytics record do not match", record)
}
})

t.Run("Detailed analytics", func(t *testing.T) {
defer resetTestConfig()
globalConf := config.Global()
globalConf.AnalyticsConfig.EnableDetailedRecording = true
config.SetGlobal(globalConf)

buildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = false
spec.Proxy.ListenPath = "/"
})

key := createSession()

authHeaders := map[string]string{
"authorization": key,
}

ts.Run(t, test.TestCase{
Path: "/", Headers: authHeaders, Code: 200,
})

// let records to to be sent
time.Sleep(recordsBufferFlushInterval + 50)

results := analytics.Store.GetAndDeleteSet(analyticsKeyName)
if len(results) != 1 {
t.Error("Should return 1 record: ", len(results))
}

var record AnalyticsRecord
msgpack.Unmarshal(results[0].([]byte), &record)
if record.ResponseCode != 200 {
t.Error("Analytics record do not match", record)
}

if record.RawRequest == "" {
t.Error("Detailed request info not found", record)
}

if record.RawResponse == "" {
t.Error("Detailed response info not found", record)
}
})

t.Run("Detailed analytics with cache", func(t *testing.T) {
defer resetTestConfig()
globalConf := config.Global()
globalConf.AnalyticsConfig.EnableDetailedRecording = true
config.SetGlobal(globalConf)

buildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = false
spec.Proxy.ListenPath = "/"
spec.CacheOptions = apidef.CacheOptions{
CacheTimeout: 120,
EnableCache: true,
CacheAllSafeRequests: true,
}
})

key := createSession()

authHeaders := map[string]string{
"authorization": key,
}

ts.Run(t, []test.TestCase{
{Path: "/", Headers: authHeaders, Code: 200},
{Path: "/", Headers: authHeaders, Code: 200},
}...)

// let records to to be sent
time.Sleep(recordsBufferFlushInterval + 50)

results := analytics.Store.GetAndDeleteSet(analyticsKeyName)
if len(results) != 2 {
t.Error("Should return 1 record: ", len(results))
}

// Take second cached request
var record AnalyticsRecord
msgpack.Unmarshal(results[1].([]byte), &record)
if record.ResponseCode != 200 {
t.Error("Analytics record do not match", record)
}

if record.RawRequest == "" {
t.Error("Detailed request info not found", record)
}

if record.RawResponse == "" {
t.Error("Detailed response info not found", record)
}
})
}

func TestListener(t *testing.T) {
Expand Down
13 changes: 11 additions & 2 deletions handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"bytes"
"encoding/base64"
"io"
"io/ioutil"
"net/http"
"runtime/pprof"
"strconv"
"strings"
"time"

cache "github.com/pmylund/go-cache"
"github.com/pmylund/go-cache"

"github.com/TykTechnologies/tyk/request"

Expand Down Expand Up @@ -151,10 +152,18 @@ func (s *SuccessHandler) RecordHit(r *http.Request, timing int64, code int, resp
// mw_redis_cache instead? is there a reason not
// to include that in the analytics?
if responseCopy != nil {
// Make a copy of response body as Response.Write resets underlying buffer to 0 len
responseCopyBodyBuffer := new(bytes.Buffer)
recordBodyBuffer := new(bytes.Buffer)
io.Copy(responseCopyBodyBuffer, responseCopy.Body) // this resets responseCopy.Body buffer to 0 len
*recordBodyBuffer = *responseCopyBodyBuffer
responseCopy.Body = ioutil.NopCloser(recordBodyBuffer) // fix responseCopy.Body buffer after io.Copy
// Get the wire format representation
var wireFormatRes bytes.Buffer
responseCopy.Write(&wireFormatRes)
responseCopy.Write(&wireFormatRes) // this resets responseCopy.Body buffer to 0 len again
rawResponse = base64.StdEncoding.EncodeToString(wireFormatRes.Bytes())
// fix Body to be read again
responseCopy.Body = ioutil.NopCloser(responseCopyBodyBuffer) // fix responseCopy.Body buffer after responseCopy.Write
}
}

Expand Down

0 comments on commit d3e0e8f

Please sign in to comment.