-
Notifications
You must be signed in to change notification settings - Fork 0
feat: debug tools #266
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
feat: debug tools #266
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Package debugtools contains tools used for debugging the application. | ||
| package debugtools | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httputil" | ||
|
|
||
| slogctx "github.com/veqryn/slog-context" | ||
| ) | ||
|
|
||
| // transport is a wrapper for an http.RoundTripper which logs the request and response dumps. | ||
| type transport struct { | ||
| base http.RoundTripper | ||
| } | ||
|
|
||
| func NewTransport(base http.RoundTripper) http.RoundTripper { | ||
| return &transport{ | ||
| base: base, | ||
| } | ||
| } | ||
|
|
||
| func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| ctx := req.Context() | ||
| reqDump, _ := httputil.DumpRequestOut(req, true) | ||
| ctx = slogctx.With(ctx, "request", reqDump) | ||
| resp, err := t.base.RoundTrip(req) | ||
| if err != nil { | ||
| slogctx.Debug(ctx, "http request executed with an error") | ||
| } else { | ||
| respDump, _ := httputil.DumpResponse(resp, true) | ||
| slogctx.Debug(ctx, "http request executed successfully", "response", respDump) | ||
| } | ||
|
|
||
| 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,58 @@ | ||
| package debugtools | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| type dummyRoundTripper struct { | ||
| resp *http.Response | ||
| err error | ||
| } | ||
|
|
||
| func (rt dummyRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { | ||
| return rt.resp, rt.err | ||
| } | ||
|
|
||
| func Test_transport_RoundTrip(t *testing.T) { | ||
| const url = "http://localhost" | ||
| resp := httptest.NewRecorder().Result() | ||
| tests := []struct { | ||
| name string | ||
| base http.RoundTripper | ||
| req *http.Request | ||
| want *http.Response | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "Round trip", | ||
| base: dummyRoundTripper{resp: resp, err: nil}, | ||
| req: httptest.NewRequest(http.MethodGet, url, nil), | ||
| want: resp, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "Return an error", | ||
| base: dummyRoundTripper{resp: nil, err: errors.New("err")}, | ||
| req: httptest.NewRequest(http.MethodGet, url, nil), | ||
| want: nil, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| tr := NewTransport(tt.base) | ||
| got, err := tr.RoundTrip(tt.req) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("transport.RoundTrip() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Errorf("transport.RoundTrip() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,81 @@ | ||
| package debugtools | ||
|
|
||
| import ( | ||
| "os" | ||
| "sync" | ||
| "sync/atomic" | ||
| ) | ||
|
|
||
| var cache sync.Map // name string -> value *setting | ||
| var empty string | ||
|
|
||
| type Setting struct { | ||
| *setting | ||
|
|
||
| name string | ||
| once sync.Once | ||
| } | ||
|
|
||
| type setting struct { | ||
| value atomic.Pointer[string] | ||
| } | ||
|
|
||
| func NewSetting(name string) *Setting { | ||
| return &Setting{name: name} | ||
| } | ||
|
|
||
| func (s *Setting) Name() string { | ||
| return s.name | ||
| } | ||
|
|
||
| func (s *Setting) Value() string { | ||
| s.once.Do(func() { | ||
| s.setting = lookup(s.Name()) | ||
| }) | ||
|
|
||
| return *s.value.Load() | ||
| } | ||
|
|
||
| func (s *Setting) String() string { | ||
| return s.Name() + "=" + s.Value() | ||
| } | ||
|
|
||
| func lookup(name string) *setting { | ||
| if v, ok := cache.Load(name); ok { | ||
| //nolint:forcetypeassert | ||
| return v.(*setting) | ||
| } | ||
|
|
||
| s := new(setting) | ||
| s.value.Store(&empty) | ||
| if v, loaded := cache.LoadOrStore(name, s); loaded { | ||
| //nolint:forcetypeassert | ||
| return v.(*setting) | ||
| } | ||
|
|
||
| return s | ||
| } | ||
|
|
||
| func init() { | ||
| env := os.Getenv("DEBUGFEATURES") | ||
| parse(env) | ||
| } | ||
|
|
||
| // parse parses DEBUGFEATURES environment variable in the form of k1=v1,k2=v2,k3=v3 | ||
| // and stores the keys. | ||
| func parse(s string) { | ||
| end := len(s) | ||
| eq := -1 | ||
| for i := end - 1; i >= -1; i-- { | ||
| if i == -1 || s[i] == ',' { | ||
| if eq >= 0 { | ||
| name, arg := s[i+1:eq], s[eq+1:end] | ||
| lookup(name).value.Store(&arg) | ||
| } | ||
| eq = -1 | ||
| end = i | ||
| } else if s[i] == '=' { | ||
| eq = i | ||
| } | ||
| } | ||
| } | ||
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,82 @@ | ||
| package debugtools | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSetting_Name(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| setting *Setting | ||
| want string | ||
| }{ | ||
| { | ||
| name: "Get setting name", | ||
| setting: NewSetting("testsetting"), | ||
| want: "testsetting", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := tt.setting.Name(); got != tt.want { | ||
| t.Errorf("Setting.Name() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSetting_Value(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| newSetting func() *Setting | ||
| want string | ||
| }{ | ||
| { | ||
| name: "Read setting value", | ||
| newSetting: func() *Setting { | ||
| const featureName = "testfeature2" | ||
| const env = "testfeature1=1," + featureName + "=somevalue,testfeature3=3" | ||
|
|
||
| parse(env) | ||
| return NewSetting(featureName) | ||
| }, | ||
| want: "somevalue", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| s := tt.newSetting() | ||
| if got := s.Value(); got != tt.want { | ||
| t.Errorf("Setting.Value() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSetting_String(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| newSetting func() *Setting | ||
| want string | ||
| }{ | ||
| { | ||
| name: "Setting kv", | ||
| newSetting: func() *Setting { | ||
| const featureName = "testfeature2" | ||
| const env = "testfeature1=1," + featureName + "=somevalue,testfeature3=3" | ||
|
|
||
| parse(env) | ||
| return NewSetting(featureName) | ||
| }, | ||
| want: "testfeature2=somevalue", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| s := tt.newSetting() | ||
| if got := s.String(); got != tt.want { | ||
| t.Errorf("Setting.String() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: typo, double parses
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't a typo. It was the format 'function_name (aka parse)' does (parses) something.