Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,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

Expand Down
29 changes: 4 additions & 25 deletions dash/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)}
Expand All @@ -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
}
11 changes: 8 additions & 3 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.

Expand All @@ -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":
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func TestRenderSummary(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 {
Expand Down
8 changes: 7 additions & 1 deletion internal/ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand Down
9 changes: 9 additions & 0 deletions internal/ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
61 changes: 61 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions internal/version/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions internal/web/assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"; }
Expand Down
1 change: 1 addition & 0 deletions internal/web/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<body>
<header class="top">
<span class="pill">promtop</span>
<span id="version" class="version"></span>
<span id="target" class="target"></span>
<span id="source" class="source"></span>
<span class="spacer"></span>
Expand Down
1 change: 1 addition & 0 deletions internal/web/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
5 changes: 3 additions & 2 deletions internal/web/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion internal/web/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
}
Expand Down
Loading