Skip to content

Commit

Permalink
feat: add version number to debug/vars (influxdata#23795)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidby-influx authored and chengshiwen committed Sep 1, 2024
1 parent 27961e4 commit 539d73c
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions services/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ",")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 539d73c

Please sign in to comment.