82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package health
|
|
|
|
import (
|
|
"context"
|
|
"go.uber.org/fx"
|
|
"universe.mrlokop.dev/core/app"
|
|
"universe.mrlokop.dev/core/modules"
|
|
)
|
|
|
|
var (
|
|
DefaultComponentGroup = "universe:health:component"
|
|
DefaultComponentTag = app.Group(DefaultComponentGroup)
|
|
)
|
|
|
|
type ProbableComponent interface {
|
|
modules.Component
|
|
Probe(ctx context.Context) (*ComponentStatus, error)
|
|
}
|
|
|
|
type ComponentStatus struct {
|
|
IsHealthy bool
|
|
IsReady bool
|
|
|
|
Message string
|
|
URL string
|
|
Additional map[string]string
|
|
Error error
|
|
}
|
|
|
|
type ComponentConfiguration struct {
|
|
IsDisabled bool `mapstructure:"disabled"`
|
|
Group string `mapstructure:"group"`
|
|
}
|
|
|
|
type simpleProbableComponent struct {
|
|
kind string
|
|
id string
|
|
|
|
prober func(context.Context) (*ComponentStatus, error)
|
|
}
|
|
|
|
func (s *simpleProbableComponent) ComponentKind() string {
|
|
return s.kind
|
|
}
|
|
|
|
func (s *simpleProbableComponent) ComponentId() string {
|
|
return s.id
|
|
}
|
|
|
|
func (s *simpleProbableComponent) Probe(ctx context.Context) (*ComponentStatus, error) {
|
|
return s.prober(ctx)
|
|
}
|
|
|
|
func SimpleComponent(kind, id string, prober func(context.Context) (*ComponentStatus, error)) ProbableComponent {
|
|
return &simpleProbableComponent{kind: kind, id: id, prober: prober}
|
|
}
|
|
|
|
func SimpleComponentProvider(kind, id string, prober func(context.Context) (*ComponentStatus, error)) any {
|
|
return SimpleComponentProviderTagged(kind, id, DefaultComponentTag, prober)
|
|
}
|
|
|
|
func SimpleComponentProviderTagged(kind, id, tag string, prober func(context.Context) (*ComponentStatus, error)) any {
|
|
return fx.Annotate(
|
|
func() ProbableComponent {
|
|
return SimpleComponent(kind, id, prober)
|
|
},
|
|
fx.ResultTags(tag),
|
|
)
|
|
}
|
|
|
|
func SimpleComponentConfigured(ctx modules.ModuleFactoryContext, config ComponentConfiguration, prober func(context.Context) (*ComponentStatus, error)) any {
|
|
if config.IsDisabled {
|
|
return fx.Options()
|
|
}
|
|
|
|
if config.Group == "" {
|
|
config.Group = DefaultComponentGroup
|
|
}
|
|
|
|
return fx.Provide(SimpleComponentProviderTagged(ctx.Kind(), ctx.Id(), app.Group(config.Group), prober))
|
|
}
|