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

Development #17

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gatego

import (
"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/pkg/monitor"
)

func createMonitorChecks(services []config.Service) []monitor.Check {
checks := make([]monitor.Check, 0)
for _, service := range services {
for _, path := range service.Paths {
for _, checkConfig := range path.Checks {
check := monitor.Check{
Name: checkConfig.Name,
Cron: checkConfig.Cron,
URL: checkConfig.URL,
Method: checkConfig.Method,
Timeout: checkConfig.Timeout,
Headers: checkConfig.Headers,
OnFailure: checkConfig.OnFailure,
}

checks = append(checks, check)
}
}
}

return checks
}
37 changes: 8 additions & 29 deletions gatego.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (

"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/contextvalues"
"github.com/hvuhsg/gatego/pkg/monitor"
)

const serviceName = "gatego"

type GateGo struct {
config config.Config
ctx context.Context
config config.Config
monitor *monitor.Monitor
ctx context.Context
}

func New(ctx context.Context, config config.Config, version string) *GateGo {
Expand All @@ -38,9 +40,10 @@ func (gg GateGo) Run() error {
defer shutdown(context.Background())
}

// Gather checks and create checker
checker := createChecker(gg.config.Services)
checker.Start()
// Create checks start monitoring
healthChecks := createMonitorChecks(gg.config.Services)
gg.monitor = monitor.New(time.Second*5, healthChecks...)
gg.monitor.Start()

server, err := newServer(gg.ctx, gg.config, useOtel)
if err != nil {
Expand All @@ -61,27 +64,3 @@ func (gg GateGo) Run() error {
return server.Shutdown(context.Background())
}
}

func createChecker(services []config.Service) *Checker {
checker := &Checker{Delay: 5 * time.Second}

for _, service := range services {
for _, path := range service.Paths {
for _, checkConfig := range path.Checks {
check := Check{
Name: checkConfig.Name,
Cron: checkConfig.Cron,
URL: checkConfig.URL,
Method: checkConfig.Method,
Timeout: checkConfig.Timeout,
Headers: checkConfig.Headers,
OnFailure: checkConfig.OnFailure,
}

checker.Checks = append(checker.Checks, check)
}
}
}

return checker
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
Expand Down Expand Up @@ -45,7 +44,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0
github.com/tdewolff/parse/v2 v2.7.17 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0
go.opentelemetry.io/otel v1.31.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.7.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.31.0
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/getkin/kin-openapi v0.128.0 h1:jqq3D9vC9pPq1dGcOCv7yOp1DaEe7c/T1vzcLbITSp4=
github.com/getkin/kin-openapi v0.128.0/go.mod h1:OZrfXzUfGrNbsKj+xmFBx6E5c6yH3At/tAKSc2UszXM=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -58,8 +56,6 @@ github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739 h1:IkjBCtQOOjIn03
github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0/go.mod h1:qxuZLtbq5QDtdeSHsS7bcf6EH6uO6jUAgk764zd3rhM=
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.7.0 h1:iNba3cIZTDPB2+IAbVY/3TUN+pCCLrNYo2GaGtsKBak=
Expand Down
20 changes: 12 additions & 8 deletions checker.go → pkg/monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gatego
package monitor

import (
"fmt"
Expand Down Expand Up @@ -77,17 +77,21 @@ func handleFailure(check Check, err error) error {
return nil
}

type Checker struct {
type Monitor struct {
Delay time.Duration
Checks []Check
scheduler *cron.Cron
}

func (c Checker) Start() error {
c.scheduler = cron.New()
func New(delay time.Duration, checks ...Check) *Monitor {
return &Monitor{Delay: delay, Checks: checks, scheduler: cron.New()}
}

func (m Monitor) Start() error {
m.scheduler = cron.New()

for _, check := range c.Checks {
err := c.scheduler.Add(uuid.NewString(), check.Cron, check.run(func(err error) {
for _, check := range m.Checks {
err := m.scheduler.Add(uuid.NewString(), check.Cron, check.run(func(err error) {
if check.OnFailure != "" {
if err := handleFailure(check, err); err != nil {
log.Default().Printf("Failed to spawn on_failure command: %s\n", err)
Expand All @@ -100,8 +104,8 @@ func (c Checker) Start() error {
}

go func() {
time.Sleep(c.Delay)
c.scheduler.Start()
time.Sleep(m.Delay)
m.scheduler.Start()
log.Default().Println("Started running automated checks.")
}()

Expand Down
14 changes: 7 additions & 7 deletions checker_test.go → pkg/monitor/monitor_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gatego
package monitor

import (
"errors"
Expand Down Expand Up @@ -89,12 +89,12 @@ func TestCheck_run(t *testing.T) {
func TestChecker_Start(t *testing.T) {
tests := []struct {
name string
checker Checker
checker Monitor
expectedError bool
}{
{
name: "successful start",
checker: Checker{
checker: Monitor{
Delay: 1 * time.Second,
Checks: []Check{
{
Expand All @@ -110,7 +110,7 @@ func TestChecker_Start(t *testing.T) {
},
{
name: "invalid cron expression",
checker: Checker{
checker: Monitor{
Delay: 1 * time.Second,
Checks: []Check{
{
Expand Down Expand Up @@ -144,12 +144,12 @@ func TestChecker_Start(t *testing.T) {
func TestChecker_OnFailure(t *testing.T) {
tests := []struct {
name string
checker Checker
checker Monitor
expectedError bool
}{
{
name: "on failure command with valid command",
checker: Checker{
checker: Monitor{
Delay: 1 * time.Second,
Checks: []Check{
{
Expand All @@ -166,7 +166,7 @@ func TestChecker_OnFailure(t *testing.T) {
},
{
name: "on failure command with invalid command",
checker: Checker{
checker: Monitor{
Delay: 1 * time.Second,
Checks: []Check{
{
Expand Down
Loading