Skip to content

Commit 1a35583

Browse files
committed
cmd/go: add tracing for querying and downloading from the proxy
This CL adds tracing spans for modload.queryPattern, modload.queryProxy, modload.QueryPattern, modload.QueryPattern.queryModule, modload.queryPrefixModules and modfetch.Download. Updates #38714 Change-Id: I537c7fa4f466c691c1b60ec73ef8a2277af49cd7 Reviewed-on: https://go-review.googlesource.com/c/go/+/242786 Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent c0cf190 commit 1a35583

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

src/cmd/go/internal/modcmd/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
120120
return
121121
}
122122
mod := module.Version{Path: m.Path, Version: m.Version}
123-
m.Zip, err = modfetch.DownloadZip(mod)
123+
m.Zip, err = modfetch.DownloadZip(ctx, mod)
124124
if err != nil {
125125
m.Error = err.Error()
126126
return

src/cmd/go/internal/modfetch/fetch.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"cmd/go/internal/par"
2525
"cmd/go/internal/renameio"
2626
"cmd/go/internal/robustio"
27+
"cmd/go/internal/trace"
2728

2829
"golang.org/x/mod/module"
2930
"golang.org/x/mod/sumdb/dirhash"
@@ -48,7 +49,7 @@ func Download(ctx context.Context, mod module.Version) (dir string, err error) {
4849
err error
4950
}
5051
c := downloadCache.Do(mod, func() interface{} {
51-
dir, err := download(mod)
52+
dir, err := download(ctx, mod)
5253
if err != nil {
5354
return cached{"", err}
5455
}
@@ -58,7 +59,10 @@ func Download(ctx context.Context, mod module.Version) (dir string, err error) {
5859
return c.dir, c.err
5960
}
6061

61-
func download(mod module.Version) (dir string, err error) {
62+
func download(ctx context.Context, mod module.Version) (dir string, err error) {
63+
ctx, span := trace.StartSpan(ctx, "modfetch.download "+mod.String())
64+
defer span.Done()
65+
6266
// If the directory exists, and no .partial file exists, the module has
6367
// already been completely extracted. .partial files may be created when a
6468
// module zip directory is extracted in place instead of being extracted to a
@@ -73,7 +77,7 @@ func download(mod module.Version) (dir string, err error) {
7377
// To avoid cluttering the cache with extraneous files,
7478
// DownloadZip uses the same lockfile as Download.
7579
// Invoke DownloadZip before locking the file.
76-
zipfile, err := DownloadZip(mod)
80+
zipfile, err := DownloadZip(ctx, mod)
7781
if err != nil {
7882
return "", err
7983
}
@@ -143,6 +147,7 @@ func download(mod module.Version) (dir string, err error) {
143147
return "", err
144148
}
145149

150+
ctx, span = trace.StartSpan(ctx, "unzip "+zipfile)
146151
if unzipInPlace {
147152
if err := ioutil.WriteFile(partialPath, nil, 0666); err != nil {
148153
return "", err
@@ -172,6 +177,7 @@ func download(mod module.Version) (dir string, err error) {
172177
return "", err
173178
}
174179
}
180+
defer span.Done()
175181

176182
if !cfg.ModCacheRW {
177183
// Make dir read-only only *after* renaming it.
@@ -196,7 +202,7 @@ var downloadZipCache par.Cache
196202

197203
// DownloadZip downloads the specific module version to the
198204
// local zip cache and returns the name of the zip file.
199-
func DownloadZip(mod module.Version) (zipfile string, err error) {
205+
func DownloadZip(ctx context.Context, mod module.Version) (zipfile string, err error) {
200206
// The par.Cache here avoids duplicate work.
201207
type cached struct {
202208
zipfile string
@@ -231,15 +237,18 @@ func DownloadZip(mod module.Version) (zipfile string, err error) {
231237
if err := os.MkdirAll(filepath.Dir(zipfile), 0777); err != nil {
232238
return cached{"", err}
233239
}
234-
if err := downloadZip(mod, zipfile); err != nil {
240+
if err := downloadZip(ctx, mod, zipfile); err != nil {
235241
return cached{"", err}
236242
}
237243
return cached{zipfile, nil}
238244
}).(cached)
239245
return c.zipfile, c.err
240246
}
241247

242-
func downloadZip(mod module.Version, zipfile string) (err error) {
248+
func downloadZip(ctx context.Context, mod module.Version, zipfile string) (err error) {
249+
ctx, span := trace.StartSpan(ctx, "modfetch.downloadZip "+zipfile)
250+
defer span.Done()
251+
243252
// Clean up any remaining tempfiles from previous runs.
244253
// This is only safe to do because the lock file ensures that their
245254
// writers are no longer active.

src/cmd/go/internal/modfetch/zip_sum_test/zip_sum_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package zip_sum_test
1717

1818
import (
19+
"context"
1920
"crypto/sha256"
2021
"encoding/csv"
2122
"encoding/hex"
@@ -119,7 +120,7 @@ func TestZipSums(t *testing.T) {
119120
name := fmt.Sprintf("%s@%s", strings.ReplaceAll(test.m.Path, "/", "_"), test.m.Version)
120121
t.Run(name, func(t *testing.T) {
121122
t.Parallel()
122-
zipPath, err := modfetch.DownloadZip(test.m)
123+
zipPath, err := modfetch.DownloadZip(context.Background(), test.m)
123124
if err != nil {
124125
if *updateTestData {
125126
t.Logf("%s: could not download module: %s (will remove from testdata)", test.m, err)

src/cmd/go/internal/modload/query.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"cmd/go/internal/modfetch"
2020
"cmd/go/internal/search"
2121
"cmd/go/internal/str"
22+
"cmd/go/internal/trace"
2223

2324
"golang.org/x/mod/module"
2425
"golang.org/x/mod/semver"
@@ -77,6 +78,9 @@ func (queryDisabledError) Error() string {
7778
}
7879

7980
func queryProxy(ctx context.Context, proxy, path, query, current string, allowed func(module.Version) bool) (*modfetch.RevInfo, error) {
81+
ctx, span := trace.StartSpan(ctx, "modload.queryProxy "+path+" "+query)
82+
defer span.Done()
83+
8084
if current != "" && !semver.IsValid(current) {
8185
return nil, fmt.Errorf("invalid previous version %q", current)
8286
}
@@ -403,6 +407,9 @@ func QueryPackage(ctx context.Context, path, query string, allowed func(module.V
403407
// the main module and only the version "latest", without checking for other
404408
// possible modules.
405409
func QueryPattern(ctx context.Context, pattern, query string, allowed func(module.Version) bool) ([]QueryResult, error) {
410+
ctx, span := trace.StartSpan(ctx, "modload.QueryPattern "+pattern+" "+query)
411+
defer span.Done()
412+
406413
base := pattern
407414

408415
firstError := func(m *search.Match) error {
@@ -470,7 +477,10 @@ func QueryPattern(ctx context.Context, pattern, query string, allowed func(modul
470477
}
471478

472479
err := modfetch.TryProxies(func(proxy string) error {
473-
queryModule := func(path string) (r QueryResult, err error) {
480+
queryModule := func(ctx context.Context, path string) (r QueryResult, err error) {
481+
ctx, span := trace.StartSpan(ctx, "modload.QueryPattern.queryModule ["+proxy+"] "+path)
482+
defer span.Done()
483+
474484
current := findCurrentVersion(path)
475485
r.Mod.Path = path
476486
r.Rev, err = queryProxy(ctx, proxy, path, query, current, allowed)
@@ -499,7 +509,7 @@ func QueryPattern(ctx context.Context, pattern, query string, allowed func(modul
499509
}
500510

501511
var err error
502-
results, err = queryPrefixModules(candidateModules, queryModule)
512+
results, err = queryPrefixModules(ctx, candidateModules, queryModule)
503513
return err
504514
})
505515

@@ -543,7 +553,10 @@ type prefixResult struct {
543553
err error
544554
}
545555

546-
func queryPrefixModules(candidateModules []string, queryModule func(path string) (QueryResult, error)) (found []QueryResult, err error) {
556+
func queryPrefixModules(ctx context.Context, candidateModules []string, queryModule func(ctx context.Context, path string) (QueryResult, error)) (found []QueryResult, err error) {
557+
ctx, span := trace.StartSpan(ctx, "modload.queryPrefixModules")
558+
defer span.Done()
559+
547560
// If the path we're attempting is not in the module cache and we don't have a
548561
// fetch result cached either, we'll end up making a (potentially slow)
549562
// request to the proxy or (often even slower) the origin server.
@@ -556,8 +569,9 @@ func queryPrefixModules(candidateModules []string, queryModule func(path string)
556569
var wg sync.WaitGroup
557570
wg.Add(len(candidateModules))
558571
for i, p := range candidateModules {
572+
ctx := trace.StartGoroutine(ctx)
559573
go func(p string, r *result) {
560-
r.QueryResult, r.err = queryModule(p)
574+
r.QueryResult, r.err = queryModule(ctx, p)
561575
wg.Done()
562576
}(p, &results[i])
563577
}

0 commit comments

Comments
 (0)