Skip to content

Commit 1dcfe7f

Browse files
authored
Merge pull request #5040 from estesp/http-trace
Enable Go HTTP tracing of registry interactions
2 parents 6762c6f + 5199213 commit 1dcfe7f

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

cmd/ctr/commands/images/images.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package images
1818

1919
import (
20+
"context"
2021
"fmt"
22+
"net/http/httptrace"
2123
"os"
2224
"sort"
2325
"strings"
@@ -332,3 +334,23 @@ var removeCommand = cli.Command{
332334
return exitErr
333335
},
334336
}
337+
338+
// NewDebugClientTrace returns a Go http trace client predefined to write DNS and connection
339+
// information to the log. This is used via the --trace flag on push and pull operations in ctr.
340+
func NewDebugClientTrace(ctx context.Context) *httptrace.ClientTrace {
341+
return &httptrace.ClientTrace{
342+
DNSStart: func(dnsInfo httptrace.DNSStartInfo) {
343+
log.G(ctx).WithField("host", dnsInfo.Host).Debugf("DNS lookup")
344+
},
345+
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
346+
if dnsInfo.Err != nil {
347+
log.G(ctx).WithField("lookup_err", dnsInfo.Err).Debugf("DNS lookup error")
348+
} else {
349+
log.G(ctx).WithField("result", dnsInfo.Addrs[0].String()).WithField("coalesced", dnsInfo.Coalesced).Debugf("DNS lookup complete")
350+
}
351+
},
352+
GotConn: func(connInfo httptrace.GotConnInfo) {
353+
log.G(ctx).WithField("reused", connInfo.Reused).WithField("remote_addr", connInfo.Conn.RemoteAddr().String()).Debugf("Connection successful")
354+
},
355+
}
356+
}

cmd/ctr/commands/images/pull.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package images
1818

1919
import (
2020
"fmt"
21+
"net/http/httptrace"
2122

2223
"github.com/containerd/containerd"
2324
"github.com/containerd/containerd/cmd/ctr/commands"
@@ -54,6 +55,10 @@ command. As part of this process, we do the following:
5455
Name: "all-platforms",
5556
Usage: "pull content and metadata from all platforms",
5657
},
58+
cli.BoolFlag{
59+
Name: "trace",
60+
Usage: "enable HTTP tracing for registry interactions",
61+
},
5762
cli.BoolFlag{
5863
Name: "all-metadata",
5964
Usage: "Pull metadata for all platforms",
@@ -88,6 +93,9 @@ command. As part of this process, we do the following:
8893
return err
8994
}
9095

96+
if context.Bool("trace") {
97+
ctx = httptrace.WithClientTrace(ctx, NewDebugClientTrace(ctx))
98+
}
9199
img, err := content.Fetch(ctx, client, ref, config)
92100
if err != nil {
93101
return err

cmd/ctr/commands/images/push.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package images
1818

1919
import (
2020
gocontext "context"
21+
"net/http/httptrace"
2122
"os"
2223
"sync"
2324
"text/tabwriter"
@@ -59,6 +60,9 @@ var pushCommand = cli.Command{
5960
Name: "manifest-type",
6061
Usage: "media type of manifest digest",
6162
Value: ocispec.MediaTypeImageManifest,
63+
}, cli.BoolFlag{
64+
Name: "trace",
65+
Usage: "enable HTTP tracing for registry interactions",
6266
}, cli.StringSliceFlag{
6367
Name: "platform",
6468
Usage: "push content from a specific platform",
@@ -119,6 +123,9 @@ var pushCommand = cli.Command{
119123
}
120124
}
121125

126+
if context.Bool("trace") {
127+
ctx = httptrace.WithClientTrace(ctx, NewDebugClientTrace(ctx))
128+
}
122129
resolver, err := commands.GetResolver(ctx, context)
123130
if err != nil {
124131
return err

0 commit comments

Comments
 (0)