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

fix(api/health): report version #18173

Merged
merged 3 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
1. [18040](https://github.com/influxdata/influxdb/pull/18040): Allow for min OR max y-axis visualization settings rather than min AND max
1. [17764](https://github.com/influxdata/influxdb/pull/17764): Add CSV to line protocol conversion library
1. [18059](https://github.com/influxdata/influxdb/pull/18059): Make the dropdown width adjustable
1. [18173](https://github.com/influxdata/influxdb/pull/18173): Add version to /health response

### Bug Fixes

Expand Down
4 changes: 3 additions & 1 deletion http/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package http
import (
"fmt"
"net/http"

platform "github.com/influxdata/influxdb/v2"
)

// HealthHandler returns the status of the process.
func HealthHandler(w http.ResponseWriter, r *http.Request) {
msg := `{"name":"influxdb", "message":"ready for queries and writes", "status":"pass", "checks":[]}`
msg := fmt.Sprintf(`{"name":"influxdb", "message":"ready for queries and writes", "status":"pass", "checks":[], "version": %q, "commit": %q}`, platform.GetBuildInfo().Version, platform.GetBuildInfo().Commit)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, msg)
Expand Down
39 changes: 28 additions & 11 deletions http/health_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -11,7 +12,7 @@ func TestHealthHandler(t *testing.T) {
type wants struct {
statusCode int
contentType string
body string
status string
}
tests := []struct {
name string
Expand All @@ -26,29 +27,45 @@ func TestHealthHandler(t *testing.T) {
wants: wants{
statusCode: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: `{"name":"influxdb", "message":"ready for queries and writes", "status":"pass", "checks":[]}`,
status: "pass",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
HealthHandler(tt.w, tt.r)
res := tt.w.Result()
content := res.Header.Get("Content-Type")
contentType := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body)

if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
}
if tt.wants.contentType != "" && content != tt.wants.contentType {
t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, content, tt.wants.contentType)
if tt.wants.contentType != "" && contentType != tt.wants.contentType {
t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, contentType, tt.wants.contentType)
}
if tt.wants.body != "" {
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
t.Errorf("%q, HealthHandler(). error unmarshaling json %v", tt.name, err)
} else if !eq {
t.Errorf("%q. HealthHandler() = ***%s***", tt.name, diff)
}
var content map[string]interface{}
if err := json.Unmarshal(body, &content); err != nil {
t.Errorf("%q, HealthHandler(). error unmarshaling json %v", tt.name, err)
sranka marked this conversation as resolved.
Show resolved Hide resolved
return
}
if _, found := content["name"]; !found {
t.Errorf("%q. HealthHandler() no name reported", tt.name)
}
if content["status"] != tt.wants.status {
t.Errorf("%q. HealthHandler() status= %v, want %v", tt.name, content["status"], tt.wants.status)
}
if _, found := content["message"]; !found {
t.Errorf("%q. HealthHandler() no message reported", tt.name)
}
if _, found := content["checks"]; !found {
t.Errorf("%q. HealthHandler() no checks reported", tt.name)
}
if _, found := content["version"]; !found {
t.Errorf("%q. HealthHandler() no version reported", tt.name)
}
if _, found := content["commit"]; !found {
t.Errorf("%q. HealthHandler() no commit reported", tt.name)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10893,6 +10893,10 @@ components:
enum:
- pass
- fail
version:
type: string
commit:
type: string
Labels:
type: array
items:
Expand Down