-
Notifications
You must be signed in to change notification settings - Fork 95
CLOUDP-319781: Added http RoundTripper that logs payload diffs for Atlas API endpoints #2525
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
6 commits
Select commit
Hold shift + click to select a range
9346b82
Added http roundtripper that logs payload diffs for Atlas API endpoints
igor-karpukhin b94f557
Regenerated licences
igor-karpukhin 49830ff
Only use diff transport if the log level is lower or equal to DEBUG
igor-karpukhin 6336762
Added debug logging
igor-karpukhin f825717
updated licences
igor-karpukhin 05ac1bb
Added licence header
igor-karpukhin 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 |
---|---|---|
@@ -1 +1 @@ | ||
100644 b70bbe378c03133042f1234347ba66493c607947 go.mod | ||
100644 2eb735b209d48bcf2f3009003bafe7735bab42dd go.mod |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,127 @@ | ||
// Copyright 2025 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package httputil | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/yudai/gojsondiff" | ||
"github.com/yudai/gojsondiff/formatter" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type TransportWithDiff struct { | ||
transport http.RoundTripper | ||
log *zap.SugaredLogger | ||
} | ||
|
||
func NewTransportWithDiff(transport http.RoundTripper, log *zap.SugaredLogger) *TransportWithDiff { | ||
return &TransportWithDiff{ | ||
transport: transport, | ||
log: log, | ||
} | ||
} | ||
|
||
func (t *TransportWithDiff) RoundTrip(req *http.Request) (*http.Response, error) { | ||
if req.Method == http.MethodPut || req.Method == http.MethodPatch { | ||
diffString, err := t.tryCalculateDiff(req, | ||
cleanLinksField, | ||
cleanCreatedField, | ||
) | ||
if err != nil { | ||
t.log.Debug("failed to calculate diff", zap.Error(err)) | ||
} else { | ||
t.log.Debug("JSON diff text", | ||
zap.String("url", req.URL.String()), | ||
zap.Any("diff", diffString), | ||
) | ||
} | ||
} | ||
return t.transport.RoundTrip(req) | ||
} | ||
|
||
type cleanupFunc func(map[string]interface{}) | ||
|
||
func cleanLinksField(data map[string]interface{}) { | ||
if _, ok := data["links"]; ok { | ||
delete(data, "links") | ||
} | ||
} | ||
|
||
func cleanCreatedField(data map[string]interface{}) { | ||
if _, ok := data["created"]; ok { | ||
delete(data, "created") | ||
} | ||
} | ||
|
||
func (t *TransportWithDiff) tryCalculateDiff(req *http.Request, cleanupFuncs ...cleanupFunc) (string, error) { | ||
var bodyCopy []byte | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: would have split this into further functions, but ok. |
||
if req.Body != nil { | ||
bodyCopy, _ = io.ReadAll(req.Body) | ||
req.Body = io.NopCloser(bytes.NewBuffer(bodyCopy)) | ||
} | ||
|
||
defer func() { | ||
req.Body = io.NopCloser(bytes.NewBuffer(bodyCopy)) | ||
}() | ||
|
||
getReq, _ := http.NewRequestWithContext(req.Context(), http.MethodGet, req.URL.String(), nil) | ||
getReq.Header = req.Header | ||
|
||
getResp, err := t.transport.RoundTrip(getReq) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to GET original resource: %w", err) | ||
} | ||
defer getResp.Body.Close() | ||
|
||
payloadFromGet, _ := io.ReadAll(getResp.Body) | ||
|
||
var payloadFromGetParsed map[string]interface{} | ||
err = json.Unmarshal(payloadFromGet, &payloadFromGetParsed) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to unmarshal payloadFromGetParsed JSON: %w", err) | ||
} | ||
|
||
for _, cFn := range cleanupFuncs { | ||
cFn(payloadFromGetParsed) | ||
} | ||
|
||
payloadBytes, err := json.Marshal(payloadFromGetParsed) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to marshal payloadFromGetParsed JSON: %w", err) | ||
} | ||
|
||
differ := gojsondiff.New() | ||
diff, err := differ.Compare(payloadBytes, bodyCopy) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to compare JSON payloads: %w", err) | ||
} | ||
|
||
fmtr := formatter.NewAsciiFormatter(payloadFromGetParsed, formatter.AsciiFormatterConfig{ | ||
ShowArrayIndex: true, | ||
Coloring: false, | ||
}) | ||
|
||
diffString, err := fmtr.Format(diff) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to format diff: %w", err) | ||
} | ||
|
||
return diffString, nil | ||
} |
Oops, something went wrong.
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.
:nit: why try...?