-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Junjie Gao <[email protected]>
- Loading branch information
Showing
8 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,36 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
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. | ||
Copied and adapted from oras (https://github.com/oras-project/oras) | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/notaryproject/notation-go/log" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// WithLoggerLevel returns a context with logrus log entry. | ||
func WithLoggerLevel(ctx context.Context, level logrus.Level) (context.Context, log.Logger) { | ||
logger := logrus.New() | ||
logger.SetFormatter(&logrus.TextFormatter{ | ||
DisableTimestamp: true, | ||
}) | ||
logger.SetLevel(level) | ||
entry := logger.WithContext(ctx) | ||
return log.WithLogger(ctx, entry), entry | ||
} |
This file contains 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,73 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
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. | ||
Copied and adapted from oras (https://github.com/oras-project/oras) | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/notaryproject/notation-go/log" | ||
) | ||
|
||
// Transport is an http.RoundTripper that keeps track of the in-flight | ||
// request and add hooks to report HTTP tracing events. | ||
type Transport struct { | ||
http.RoundTripper | ||
} | ||
|
||
func NewTransport(base http.RoundTripper) *Transport { | ||
return &Transport{base} | ||
} | ||
|
||
// RoundTrip calls base roundtrip while keeping track of the current request. | ||
func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) { | ||
ctx := req.Context() | ||
e := log.GetLogger(ctx) | ||
|
||
e.Debugf(" Request URL: %q", req.URL) | ||
e.Debugf(" Request method: %q", req.Method) | ||
e.Debugf(" Request headers:") | ||
logHeader(req.Header, e) | ||
|
||
resp, err = t.RoundTripper.RoundTrip(req) | ||
if err != nil { | ||
e.Errorf("Error in getting response: %w", err) | ||
} else if resp == nil { | ||
e.Errorf("No response obtained for request %s %q", req.Method, req.URL) | ||
} else { | ||
e.Debugf(" Response Status: %q", resp.Status) | ||
e.Debugf(" Response headers:") | ||
logHeader(resp.Header, e) | ||
} | ||
return resp, err | ||
} | ||
|
||
// logHeader prints out the provided header keys and values, with auth header | ||
// scrubbed. | ||
func logHeader(header http.Header, e log.Logger) { | ||
if len(header) > 0 { | ||
for k, v := range header { | ||
if strings.EqualFold(k, "Authorization") { | ||
v = []string{"*****"} | ||
} | ||
e.Debugf(" %q: %q", k, strings.Join(v, ", ")) | ||
} | ||
} else { | ||
e.Debugf(" Empty header") | ||
} | ||
} |