-
Notifications
You must be signed in to change notification settings - Fork 208
libgit2: add contextual logging to subtransports #778
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
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -45,6 +45,7 @@ package managed | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
|
@@ -55,9 +56,12 @@ import ( | |
"strings" | ||
"sync" | ||
|
||
"github.com/fluxcd/pkg/runtime/logger" | ||
pool "github.com/fluxcd/source-controller/internal/transport" | ||
"github.com/fluxcd/source-controller/pkg/git" | ||
"github.com/go-logr/logr" | ||
git2go "github.com/libgit2/git2go/v33" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
var actionSuffixes = []string{ | ||
|
@@ -81,10 +85,11 @@ func registerManagedHTTP() error { | |
} | ||
|
||
func httpSmartSubtransportFactory(remote *git2go.Remote, transport *git2go.Transport) (git2go.SmartSubtransport, error) { | ||
traceLog.Info("[http]: httpSmartSubtransportFactory") | ||
sst := &httpSmartSubtransport{ | ||
transport: transport, | ||
httpTransport: pool.NewOrIdle(nil), | ||
ctx: context.Background(), | ||
logger: logr.Discard(), | ||
} | ||
|
||
return sst, nil | ||
|
@@ -93,6 +98,21 @@ func httpSmartSubtransportFactory(remote *git2go.Remote, transport *git2go.Trans | |
type httpSmartSubtransport struct { | ||
transport *git2go.Transport | ||
httpTransport *http.Transport | ||
|
||
// once is used to ensure that logger and ctx is set only once, | ||
// on the initial (or only) Action call. Without this a mutex must | ||
// be applied to ensure that ctx won't be changed, as this would be | ||
// prone to race conditions in the stdout processing goroutine. | ||
once sync.Once | ||
// ctx defines the context to be used across long-running or | ||
// cancellable operations. | ||
// Defaults to context.Background(). | ||
ctx context.Context | ||
// logger keeps a Logger instance for logging. This was preferred | ||
// due to the need to have a correlation ID and URL set and | ||
// reused across all log calls. | ||
// If context is not set, this defaults to logr.Discard(). | ||
logger logr.Logger | ||
} | ||
|
||
func (t *httpSmartSubtransport) Action(transportOptionsURL string, action git2go.SmartServiceAction) (git2go.SmartSubtransportStream, error) { | ||
|
@@ -133,6 +153,15 @@ func (t *httpSmartSubtransport) Action(transportOptionsURL string, action git2go | |
} | ||
t.httpTransport.DisableCompression = false | ||
|
||
t.once.Do(func() { | ||
if opts.Context != nil { | ||
t.ctx = opts.Context | ||
t.logger = ctrl.LoggerFrom(t.ctx, | ||
"transportType", "http", | ||
"url", opts.TargetURL) | ||
} | ||
}) | ||
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. This is fine how it is, but I'd expect such one time configurations to be done at the very beginning of the function when we have all the things we need to do it. |
||
|
||
client, req, err := createClientRequest(targetURL, action, t.httpTransport, opts.AuthOpts) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -176,8 +205,10 @@ func (t *httpSmartSubtransport) Action(transportOptionsURL string, action git2go | |
opts.TargetURL = trimActionSuffix(newURL.String()) | ||
AddTransportOptions(transportOptionsURL, *opts) | ||
|
||
debugLog.Info("[http]: server responded with redirect", | ||
"newURL", opts.TargetURL, "StatusCode", req.Response.StatusCode) | ||
// show as info, as this should be visible regardless of the | ||
// chosen log-level. | ||
t.logger.Info("server responded with redirect", | ||
"newUrl", opts.TargetURL, "StatusCode", req.Response.StatusCode) | ||
} | ||
} | ||
} | ||
|
@@ -270,15 +301,16 @@ func createClientRequest(targetURL string, action git2go.SmartServiceAction, | |
} | ||
|
||
func (t *httpSmartSubtransport) Close() error { | ||
traceLog.Info("[http]: httpSmartSubtransport.Close()") | ||
t.logger.V(logger.TraceLevel).Info("httpSmartSubtransport.Close()") | ||
return nil | ||
} | ||
|
||
func (t *httpSmartSubtransport) Free() { | ||
traceLog.Info("[http]: httpSmartSubtransport.Free()") | ||
t.logger.V(logger.TraceLevel).Info("httpSmartSubtransport.Free()") | ||
|
||
if t.httpTransport != nil { | ||
traceLog.Info("[http]: release http transport back to pool") | ||
t.logger.V(logger.TraceLevel).Info("release http transport back to pool") | ||
|
||
pool.Release(t.httpTransport) | ||
t.httpTransport = nil | ||
} | ||
|
@@ -345,18 +377,18 @@ func (self *httpSmartSubtransportStream) Write(buf []byte) (int, error) { | |
|
||
func (self *httpSmartSubtransportStream) Free() { | ||
if self.resp != nil { | ||
traceLog.Info("[http]: httpSmartSubtransportStream.Free()") | ||
self.owner.logger.V(logger.TraceLevel).Info("httpSmartSubtransportStream.Free()") | ||
|
||
if self.resp.Body != nil { | ||
// ensure body is fully processed and closed | ||
// for increased likelihood of transport reuse in HTTP/1.x. | ||
// it should not be a problem to do this more than once. | ||
if _, err := io.Copy(io.Discard, self.resp.Body); err != nil { | ||
traceLog.Error(err, "[http]: cannot discard response body") | ||
self.owner.logger.V(logger.TraceLevel).Error(err, "cannot discard response body") | ||
} | ||
|
||
if err := self.resp.Body.Close(); err != nil { | ||
traceLog.Error(err, "[http]: cannot close response body") | ||
self.owner.logger.V(logger.TraceLevel).Error(err, "cannot close response body") | ||
} | ||
} | ||
} | ||
|
@@ -399,7 +431,7 @@ func (self *httpSmartSubtransportStream) sendRequest() error { | |
req.ContentLength = -1 | ||
} | ||
|
||
traceLog.Info("[http]: new request", "method", req.Method, "URL", req.URL) | ||
self.owner.logger.V(logger.TraceLevel).Info("new request", "method", req.Method, "postUrl", req.URL) | ||
resp, err = self.client.Do(req) | ||
if err != nil { | ||
return 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
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.
Uh oh!
There was an error while loading. Please reload this page.