-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
318 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
tz "github.com/ecadlabs/gotez/v2" | ||
) | ||
|
||
type Config struct { | ||
Listen string `yaml:"listen"` | ||
URL string `yaml:"url"` | ||
ChainID *tz.ChainID `yaml:"chain_id"` | ||
Timeout time.Duration `yaml:"timeout"` | ||
Tolerance time.Duration `yaml:"tolerance"` | ||
ReconnectDelay time.Duration `yaml:"reconnect_delay"` | ||
UseTimestamps bool `yaml:"use_timestamps"` | ||
CheckBlockDelay bool `yaml:"check_block_delay"` | ||
CheckBootstrapped bool `yaml:"check_bootstrapped"` | ||
CheckSyncState bool `yaml:"check_sync_state"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
listen: :8080 | ||
url: http://ghostnet.ecadinfra.com:8080 | ||
chain_id: NetXnHfVqm9iesp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
tz "github.com/ecadlabs/gotez/v2" | ||
"github.com/ecadlabs/gotez/v2/client" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type HealthChecker struct { | ||
Monitor *HeadMonitor | ||
Client *client.Client | ||
ChainID *tz.ChainID | ||
Timeout time.Duration | ||
|
||
CheckBlockDelay bool | ||
CheckBootstrapped bool | ||
CheckSyncState bool | ||
} | ||
|
||
type HealthStatus struct { | ||
IsBootstrapped bool `json:"bootstrapped"` | ||
IsSynced bool `json:"synced"` | ||
BlockDelayOk bool `json:"block_delay_ok"` | ||
} | ||
|
||
func (h *HealthChecker) HealthStatus(ctx context.Context) (*HealthStatus, bool, error) { | ||
var status HealthStatus | ||
ok := true | ||
if h.CheckBootstrapped || h.CheckSyncState { | ||
c, cancel := context.WithTimeout(ctx, h.Timeout) | ||
defer cancel() | ||
resp, err := h.Client.IsBootstrapped(c, h.ChainID) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
if h.CheckBootstrapped { | ||
status.IsBootstrapped = resp.Bootstrapped | ||
ok = ok && status.IsBootstrapped | ||
} | ||
if h.CheckSyncState { | ||
status.IsSynced = resp.SyncState == client.SyncStateSynced | ||
ok = ok && status.IsSynced | ||
} | ||
} | ||
if h.CheckBlockDelay { | ||
status.BlockDelayOk = h.Monitor.Status() | ||
ok = ok && status.BlockDelayOk | ||
} | ||
|
||
if !ok { | ||
log.WithFields(log.Fields{ | ||
"chain_id": h.ChainID, | ||
"bootstrapped": status.IsBootstrapped, | ||
"synced": status.IsSynced, | ||
"block_delay_ok": status.BlockDelayOk, | ||
}).Warn("Chain health is not ok") | ||
} | ||
return &status, ok, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
// Logging middleware inspired by github.com/urfave/negroni | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func (rw *responseWriter) Status() int { | ||
return rw.status | ||
} | ||
|
||
func (rw *responseWriter) WriteHeader(s int) { | ||
rw.status = s | ||
rw.ResponseWriter.WriteHeader(s) | ||
} | ||
|
||
func (rw *responseWriter) Write(data []byte) (int, error) { | ||
if rw.status == 0 { | ||
rw.status = http.StatusOK | ||
} | ||
|
||
return rw.ResponseWriter.Write(data) | ||
} | ||
|
||
var _ http.ResponseWriter = &responseWriter{} | ||
var _ http.ResponseWriter = &responseWriterHijacker{} | ||
var _ http.Hijacker = &responseWriterHijacker{} | ||
|
||
// ResponseStatusWriter wraps http.ResponseWriter to save HTTP status code | ||
type ResponseStatusWriter interface { | ||
http.ResponseWriter | ||
Status() int | ||
} | ||
|
||
type responseWriter struct { | ||
http.ResponseWriter | ||
status int | ||
} | ||
|
||
type responseWriterHijacker struct { | ||
*responseWriter | ||
http.Hijacker | ||
} | ||
|
||
func newResponseStatusWriter(w http.ResponseWriter) ResponseStatusWriter { | ||
ret := &responseWriter{ | ||
ResponseWriter: w, | ||
} | ||
|
||
if h, ok := w.(http.Hijacker); ok { | ||
return &responseWriterHijacker{ | ||
responseWriter: ret, | ||
Hijacker: h, | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
// Logging is a logrus-enabled logging middleware | ||
type Logging struct { | ||
Logger *log.Logger | ||
} | ||
|
||
func (l *Logging) log() *log.Logger { | ||
if l.Logger != nil { | ||
return l.Logger | ||
} | ||
return log.StandardLogger() | ||
} | ||
|
||
// Handler wraps provided http.Handler with middleware | ||
func (l *Logging) Handler(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
timestamp := time.Now() | ||
|
||
rw := newResponseStatusWriter(w) | ||
h.ServeHTTP(rw, r) | ||
|
||
fields := log.Fields{ | ||
"start_time": timestamp.Format(time.RFC3339), | ||
"duration": time.Since(timestamp), | ||
"status": rw.Status(), | ||
"hostname": r.Host, | ||
"method": r.Method, | ||
"path": r.URL.Path, | ||
} | ||
|
||
l.log().WithFields(fields).Println(r.Method + " " + r.URL.Path) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"flag" | ||
|
||
"github.com/ecadlabs/gotez/v2/client" | ||
"github.com/gorilla/mux" | ||
log "github.com/sirupsen/logrus" | ||
"golang.org/x/sys/unix" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const ( | ||
defaultListen = ":8080" | ||
defaultTimeout = 30 * time.Second | ||
defaultTolerance = 1 * time.Second | ||
defaultReconnectDelay = 10 * time.Second | ||
) | ||
|
||
func main() { | ||
logLevel := flag.String("l", "info", "Log level: [error, warn, info, debug, trace]") | ||
confPath := flag.String("c", "", "Config file path") | ||
flag.Parse() | ||
|
||
ll, err := log.ParseLevel(*logLevel) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.SetLevel(ll) | ||
|
||
conf := Config{ | ||
Listen: defaultListen, | ||
Timeout: defaultTimeout, | ||
Tolerance: defaultTolerance, | ||
ReconnectDelay: defaultReconnectDelay, | ||
CheckBlockDelay: true, | ||
CheckBootstrapped: true, | ||
CheckSyncState: true, | ||
} | ||
|
||
buf, err := os.ReadFile(*confPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := yaml.Unmarshal(buf, &conf); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
cl := client.Client{ | ||
URL: conf.URL, | ||
} | ||
|
||
mon := HeadMonitor{ | ||
Client: &cl, | ||
ChainID: conf.ChainID, | ||
Timeout: conf.Timeout, | ||
Tolerance: conf.Tolerance, | ||
ReconnectDelay: conf.ReconnectDelay, | ||
UseTimestamps: conf.UseTimestamps, | ||
} | ||
checker := HealthChecker{ | ||
Monitor: &mon, | ||
Client: &cl, | ||
ChainID: conf.ChainID, | ||
Timeout: conf.Timeout, | ||
CheckBlockDelay: conf.CheckBlockDelay, | ||
CheckBootstrapped: conf.CheckBootstrapped, | ||
CheckSyncState: conf.CheckSyncState, | ||
} | ||
if conf.CheckBlockDelay { | ||
mon.Start() | ||
defer mon.Stop(context.Background()) | ||
} | ||
|
||
r := mux.NewRouter() | ||
r.Methods("GET").Path("/health").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
status, ok, err := checker.HealthStatus(r.Context()) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprintf(w, "%v", err) | ||
return | ||
} | ||
var code int | ||
if ok { | ||
code = http.StatusOK | ||
} else { | ||
code = http.StatusInternalServerError | ||
} | ||
w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
w.WriteHeader(code) | ||
json.NewEncoder(w).Encode(status) | ||
}) | ||
r.Use((&Logging{}).Handler) | ||
|
||
srv := &http.Server{ | ||
Handler: r, | ||
Addr: conf.Listen, | ||
} | ||
go func() { | ||
log.Infof("Listening on %s", conf.Listen) | ||
srv.ListenAndServe() | ||
}() | ||
|
||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, unix.SIGINT, unix.SIGTERM) | ||
<-c | ||
|
||
srv.Shutdown(context.Background()) | ||
} |
Oops, something went wrong.