Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-12702] revert wrappedServeHTTP to use recordDetail #6654

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ tyk_linux_*
*.test

main

/coprocess/*.pb.go-e
19 changes: 8 additions & 11 deletions gateway/handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,19 +343,16 @@ func recordDetail(r *http.Request, spec *APISpec) bool {
}
}

// Are we even checking?
if !spec.GlobalConfig.EnforceOrgDataDetailLogging {
return spec.GlobalConfig.AnalyticsConfig.EnableDetailedRecording
}

// We are, so get session data
session, ok := r.Context().Value(ctx.OrgSessionContext).(*user.SessionState)
if ok && session != nil {
return session.EnableDetailedRecording || session.EnableDetailRecording // nolint:staticcheck // Deprecated DetailRecording
// decide based on org session.
if spec.GlobalConfig.EnforceOrgDataDetailLogging {
session, ok := r.Context().Value(ctx.OrgSessionContext).(*user.SessionState)
if ok && session != nil {
return session.EnableDetailedRecording || session.EnableDetailRecording // nolint:staticcheck // Deprecated DetailRecording
}
}

// no session found, use global config
return spec.GlobalConfig.AnalyticsConfig.EnableDetailedRecording
// no org session found, use global config
return spec.GraphQL.Enabled || spec.GlobalConfig.AnalyticsConfig.EnableDetailedRecording
}

// ServeHTTP will store the request details in the analytics store if necessary and proxy the request to it's
Expand Down
7 changes: 7 additions & 0 deletions gateway/handler_success_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ func TestRecordDetail(t *testing.T) {
},
expect: true,
},
{
title: "graphql request",
spec: testAPISpec(func(spec *APISpec) {
spec.GraphQL.Enabled = true
}),
expect: true,
},
}

for _, tc := range testcases {
Expand Down
2 changes: 1 addition & 1 deletion gateway/reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) Prox
startTime := time.Now()
p.logger.WithField("ts", startTime.UnixNano()).Debug("Started")

resp := p.WrappedServeHTTP(rw, req, true)
resp := p.WrappedServeHTTP(rw, req, recordDetail(req, p.TykAPISpec))

finishTime := time.Since(startTime)
p.logger.WithField("ns", finishTime.Nanoseconds()).Debug("Finished")
Expand Down
43 changes: 43 additions & 0 deletions gateway/reverse_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -2023,3 +2024,45 @@
})

}

func BenchmarkLargeResponsePayload(b *testing.B) {
ts := StartTest(func(_ *config.Config) {})
b.Cleanup(func() {
ts.Close()
jeffy-mathew marked this conversation as resolved.
Show resolved Hide resolved
})

// Create a 500 MB payload of zeros
payloadSize := 500 * 1024 * 1024 // 500 MB in bytes
largePayload := bytes.Repeat([]byte("x"), payloadSize)

largePayloadHandler := func(w http.ResponseWriter, _ *http.Request) {

// Write the payload to the response writer
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(payloadSize))
w.WriteHeader(http.StatusOK)
_, _ = w.Write(largePayload)

Check failure on line 2044 in gateway/reverse_proxy_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `w.Write` is not checked (errcheck)
}
jeffy-mathew marked this conversation as resolved.
Show resolved Hide resolved

// Create a test server with the largePayloadHandler
testServer := httptest.NewServer(http.HandlerFunc(largePayloadHandler))
b.Cleanup(func() {
testServer.Close()
})

ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = true
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = testServer.URL
})

b.ReportAllocs()

for i := 0; i < b.N; i++ {
ts.Run(b, test.TestCase{
Method: http.MethodGet,
Path: "/",
Code: http.StatusOK,
})
}
}
Loading