Files
zot/pkg/debug/pprof/pprof.go
T
Luca Muscariello 2402296e9a fix: migrate to Go module v2 for proper semantic versioning (#3462)
* fix: migrate to Go module v2 for proper semantic versioning

This change updates the module path from 'zotregistry.dev/zot' to
'zotregistry.dev/zot/v2' to comply with Go's semantic versioning rules.

According to Go's module versioning requirements, major version v2+
must include the major version in the module path. The current
module path 'zotregistry.dev/zot' only supports v0.x.x and v1.x.x
versions, making existing v2.x.x tags (like v2.1.8) unusable.

Changes:
- Updated go.mod module path to zotregistry.dev/zot/v2
- Updated all internal import paths across 280+ Go source files
- Updated configuration files (golangcilint.yaml, gqlgen.yml)
- Updated README.md Go reference badge

This fix enables proper use of existing v2.x.x Git tags and allows
external packages to import zot v2+ versions without compatibility
errors.

Resolves: Go module import compatibility for v2+ versions
Fixes: #3071
Signed-off-by: Luca Muscariello <muscariello@ieee.org>

* fix: regenerate GraphQL files with updated v2 import paths

The gqlgen tool needs to regenerate the GraphQL schema files after
the module path change to use the new v2 imports.

Signed-off-by: Luca Muscariello <muscariello@ieee.org>

---------

Signed-off-by: Luca Muscariello <muscariello@ieee.org>
2025-10-16 22:43:47 -07:00

154 lines
4.3 KiB
Go

//go:build profile
// +build profile
package pprof
import (
"bytes"
"fmt"
"html"
"io"
"net/http"
"net/http/pprof"
"net/url"
runPprof "runtime/pprof"
"sort"
"strings"
"github.com/gorilla/mux"
"zotregistry.dev/zot/v2/pkg/api/config"
registryConst "zotregistry.dev/zot/v2/pkg/api/constants"
zcommon "zotregistry.dev/zot/v2/pkg/common"
"zotregistry.dev/zot/v2/pkg/debug/constants"
"zotregistry.dev/zot/v2/pkg/log"
)
type profileEntry struct {
Name string
Href string
Desc string
Count int
}
var profileDescriptions = map[string]string{ //nolint: gochecknoglobals
"allocs": "A sampling of all past memory allocations",
"block": "Stack traces that led to blocking on synchronization primitives",
"cmdline": "The command line invocation of the current program",
"goroutine": "Stack traces of all current goroutines. Use debug=2 as a query parameter to export in the same format as an unrecovered panic.", //nolint: lll
"heap": "A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.", //nolint: lll
"mutex": "Stack traces of holders of contended mutexes",
"profile": "CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.", //nolint: lll
"threadcreate": "Stack traces that led to the creation of new OS threads",
"trace": "A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.", //nolint: lll
}
func SetupPprofRoutes(conf *config.Config, router *mux.Router, authFunc mux.MiddlewareFunc,
log log.Logger,
) {
// If authn/authz are enabled the endpoints for pprof should be available only to admins
pprofRouter := router.PathPrefix(constants.ProfilingEndpoint).Subrouter()
pprofRouter.Use(zcommon.AuthzOnlyAdminsMiddleware(conf))
pprofRouter.Methods(http.MethodGet).Handler(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if name, found := strings.CutPrefix(r.URL.Path,
registryConst.RoutePrefix+constants.ProfilingEndpoint); found {
if name != "" {
switch name {
case "profile": // not available through pprof.Handler
pprof.Profile(w, r)
return
case "trace": // not available through pprof.Handler
pprof.Trace(w, r)
return
default:
pprof.Handler(name).ServeHTTP(w, r)
return
}
}
}
var profiles []profileEntry
for _, p := range runPprof.Profiles() {
profiles = append(profiles, profileEntry{
Name: p.Name(),
Href: p.Name(),
Desc: profileDescriptions[p.Name()],
Count: p.Count(),
})
}
// Adding other profiles exposed from within this package
for _, p := range []string{"cmdline", "profile", "trace"} {
profiles = append(profiles, profileEntry{
Name: p,
Href: p,
Desc: profileDescriptions[p],
})
}
sort.Slice(profiles, func(i, j int) bool {
return profiles[i].Name < profiles[j].Name
})
if err := indexTmplExecute(w, profiles); err != nil {
log.Error().Err(err).Msg("unexpected failure")
}
}))
}
func indexTmplExecute(writer io.Writer, profiles []profileEntry) error {
var buff bytes.Buffer
buff.WriteString(`<html>
<head>
<title>/v2/_zot/pprof/</title>
<style>
.profile-name{
display:inline-block;
width:6rem;
}
</style>
</head>
<body>
/debug/pprof/
<br>
<p>Set debug=1 as a query parameter to export in legacy text format</p>
<br>
Types of profiles available:
<table>
<thead><td>Count</td><td>Profile</td></thead>
`)
for _, profile := range profiles {
link := &url.URL{Path: profile.Href, RawQuery: "debug=1"}
fmt.Fprintf(&buff, "<tr><td>%d</td><td><a href='%s'>%s</a></td></tr>\n",
profile.Count, link, html.EscapeString(profile.Name))
}
buff.WriteString(`</table>
<a href="goroutine?debug=2">full goroutine stack dump</a>
<br>
<p>
Profile Descriptions:
<ul>
`)
for _, profile := range profiles {
fmt.Fprintf(&buff, "<li><div class=profile-name>%s: </div> %s</li>\n",
html.EscapeString(profile.Name), html.EscapeString(profile.Desc))
}
buff.WriteString(`</ul>
</p>
</body>
</html>`)
_, err := writer.Write(buff.Bytes())
return err
}