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

feat: health check endpoint #971

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
59 changes: 59 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,65 @@ func (api *api) GetLogOutput(ctx context.Context, logType string, getLogRequest
return &GetLogOutputResponse{Log: string(logData)}, nil
}

func (api *api) Health(ctx context.Context) (*HealthResponse, error) {
var alarms []HealthAlarm

oauthStatus, err := api.albyOAuthSvc.GetInfo(ctx)
rdmitr marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
if !oauthStatus.Healthy {
alarms = append(alarms, NewHealthAlarm(HealthAlarmKindAlbyService, oauthStatus))
}

isNostrRelayReady := api.svc.IsRelayReady()
if !isNostrRelayReady {
alarms = append(alarms, NewHealthAlarm(HealthAlarmKindNostrRelayOffline, nil))
}

lnClient := api.svc.GetLNClient()

if lnClient != nil {
nodeStatus, err := lnClient.GetNodeStatus(ctx)
if err != nil {
return nil, err
}
if nodeStatus == nil || !nodeStatus.IsReady {
alarms = append(alarms, NewHealthAlarm(HealthAlarmKindNodeNotReady, nodeStatus))
}

channels, err := lnClient.ListChannels(ctx)
if err != nil {
return nil, err
}

offlineChannels := slices.DeleteFunc(channels, func(channel lnclient.Channel) bool {
return channel.Active
})

if len(offlineChannels) > 0 {
alarms = append(alarms, NewHealthAlarm(HealthAlarmKindChannelsOffline, offlineChannels))
}

peers, err := lnClient.ListPeers(ctx)
if err != nil {
return nil, err
}

disconnectedPeers := slices.DeleteFunc(peers, func(peer lnclient.PeerDetails) bool {
return peer.IsConnected
})

if len(peers) == 0 || len(disconnectedPeers) > 0 {
alarms = append(alarms, NewHealthAlarm(HealthAlarmKindPeerConnectivity, disconnectedPeers))
}
} else {
alarms = append(alarms, NewHealthAlarm(HealthAlarmKindNodeNotInitialized, nil))
}

return &HealthResponse{Alarms: alarms}, nil
}

