Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return build info under /loki/api/v1/status/buildinfo. #3972

Merged
merged 8 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 7 additions & 0 deletions docs/sources/api/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ These endpoints are exposed by all components:
- [`GET /ready`](#get-ready)
- [`GET /metrics`](#get-metrics)
- [`GET /config`](#get-config)
- [`GET /version`](#get-version)

These endpoints are exposed by the querier and the frontend:

Expand Down Expand Up @@ -823,6 +824,12 @@ and the current are returned. A value of `defaults` returns the default configur

In microservices mode, the `/config` endpoint is exposed by all components.

## `GET /version`

`/version` exposes the build information in a JSON object. The fields are `version`, `revision`, `branch`, `build_date`, and `build_user`.

In microservices mode, the `/version` endpoint is exposed by all components.

## Series

The Series API is available under the following:
Expand Down
3 changes: 3 additions & 0 deletions pkg/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ func (t *Loki) Run() error {
// This adds a way to see the config and the changes compared to the defaults
t.Server.HTTP.Path("/config").HandlerFunc(configHandler(t.Cfg, newDefaultConfig()))

// Each component serves its version.
t.Server.HTTP.Path("/version").HandlerFunc(versionHandler())

t.Server.HTTP.Path("/debug/fgprof").Handler(fgprof.Handler())

// Let's listen for events from this manager, and log them.
Expand Down
36 changes: 36 additions & 0 deletions pkg/loki/version_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package loki

import (
"encoding/json"
"net/http"

"github.com/grafana/loki/pkg/util/build"
)

type buildInfo struct {
Version string `json:"version"`
Revision string `json:"revision"`
Branch string `json:"branch"`
BuildUser string `json:"build_user"`
BuildDate string `json:"build_date"`
}

func versionHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
info := buildInfo{
Version: build.Version,
Revision: build.Revision,
Branch: build.Branch,
BuildUser: build.BuildUser,
BuildDate: build.BuildDate,
}

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)
}
}
39 changes: 39 additions & 0 deletions pkg/loki/version_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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"

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",
"build_date":"yesterday",
"build_user":"Turing",
"revision":"foobar"
}`
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.JSONEq(t, expected, string(body))
}
2 changes: 1 addition & 1 deletion pkg/querier/queryrange/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func NewLogFilterTripperware(
}, nil
}

// NewSeriesripperware creates a new frontend tripperware responsible for handling series requests
// NewSeriesTripperware creates a new frontend tripperware responsible for handling series requests
func NewSeriesTripperware(
cfg Config,
log log.Logger,
Expand Down