Skip to content

Commit fbd483b

Browse files
authored
Merge pull request #65 from NETWAYS/logstash6-health
Add Logstash 6 compatibility for health check
2 parents 0422ae4 + ef8bb6e commit fbd483b

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

cmd/health.go

+10
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ var healthCmd = &cobra.Command{
180180
check.ExitError(err)
181181
}
182182

183+
// Enable some backwards compatibility
184+
// Can be changed to a switch statement in the future,
185+
// when more versions need special cases
186+
// For Logstash 6, we assume a parsed JSON response
187+
// is enough to declare the instance running, since there
188+
// is no status field.
189+
if stat.MajorVersion == 6 {
190+
stat.Status = "green"
191+
}
192+
183193
// Logstash Health Status
184194
switch stat.Status {
185195
default:

cmd/health_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,47 @@ type HealthTest struct {
2929
expected string
3030
}
3131

32+
func TestHealthCmd_Logstash6(t *testing.T) {
33+
tests := []HealthTest{
34+
{
35+
name: "version-error",
36+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37+
w.WriteHeader(http.StatusOK)
38+
w.Write([]byte(`{"host":"logstash","version":"foo"}`))
39+
})),
40+
args: []string{"run", "../main.go", "health"},
41+
expected: "UNKNOWN - Could not determine version",
42+
},
43+
{
44+
name: "health-ok",
45+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
46+
w.WriteHeader(http.StatusOK)
47+
w.Write([]byte(`{"host":"logstash","version":"6.8.23","http_address":"0.0.0.0:9600","id":"123","name":"logstash","jvm":{"threads":{"count":1,"peak_count":2},"mem":{},"gc":{},"uptime_in_millis":123},"process":{},"events":{},"pipelines":{"main":{}},"reloads":{"failures":0,"successes":0},"os":{}}`))
48+
})),
49+
args: []string{"run", "../main.go", "health"},
50+
expected: "OK - Logstash is healthy",
51+
},
52+
}
53+
54+
for _, test := range tests {
55+
t.Run(test.name, func(t *testing.T) {
56+
defer test.server.Close()
57+
58+
// We need the random Port extracted
59+
u, _ := url.Parse(test.server.URL)
60+
cmd := exec.Command("go", append(test.args, "--port", u.Port())...)
61+
out, _ := cmd.CombinedOutput()
62+
63+
actual := string(out)
64+
65+
if !strings.Contains(actual, test.expected) {
66+
t.Error("\nActual: ", actual, "\nExpected: ", test.expected)
67+
}
68+
69+
})
70+
}
71+
}
72+
3273
func TestHealthCmd_Logstash7(t *testing.T) {
3374
tests := []HealthTest{
3475
{

internal/logstash/api.go

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package logstash
22

3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
)
9+
310
// https://www.elastic.co/guide/en/logstash/current/node-stats-api.html
411

512
type Pipeline struct {
@@ -44,9 +51,40 @@ type JVM struct {
4451
}
4552

4653
type Stat struct {
47-
Host string `json:"host"`
48-
Version string `json:"version"`
49-
Status string `json:"status"`
50-
Process Process `json:"process"`
51-
Jvm JVM `json:"jvm"`
54+
Host string `json:"host"`
55+
Version string `json:"version"`
56+
Status string `json:"status"`
57+
Process Process `json:"process"`
58+
Jvm JVM `json:"jvm"`
59+
MajorVersion int
60+
}
61+
62+
// Custom Unmarshal since we might want to add or parse
63+
// further fields in the future. This is simpler to extend and
64+
// to test here than during the CheckPlugin logic.
65+
func (s *Stat) UnmarshalJSON(b []byte) error {
66+
type Temp Stat
67+
68+
t := (*Temp)(s)
69+
70+
err := json.Unmarshal(b, t)
71+
72+
if err != nil {
73+
return err
74+
}
75+
76+
// Could also use some semver package,
77+
// but decided against the depedency
78+
if s.Version != "" {
79+
v := strings.Split(s.Version, ".")
80+
majorVersion, convErr := strconv.Atoi(v[0])
81+
82+
if convErr != nil {
83+
return fmt.Errorf("Could not determine version")
84+
}
85+
86+
s.MajorVersion = majorVersion
87+
}
88+
89+
return nil
5290
}

0 commit comments

Comments
 (0)