From 7eb522f115735d516e2e7817d29c6c6d40065766 Mon Sep 17 00:00:00 2001 From: Igor Artasevych Date: Thu, 29 Oct 2026 18:03:24 +0200 Subject: [PATCH] promtop: show the version in the terminal UI, the web page and promtop version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -version printed "dev" for anything but a release build, and neither the terminal UI nor the web page showed a version at all. internal/version picks it from the release ldflags, else the module version (go install …@v0.3.0, or promtop/dash built into a service), else the git tag or commit of a local build. The help screen (?) and the web page next to the name show it, the w key's dashboard and dash included; "promtop version" prints it like -version. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01US3HyE2LFapBHwkSQRKHsv --- README.md | 6 +++- dash/dash.go | 29 +++------------ internal/cli/cli.go | 11 ++++-- internal/cli/cli_test.go | 6 ++++ internal/ui/app.go | 8 ++++- internal/ui/ui_test.go | 9 +++++ internal/version/version.go | 61 ++++++++++++++++++++++++++++++++ internal/version/version_test.go | 35 ++++++++++++++++++ internal/web/assets/app.js | 1 + internal/web/assets/index.html | 1 + internal/web/assets/style.css | 1 + internal/web/frame.go | 5 +-- internal/web/server.go | 2 ++ internal/web/web_test.go | 3 +- 14 files changed, 145 insertions(+), 33 deletions(-) create mode 100644 internal/version/version.go create mode 100644 internal/version/version_test.go diff --git a/README.md b/README.md index 63a7826..d3edc7a 100644 --- a/README.md +++ b/README.md @@ -243,9 +243,13 @@ wheel, right-click to go back. -no-color disable colours (NO_COLOR is honoured too) -web serve the dashboard over HTTP and open it in the browser -web-addr addr listen address for -web and the w key (default 127.0.0.1:0, a free local port) +-version print the version and exit (also: promtop version) ``` -`promtop render -h` and `promtop record -h` list the flags of the snapshot commands. +`promtop render -h` and `promtop record -h` list the flags of the snapshot commands. The version +is also on the help screen (`?`) and next to the name on the web page. A release build carries +its tag; `go install …@v0.3.0` the module version; a local build the git tag or commit it was +built from (`0.3.0+dirty` with uncommitted changes). ## Try it diff --git a/dash/dash.go b/dash/dash.go index 3471d36..f42caf9 100644 --- a/dash/dash.go +++ b/dash/dash.go @@ -24,11 +24,11 @@ package dash import ( "net/http" - "runtime/debug" "sync/atomic" "time" "github.com/igorrius/promtop/internal/scrape" + "github.com/igorrius/promtop/internal/version" "github.com/igorrius/promtop/internal/web" ) @@ -100,12 +100,13 @@ func New(metrics http.Handler, opts ...Option) *Dashboard { for _, o := range opts { o(&c) } + v := version.Get("") srv := web.New(web.Config{ Source: &scrape.Handler{ - H: metrics, Name: c.title, UserAgent: "promtop-dash/" + version(), + H: metrics, Name: c.title, UserAgent: "promtop-dash/" + v, Timeout: min(c.interval, maxTimeout), }, - Interval: c.interval, History: c.history, Filter: c.filter, Denied: denied, + Interval: c.interval, History: c.history, Filter: c.filter, Denied: denied, Version: v, }) srv.Start() return &Dashboard{srv: srv, h: srv.Handler(c.allow)} @@ -131,25 +132,3 @@ func (d *Dashboard) Close() error { d.srv.Close() return nil } - -// version is promtop's module version as built into the program. -func version() string { - const mod = "github.com/igorrius/promtop" - bi, ok := debug.ReadBuildInfo() - if !ok { - return "dev" - } - v := "" - if bi.Main.Path == mod { - v = bi.Main.Version - } - for _, d := range bi.Deps { - if d.Path == mod { - v = d.Version - } - } - if v == "" || v == "(devel)" { - return "dev" - } - return v -} diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 3860f53..2ec677c 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -13,6 +13,7 @@ import ( "time" "github.com/igorrius/promtop/internal/ui" + buildinfo "github.com/igorrius/promtop/internal/version" webui "github.com/igorrius/promtop/internal/web" ) @@ -26,6 +27,7 @@ Usage: promtop -web [flags] [target] the same in the browser promtop render [flags] [target] draw once into PNG, SVG, JSON or text promtop record -state f [target] add a scrape to a history file (for cron) + promtop version print the version Run "promtop render -h" or "promtop record -h" for their flags. @@ -41,9 +43,12 @@ Flags: // Main runs promtop with the given arguments (without the program name) // and returns the exit code. func Main(args []string, ver string) int { - version = ver + version = buildinfo.Get(ver) if len(args) > 0 { switch args[0] { + case "version": + fmt.Println("promtop", version) + return 0 case "render": return runRender(args[1:]) case "record": @@ -95,7 +100,7 @@ func runLive(args []string) int { if *web { srv := webui.New(webui.Config{ Source: src, Interval: *sf.interval, History: *history, ShowAll: *all, Filter: *filter, - Addr: *webAddr, Open: true, Out: os.Stderr, + Addr: *webAddr, Open: true, Out: os.Stderr, Version: version, }) if err := srv.Run(ctx); err != nil { return fail(err) @@ -109,7 +114,7 @@ func runLive(args []string) int { } app := ui.New(ui.Config{ Source: src, Interval: *sf.interval, History: *history, Color: color, - ShowAll: *all, Filter: *filter, Width: *width, WebAddr: *webAddr, + ShowAll: *all, Filter: *filter, Width: *width, WebAddr: *webAddr, Version: version, }) if *once { err = app.RunOnce(ctx) diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 7605660..7a6ff32 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -41,6 +41,12 @@ func TestRenderCommand(t *testing.T) { } } +func TestVersionCommand(t *testing.T) { + if code := Main([]string{"version"}, "v1.2.3"); code != 0 || version != "1.2.3" { + t.Errorf("exit %d, version %q", code, version) + } +} + func TestRecordCommand(t *testing.T) { state := filepath.Join(t.TempDir(), "state") if code := runRecord([]string{fixture}); code != 2 { diff --git a/internal/ui/app.go b/internal/ui/app.go index ec7c0d9..9583c96 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -39,6 +39,7 @@ type Config struct { Width int // --once: render width (0 = terminal or 120) Out io.Writer WebAddr string // listen address of the web dashboard the w key starts + Version string // shown on the help screen and the web page } // App is the Bubble Tea model. @@ -415,6 +416,7 @@ func (a *App) openWeb() { srv := webui.New(webui.Config{ Source: a.cfg.Source, Interval: a.interval, History: a.cfg.History, ShowAll: a.showAll, Filter: a.filterSrc, Addr: a.cfg.WebAddr, Fed: true, + Version: a.cfg.Version, }) u, err := srv.Listen() if err != nil { @@ -641,7 +643,11 @@ var legend = []string{ func (a *App) helpScreen() []string { t := a.th - out := []string{"", " " + t.bold(t.title, "promtop") + t.paint(t.dim, " — live terminal view of a Prometheus /metrics endpoint"), ""} + title := t.bold(t.title, "promtop") + if a.cfg.Version != "" { + title += " " + t.paint(t.accent, a.cfg.Version) + } + out := []string{"", " " + title + t.paint(t.dim, " — live terminal view of a Prometheus /metrics endpoint"), ""} for _, l := range legend { out = append(out, " "+t.paint(t.dim, l)) } diff --git a/internal/ui/ui_test.go b/internal/ui/ui_test.go index edaada6..a9407ab 100644 --- a/internal/ui/ui_test.go +++ b/internal/ui/ui_test.go @@ -357,6 +357,15 @@ func TestFooter(t *testing.T) { } } +func TestHelpVersion(t *testing.T) { + a := newTestApp(t, 120, 40) + a.cfg.Version = "0.3.0" + press(a, "?") + if got := ansi.Strip(strings.Join(a.frame(), "\n")); !strings.Contains(got, "promtop 0.3.0 — live") { + t.Errorf("help screen lacks the version:\n%s", got) + } +} + // The detail chart of a non-negative series never shows a negative axis. func TestDetailAxis(t *testing.T) { a := newTestApp(t, 120, 40) diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..376df52 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,61 @@ +// Package version names the running promtop build. +package version + +import ( + "runtime/debug" + "strings" +) + +const module = "github.com/igorrius/promtop" + +// Get returns the version to show: the one a release build sets with +// -ldflags, else the module version ("go install …@v0.3.0", or promtop +// built into another program as a dependency), else the commit of a +// local build ("dev+3b2a9d1", "-dirty" with uncommitted changes), else +// "dev". A leading "v" is dropped, as in release builds. +func Get(ldflags string) string { + if ldflags != "" && ldflags != "dev" { + return strings.TrimPrefix(ldflags, "v") + } + bi, ok := debug.ReadBuildInfo() + if !ok { + return "dev" + } + return fromBuildInfo(bi) +} + +func fromBuildInfo(bi *debug.BuildInfo) string { + mod := bi.Main + for _, d := range bi.Deps { + if d.Path == module { + mod = *d + } + } + if mod.Path != module { + return "dev" + } + if v := mod.Version; v != "" && v != "(devel)" { + return strings.TrimPrefix(v, "v") + } + if mod != bi.Main { + return "dev" // a replaced dependency: the VCS stamp is the host program's + } + var rev string + var dirty bool + for _, s := range bi.Settings { + switch s.Key { + case "vcs.revision": + rev = s.Value + case "vcs.modified": + dirty = s.Value == "true" + } + } + if rev == "" { + return "dev" + } + v := "dev+" + rev[:min(7, len(rev))] + if dirty { + v += "-dirty" + } + return v +} diff --git a/internal/version/version_test.go b/internal/version/version_test.go new file mode 100644 index 0000000..2997555 --- /dev/null +++ b/internal/version/version_test.go @@ -0,0 +1,35 @@ +package version + +import ( + "runtime/debug" + "testing" +) + +func TestFromBuildInfo(t *testing.T) { + vcs := []debug.BuildSetting{{Key: "vcs.revision", Value: "3b2a9d1f00c0ffee"}, {Key: "vcs.modified", Value: "true"}} + for _, c := range []struct { + name string + bi debug.BuildInfo + want string + }{ + {"go install @version", debug.BuildInfo{Main: debug.Module{Path: module, Version: "v0.3.0"}}, "0.3.0"}, + {"local build", debug.BuildInfo{Main: debug.Module{Path: module, Version: "(devel)"}, Settings: vcs}, "dev+3b2a9d1-dirty"}, + {"no vcs", debug.BuildInfo{Main: debug.Module{Path: module, Version: "(devel)"}}, "dev"}, + {"dependency", debug.BuildInfo{ + Main: debug.Module{Path: "example.com/shop"}, Settings: vcs, + Deps: []*debug.Module{{Path: module, Version: "v0.4.1"}}, + }, "0.4.1"}, + {"replaced dependency", debug.BuildInfo{ + Main: debug.Module{Path: "example.com/shop"}, Settings: vcs, + Deps: []*debug.Module{{Path: module, Version: "(devel)"}}, + }, "dev"}, + {"other program", debug.BuildInfo{Main: debug.Module{Path: "example.com/shop", Version: "v1.0.0"}}, "dev"}, + } { + if got := fromBuildInfo(&c.bi); got != c.want { + t.Errorf("%s: %q, want %q", c.name, got, c.want) + } + } + if got := Get("v0.5.0"); got != "0.5.0" { + t.Errorf("ldflags: %q", got) + } +} diff --git a/internal/web/assets/app.js b/internal/web/assets/app.js index 401bc6d..b2d06cf 100644 --- a/internal/web/assets/app.js +++ b/internal/web/assets/app.js @@ -284,6 +284,7 @@ function render() { if (!fr) return; $("target").textContent = fr.target || ""; $("source").textContent = fr.source; + $("version").textContent = fr.version || ""; const st = $("state"); if (state.paused) { st.className = "state paused"; st.textContent = "❚❚ paused"; } else if (fr.err) { st.className = "state err"; st.textContent = "error"; } diff --git a/internal/web/assets/index.html b/internal/web/assets/index.html index 800a77e..6d8797d 100644 --- a/internal/web/assets/index.html +++ b/internal/web/assets/index.html @@ -11,6 +11,7 @@
promtop + diff --git a/internal/web/assets/style.css b/internal/web/assets/style.css index 569575d..891b6fa 100644 --- a/internal/web/assets/style.css +++ b/internal/web/assets/style.css @@ -69,6 +69,7 @@ button, input { font: inherit; color: inherit; } padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--border); white-space: nowrap; } .pill { background: var(--accent); color: var(--pill-fg); font-weight: 700; padding: 1px 8px; border-radius: 6px; } +.version { color: var(--dim); font-size: 0.85em; } .target { font-weight: 600; color: var(--title); } .source { color: var(--dim); overflow: hidden; text-overflow: ellipsis; min-width: 0; } .spacer { flex: 1; } diff --git a/internal/web/frame.go b/internal/web/frame.go index 14888c9..45f1af1 100644 --- a/internal/web/frame.go +++ b/internal/web/frame.go @@ -71,8 +71,9 @@ func tone(t view.Tone) string { type Frame struct { Target string `json:"target,omitempty"` Source string `json:"source"` - Interval float64 `json:"interval"` // seconds - At int64 `json:"at"` // unix ms of the latest scrape + Version string `json:"version,omitempty"` // promtop's + Interval float64 `json:"interval"` // seconds + At int64 `json:"at"` // unix ms of the latest scrape Took string `json:"took,omitempty"` Err string `json:"err,omitempty"` Metrics int `json:"metrics"` diff --git a/internal/web/server.go b/internal/web/server.go index ba431eb..0bcf72f 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -45,6 +45,7 @@ type Config struct { Out io.Writer Denied string // body of a 403; default "promtop only answers to localhost" Fed bool // scrapes arrive through Ingest; Start runs no scrape loop + Version string // promtop's version, shown next to the name } // Server scrapes the target and serves the dashboard. @@ -366,6 +367,7 @@ func (s *Server) frame(o options) []byte { } fr := buildFrame(s.st, s.sections(o), time.Now()) fr.Source = s.cfg.Source.String() + fr.Version = s.cfg.Version fr.Interval = s.cfg.Interval.Seconds() fr.At = s.lastAt.UnixMilli() fr.Took = s.lastDur.Round(time.Millisecond).String() diff --git a/internal/web/web_test.go b/internal/web/web_test.go index 180208c..429f219 100644 --- a/internal/web/web_test.go +++ b/internal/web/web_test.go @@ -51,6 +51,7 @@ func newServer(t *testing.T) *Server { func TestFrame(t *testing.T) { s := newServer(t) + s.cfg.Version = "0.3.0" s.mu.Lock() b := s.frame(options{key: "x"}) s.mu.Unlock() @@ -77,7 +78,7 @@ func TestFrame(t *testing.T) { t.Errorf("%s: view %q, want %q", id, views[id], v) } } - if fr.Metrics == 0 || fr.Scrapes != 2 || len(fr.Names) == 0 { + if fr.Metrics == 0 || fr.Scrapes != 2 || len(fr.Names) == 0 || fr.Version != "0.3.0" { t.Errorf("header = %+v", fr) } }