mirror of https://github.com/grafana/loki
Return build info under `/loki/api/v1/status/buildinfo`. (#3972)
* Return build info under `/version`. Closes #3221 * Update documentation. * Correct docs. * Format version handler code. * Ignore write error. * Sort imports. * Use `PrometheusVersion` struct. * Set Go version.pull/3981/head
parent
1cca922e6d
commit
af452047d3
@ -0,0 +1,31 @@ |
||||
package loki |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"net/http" |
||||
|
||||
prom "github.com/prometheus/prometheus/web/api/v1" |
||||
|
||||
"github.com/grafana/loki/pkg/util/build" |
||||
) |
||||
|
||||
func versionHandler() http.HandlerFunc { |
||||
return func(w http.ResponseWriter, r *http.Request) { |
||||
info := prom.PrometheusVersion{ |
||||
Version: build.Version, |
||||
Revision: build.Revision, |
||||
Branch: build.Branch, |
||||
BuildUser: build.BuildUser, |
||||
BuildDate: build.BuildDate, |
||||
GoVersion: build.GoVersion, |
||||
} |
||||
|
||||
w.Header().Set("Content-Type", "application/json") |
||||
w.WriteHeader(http.StatusOK) |
||||
|
||||
// We ignore errors here, because we cannot do anything about them.
|
||||
// Write will trigger sending Status code, so we cannot send a different status code afterwards.
|
||||
// Also this isn't internal error, but error communicating with client.
|
||||
_ = json.NewEncoder(w).Encode(info) |
||||
} |
||||
} |
@ -0,0 +1,41 @@ |
||||
package loki |
||||
|
||||
import ( |
||||
"io/ioutil" |
||||
"net/http" |
||||
"net/http/httptest" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
|
||||
"github.com/grafana/loki/pkg/util/build" |
||||
) |
||||
|
||||
func TestVersionHandler(t *testing.T) { |
||||
build.Version = "0.0.1" |
||||
build.Branch = "main" |
||||
build.Revision = "foobar" |
||||
build.BuildDate = "yesterday" |
||||
build.BuildUser = "Turing" |
||||
build.GoVersion = "42" |
||||
|
||||
req := httptest.NewRequest("GET", "http://test.com/config?mode=diff", nil) |
||||
w := httptest.NewRecorder() |
||||
|
||||
h := versionHandler() |
||||
h(w, req) |
||||
resp := w.Result() |
||||
assert.Equal(t, http.StatusOK, resp.StatusCode) |
||||
|
||||
expected := `{ |
||||
"version":"0.0.1", |
||||
"branch":"main", |
||||
"buildDate":"yesterday", |
||||
"buildUser":"Turing", |
||||
"revision":"foobar", |
||||
"goVersion": "42" |
||||
}` |
||||
body, err := ioutil.ReadAll(resp.Body) |
||||
assert.NoError(t, err) |
||||
assert.JSONEq(t, expected, string(body)) |
||||
} |
Loading…
Reference in new issue