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
@@ -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
|
||||
|
||||
Reference in new issue
Block a user