-
Notifications
You must be signed in to change notification settings - Fork 450
feat(telemetry): stitch CLI identity from API response header #5054
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5777331
feat(telemetry): add identity transport to read X-Gotrue-Id header
seanoliver 2be0b48
feat(telemetry): wire identity stitching from API response header
seanoliver 42b28cd
fix(telemetry): sync.Once for identity stitch, rename constant, add t…
seanoliver a271d96
fix(telemetry): use pointer indirection for identity transport callback
seanoliver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package utils | ||
|
|
||
| import "net/http" | ||
|
|
||
| const HeaderGotrueID = "X-Gotrue-Id" | ||
|
|
||
| type identityTransport struct { | ||
| http.RoundTripper | ||
| onGotrueID *func(string) | ||
| } | ||
|
|
||
| func (t *identityTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| resp, err := t.RoundTripper.RoundTrip(req) | ||
| if err != nil { | ||
| return resp, err | ||
| } | ||
| if id := resp.Header.Get(HeaderGotrueID); id != "" && t.onGotrueID != nil && *t.onGotrueID != nil { | ||
| (*t.onGotrueID)(id) | ||
| } | ||
| return resp, err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestIdentityTransport_CapturesGotrueIdHeader(t *testing.T) { | ||
| var captured string | ||
| cb := func(id string) { captured = id } | ||
| transport := &identityTransport{ | ||
| RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| return &http.Response{ | ||
| StatusCode: 200, | ||
| Header: http.Header{"X-Gotrue-Id": []string{"user-abc-123"}}, | ||
| }, nil | ||
| }), | ||
| onGotrueID: &cb, | ||
| } | ||
| req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) | ||
| resp, err := transport.RoundTrip(req) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, 200, resp.StatusCode) | ||
| assert.Equal(t, "user-abc-123", captured) | ||
| } | ||
|
|
||
| func TestIdentityTransport_IgnoresWhenHeaderMissing(t *testing.T) { | ||
| var captured string | ||
| cb := func(id string) { captured = id } | ||
| transport := &identityTransport{ | ||
| RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| return &http.Response{ | ||
| StatusCode: 200, | ||
| Header: http.Header{}, | ||
| }, nil | ||
| }), | ||
| onGotrueID: &cb, | ||
| } | ||
| req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) | ||
| _, err := transport.RoundTrip(req) | ||
| assert.NoError(t, err) | ||
| assert.Empty(t, captured) | ||
| } | ||
|
|
||
| func TestIdentityTransport_NilCallbackDoesNotPanic(t *testing.T) { | ||
| transport := &identityTransport{ | ||
| RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| return &http.Response{ | ||
| StatusCode: 200, | ||
| Header: http.Header{"X-Gotrue-Id": []string{"user-abc-123"}}, | ||
| }, nil | ||
| }), | ||
| onGotrueID: nil, | ||
| } | ||
| req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) | ||
| resp, err := transport.RoundTrip(req) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, 200, resp.StatusCode) | ||
| } | ||
|
|
||
| func TestIdentityTransport_NilFuncBehindPointerDoesNotPanic(t *testing.T) { | ||
| var cb func(string) // nil func | ||
| transport := &identityTransport{ | ||
| RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| return &http.Response{ | ||
| StatusCode: 200, | ||
| Header: http.Header{"X-Gotrue-Id": []string{"user-abc-123"}}, | ||
| }, nil | ||
| }), | ||
| onGotrueID: &cb, | ||
| } | ||
| req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) | ||
| resp, err := transport.RoundTrip(req) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, 200, resp.StatusCode) | ||
| } | ||
|
|
||
| func TestIdentityTransport_InnerTransportError(t *testing.T) { | ||
| var captured string | ||
| cb := func(id string) { captured = id } | ||
| transport := &identityTransport{ | ||
| RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| return nil, assert.AnError | ||
| }), | ||
| onGotrueID: &cb, | ||
| } | ||
| req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) | ||
| resp, err := transport.RoundTrip(req) | ||
| assert.Error(t, err) | ||
| assert.Nil(t, resp) | ||
| assert.Empty(t, captured) | ||
| } | ||
|
|
||
| // roundTripFunc is a test helper to create inline RoundTrippers. | ||
| type roundTripFunc func(*http.Request) (*http.Response, error) | ||
|
|
||
| func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| return f(req) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.