diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c0ee6fb2..cf72d1582 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Added a dedicated API endpoint for overview modal. - Individual alert details are now lazy-loaded to improve performance when dealing with a huge number of alerts per group. +- Added `/version` endpoint returning karma and Go runtime version #3332. ### Changed diff --git a/cmd/karma/main.go b/cmd/karma/main.go index 7a82a80a9..dc8c773ed 100644 --- a/cmd/karma/main.go +++ b/cmd/karma/main.go @@ -114,6 +114,7 @@ func setupRouter(router *chi.Mux, historyPoller *historyPoller) { allowAuthBypass := []string{ getViewURL("/health"), getViewURL("/metrics"), + getViewURL("/version"), } if config.Config.Authentication.Header.Name != "" { config.Config.Authentication.Enabled = true @@ -131,6 +132,7 @@ func setupRouter(router *chi.Mux, historyPoller *historyPoller) { router.Get(getViewURL(""), redirectIndex) } router.Get(getViewURL("/"), index) + router.Get(getViewURL("/version"), versionHandler) router.Get(getViewURL("/health"), pong) router.Get(getViewURL("/robots.txt"), robots) router.Get(getViewURL("/metrics"), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/karma/views.go b/cmd/karma/views.go index 8dddd0d7a..ef33c4100 100644 --- a/cmd/karma/views.go +++ b/cmd/karma/views.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "net/http" + "runtime" "sort" "strings" "time" @@ -39,6 +40,20 @@ func badRequestJSON(w http.ResponseWriter, error string) { _, _ = w.Write(out) } +type KarmaVersion struct { + Version string `json:"version"` + Golang string `json:"golang"` +} + +func versionHandler(w http.ResponseWriter, r *http.Request) { + ver := KarmaVersion{ + Version: version, + Golang: runtime.Version(), + } + data, _ := json.MarshalIndent(ver, "", " ") + _, _ = w.Write(data) +} + func pong(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("Pong\n")) } diff --git a/cmd/karma/views_test.go b/cmd/karma/views_test.go index de95efaf3..b9aa0a9a5 100644 --- a/cmd/karma/views_test.go +++ b/cmd/karma/views_test.go @@ -89,6 +89,18 @@ func TestRobots(t *testing.T) { } } +func TestVersion(t *testing.T) { + mockConfig() + r := testRouter() + setupRouter(r, nil) + req := httptest.NewRequest("GET", "/version", nil) + resp := httptest.NewRecorder() + r.ServeHTTP(resp, req) + if resp.Code != http.StatusOK { + t.Errorf("GET /version returned status %d", resp.Code) + } +} + func TestHealthPrefix(t *testing.T) { os.Setenv("LISTEN_PREFIX", "/prefix") defer os.Unsetenv("LISTEN_PREFIX")