From 3aece958b15ee3e1fb1fb91780b7c9b1eb8eaad5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 2 Mar 2022 16:58:32 +0100 Subject: [PATCH] Update to github.com/eosswedenorg-go/haproxy@v0.1.0 --- go.mod | 4 ++-- go.sum | 8 ++++--- src/api/eosio-contract.go | 47 ++++++++++++++++++++++++++------------- src/api/interface.go | 4 ++-- src/api/v1.go | 34 +++++++++++++++++++--------- src/api/v2.go | 28 +++++++++++++---------- src/server.go | 16 ++++++++----- 7 files changed, 90 insertions(+), 51 deletions(-) diff --git a/go.mod b/go.mod index 3d0de52..f1bc0eb 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/eosswedenorg/eosio-api-healthcheck go 1.14 require ( - github.com/eosswedenorg-go/eos-contract-api-client v0.0.0-20220221105418-dc591fcc0dc5 // indirect + github.com/eosswedenorg-go/eos-contract-api-client v0.0.0-20220221105418-dc591fcc0dc5 github.com/eosswedenorg-go/eosapi v0.1.0 - github.com/eosswedenorg-go/haproxy v0.0.0-20220101140534-fccfdd93a8cd + github.com/eosswedenorg-go/haproxy v0.1.0 github.com/eosswedenorg-go/pid v1.0.0 github.com/eosswedenorg-go/tcp_server v0.1.0 github.com/go-stack/stack v1.8.1 // indirect diff --git a/go.sum b/go.sum index 89d04e8..2c17a59 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/eosswedenorg-go/eos-contract-api-client v0.0.0-20220221105418-dc591fc github.com/eosswedenorg-go/eos-contract-api-client v0.0.0-20220221105418-dc591fcc0dc5/go.mod h1:o/H/XtSXtaNe2ooXIAcp9vApmy2pY5YyeSl3yWgEnLw= github.com/eosswedenorg-go/eosapi v0.1.0 h1:SnVMx1QGPBZoQknjnAiGzjL6hVfrXPLrOdxoUstYUrk= github.com/eosswedenorg-go/eosapi v0.1.0/go.mod h1:7VrkU30cSqRtGDE6bXygWqMcEhCyWOaC9yVA34QIQzM= -github.com/eosswedenorg-go/haproxy v0.0.0-20220101140534-fccfdd93a8cd h1:e59v3HnnG60uE50OtziOxyiSNDADr1lDbzn33btn3yE= -github.com/eosswedenorg-go/haproxy v0.0.0-20220101140534-fccfdd93a8cd/go.mod h1:l5DTEb0dcTZyATo2cuaYQPqwBRuzDw0UgupNo8SAgXo= +github.com/eosswedenorg-go/haproxy v0.1.0 h1:E/9Ptt5upbVoSpkiXwffjapDF0FRLeFePTCAkvA0STQ= +github.com/eosswedenorg-go/haproxy v0.1.0/go.mod h1:WnDKkwYbgrpuKUOtUtWlGCgj43DMV+r4VKGdWSTYQeA= github.com/eosswedenorg-go/pid v1.0.0 h1:k1ra19cgWBHnX5gWQq+eUhNHIjT7hdhsYGp+Ovfvd2U= github.com/eosswedenorg-go/pid v1.0.0/go.mod h1:c9nVBDmo8pa3dVjrZjDUN4/DT1ChmfuCWXt6lHsagRc= github.com/eosswedenorg-go/tcp_server v0.1.0 h1:nbXJK4sPSGn3zGPGWR2vdmj+s/Ywp58k7dZ9fuYNZlI= @@ -37,11 +37,13 @@ github.com/pborman/getopt/v2 v2.1.0/go.mod h1:4NtW75ny4eBw9fO1bhtNdYTlZKYX5/tBLt github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/api/eosio-contract.go b/src/api/eosio-contract.go index ddc3e12..5abc9fb 100644 --- a/src/api/eosio-contract.go +++ b/src/api/eosio-contract.go @@ -4,7 +4,7 @@ package api import ( "fmt" "time" - "github.com/eosswedenorg-go/haproxy" + "github.com/eosswedenorg-go/haproxy/agentcheck" contract_api "github.com/eosswedenorg-go/eos-contract-api-client" ) @@ -32,30 +32,40 @@ func (e EosioContract) LogInfo() LogParams { // check_api - Validates head block time. // --------------------------------------------------------- -func (e EosioContract) Call() (haproxy.HealthCheckStatus, string) { +func (e EosioContract) Call() (agentcheck.Response, string) { h, err := e.client.GetHealth() if err != nil { - msg := fmt.Sprintf("%s", err); - return haproxy.HealthCheckFailed, msg + resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "Failed to contact api") + return resp, err.Error() } // Check HTTP Status Code if h.HTTPStatusCode > 299 { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because %v was received from backend", h.HTTPStatusCode) + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("HTTP %v", h.HTTPStatusCode)) + + msg := "Taking offline because %v was received from backend" + return resp, fmt.Sprintf(msg, h.HTTPStatusCode) } // Check postgres if h.Data.Postgres.Status != "OK" { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because Postgres reported '%s'", h.Data.Postgres.Status) + + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("Postgres: %s", h.Data.Postgres.Status)) + + msg := "Taking offline because Postgres reported '%s'" + return resp, fmt.Sprintf(msg, h.Data.Postgres.Status) } // Check redis if h.Data.Redis.Status != "OK" { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because Redis reported '%s'", h.Data.Redis.Status) + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("Redis: %s", h.Data.Redis.Status)) + + msg := "Taking offline because Redis reported '%s'" + return resp, fmt.Sprintf(msg, h.Data.Redis.Status) } // Validate head block. @@ -63,12 +73,19 @@ func (e EosioContract) Call() (haproxy.HealthCheckStatus, string) { diff := now.Sub(h.Data.Chain.HeadTime).Seconds() if diff > e.block_time { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because head block is lagging %.0f seconds", diff) + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("headblock is %.0f seconds behind", diff)) + + msg := "Taking offline because head block is lagging %.0f seconds" + return resp, fmt.Sprintf(msg, diff) } else if diff < -e.block_time { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because head block is %.0f seconds into the future", diff) + + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("headblock is %.0f into the future", diff)) + + msg := "Taking offline because head block is %.0f seconds into the future" + return resp, fmt.Sprintf(msg, diff) } - return haproxy.HealthCheckUp, "OK" + return agentcheck.NewStatusResponse(agentcheck.Up), "OK" } diff --git a/src/api/interface.go b/src/api/interface.go index 0c78f8b..910b6ac 100644 --- a/src/api/interface.go +++ b/src/api/interface.go @@ -2,7 +2,7 @@ package api import ( - "github.com/eosswedenorg-go/haproxy" + "github.com/eosswedenorg-go/haproxy/agentcheck" ) type ApiInterface interface { @@ -11,5 +11,5 @@ type ApiInterface interface { LogInfo() LogParams // Call api and validate it's status. - Call() (haproxy.HealthCheckStatus, string) + Call() (agentcheck.Response, string) } diff --git a/src/api/v1.go b/src/api/v1.go index 4f4f9b3..03d7677 100644 --- a/src/api/v1.go +++ b/src/api/v1.go @@ -4,7 +4,7 @@ package api import ( "fmt" "time" - "github.com/eosswedenorg-go/haproxy" + "github.com/eosswedenorg-go/haproxy/agentcheck" "github.com/eosswedenorg-go/eosapi" ) @@ -35,18 +35,22 @@ func (e EosioV1) LogInfo() LogParams { return p } -func (e EosioV1) Call() (haproxy.HealthCheckStatus, string) { +func (e EosioV1) Call() (agentcheck.Response, string) { info, err := eosapi.GetInfo(e.params) if err != nil { - msg := fmt.Sprintf("%s", err); - return haproxy.HealthCheckFailed, msg + resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "Failed to contact api") + return resp, err.Error() } // Check HTTP Status Code if info.HTTPStatusCode > 299 { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because %v was received from backend", info.HTTPStatusCode) + + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("HTTP %v", info.HTTPStatusCode)) + + msg := "Taking offline because %v was received from backend" + return resp, fmt.Sprintf(msg, info.HTTPStatusCode) } // Validate head block. @@ -54,11 +58,19 @@ func (e EosioV1) Call() (haproxy.HealthCheckStatus, string) { diff := now.Sub(info.HeadBlockTime).Seconds() if diff > e.block_time { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because head block is lagging %.0f seconds", diff) + + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("headblock is %.0f seconds behind", diff)) + + msg := "Taking offline because head block is lagging %.0f seconds" + return resp, fmt.Sprintf(msg, diff) } else if diff < -e.block_time { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because head block is %.0f seconds into the future", diff) + + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("headblock is %.0f into the future", diff)) + + msg := "Taking offline because head block is %.0f seconds into the future" + return resp, fmt.Sprintf(msg, diff) } - return haproxy.HealthCheckUp, "OK" + return agentcheck.NewStatusResponse(agentcheck.Up), "OK" } diff --git a/src/api/v2.go b/src/api/v2.go index 39bef25..7374bd9 100644 --- a/src/api/v2.go +++ b/src/api/v2.go @@ -4,7 +4,7 @@ package api import ( "fmt" "github.com/eosswedenorg/eosio-api-healthcheck/src/utils" - "github.com/eosswedenorg-go/haproxy" + "github.com/eosswedenorg-go/haproxy/agentcheck" "github.com/eosswedenorg-go/eosapi" ) @@ -35,18 +35,18 @@ func (e EosioV2) LogInfo() LogParams { return p } -func (e EosioV2) Call() (haproxy.HealthCheckStatus, string) { +func (e EosioV2) Call() (agentcheck.Response, string) { health, err := eosapi.GetHealth(e.params) if err != nil { - msg := fmt.Sprintf("%s", err); - return haproxy.HealthCheckFailed, msg + resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "Failed to contact api") + return resp, err.Error() } // Check HTTP Status Code if health.HTTPStatusCode > 299 { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode) + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, fmt.Sprintf("HTTP %v", health.HTTPStatusCode)) + return resp, fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode) } // Fetch elasticsearch and nodeos block numbers from json. @@ -65,17 +65,21 @@ func (e EosioV2) Call() (haproxy.HealthCheckStatus, string) { if es_block == 0 || node_block == 0 { msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos " + "block numbers (es: %d, eos: %d)", es_block, node_block) - return haproxy.HealthCheckFailed, msg + + resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, msg) + return resp, msg } // Check if ES is behind or in the future. diff := node_block - es_block; if diff > e.offset { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff) + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("Elastic is %d blocks behind", diff)) + return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff) } else if diff < -e.offset { - return haproxy.HealthCheckDown, - fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff) + resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, + fmt.Sprintf("Elastic is %d blocks into the future", -1 * diff)) + return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff) } - return haproxy.HealthCheckUp, "OK" + return agentcheck.NewStatusResponse(agentcheck.Up), "OK" } diff --git a/src/server.go b/src/server.go index 230b902..4ee20b9 100644 --- a/src/server.go +++ b/src/server.go @@ -6,7 +6,7 @@ import ( "strconv" "github.com/eosswedenorg/eosio-api-healthcheck/src/api" "github.com/eosswedenorg-go/eosapi" - "github.com/eosswedenorg-go/haproxy" + "github.com/eosswedenorg-go/haproxy/agentcheck" "github.com/eosswedenorg-go/tcp_server" ) @@ -53,7 +53,9 @@ func onTcpMessage(c *tcp_server.Client, args string) { msg := "Invalid number of parameters in agent request" logger.Warn("Agent request error", "message", msg, "args", split) - c.WriteString(fmt.Sprintf("%s#%s\n", haproxy.HealthCheckFailed, msg)) + resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, msg) + + c.WriteString(resp.String()) c.Close() return } @@ -82,7 +84,9 @@ func onTcpMessage(c *tcp_server.Client, args string) { healthCheckApi, err := createApi(&a) if err != nil { logger.Warn("Agent request error", "message", err) - c.WriteString(fmt.Sprintf("%s#%s\n", haproxy.HealthCheckFailed, err)) + resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, err.Error()) + + c.WriteString(resp.String()) c.Close() return } @@ -90,15 +94,15 @@ func onTcpMessage(c *tcp_server.Client, args string) { status, msg := healthCheckApi.Call() logger.Info("API Check", append([]interface{}{ - "status", status}, + "status", strings.TrimSpace(status.String())}, healthCheckApi.LogInfo().ToSlice()...)...) - if status != haproxy.HealthCheckUp && len(msg) > 0 { + if msg != "OK" && len(msg) > 0 { logger.Warn("API Check Failed", "message", msg) } // Report status to HAproxy - c.WriteString(fmt.Sprintln(status)) + c.WriteString(status.String()) c.Close() }