mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-22 16:03:44 +01:00
Add enabled parameter to service (#175)
* feat: Add enabled flag to service * Add IsEnabled method Co-authored-by: 1newsr <1newsr@users.noreply.github.com>
This commit is contained in:
parent
d6b4c2394a
commit
9bd5c38a96
@ -144,6 +144,7 @@ If you want to test it locally, see [Docker](#docker).
|
|||||||
| `metrics` | Whether to expose metrics at /metrics. | `false` |
|
| `metrics` | Whether to expose metrics at /metrics. | `false` |
|
||||||
| `storage` | [Storage configuration](#storage) | `{}` |
|
| `storage` | [Storage configuration](#storage) | `{}` |
|
||||||
| `services` | List of services to monitor. | Required `[]` |
|
| `services` | List of services to monitor. | Required `[]` |
|
||||||
|
| `services[].enabled` | Whether to enable the service. | `true` |
|
||||||
| `services[].name` | Name of the service. Can be anything. | Required `""` |
|
| `services[].name` | Name of the service. Can be anything. | Required `""` |
|
||||||
| `services[].group` | Group name. Used to group multiple services together on the dashboard. <br />See [Service groups](#service-groups). | `""` |
|
| `services[].group` | Group name. Used to group multiple services together on the dashboard. <br />See [Service groups](#service-groups). | `""` |
|
||||||
| `services[].url` | URL to send the request to. | Required `""` |
|
| `services[].url` | URL to send the request to. | Required `""` |
|
||||||
|
@ -45,6 +45,9 @@ var (
|
|||||||
|
|
||||||
// Service is the configuration of a monitored endpoint
|
// Service is the configuration of a monitored endpoint
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
// Enabled defines whether to enable the service
|
||||||
|
Enabled *bool `yaml:"enabled,omitempty"`
|
||||||
|
|
||||||
// Name of the service. Can be anything.
|
// Name of the service. Can be anything.
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
|
|
||||||
@ -91,6 +94,14 @@ type Service struct {
|
|||||||
NumberOfSuccessesInARow int
|
NumberOfSuccessesInARow int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsEnabled returns whether the service is enabled or not
|
||||||
|
func (service Service) IsEnabled() bool {
|
||||||
|
if service.Enabled == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return *service.Enabled
|
||||||
|
}
|
||||||
|
|
||||||
// ValidateAndSetDefaults validates the service's configuration and sets the default value of fields that have one
|
// ValidateAndSetDefaults validates the service's configuration and sets the default value of fields that have one
|
||||||
func (service *Service) ValidateAndSetDefaults() error {
|
func (service *Service) ValidateAndSetDefaults() error {
|
||||||
// Set default values
|
// Set default values
|
||||||
|
@ -10,6 +10,18 @@ import (
|
|||||||
"github.com/TwinProduction/gatus/client"
|
"github.com/TwinProduction/gatus/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestService_IsEnabled(t *testing.T) {
|
||||||
|
if !(Service{Enabled: nil}).IsEnabled() {
|
||||||
|
t.Error("service.IsEnabled() should've returned true, because Enabled was set to nil")
|
||||||
|
}
|
||||||
|
if value := false; (Service{Enabled: &value}).IsEnabled() {
|
||||||
|
t.Error("service.IsEnabled() should've returned false, because Enabled was set to false")
|
||||||
|
}
|
||||||
|
if value := true; !(Service{Enabled: &value}).IsEnabled() {
|
||||||
|
t.Error("Service.IsEnabled() should've returned true, because Enabled was set to true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestService_ValidateAndSetDefaults(t *testing.T) {
|
func TestService_ValidateAndSetDefaults(t *testing.T) {
|
||||||
condition := Condition("[STATUS] == 200")
|
condition := Condition("[STATUS] == 200")
|
||||||
service := Service{
|
service := Service{
|
||||||
|
@ -26,9 +26,11 @@ var (
|
|||||||
func Monitor(cfg *config.Config) {
|
func Monitor(cfg *config.Config) {
|
||||||
ctx, cancelFunc = context.WithCancel(context.Background())
|
ctx, cancelFunc = context.WithCancel(context.Background())
|
||||||
for _, service := range cfg.Services {
|
for _, service := range cfg.Services {
|
||||||
// To prevent multiple requests from running at the same time, we'll wait for a little bit before each iteration
|
if service.IsEnabled() {
|
||||||
time.Sleep(1111 * time.Millisecond)
|
// To prevent multiple requests from running at the same time, we'll wait for a little bit before each iteration
|
||||||
go monitor(service, cfg.Alerting, cfg.DisableMonitoringLock, cfg.Metrics, cfg.Debug, ctx)
|
time.Sleep(1111 * time.Millisecond)
|
||||||
|
go monitor(service, cfg.Alerting, cfg.DisableMonitoringLock, cfg.Metrics, cfg.Debug, ctx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user