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

feat: health checks need to consider DB health #1382

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
33 changes: 33 additions & 0 deletions internal/driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
type (
RegistryDefault struct {
p persistence.Persister
migrationStatus popx.MigrationStatuses
traverser relationtuple.Traverser
mb *popx.MigrationBox
extraMigrations []fs.FS
Expand Down Expand Up @@ -130,6 +131,38 @@ func (r *RegistryDefault) HealthHandler() *healthx.Handler {
if r.healthReadyCheckers == nil {
r.healthReadyCheckers = healthx.ReadyCheckers{}
}

if _, found := r.healthReadyCheckers["database"]; !found {
r.healthReadyCheckers["database"] = func(_ *http.Request) error {
return r.p.Ping()
}
}

if _, found := r.healthReadyCheckers["migrations"]; !found {
r.healthReadyCheckers["migrations"] = func(req *http.Request) error {
if r.migrationStatus != nil && !r.migrationStatus.HasPending() {
return nil
}

mb, err := r.MigrationBox(req.Context())
if err != nil {
return err
}

status, err := mb.Status(req.Context())
if err != nil {
return err
}

if status.HasPending() {
return errors.Errorf("migrations have not yet been fully applied")
}

r.migrationStatus = status
return nil
}
}

r.healthH = healthx.NewHandler(r.Writer(), config.Version, r.healthReadyCheckers)
}

Expand Down
2 changes: 2 additions & 0 deletions internal/persistence/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type (
NetworkID(ctx context.Context) uuid.UUID
SetNetwork(nid uuid.UUID)
Transaction(ctx context.Context, f func(ctx context.Context) error) error

Ping() error
}
Migrator interface {
MigrationBox(ctx context.Context) (*popx.MigrationBox, error)
Expand Down
9 changes: 9 additions & 0 deletions internal/persistence/sql/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func (p *Persister) Connection(ctx context.Context) *pop.Connection {
return popx.GetConnection(ctx, p.conn.WithContext(ctx))
}

func (p *Persister) Ping() error {
type pinger interface {
Ping() error
}

// This can not be contextualized because of some gobuffalo/pop limitations.
return errors.WithStack(p.conn.Store.(pinger).Ping())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work because the "original" Store is wrapped in a contextStore, which for some reason doesn't implement Ping anymore: interface conversion: pop.contextStore is not sql.pinger: missing method Ping

In Kratos we don't do that. Not sure why we do it Keto.

}

func (p *Persister) createWithNetwork(ctx context.Context, v interface{}) (err error) {
ctx, span := p.d.Tracer(ctx).Tracer().Start(ctx, "persistence.sql.createWithNetwork")
defer otelx.End(span, &err)
Expand Down
Loading