Skip to content
Open
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
63 changes: 0 additions & 63 deletions healthcheck/audit.go

This file was deleted.

44 changes: 0 additions & 44 deletions healthcheck/audit_test.go

This file was deleted.

9 changes: 4 additions & 5 deletions healthcheck/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -18,7 +17,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)
AddKafkaConsumer(name string, consumer KafkaConsumer, cacheDuration time.Duration)
MakeHandler(rateLimit ratelimit.Allower) http.HandlerFunc
}

Expand Down Expand Up @@ -134,9 +133,9 @@ func (hc *healthchecker) AddDatabase(name string, db HealthDatabase, cacheDurati
hc.AddHealthChecker(name, newDatabaseChecker(name, db, cacheDuration, RealTimeProvider{}))
}

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, RealTimeProvider{}))
func (hc *healthchecker) AddKafkaConsumer(name string, consumer KafkaConsumer, cacheDuration time.Duration) {
hc.logger.Info(context.Background(), "msg", "Adding Kafka consumer", "processor", name)
hc.AddHealthChecker(name, newKafkaConsumerChecker(name, consumer, cacheDuration, RealTimeProvider{}))
}

// MakeHandler makes a HTTP handler that returns health check information
Expand Down
47 changes: 47 additions & 0 deletions healthcheck/kafkaconsumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package healthcheck

import (
"time"
)

type KafkaConsumer interface {
IsLive() bool
}

type kafkaConsumerChecker struct {
alias string
consumer KafkaConsumer
response HealthStatus
failureCounter int
}

func newKafkaConsumerChecker(alias string, consumer KafkaConsumer, cacheDuration time.Duration, timeProvider TimeProvider) BasicChecker {
healthStatusType := "kafkaConsumer"
response := HealthStatus{Name: &alias, Type: &healthStatusType, CacheDuration: cacheDuration, TimeProvider: timeProvider}
response.connection("init")
response.stateUp()
return &kafkaConsumerChecker{
alias: alias,
consumer: consumer,
response: response,
failureCounter: 0,
}
}

func (k *kafkaConsumerChecker) CheckStatus() HealthStatus {
if !k.response.hasExpired() {
return k.response
}

if k.consumer.IsLive() {
k.response.connection("consuming")
k.response.stateUp()
k.failureCounter = 0
} else {
k.response.stateDown("Kafka consumer is not live")
k.failureCounter++
}

k.response.touch()
return k.response
}
92 changes: 92 additions & 0 deletions healthcheck/kafkaconsumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package healthcheck

import (
"testing"
"time"

"github.com/cloudtrust/common-service/v2/healthcheck/mock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestKafkaConsumerHealthCheckLiveAndCached(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockTime := mock.NewTimeProvider(mockCtrl)
mockTime.EXPECT().Now().Return(testTime).AnyTimes()

consumer := mock.NewKafkaConsumer(mockCtrl)
consumer.EXPECT().IsLive().Return(true).Times(1)
checker := newKafkaConsumerChecker("alias", consumer, 10*time.Second, mockTime)

status := checker.CheckStatus()
assert.Equal(t, "UP", *status.State)
assert.Equal(t, "consuming", *status.Connection)
assert.Nil(t, status.Message)

status = checker.CheckStatus()
assert.Equal(t, "UP", *status.State)
assert.Equal(t, "consuming", *status.Connection)

internal := checker.(*kafkaConsumerChecker)
assert.Equal(t, 0, internal.failureCounter)
}

func TestKafkaConsumerHealthCheckDownAndCached(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockTime := mock.NewTimeProvider(mockCtrl)
mockTime.EXPECT().Now().Return(testTime).AnyTimes()

consumer := mock.NewKafkaConsumer(mockCtrl)
consumer.EXPECT().IsLive().Return(false).Times(1)
checker := newKafkaConsumerChecker("alias", consumer, 10*time.Second, mockTime)

status := checker.CheckStatus()
assert.Equal(t, "DOWN", *status.State)
assert.Nil(t, status.Connection)
assert.NotNil(t, status.Message)
assert.Equal(t, "Kafka consumer is not live", *status.Message)

status = checker.CheckStatus()
assert.Equal(t, "DOWN", *status.State)
assert.Equal(t, "Kafka consumer is not live", *status.Message)

internal := checker.(*kafkaConsumerChecker)
assert.Equal(t, 1, internal.failureCounter)
}

func TestKafkaConsumerHealthCheckFailureCounterResetOnRecovery(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockTime := mock.NewTimeProvider(mockCtrl)
gomock.InOrder(
mockTime.EXPECT().Now().Return(testTime),
mockTime.EXPECT().Now().Return(testTime),
mockTime.EXPECT().Now().Return(testTime.Add(10*time.Second).Add(time.Millisecond)),
mockTime.EXPECT().Now().Return(testTime.Add(10*time.Second).Add(time.Millisecond)),
)

consumer := mock.NewKafkaConsumer(mockCtrl)
gomock.InOrder(
consumer.EXPECT().IsLive().Return(false),
consumer.EXPECT().IsLive().Return(true),
)
checker := newKafkaConsumerChecker("alias", consumer, 10*time.Second, mockTime)

status := checker.CheckStatus()
assert.Equal(t, "DOWN", *status.State)
assert.Equal(t, "Kafka consumer is not live", *status.Message)

internal := checker.(*kafkaConsumerChecker)
assert.Equal(t, 1, internal.failureCounter)

status = checker.CheckStatus()
assert.Equal(t, "UP", *status.State)
assert.Equal(t, "consuming", *status.Connection)
assert.Nil(t, status.Message)
assert.Equal(t, 0, internal.failureCounter)
}
54 changes: 0 additions & 54 deletions healthcheck/mock/eventsreportermodule.go

This file was deleted.

54 changes: 54 additions & 0 deletions healthcheck/mock/kafka.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion healthcheck/mock_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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
//go:generate mockgen --build_flags=--mod=mod -destination=./mock/timeprovider.go -package=mock -mock_names=TimeProvider=TimeProvider github.com/cloudtrust/common-service/v2/healthcheck TimeProvider
//go:generate mockgen --build_flags=--mod=mod -destination=./mock/kafka.go -package=mock -mock_names=KafkaConsumer=KafkaConsumer github.com/cloudtrust/common-service/v2/healthcheck KafkaConsumer