feat(presence): expose service dependency checks
This commit is contained in:
1 parent
78d369435e
commit
33a0928ac8
14 files changed
+1186
-1004
No files matched your search
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/curator"
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/presence"
|
||||
@@ -28,6 +29,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/readiness"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/secretengine"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
||||
@@ -201,7 +203,7 @@ func (b *server) openState(ctx context.Context) error {
|
||||
b.cfg, b.runtimeVersions = cfg, versions
|
||||
b.cfgHolder.Set(cfg)
|
||||
go b.applyStatus.Run(ctx)
|
||||
go presence.Run(ctx, presence.Gateway, servicekit.Build(), client, nil, b.log)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -232,6 +234,16 @@ func (b *server) connectNATS(ctx context.Context) (*natsio.Conn, error) {
|
||||
}
|
||||
b.addCleanup(nc.Close)
|
||||
b.taskActionsConn = nc
|
||||
registry := health.NewRegistry(
|
||||
readiness.NewContractProbe("state_store", b.cfg.Health.DependencyTimeout.Std(), func(ctx context.Context) error { _, err := b.stateStore.StatusCounts(ctx); return err }),
|
||||
readiness.NewContractProbe("nats", b.cfg.Health.DependencyTimeout.Std(), func(context.Context) error {
|
||||
if !nc.IsConnected() {
|
||||
return fmt.Errorf("broker disconnected")
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
)
|
||||
go presence.Run(ctx, presence.Gateway, servicekit.Build(), b.stateStore, registry, b.log)
|
||||
return nc, nil
|
||||
}
|
||||
|
||||
|
||||
+1015
-931
File diff suppressed because it is too large.
Load diff
@@ -51,7 +51,7 @@ func Run(ctx context.Context, service string, build Build, store storecontract.P
|
||||
ticker := time.NewTicker(applystatus.RestampInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
record.Ready, record.Detail = summarise(ctx, registry)
|
||||
record.Ready, record.Detail, record.Checks = summarise(ctx, registry)
|
||||
record.ReportedAt = time.Now().UTC()
|
||||
if err := store.PutPresence(ctx, record); err != nil && ctx.Err() == nil {
|
||||
log.Warn("presence not reported", "service", service, "err", err)
|
||||
@@ -64,9 +64,9 @@ func Run(ctx context.Context, service string, build Build, store storecontract.P
|
||||
}
|
||||
}
|
||||
|
||||
func summarise(ctx context.Context, registry *health.Registry) (bool, string) {
|
||||
func summarise(ctx context.Context, registry *health.Registry) (bool, string, []health.Component) {
|
||||
if registry == nil {
|
||||
return true, ""
|
||||
return true, "", []health.Component{}
|
||||
}
|
||||
report := registry.Run(ctx)
|
||||
var degraded []string
|
||||
@@ -75,7 +75,7 @@ func summarise(ctx context.Context, registry *health.Registry) (bool, string) {
|
||||
degraded = append(degraded, c.Name)
|
||||
}
|
||||
}
|
||||
return report.Status == health.StatusOK, strings.Join(degraded, ", ")
|
||||
return report.Status == health.StatusOK, strings.Join(degraded, ", "), report.Components
|
||||
}
|
||||
|
||||
func instanceID() string {
|
||||
@@ -120,6 +120,7 @@ func Mesh(records []storecontract.Presence, now time.Time) []Service {
|
||||
if service.State == StateDown {
|
||||
// The last probe result describes a process that is gone.
|
||||
service.Detail = ""
|
||||
service.Checks = nil
|
||||
}
|
||||
services = append(services, service)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
||||
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
@@ -416,14 +417,15 @@ type ApplyStatus struct {
|
||||
// Presence is one running service instance. Ready and Detail summarise its
|
||||
// readiness probes; Detail names the degraded ones.
|
||||
type Presence struct {
|
||||
Service string `json:"service"`
|
||||
InstanceID string `json:"instance_id"`
|
||||
Version string `json:"version"`
|
||||
InstallType string `json:"install_type"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
ReportedAt time.Time `json:"reported_at"`
|
||||
Ready bool `json:"ready"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
Checks []health.Component `json:"checks"`
|
||||
Service string `json:"service"`
|
||||
InstanceID string `json:"instance_id"`
|
||||
Version string `json:"version"`
|
||||
InstallType string `json:"install_type"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
ReportedAt time.Time `json:"reported_at"`
|
||||
Ready bool `json:"ready"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
// WorkflowStat is one row of the per-workflow metrics table.
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE presence ADD COLUMN checks jsonb NOT NULL DEFAULT '[]'::jsonb;
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE presence DROP COLUMN checks;
|
||||
@@ -295,6 +295,7 @@ type Presence struct {
|
||||
ReportedAt time.Time
|
||||
Ready bool
|
||||
Detail string
|
||||
Checks []byte
|
||||
}
|
||||
|
||||
type Resource struct {
|
||||
|
||||
@@ -110,7 +110,7 @@ func (q *Queries) ListChannelStatus(ctx context.Context) ([]ChannelStatus, error
|
||||
}
|
||||
|
||||
const listPresence = `-- name: ListPresence :many
|
||||
SELECT service, instance_id, version, install_type, started_at, reported_at, ready, detail
|
||||
SELECT service, instance_id, version, install_type, started_at, reported_at, ready, detail, checks
|
||||
FROM presence ORDER BY service, instance_id
|
||||
`
|
||||
|
||||
@@ -132,6 +132,7 @@ func (q *Queries) ListPresence(ctx context.Context) ([]Presence, error) {
|
||||
&i.ReportedAt,
|
||||
&i.Ready,
|
||||
&i.Detail,
|
||||
&i.Checks,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -231,15 +232,16 @@ func (q *Queries) UpsertConfigSnapshot(ctx context.Context, arg UpsertConfigSnap
|
||||
}
|
||||
|
||||
const upsertPresence = `-- name: UpsertPresence :exec
|
||||
INSERT INTO presence (service, instance_id, version, install_type, started_at, reported_at, ready, detail)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
INSERT INTO presence (service, instance_id, version, install_type, started_at, reported_at, ready, detail, checks)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (service, instance_id) DO UPDATE SET
|
||||
version = excluded.version,
|
||||
install_type = excluded.install_type,
|
||||
started_at = excluded.started_at,
|
||||
reported_at = excluded.reported_at,
|
||||
ready = excluded.ready,
|
||||
detail = excluded.detail
|
||||
detail = excluded.detail,
|
||||
checks = excluded.checks
|
||||
`
|
||||
|
||||
type UpsertPresenceParams struct {
|
||||
@@ -251,6 +253,7 @@ type UpsertPresenceParams struct {
|
||||
ReportedAt time.Time
|
||||
Ready bool
|
||||
Detail string
|
||||
Checks []byte
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertPresence(ctx context.Context, arg UpsertPresenceParams) error {
|
||||
@@ -263,6 +266,7 @@ func (q *Queries) UpsertPresence(ctx context.Context, arg UpsertPresenceParams)
|
||||
arg.ReportedAt,
|
||||
arg.Ready,
|
||||
arg.Detail,
|
||||
arg.Checks,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
func TestPresenceReplacesDependencyChecks(t *testing.T) {
|
||||
store := postgres.New(pgstore.Pool(t))
|
||||
record := storecontract.Presence{Service: "gateway", InstanceID: "instance", StartedAt: time.Now(), ReportedAt: time.Now()}
|
||||
for _, checks := range [][]health.Component{
|
||||
{{Name: "nats", Status: health.StatusDegraded, Detail: "disconnected"}},
|
||||
{{Name: "nats", Status: health.StatusOK, Ready: true}, {Name: "state_store", Status: health.StatusOK, Ready: true}},
|
||||
{},
|
||||
} {
|
||||
record.Checks = checks
|
||||
if err := store.PutPresence(t.Context(), record); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, err := store.ListPresence(t.Context())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rows) != 1 || !reflect.DeepEqual(rows[0].Checks, checks) {
|
||||
t.Fatalf("presence = %+v; want replacement checks %+v", rows, checks)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,19 +44,20 @@ SELECT process, kind, applied_version, error, reported_at
|
||||
FROM apply_status ORDER BY process, kind;
|
||||
|
||||
-- name: UpsertPresence :exec
|
||||
INSERT INTO presence (service, instance_id, version, install_type, started_at, reported_at, ready, detail)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
INSERT INTO presence (service, instance_id, version, install_type, started_at, reported_at, ready, detail, checks)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (service, instance_id) DO UPDATE SET
|
||||
version = excluded.version,
|
||||
install_type = excluded.install_type,
|
||||
started_at = excluded.started_at,
|
||||
reported_at = excluded.reported_at,
|
||||
ready = excluded.ready,
|
||||
detail = excluded.detail;
|
||||
detail = excluded.detail,
|
||||
checks = excluded.checks;
|
||||
|
||||
-- name: DeletePresenceBefore :exec
|
||||
DELETE FROM presence WHERE reported_at < $1;
|
||||
|
||||
-- name: ListPresence :many
|
||||
SELECT service, instance_id, version, install_type, started_at, reported_at, ready, detail
|
||||
SELECT service, instance_id, version, install_type, started_at, reported_at, ready, detail, checks
|
||||
FROM presence ORDER BY service, instance_id;
|
||||
@@ -2,12 +2,14 @@ package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
)
|
||||
@@ -127,6 +129,14 @@ func (s *Store) ListApplyStatus(ctx context.Context) ([]storecontract.ApplyStatu
|
||||
// PutPresence replaces one instance's record and drops records whose
|
||||
// instance stopped re-stamping long ago, so restarts do not accumulate rows.
|
||||
func (s *Store) PutPresence(ctx context.Context, presence storecontract.Presence) error {
|
||||
checks := presence.Checks
|
||||
if checks == nil {
|
||||
checks = []health.Component{}
|
||||
}
|
||||
raw, err := json.Marshal(checks)
|
||||
if err != nil {
|
||||
return fmt.Errorf("store: encode presence checks: %w", err)
|
||||
}
|
||||
reported := presence.ReportedAt
|
||||
if reported.IsZero() {
|
||||
reported = time.Now()
|
||||
@@ -135,7 +145,7 @@ func (s *Store) PutPresence(ctx context.Context, presence storecontract.Presence
|
||||
if err := q.UpsertPresence(ctx, postgresdb.UpsertPresenceParams{
|
||||
Service: presence.Service, InstanceID: presence.InstanceID, Version: presence.Version,
|
||||
InstallType: presence.InstallType, StartedAt: presence.StartedAt.UTC(), ReportedAt: reported.UTC(),
|
||||
Ready: presence.Ready, Detail: presence.Detail,
|
||||
Ready: presence.Ready, Detail: presence.Detail, Checks: raw,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("store: put presence: %w", err)
|
||||
}
|
||||
@@ -156,9 +166,13 @@ func (s *Store) ListPresence(ctx context.Context) ([]storecontract.Presence, err
|
||||
}
|
||||
out := make([]storecontract.Presence, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
var checks []health.Component
|
||||
if err := json.Unmarshal(r.Checks, &checks); err != nil {
|
||||
return nil, fmt.Errorf("store: decode presence checks: %w", err)
|
||||
}
|
||||
out = append(out, storecontract.Presence{
|
||||
Service: r.Service, InstanceID: r.InstanceID, Version: r.Version, InstallType: r.InstallType,
|
||||
StartedAt: r.StartedAt, ReportedAt: r.ReportedAt, Ready: r.Ready, Detail: r.Detail,
|
||||
StartedAt: r.StartedAt, ReportedAt: r.ReportedAt, Ready: r.Ready, Detail: r.Detail, Checks: checks,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
||||
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
@@ -613,6 +614,10 @@ func applyStatusValue(status *pb.ApplyStatus) storecontract.ApplyStatus {
|
||||
}
|
||||
|
||||
func presenceProto(presence storecontract.Presence) *pb.Presence {
|
||||
checks := make([]*pb.DependencyCheck, 0, len(presence.Checks))
|
||||
for _, check := range presence.Checks {
|
||||
checks = append(checks, &pb.DependencyCheck{Name: check.Name, Status: string(check.Status), Ready: check.Ready, Detail: check.Detail})
|
||||
}
|
||||
return &pb.Presence{
|
||||
Service: presence.Service,
|
||||
InstanceId: presence.InstanceID,
|
||||
@@ -622,6 +627,7 @@ func presenceProto(presence storecontract.Presence) *pb.Presence {
|
||||
ReportedAt: timestamp(presence.ReportedAt),
|
||||
Ready: presence.Ready,
|
||||
Detail: presence.Detail,
|
||||
Checks: checks,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -629,6 +635,10 @@ func presenceValue(presence *pb.Presence) storecontract.Presence {
|
||||
if presence == nil {
|
||||
return storecontract.Presence{}
|
||||
}
|
||||
checks := make([]health.Component, 0, len(presence.Checks))
|
||||
for _, check := range presence.Checks {
|
||||
checks = append(checks, health.Component{Name: check.Name, Status: health.Status(check.Status), Ready: check.Ready, Detail: check.Detail})
|
||||
}
|
||||
return storecontract.Presence{
|
||||
Service: presence.Service,
|
||||
InstanceID: presence.InstanceId,
|
||||
@@ -638,6 +648,7 @@ func presenceValue(presence *pb.Presence) storecontract.Presence {
|
||||
ReportedAt: timeValue(presence.ReportedAt),
|
||||
Ready: presence.Ready,
|
||||
Detail: presence.Detail,
|
||||
Checks: checks,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user