From 539d73ca81f21adaca7bfe536b7bfa98e6c41486 Mon Sep 17 00:00:00 2001 From: davidby-influx <72418212+davidby-influx@users.noreply.github.com> Date: Thu, 13 Oct 2022 11:49:30 -0700 Subject: [PATCH] feat: add version number to debug/vars (#23795) closes https://github.com/influxdata/influxdb/issues/23793 --- services/httpd/handler.go | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/services/httpd/handler.go b/services/httpd/handler.go index 47455f23f18..3d17d98c23f 100644 --- a/services/httpd/handler.go +++ b/services/httpd/handler.go @@ -1530,6 +1530,25 @@ func (h *Handler) serveExpvar(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "{") } + if val := diags["build"]; val != nil { + jv, err := parseBuildInfo(val) + if err != nil { + h.httpError(w, err.Error(), http.StatusInternalServerError) + return + } + data, err := json.Marshal(jv) + if err != nil { + h.httpError(w, err.Error(), http.StatusInternalServerError) + return + } + + if !first { + fmt.Fprintln(w, ",") + } + first = false + fmt.Fprintf(w, "\"build\": %s", data) + } + if val := expvar.Get("cmdline"); val != nil { if !first { fmt.Fprintln(w, ",") @@ -1695,6 +1714,32 @@ func parseSystemDiagnostics(d *diagnostics.Diagnostics) (map[string]interface{}, return m, nil } +// parseBuildInfo converts the build info diagnostics into an appropriate +// format for marshaling to JSON in the /debug/vars format. +func parseBuildInfo(d *diagnostics.Diagnostics) (map[string]interface{}, error) { + m := map[string]interface{}{"Version": nil, "Commit": nil, "Branch": nil, "Build Time": nil} + for key := range m { + // Find the associated column. + ci := -1 + for i, col := range d.Columns { + if col == key { + ci = i + break + } + } + + if ci == -1 { + return nil, fmt.Errorf("unable to find column %q", key) + } + + if len(d.Rows) < 1 || len(d.Rows[0]) <= ci { + return nil, fmt.Errorf("no data for column %q", key) + } + m[key] = d.Rows[0][ci] + } + return m, nil +} + // httpError writes an error to the client in a standard format. func (h *Handler) httpError(w http.ResponseWriter, errmsg string, code int) { if code == http.StatusUnauthorized {