diff options
| author | Ethan Lee <ethanalee@google.com> | 2026-01-12 19:57:54 +0000 |
|---|---|---|
| committer | Ethan Lee <ethanalee@google.com> | 2026-01-15 12:21:49 -0800 |
| commit | 576fb9481e5c52c5d6679fdefa4b07607989d00d (patch) | |
| tree | fdb49a91e0bfa20b2d8338ab63c8ccf5b1ad3e90 | |
| parent | ddf96859c35103ea7898296565dc4e8272d7c6d3 (diff) | |
| download | go-x-pkgsite-576fb9481e5c52c5d6679fdefa4b07607989d00d.tar.xz | |
internal/frontend: add module and package metadata to codewiki link
- These metrics can provide further granularity for filtering usage
patterns.
Change-Id: I110854ac2cbcef54d02f1dd02cb27b917ab17705
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/735800
Auto-Submit: Ethan Lee <ethanalee@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
| -rw-r--r-- | internal/dcensus/dcensus.go | 10 | ||||
| -rw-r--r-- | internal/frontend/links.go | 13 | ||||
| -rw-r--r-- | internal/frontend/server.go | 6 |
3 files changed, 21 insertions, 8 deletions
diff --git a/internal/dcensus/dcensus.go b/internal/dcensus/dcensus.go index 2f6ac7a5..b40b4936 100644 --- a/internal/dcensus/dcensus.go +++ b/internal/dcensus/dcensus.go @@ -57,10 +57,12 @@ func NewRouter(tagger RouteTagger) *Router { } } -func RecordClick(ctx context.Context, url string, target string) { +func RecordClick(ctx context.Context, url, target, module, pkg string) { mutators := []tag.Mutator{ tag.Upsert(KeyTargetURL, url), tag.Upsert(KeyReferrer, target), + tag.Upsert(KeyModule, module), + tag.Upsert(KeyPackage, pkg), } stats.RecordWithTags(ctx, mutators, CodeWikiClickCount.M(1)) } @@ -254,12 +256,16 @@ var ( KeyReferrer = tag.MustNewKey("referrer") // KeyTargetURL is a tag key for the target URL. KeyTargetURL = tag.MustNewKey("target_url") + // KeyModule is a tag key for the module path. + KeyModule = tag.MustNewKey("module") + // KeyPackage is a tag key for the package path. + KeyPackage = tag.MustNewKey("package") CodeWikiClickCount = stats.Int64("go-discovery/frontend_codewiki_clicks", "Codewiki link clicks", stats.UnitDimensionless) CodeWikiClickCountView = &view.View{ Name: "go-discovery/frontend/codewiki_clicks", Description: "Count of Codewiki link clicks", - TagKeys: []tag.Key{KeyReferrer, KeyTargetURL}, + TagKeys: []tag.Key{KeyReferrer, KeyTargetURL, KeyModule, KeyPackage}, Measure: CodeWikiClickCount, Aggregation: view.Count(), } diff --git a/internal/frontend/links.go b/internal/frontend/links.go index 3e8a4de4..e4ecb701 100644 --- a/internal/frontend/links.go +++ b/internal/frontend/links.go @@ -24,7 +24,7 @@ const ( // depsDevTimeout is the time budget for making requests to deps.dev. depsDevTimeout = 250 * time.Millisecond - codeWikiPrefix = "/codewiki?url=" + codeWikiPrefix = "/codewiki?" attributionParams = "?utm_source=first_party_link&utm_medium=go_pkg_web&utm_campaign=" ) @@ -111,14 +111,15 @@ func fetchDepsDevURL(ctx context.Context, client *http.Client, modulePath, versi // codeWikiTimeout then the empty string is returned instead. func codeWikiURLGenerator(ctx context.Context, client *http.Client, um *internal.UnitMeta, recordClick bool) func() string { fetch := func(ctx context.Context, client *http.Client) (string, error) { - return fetchCodeWikiURL(ctx, client, um.ModulePath, recordClick) + return fetchCodeWikiURL(ctx, client, um, recordClick) } return newURLGenerator(ctx, client, "codewiki.google", codeWikiTimeout, fetch) } // fetchCodeWikiURL makes a request to codewiki to check whether the given // path is known there, and if so it returns the link to that page. -func fetchCodeWikiURL(ctx context.Context, client *http.Client, path string, recordClick bool) (string, error) { +func fetchCodeWikiURL(ctx context.Context, client *http.Client, um *internal.UnitMeta, recordClick bool) (string, error) { + path := um.ModulePath if strings.HasPrefix(path, "golang.org/x/") { path = strings.Replace(path, "golang.org/x/", "github.com/golang/", 1) } @@ -144,7 +145,11 @@ func fetchCodeWikiURL(ctx context.Context, client *http.Client, path string, rec } res := codeWikiURLBase + path + attributionParams + path if recordClick { - res = codeWikiPrefix + url.QueryEscape(res) + v := url.Values{} + v.Set("url", res) + v.Set("module", um.ModulePath) + v.Set("package", um.Path) + res = codeWikiPrefix + v.Encode() } return res, nil } diff --git a/internal/frontend/server.go b/internal/frontend/server.go index 9648c5e7..5a554635 100644 --- a/internal/frontend/server.go +++ b/internal/frontend/server.go @@ -78,7 +78,7 @@ type FetchServerInterface interface { ds internal.PostgresDB, fullPath, modulePath, requestedVersion string) (err error) } -type RecordClickFunc func(ctx context.Context, url string, target string) +type RecordClickFunc func(ctx context.Context, url, target, module, pkg string) // ServerConfig contains everything needed by a Server. type ServerConfig struct { @@ -148,13 +148,15 @@ func NewServer(scfg ServerConfig) (_ *Server, err error) { func (s *Server) handleCodeWikiRedirect(w http.ResponseWriter, r *http.Request) { url := r.FormValue("url") + module := r.FormValue("module") + pkg := r.FormValue("package") if url == "" { http.Error(w, "missing url", http.StatusBadRequest) return } ctx := r.Context() if s.recordCodeWikiMetrics != nil { - s.recordCodeWikiMetrics(ctx, url, r.Header.Get("Referer")) + s.recordCodeWikiMetrics(ctx, url, r.Header.Get("Referer"), module, pkg) } http.Redirect(w, r, url, http.StatusFound) } |
