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 all 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: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- master
- release-**
tags:
- 'v*'

Check warning on line 24 in .github/workflows/release.yml

View workflow job for this annotation

GitHub Actions / test-controller-api

Using non-prod variation

Check warning on line 24 in .github/workflows/release.yml

View workflow job for this annotation

GitHub Actions / test-controller-distros

Using test variation
env:
GOPRIVATE: github.com/TykTechnologies
VARIATION: inverted
Expand Down Expand Up @@ -334,7 +334,7 @@
env:
GH_TOKEN: ${{ github.token }}
run: |
gh release download --repo github.com/tyklabs/tyk-pro --archive tar.gz -O env.tgz
gh release download --repo github.com/TykTechnologies/tyk-pro --archive tar.gz -O env.tgz
mkdir auto && tar --strip-components=1 -C auto -xzvf env.tgz
- name: env up
shell: bash
Expand Down
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
38 changes: 38 additions & 0 deletions gateway/reverse_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -2023,3 +2024,40 @@ func TestQuotaResponseHeaders(t *testing.T) {
})

}

func BenchmarkLargeResponsePayload(b *testing.B) {
ts := StartTest(func(_ *config.Config) {})
b.Cleanup(ts.Close)

// 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) {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(payloadSize))
w.WriteHeader(http.StatusOK)
_, err := w.Write(largePayload)
assert.NoError(b, err)
}
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(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