Implement lab6 observability with OpenTelemetry and Grafana

This commit is contained in:
ferraronp
2026-11-17 19:14:43 +03:00
parent 470d9d6f1e
commit e636d4cc76
68 changed files with 5863 additions and 47 deletions
+33
View File
@@ -0,0 +1,33 @@
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
username VARCHAR(32) NOT NULL UNIQUE,
display_name BYTEA NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT users_display_bytes CHECK (octet_length(display_name) BETWEEN 1 AND 400)
);
CREATE TABLE IF NOT EXISTS chats (
id UUID PRIMARY KEY,
title BYTEA NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT chats_title_bytes CHECK (octet_length(title) BETWEEN 1 AND 400)
);
CREATE TABLE IF NOT EXISTS chat_members (
chat_id UUID NOT NULL REFERENCES chats(id),
user_id UUID NOT NULL REFERENCES users(id),
position INTEGER NOT NULL,
PRIMARY KEY (chat_id, user_id),
UNIQUE (chat_id, position)
);
CREATE INDEX IF NOT EXISTS idx_members_user ON chat_members (user_id, chat_id);
CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id),
sender_id UUID NOT NULL,
text BYTEA NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id),
CONSTRAINT messages_text_bytes CHECK (octet_length(text) BETWEEN 1 AND 16000)
);
CREATE INDEX IF NOT EXISTS idx_messages_chat_history ON messages (chat_id, created_at, id);
CREATE INDEX IF NOT EXISTS idx_chats_history ON chats (created_at, id);
+12
View File
@@ -0,0 +1,12 @@
-- Same transaction creates message and idempotency receipt. Unique key arbitrates concurrent replicas.
CREATE TABLE IF NOT EXISTS message_idempotency (
actor_id UUID NOT NULL REFERENCES users(id),
chat_id UUID NOT NULL REFERENCES chats(id),
method VARCHAR(8) NOT NULL DEFAULT 'POST',
idem_key VARCHAR(128) NOT NULL,
body_sha256 CHAR(64) NOT NULL,
message_id UUID NOT NULL REFERENCES messages(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (actor_id, chat_id, method, idem_key)
);
CREATE INDEX IF NOT EXISTS idx_idempotency_age ON message_idempotency (created_at);
+25
View File
@@ -0,0 +1,25 @@
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_bytes;
ALTER TABLE messages ADD CONSTRAINT messages_text_bytes CHECK (octet_length(text) BETWEEN 0 AND 16000);
CREATE TABLE IF NOT EXISTS attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id),
owner_id UUID NOT NULL REFERENCES users(id),
object_key TEXT NOT NULL UNIQUE,
backup_key TEXT NOT NULL UNIQUE,
filename TEXT NOT NULL,
content_type VARCHAR(32) NOT NULL CHECK(content_type IN ('image/png','image/jpeg')),
size_bytes BIGINT NOT NULL CHECK(size_bytes BETWEEN 1 AND 10485760),
sha256 CHAR(64) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (chat_id,owner_id) REFERENCES chat_members(chat_id,user_id)
);
CREATE INDEX IF NOT EXISTS idx_attachments_chat ON attachments(chat_id);
CREATE TABLE IF NOT EXISTS message_attachments (
message_id UUID NOT NULL REFERENCES messages(id),
attachment_id UUID NOT NULL REFERENCES attachments(id),
position INTEGER NOT NULL CHECK(position BETWEEN 0 AND 9),
PRIMARY KEY (message_id, attachment_id),
UNIQUE (message_id,position)
);
CREATE INDEX IF NOT EXISTS idx_message_attachments_attachment ON message_attachments(attachment_id);
+58
View File
@@ -0,0 +1,58 @@
-- Lab5: stage durably, then transact job and outbox atomically.
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'ready'
CHECK (status IN ('queued','processing','ready','failed'));
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS error_code TEXT;
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key TEXT;
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS pipeline_version INTEGER NOT NULL DEFAULT 1;
CREATE UNIQUE INDEX IF NOT EXISTS idx_attachments_staging ON attachments(staging_key) WHERE staging_key IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_attachments_status ON attachments(status);
CREATE TABLE IF NOT EXISTS attachment_variants (
attachment_id UUID NOT NULL REFERENCES attachments(id),
pipeline_version INTEGER NOT NULL CHECK (pipeline_version >= 1),
name TEXT NOT NULL CHECK (name IN ('thumbnail','crop','optimized')),
object_key TEXT NOT NULL UNIQUE,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL CHECK (size_bytes > 0),
sha256 CHAR(64) NOT NULL,
width INTEGER NOT NULL CHECK (width > 0),
height INTEGER NOT NULL CHECK (height > 0),
PRIMARY KEY (attachment_id,pipeline_version,name)
);
CREATE TABLE IF NOT EXISTS processing_jobs (
id UUID PRIMARY KEY,
attachment_id UUID NOT NULL UNIQUE REFERENCES attachments(id),
pipeline_version INTEGER NOT NULL DEFAULT 1,
status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued','processing','ready','failed')),
attempt INTEGER NOT NULL DEFAULT 1 CHECK (attempt >= 1),
next_run_at TIMESTAMPTZ NOT NULL DEFAULT now(),
lease_token UUID,
leased_until TIMESTAMPTZ,
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_jobs_leases ON processing_jobs(status, leased_until);
CREATE TABLE IF NOT EXISTS job_outbox (
id UUID PRIMARY KEY,
job_id UUID NOT NULL REFERENCES processing_jobs(id),
payload JSONB NOT NULL,
available_at TIMESTAMPTZ NOT NULL DEFAULT now(),
published_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_outbox_due ON job_outbox(available_at, created_at) WHERE published_at IS NULL;
CREATE TABLE IF NOT EXISTS job_replay_audit (
id UUID PRIMARY KEY,
job_id UUID NOT NULL REFERENCES processing_jobs(id),
replayed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
action TEXT NOT NULL DEFAULT 'operator_replay'
);
CREATE TABLE IF NOT EXISTS job_fault_once (
job_id UUID NOT NULL REFERENCES processing_jobs(id),
fault TEXT NOT NULL,
PRIMARY KEY (job_id,fault)
);