fix(analytics): keep package names and count artifact downloads only

The proxy passed request-buffer names to the analytics engine, which kept
them. The server reuses those buffers, so later requests rewrote the
stored names (for example lodash became lodas, and API paths appeared as
package names). The engine now copies the names it keeps.

Metadata pages (npm packuments, Go list, info and mod files, PyPI pages)
no longer count as downloads, and a Go module is one analytics key by its
module path. The version count now counts distinct versions.
This commit is contained in:
2026-11-24 02:05:53 +00:00
parent 8b4d4746df
commit 7470c41560
4 changed files with 223 additions and 3 deletions
+13 -2
View File
@@ -4,6 +4,7 @@ package analytics
import (
"sort"
"strings"
"sync"
"time"
@@ -23,6 +24,7 @@ type PackageDownload struct {
// PackageStats holds statistics for a package
type PackageStats struct {
versions map[string]struct{}
LastDownload time.Time
FirstSeen time.Time
Registry string
@@ -111,6 +113,11 @@ func NewEngine(cfg Config) *Engine {
// engine: append to the buffer under downloadsMu, release it, then update
// stats under statsMu.
func (e *Engine) TrackDownload(download PackageDownload) {
// Callers pass names sliced from a request buffer that the server reuses; keep our own copy.
download.Registry = strings.Clone(download.Registry)
download.Name = strings.Clone(download.Name)
download.Version = strings.Clone(download.Version)
// Append to buffer and decide whether buffer is full.
e.downloadsMu.Lock()
e.downloads = append(e.downloads, download)
@@ -154,8 +161,11 @@ func (e *Engine) updateStats(download PackageDownload) {
stats.BytesServed += download.BytesSize
stats.LastDownload = download.Timestamp
// Track unique versions (simplified)
stats.UniqueVersions++
if stats.versions == nil {
stats.versions = make(map[string]struct{})
}
stats.versions[download.Version] = struct{}{}
stats.UniqueVersions = len(stats.versions)
}
// GetPackageStats returns statistics for a specific package
@@ -171,6 +181,7 @@ func (e *Engine) GetPackageStats(registry, name string) (*PackageStats, bool) {
// Return a copy to avoid race conditions
statsCopy := *stats
statsCopy.versions = nil
return &statsCopy, true
}
+52
View File
@@ -0,0 +1,52 @@
package analytics
import (
"testing"
"time"
"unsafe"
)
func newTestEngine(t *testing.T) *Engine {
t.Helper()
e := NewEngine(Config{MaxEvents: 100, FlushInterval: time.Hour})
t.Cleanup(e.Close)
return e
}
// The proxy passes names sliced from a request buffer the server reuses.
func TestTrackDownloadKeepsItsOwnCopyOfTheNames(t *testing.T) {
e := newTestEngine(t)
shared := []byte("lodash")
aliased := unsafe.String(&shared[0], len(shared)) // #nosec G103 -- reproduces a string backed by a reused buffer
e.TrackDownload(PackageDownload{Registry: "npm", Name: aliased, Version: "1.0.0", Timestamp: time.Now()})
copy(shared, "xxxxxx")
if _, ok := e.GetPackageStats("npm", "lodash"); !ok {
t.Errorf("stats lost the package name after the caller reused its buffer: %v", e.GetTopPackages(0))
}
for _, p := range e.GetTopPackages(0) {
if p.Name != "lodash" {
t.Errorf("name = %q, want lodash", p.Name)
}
}
}
func TestUniqueVersionsCountsDistinctVersions(t *testing.T) {
e := newTestEngine(t)
for _, v := range []string{"1.0.0", "1.0.0", "1.1.0", "1.0.0"} {
e.TrackDownload(PackageDownload{Registry: "npm", Name: "left-pad", Version: v, Timestamp: time.Now(), BytesSize: 10})
}
stats, ok := e.GetPackageStats("npm", "left-pad")
if !ok {
t.Fatal("no stats")
}
if stats.TotalDownloads != 4 || stats.UniqueVersions != 2 || stats.BytesServed != 40 {
t.Errorf("stats = %+v, want 4 downloads, 2 versions, 40 bytes", stats)
}
}
func TestUnknownPackageHasNoStats(t *testing.T) {
if _, ok := newTestEngine(t).GetPackageStats("npm", "nope"); ok {
t.Error("unknown package reported stats")
}
}