func (api *api) parseExpiresAt(expiresAtString string) (*time.Time, error) {
var expiresAt *time.Time
if expiresAtString != "" {
Expand Down
28 changes: 28 additions & 0 deletions api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type API interface {
RestoreBackup(unlockPassword string, r io.Reader) error
MigrateNodeStorage(ctx context.Context, to string) error
GetWalletCapabilities(ctx context.Context) (*WalletCapabilitiesResponse, error)
Health(ctx context.Context) (*HealthResponse, error)
}

type App struct {
Expand Down Expand Up @@ -365,3 +366,30 @@ type Channel struct {
type MigrateNodeStorageRequest struct {
To string `json:"to"`
}

type HealthAlarmKind string

const (
HealthAlarmKindAlbyService HealthAlarmKind = "alby_service"
HealthAlarmKindNodeNotInitialized = "node_not_initialized"
HealthAlarmKindNodeNotReady = "node_not_ready"
HealthAlarmKindChannelsOffline = "channels_offline"
HealthAlarmKindNostrRelayOffline = "nostr_relay_offline"
HealthAlarmKindPeerConnectivity = "peer_connectivity"
)

type HealthAlarm struct {
Kind HealthAlarmKind `json:"kind"`
RawDetails any `json:"rawDetails,omitempty"`
}

func NewHealthAlarm(kind HealthAlarmKind, rawDetails any) HealthAlarm {
return HealthAlarm{
Kind: kind,
RawDetails: rawDetails,
}
}

type HealthResponse struct {
Alarms []HealthAlarm `json:"alarms,omitempty"`
}
12 changes: 12 additions & 0 deletions http/http_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (httpSvc *HttpService) RegisterSharedRoutes(e *echo.Echo) {
restrictedGroup.POST("/api/send-payment-probes", httpSvc.sendPaymentProbesHandler)
restrictedGroup.POST("/api/send-spontaneous-payment-probes", httpSvc.sendSpontaneousPaymentProbesHandler)
restrictedGroup.GET("/api/log/:type", httpSvc.getLogOutputHandler)
restrictedGroup.GET("/api/health", httpSvc.healthHandler)

httpSvc.albyHttpSvc.RegisterSharedRoutes(restrictedGroup, e)
}
Expand Down Expand Up @@ -1072,3 +1073,14 @@ func (httpSvc *HttpService) restoreBackupHandler(c echo.Context) error {

return c.NoContent(http.StatusNoContent)
}

func (httpSvc *HttpService) healthHandler(c echo.Context) error {
healthResponse, err := httpSvc.api.Health(c.Request().Context())
if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: fmt.Sprintf("Failed to check node health: %v", err),
})
}

return c.JSON(http.StatusOK, healthResponse)
}
4 changes: 3 additions & 1 deletion lnclient/ldk/ldk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1644,8 +1644,10 @@ func deleteOldLDKLogs(ldkLogDir string) {
}

func (ls *LDKService) GetNodeStatus(ctx context.Context) (nodeStatus *lnclient.NodeStatus, err error) {
status := ls.node.Status()
return &lnclient.NodeStatus{
InternalNodeStatus: ls.node.Status(),
IsReady: status.IsRunning && status.IsListening,
InternalNodeStatus: status,
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion lnclient/lnd/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ func lndPaymentToTransaction(payment *lnrpc.Payment) (*lnclient.Transaction, err
DescriptionHash: descriptionHash,
ExpiresAt: expiresAt,
SettledAt: settledAt,
//TODO: Metadata: (e.g. keysend),
// TODO: Metadata: (e.g. keysend),
}, nil
}

Expand Down Expand Up @@ -1131,6 +1131,7 @@ func (svc *LNDService) GetNodeStatus(ctx context.Context) (nodeStatus *lnclient.
}

return &lnclient.NodeStatus{
IsReady: true, // Assuming that, if GetNodeInfo() succeeds, the node is online and accessible.
InternalNodeStatus: map[string]interface{}{
"info": info,
"config": debugInfo.Config,
Expand Down
1 change: 1 addition & 0 deletions lnclient/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type Channel struct {
}

type NodeStatus struct {
IsReady bool `json:"isReady"`
InternalNodeStatus interface{} `json:"internalNodeStatus"`
}

Expand Down
4 changes: 3 additions & 1 deletion service/models.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package service

import (
"gorm.io/gorm"

"github.com/getAlby/hub/alby"
"github.com/getAlby/hub/config"
"github.com/getAlby/hub/events"
"github.com/getAlby/hub/lnclient"
"github.com/getAlby/hub/service/keys"
"github.com/getAlby/hub/transactions"
"gorm.io/gorm"
)

type Service interface {
Expand All @@ -23,4 +24,5 @@ type Service interface {
GetDB() *gorm.DB
GetConfig() config.Config
GetKeys() keys.Keys
IsRelayReady() bool
}
8 changes: 8 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,11 @@ func (svc *service) GetTransactionsService() transactions.TransactionsService {
func (svc *service) GetKeys() keys.Keys {
return svc.keys
}

func (svc *service) setRelayReady(ready bool) {
svc.isRelayReady.Store(ready)
}

func (svc *service) IsRelayReady() bool {
return svc.isRelayReady.Load()
}
8 changes: 0 additions & 8 deletions service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,3 @@ func (svc *service) requestVssToken(ctx context.Context) (string, error) {
}
return vssToken, nil
}

func (svc *service) setRelayReady(ready bool) {
svc.isRelayReady.Store(ready)
}

func (svc *service) IsRelayReady() bool {
return svc.isRelayReady.Load()
}
14 changes: 13 additions & 1 deletion wails/wails_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (

"github.com/sirupsen/logrus"

"github.com/wailsapp/wails/v2/pkg/runtime"

"github.com/getAlby/hub/alby"
"github.com/getAlby/hub/api"
"github.com/getAlby/hub/logger"
"github.com/wailsapp/wails/v2/pkg/runtime"
)

type WailsRequestRouterResponse struct {
Expand Down Expand Up @@ -894,6 +895,17 @@ func (app *WailsApp) WailsRequestRouter(route string, method string, body string
return WailsRequestRouterResponse{Body: nil, Error: err.Error()}
}
return WailsRequestRouterResponse{Body: nil, Error: ""}
case "/api/health":
nodeHealth, err := app.api.Health(ctx)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"route": route,
"method": method,
"body": body,
}).WithError(err).Error("Failed to check node health")
return WailsRequestRouterResponse{Body: nil, Error: err.Error()}
}
return WailsRequestRouterResponse{Body: *nodeHealth, Error: ""}
}

if strings.HasPrefix(route, "/api/log/") {
Expand Down
Loading