@@ -13,6 +13,7 @@ import (
1313 "github.com/TwiN/gatus/v5/config/maintenance"
1414 "github.com/TwiN/gatus/v5/metrics"
1515 "github.com/TwiN/gatus/v5/storage/store"
16+ "github.com/TwiN/logr"
1617)
1718
1819var (
@@ -31,31 +32,31 @@ func Monitor(cfg *config.Config) {
3132 if endpoint .IsEnabled () {
3233 // To prevent multiple requests from running at the same time, we'll wait for a little before each iteration
3334 time .Sleep (777 * time .Millisecond )
34- go monitor (endpoint , cfg .Alerting , cfg .Maintenance , cfg .Connectivity , cfg .DisableMonitoringLock , cfg .Metrics , cfg . Debug , ctx )
35+ go monitor (endpoint , cfg .Alerting , cfg .Maintenance , cfg .Connectivity , cfg .DisableMonitoringLock , cfg .Metrics , ctx )
3536 }
3637 }
3738}
3839
3940// monitor a single endpoint in a loop
40- func monitor (ep * endpoint.Endpoint , alertingConfig * alerting.Config , maintenanceConfig * maintenance.Config , connectivityConfig * connectivity.Config , disableMonitoringLock , enabledMetrics , debug bool , ctx context.Context ) {
41+ func monitor (ep * endpoint.Endpoint , alertingConfig * alerting.Config , maintenanceConfig * maintenance.Config , connectivityConfig * connectivity.Config , disableMonitoringLock bool , enabledMetrics bool , ctx context.Context ) {
4142 // Run it immediately on start
42- execute (ep , alertingConfig , maintenanceConfig , connectivityConfig , disableMonitoringLock , enabledMetrics , debug )
43+ execute (ep , alertingConfig , maintenanceConfig , connectivityConfig , disableMonitoringLock , enabledMetrics )
4344 // Loop for the next executions
4445 for {
4546 select {
4647 case <- ctx .Done ():
4748 log .Printf ("[watchdog.monitor] Canceling current execution of group=%s; endpoint=%s" , ep .Group , ep .Name )
4849 return
4950 case <- time .After (ep .Interval ):
50- execute (ep , alertingConfig , maintenanceConfig , connectivityConfig , disableMonitoringLock , enabledMetrics , debug )
51+ execute (ep , alertingConfig , maintenanceConfig , connectivityConfig , disableMonitoringLock , enabledMetrics )
5152 }
5253 }
5354 // Just in case somebody wandered all the way to here and wonders, "what about ExternalEndpoints?"
5455 // Alerting is checked every time an external endpoint is pushed to Gatus, so they're not monitored
5556 // periodically like they are for normal endpoints.
5657}
5758
58- func execute (ep * endpoint.Endpoint , alertingConfig * alerting.Config , maintenanceConfig * maintenance.Config , connectivityConfig * connectivity.Config , disableMonitoringLock , enabledMetrics , debug bool ) {
59+ func execute (ep * endpoint.Endpoint , alertingConfig * alerting.Config , maintenanceConfig * maintenance.Config , connectivityConfig * connectivity.Config , disableMonitoringLock bool , enabledMetrics bool ) {
5960 if ! disableMonitoringLock {
6061 // By placing the lock here, we prevent multiple endpoints from being monitored at the exact same time, which
6162 // could cause performance issues and return inaccurate results
@@ -64,37 +65,33 @@ func execute(ep *endpoint.Endpoint, alertingConfig *alerting.Config, maintenance
6465 }
6566 // If there's a connectivity checker configured, check if Gatus has internet connectivity
6667 if connectivityConfig != nil && connectivityConfig .Checker != nil && ! connectivityConfig .Checker .IsConnected () {
67- log . Println ("[watchdog.execute] No connectivity; skipping execution" )
68+ logr . Infof ("[watchdog.execute] No connectivity; skipping execution" )
6869 return
6970 }
70- if debug {
71- log .Printf ("[watchdog.execute] Monitoring group=%s; endpoint=%s" , ep .Group , ep .Name )
72- }
71+ logr .Debugf ("[watchdog.execute] Monitoring group=%s; endpoint=%s" , ep .Group , ep .Name )
7372 result := ep .EvaluateHealth ()
7473 if enabledMetrics {
7574 metrics .PublishMetricsForEndpoint (ep , result )
7675 }
7776 UpdateEndpointStatuses (ep , result )
78- if debug && ! result .Success {
79- log . Printf ("[watchdog.execute] Monitored group=%s; endpoint=%s; success=%v; errors=%d; duration=%s; body=%s" , ep .Group , ep .Name , result .Success , len (result .Errors ), result .Duration .Round (time .Millisecond ), result .Body )
77+ if logr . GetThreshold () == logr . LevelDebug && ! result .Success {
78+ logr . Debugf ("[watchdog.execute] Monitored group=%s; endpoint=%s; success=%v; errors=%d; duration=%s; body=%s" , ep .Group , ep .Name , result .Success , len (result .Errors ), result .Duration .Round (time .Millisecond ), result .Body )
8079 } else {
81- log . Printf ("[watchdog.execute] Monitored group=%s; endpoint=%s; success=%v; errors=%d; duration=%s" , ep .Group , ep .Name , result .Success , len (result .Errors ), result .Duration .Round (time .Millisecond ))
80+ logr . Infof ("[watchdog.execute] Monitored group=%s; endpoint=%s; success=%v; errors=%d; duration=%s" , ep .Group , ep .Name , result .Success , len (result .Errors ), result .Duration .Round (time .Millisecond ))
8281 }
8382 if ! maintenanceConfig .IsUnderMaintenance () {
8483 // TODO: Consider moving this after the monitoring lock is unlocked? I mean, how much noise can a single alerting provider cause...
85- HandleAlerting (ep , result , alertingConfig , debug )
86- } else if debug {
87- log .Println ("[watchdog.execute] Not handling alerting because currently in the maintenance window" )
88- }
89- if debug {
90- log .Printf ("[watchdog.execute] Waiting for interval=%s before monitoring group=%s endpoint=%s again" , ep .Interval , ep .Group , ep .Name )
84+ HandleAlerting (ep , result , alertingConfig , logr .GetThreshold () == logr .LevelDebug )
85+ } else {
86+ logr .Debugf ("[watchdog.execute] Not handling alerting because currently in the maintenance window" )
9187 }
88+ logr .Debugf ("[watchdog.execute] Waiting for interval=%s before monitoring group=%s endpoint=%s again" , ep .Interval , ep .Group , ep .Name )
9289}
9390
9491// UpdateEndpointStatuses updates the slice of endpoint statuses
9592func UpdateEndpointStatuses (ep * endpoint.Endpoint , result * endpoint.Result ) {
9693 if err := store .Get ().Insert (ep , result ); err != nil {
97- log . Println ("[watchdog.UpdateEndpointStatuses] Failed to insert result in storage:" , err .Error ())
94+ logr . Errorf ("[watchdog.UpdateEndpointStatuses] Failed to insert result in storage:" , err .Error ())
9895 }
9996}
10097
0 commit comments