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

Add health check #19

Merged
merged 5 commits into from
Oct 25, 2022
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
73 changes: 23 additions & 50 deletions plugins/is_alived/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"

health "github.com/dsrvlabs/vatz-plugin-cosmoshub/rpc/cosmos"
pluginpb "github.com/dsrvlabs/vatz-proto/plugin/v1"
"github.com/dsrvlabs/vatz/sdk"
"github.com/rs/zerolog/log"
Expand All @@ -15,14 +13,15 @@ import (
)

const (
defaultAddr = "127.0.0.1"
defaultPort = 9098
localRPCAddr = "http://localhost:26657"
defaultAddr = "127.0.0.1"
defaultPort = 9098
defaultRPCAddr = "http://localhost:26657"
)

var (
addr string
port int
addr string
port int
rpcAddr string
)

// Health is response entity from REST.
Expand All @@ -38,7 +37,8 @@ type HealthResult struct {

func main() {
flag.StringVar(&addr, "addr", defaultAddr, "IP Address(e.g. 0.0.0.0, 127.0.0.1)")
flag.IntVar(&port, "port", defaultPort, "Port number, default 9098")
flag.IntVar(&port, "port", defaultPort, "Port number")
flag.StringVar(&rpcAddr, "rpcAddr", defaultRPCAddr, "RPC addrest:port (e.g. http://127.0.0.1:26667)")

flag.Parse()

Expand All @@ -54,60 +54,33 @@ func main() {
func pluginFeature(info, option map[string]*structpb.Value) (sdk.CallResponse, error) {
state := pluginpb.STATE_NONE
severity := pluginpb.SEVERITY_INFO

req, err := http.NewRequest("GET", localRPCAddr+"/health", nil)
healthStatus, err := health.GetHealth(rpcAddr)
if err != nil {
contentMSG := "request error"
severity := pluginpb.SEVERITY_ERROR
contentMSG := "UNHEALTHY"
return sdk.CallResponse{
FuncName: "gaiadUP",
FuncName: "GetHealth",
Message: contentMSG,
Severity: severity,
State: state,
Severity: pluginpb.SEVERITY_CRITICAL,
State: pluginpb.STATE_FAILURE,
AlertTypes: []pluginpb.ALERT_TYPE{pluginpb.ALERT_TYPE_DISCORD},
}, err
}, nil
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
contentMSG := "request progress error"
severity := pluginpb.SEVERITY_ERROR
return sdk.CallResponse{
FuncName: "gaiadUP",
Message: contentMSG,
Severity: severity,
State: state,
AlertTypes: []pluginpb.ALERT_TYPE{pluginpb.ALERT_TYPE_DISCORD},
}, err
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
contentMSG := "request result error"
severity := pluginpb.SEVERITY_ERROR
return sdk.CallResponse{
FuncName: "gaiadUP",
Message: contentMSG,
Severity: severity,
State: state,
AlertTypes: []pluginpb.ALERT_TYPE{pluginpb.ALERT_TYPE_DISCORD},
}, err
}
var health Health
json.Unmarshal([]byte(string(bytes)), &health)

contentMSG := ""
if health.Result == (HealthResult{}) {
log.Info().Str("process", "up").Msg(fmt.Sprintf("gaiad Process alive"))
contentMSG = "gaiad Process is UP"

if healthStatus == 200 {
log.Info().Str("process", "up").Msg(fmt.Sprintf("HEALTHY"))
contentMSG = "HEALTHY"
state = pluginpb.STATE_SUCCESS
} else {
log.Info().Str("process", "up").Msg(fmt.Sprintf("gaiad Process died"))
contentMSG = "gaiad Process is DOWN"
log.Info().Str("process", "up").Msg(fmt.Sprintf("UNHEALTHY"))
contentMSG = "UNHEALTHY"
state = pluginpb.STATE_SUCCESS
severity = pluginpb.SEVERITY_CRITICAL
}

ret := sdk.CallResponse{
FuncName: "gaiadUP",
FuncName: "GetHealth",
Message: contentMSG,
Severity: severity,
State: state,
Expand Down
6 changes: 6 additions & 0 deletions rpc/cosmos/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ const fixtureStatus string = `{
}
}
}`

const fixtureHealth string = `{
"id": 0,
"jsonrpc": "2.0",
"result": {}
}`
21 changes: 21 additions & 0 deletions rpc/cosmos/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package common

import (
"net/http"
)

// GetHealth is cosmos health chcke function
func GetHealth(rpcAddr string) (int, error) {
req, err := http.NewRequest(http.MethodGet, rpcAddr+"/health", nil)
if err != nil {
return -1, err
}

req.Header.Add("Content-Type", "application/json")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return -1, err
}
return resp.StatusCode, nil
}
24 changes: 24 additions & 0 deletions rpc/cosmos/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package common

import (
"net/http"
"testing"

"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)

func TestGetHealth(t *testing.T) {
httpmock.Activate()

httpmock.RegisterResponder(
http.MethodGet,
"http://localhost:26657/health",
httpmock.NewStringResponder(http.StatusOK, fixtureHealth),
)
status, err := GetHealth("http://localhost:26657")

assert.Nil(t, err)
assert.Equal(t, 200, status)
assert.False(t, status == 500, true)
}