Merge pull request #18173 from influxdata/13907/addVersionToHealth

fix(api/health): report version
pull/18211/head
Pavel Závora 2020-05-26 14:25:32 +02:00 committed by GitHub
commit d5846be10c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 12 deletions

View File

@ -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. [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. [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. [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 ### Bug Fixes

View File

@ -3,11 +3,13 @@ package http
import ( import (
"fmt" "fmt"
"net/http" "net/http"
platform "github.com/influxdata/influxdb/v2"
) )
// HealthHandler returns the status of the process. // HealthHandler returns the status of the process.
func HealthHandler(w http.ResponseWriter, r *http.Request) { 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.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, msg) fmt.Fprintln(w, msg)

View File

@ -1,6 +1,7 @@
package http package http
import ( import (
"encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -11,7 +12,7 @@ func TestHealthHandler(t *testing.T) {
type wants struct { type wants struct {
statusCode int statusCode int
contentType string contentType string
body string status string
} }
tests := []struct { tests := []struct {
name string name string
@ -26,7 +27,7 @@ func TestHealthHandler(t *testing.T) {
wants: wants{ wants: wants{
statusCode: http.StatusOK, statusCode: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: `{"name":"influxdb", "message":"ready for queries and writes", "status":"pass", "checks":[]}`, status: "pass",
}, },
}, },
} }
@ -34,21 +35,37 @@ func TestHealthHandler(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
HealthHandler(tt.w, tt.r) HealthHandler(tt.w, tt.r)
res := tt.w.Result() res := tt.w.Result()
content := res.Header.Get("Content-Type") contentType := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body) body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != tt.wants.statusCode { if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, 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 { if tt.wants.contentType != "" && contentType != tt.wants.contentType {
t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, content, tt.wants.contentType) t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, contentType, tt.wants.contentType)
} }
if tt.wants.body != "" { var content map[string]interface{}
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil { if err := json.Unmarshal(body, &content); err != nil {
t.Errorf("%q, HealthHandler(). error unmarshaling json %v", tt.name, err) t.Errorf("%q, HealthHandler(). error unmarshaling json %v", tt.name, err)
} else if !eq { return
t.Errorf("%q. HealthHandler() = ***%s***", tt.name, diff)
} }
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)
} }
}) })
} }

View File

@ -10893,6 +10893,10 @@ components:
enum: enum:
- pass - pass
- fail - fail
version:
type: string
commit:
type: string
Labels: Labels:
type: array type: array
items: items: