Skip to content

Commit

Permalink
[CLOUDTRUST-5196] Add health check for AuditEventsReporterModule (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
basbeu authored Sep 26, 2023
1 parent 1fcfe29 commit 070c0a7
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
59 changes: 59 additions & 0 deletions healthcheck/audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package healthcheck

import (
"context"
"time"

"github.com/cloudtrust/common-service/v2/events"
log "github.com/cloudtrust/common-service/v2/log"
)

type auditEventsReporterChecker struct {
alias string
reporter events.AuditEventsReporterModule
timeout time.Duration
response HealthStatus
logger log.Logger
}

func newAuditEventsReporterChecker(alias string, reporter events.AuditEventsReporterModule, timeout time.Duration, cacheDuration time.Duration, logger log.Logger) BasicChecker {
healthStatusType := "auditEventreporter"
response := HealthStatus{Name: &alias, Type: &healthStatusType, CacheDuration: cacheDuration}
response.connection("init")
response.stateUp()
return &auditEventsReporterChecker{
alias: alias,
reporter: reporter,
timeout: timeout,
response: response,
logger: logger,
}
}

func (a *auditEventsReporterChecker) CheckStatus() HealthStatus {
if a.response.hasExpired() {
go a.updateStatus()
}

return a.response
}

func (a *auditEventsReporterChecker) updateStatus() {
finished := make(chan bool)
go func() {
event := events.NewEvent("healthcheck", "", "master", "health_checker", "health_checker", "master", map[string]string{})
a.reporter.ReportEvent(context.Background(), event)
finished <- true
}()

select {
case <-finished:
a.response.connection("established")
a.response.stateUp()
case <-time.After(a.timeout):
a.response.stateDown("Events reporter timeout")
a.logger.Error(context.Background(), "msg", "Audit Events Reporter timeout to produce events", "timeout", a.timeout)
}

a.response.touch()
}
42 changes: 42 additions & 0 deletions healthcheck/audit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package healthcheck

import (
"testing"
"time"

"github.com/cloudtrust/common-service/v2/healthcheck/mock"
log "github.com/cloudtrust/common-service/v2/log"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestAuditEventsReporterChecker(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()

var mockAuditEventReporter = mock.NewAuditEventsReporterModule(mockCtrl)

t.Run("Success ", func(t *testing.T) {
var auditEventReporterChecker = newAuditEventsReporterChecker("alias", mockAuditEventReporter, 10*time.Second, 10*time.Second, log.NewNopLogger())
internalChecker := auditEventReporterChecker.(*auditEventsReporterChecker)
mockAuditEventReporter.EXPECT().ReportEvent(gomock.Any(), gomock.Any()).Times(1)

internalChecker.updateStatus()
var res = internalChecker.response
assert.NotNil(t, res.Connection)
assert.Equal(t, "established", *res.Connection)
})

t.Run("Failure ", func(t *testing.T) {
var auditEventReporterChecker = newAuditEventsReporterChecker("alias", mockAuditEventReporter, 1*time.Second, 10*time.Second, log.NewNopLogger())
internalChecker := auditEventReporterChecker.(*auditEventsReporterChecker)
mockAuditEventReporter.EXPECT().ReportEvent(gomock.Any(), gomock.Any()).Do(func(arg0 interface{}, arg1 interface{}) {
time.Sleep(2 * time.Second)
})

internalChecker.updateStatus()
res := internalChecker.response
assert.NotNil(t, res.Message)
assert.Equal(t, "Events reporter timeout", *res.Message)
})
}
7 changes: 7 additions & 0 deletions healthcheck/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"time"

"github.com/cloudtrust/common-service/v2/events"
commonhttp "github.com/cloudtrust/common-service/v2/http"
log "github.com/cloudtrust/common-service/v2/log"
"github.com/go-kit/kit/ratelimit"
Expand All @@ -17,6 +18,7 @@ type HealthChecker interface {
AddHTTPEndpoint(name string, targetURL string, timeoutDuration time.Duration, expectedStatus int, cacheDuration time.Duration)
AddHTTPEndpoints(endpoints map[string]string, timeoutDuration time.Duration, expectedStatus int, cacheDuration time.Duration)
AddDatabase(name string, db HealthDatabase, cacheDuration time.Duration)
AddAuditEventsReporterModule(name string, reporter events.AuditEventsReporterModule, timeout time.Duration, cacheDuration time.Duration)
MakeHandler(rateLimit ratelimit.Allower) http.HandlerFunc
}

Expand Down Expand Up @@ -131,6 +133,11 @@ func (hc *healthchecker) AddDatabase(name string, db HealthDatabase, cacheDurati
hc.AddHealthChecker(name, newDatabaseChecker(name, db, cacheDuration))
}

func (hc *healthchecker) AddAuditEventsReporterModule(name string, reporter events.AuditEventsReporterModule, timeout time.Duration, cacheDuration time.Duration) {
hc.logger.Info(context.Background(), "msg", "Adding audit event reporter module", "processor", name)
hc.AddHealthChecker(name, newAuditEventsReporterChecker(name, reporter, timeout, cacheDuration, hc.logger))
}

// MakeHandler makes a HTTP handler that returns health check information
func (hc *healthchecker) MakeHandler(rateLimit ratelimit.Allower) http.HandlerFunc {
var ctx = context.Background()
Expand Down
1 change: 1 addition & 0 deletions healthcheck/mock_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package healthcheck

//go:generate mockgen --build_flags=--mod=mod -destination=./mock/healthcheck.go -package=mock -mock_names=HealthDatabase=HealthDatabase github.com/cloudtrust/common-service/v2/healthcheck HealthDatabase
//go:generate mockgen --build_flags=--mod=mod -destination=./mock/eventsreportermodule.go -package=mock -mock_names=AuditEventsReporterModule=AuditEventsReporterModule github.com/cloudtrust/common-service/v2/events AuditEventsReporterModule

0 comments on commit 070c0a7

Please sign in to comment.