@@ -11,6 +11,7 @@ import (
1111 "github.com/lightyeario/kelp/model"
1212 "github.com/lightyeario/kelp/plugins"
1313 "github.com/lightyeario/kelp/support/monitoring"
14+ "github.com/lightyeario/kelp/support/networking"
1415 "github.com/lightyeario/kelp/support/utils"
1516 "github.com/lightyeario/kelp/trader"
1617 "github.com/spf13/cobra"
@@ -119,6 +120,7 @@ func init() {
119120 // we want to delete all the offers and exit here since there is something wrong with our setup
120121 deleteAllOffersAndExit (botConfig , client , sdex )
121122 }
123+
122124 bot := trader .MakeBot (
123125 client ,
124126 botConfig .AssetBase (),
@@ -136,6 +138,22 @@ func init() {
136138 validateTrustlines (client , & botConfig )
137139 log .Printf ("trustlines valid\n " )
138140
141+ // --- start initialization of services ---
142+ if botConfig .MonitoringPort != 0 {
143+ go func () {
144+ e := startMonitoringServer (botConfig )
145+ if e != nil {
146+ log .Println ()
147+ log .Printf ("unable to start the monitoring server or problem encountered while running server: %s\n " , e )
148+ // we want to delete all the offers and exit here because we don't want the bot to run if monitoring isn't working
149+ // if monitoring is desired but not working properly, we want the bot to be shut down and guarantee that there
150+ // aren't outstanding offers.
151+ deleteAllOffersAndExit (botConfig , client , sdex )
152+ }
153+ }()
154+ }
155+ // --- end initialization of services ---
156+
139157 log .Println ("Starting the trader bot..." )
140158 for {
141159 bot .Start ()
@@ -144,6 +162,26 @@ func init() {
144162 }
145163}
146164
165+ func startMonitoringServer (botConfig trader.BotConfig ) error {
166+ healthMetrics , e := monitoring .MakeMetricsRecorder (map [string ]interface {}{"success" : true })
167+ if e != nil {
168+ return fmt .Errorf ("unable to make metrics recorder for the health endpoint: %s" , e )
169+ }
170+
171+ healthEndpoint , e := monitoring .MakeMetricsEndpoint ("/health" , healthMetrics , networking .NoAuth )
172+ if e != nil {
173+ return fmt .Errorf ("unable to make /health endpoint: %s" , e )
174+ }
175+
176+ server , e := networking .MakeServer ([]networking.Endpoint {healthEndpoint })
177+ if e != nil {
178+ return fmt .Errorf ("unable to initialize the metrics server: %s" , e )
179+ }
180+
181+ log .Printf ("Starting monitoring server on port %d\n " , botConfig .MonitoringPort )
182+ return server .StartServer (botConfig .MonitoringPort , "" , "" )
183+ }
184+
147185func validateTrustlines (client * horizon.Client , botConfig * trader.BotConfig ) {
148186 account , e := client .LoadAccount (botConfig .TradingAccount ())
149187 if e != nil {
0 commit comments