lab6: observability — OTel Collector, Prometheus, Loki, Tempo, Grafana
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
||||
@@ -0,0 +1,46 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
database_url = os.environ.get("DATABASE_URL")
|
||||
if not database_url:
|
||||
raise RuntimeError("DATABASE_URL environment variable is required for migrations")
|
||||
|
||||
target_metadata = None
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Оффлайн-режим: печатает SQL, не подключаясь к БД."""
|
||||
context.configure(
|
||||
url=database_url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Онлайн-режим: подключается к БД и применяет миграции."""
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
connectable = create_engine(database_url, pool_pre_ping=True)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,26 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,78 @@
|
||||
"""lab5: attachment statuses, jobs outbox
|
||||
|
||||
Revision ID: <новый_hash>
|
||||
Revises: c3f18af4ec56
|
||||
Create Date: <оставь>
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "25c48b226c14"
|
||||
down_revision = "c3f18af4ec56"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("""
|
||||
ALTER TABLE attachments
|
||||
DROP CONSTRAINT IF EXISTS attachments_status_check
|
||||
""")
|
||||
op.execute("""
|
||||
ALTER TABLE attachments
|
||||
ADD CONSTRAINT attachments_status_check
|
||||
CHECK (status IN ('queued', 'processing', 'ready', 'failed'))
|
||||
""")
|
||||
op.execute("""
|
||||
ALTER TABLE attachments
|
||||
ALTER COLUMN status SET DEFAULT 'queued'
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
ALTER TABLE attachments
|
||||
ADD COLUMN IF NOT EXISTS attempt INTEGER NOT NULL DEFAULT 0
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
ALTER TABLE attachments
|
||||
ADD COLUMN IF NOT EXISTS staging_key TEXT
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
CREATE TABLE IF NOT EXISTS jobs (
|
||||
job_id UUID PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
pipeline_version INTEGER NOT NULL DEFAULT 1,
|
||||
attempt INTEGER NOT NULL DEFAULT 1,
|
||||
status TEXT NOT NULL DEFAULT 'pending',
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
published_at TIMESTAMPTZ,
|
||||
CONSTRAINT jobs_status_check
|
||||
CHECK (status IN ('pending', 'published', 'failed'))
|
||||
)
|
||||
""")
|
||||
op.execute("""
|
||||
CREATE INDEX IF NOT EXISTS jobs_pending_idx
|
||||
ON jobs (status, created_at)
|
||||
WHERE status = 'pending'
|
||||
""")
|
||||
op.execute("""
|
||||
CREATE INDEX IF NOT EXISTS jobs_attachment_idx
|
||||
ON jobs (attachment_id)
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("DROP TABLE IF EXISTS jobs")
|
||||
op.execute("ALTER TABLE attachments DROP COLUMN IF EXISTS staging_key")
|
||||
op.execute("ALTER TABLE attachments DROP COLUMN IF EXISTS attempt")
|
||||
op.execute("""
|
||||
ALTER TABLE attachments
|
||||
DROP CONSTRAINT IF EXISTS attachments_status_check
|
||||
""")
|
||||
op.execute("""
|
||||
ALTER TABLE attachments
|
||||
ADD CONSTRAINT attachments_status_check
|
||||
CHECK (status = 'ready')
|
||||
""")
|
||||
@@ -0,0 +1,33 @@
|
||||
"""add traceparent tracestate to jobs
|
||||
|
||||
Revision ID: 2667e1880881
|
||||
Revises: 390b34423ded
|
||||
Create Date: 2026-11-10 13:31:26.245215
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision: str = '2667e1880881'
|
||||
down_revision: Union[str, None] = '390b34423ded'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("""
|
||||
ALTER TABLE jobs
|
||||
ADD COLUMN IF NOT EXISTS traceparent TEXT
|
||||
""")
|
||||
op.execute("""
|
||||
ALTER TABLE jobs
|
||||
ADD COLUMN IF NOT EXISTS tracestate TEXT
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("ALTER TABLE jobs DROP COLUMN IF EXISTS tracestate")
|
||||
op.execute("ALTER TABLE jobs DROP COLUMN IF EXISTS traceparent")
|
||||
@@ -0,0 +1,33 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "2803f3a0e579"
|
||||
down_revision = "447472b8acec"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("""
|
||||
CREATE TABLE idempotency_keys (
|
||||
user_id UUID NOT NULL,
|
||||
method TEXT NOT NULL,
|
||||
path TEXT NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
request_hash TEXT NOT NULL,
|
||||
response_status INTEGER NOT NULL,
|
||||
response_body JSONB NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
PRIMARY KEY (user_id, method, path, key)
|
||||
)
|
||||
""")
|
||||
op.execute("""
|
||||
CREATE INDEX idempotency_keys_created_at_idx
|
||||
ON idempotency_keys (created_at)
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("DROP INDEX IF EXISTS idempotency_keys_created_at_idx")
|
||||
op.execute("DROP TABLE IF EXISTS idempotency_keys")
|
||||
@@ -0,0 +1,34 @@
|
||||
"""allow processing status in jobs
|
||||
|
||||
Revision ID: <hash>
|
||||
Revises: <id lease_until>
|
||||
Create Date: <date>
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "390b34423ded"
|
||||
down_revision = "6980f998def1"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("""
|
||||
ALTER TABLE jobs DROP CONSTRAINT IF EXISTS jobs_status_check
|
||||
""")
|
||||
op.execute("""
|
||||
ALTER TABLE jobs
|
||||
ADD CONSTRAINT jobs_status_check
|
||||
CHECK (status IN ('pending', 'processing', 'published', 'failed'))
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("ALTER TABLE jobs DROP CONSTRAINT IF EXISTS jobs_status_check")
|
||||
op.execute("""
|
||||
ALTER TABLE jobs
|
||||
ADD CONSTRAINT jobs_status_check
|
||||
CHECK (status IN ('pending', 'published', 'failed'))
|
||||
""")
|
||||
@@ -0,0 +1,83 @@
|
||||
"""create initial schema
|
||||
|
||||
Revision ID: <оставь тот, что сгенерировал alembic>
|
||||
Revises:
|
||||
Create Date: <оставь>
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "447472b8acec"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("""
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
display_name TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
)
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
CREATE TABLE chats (
|
||||
id UUID PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
)
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
CREATE TABLE chat_members (
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
)
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
CREATE INDEX chat_members_user_idx ON chat_members (user_id)
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
CREATE TABLE messages (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
sender_id UUID NOT NULL REFERENCES users(id),
|
||||
text TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
)
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id)
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
CREATE TABLE sessions (
|
||||
token_hash TEXT PRIMARY KEY,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
)
|
||||
""")
|
||||
|
||||
op.execute("""
|
||||
CREATE INDEX sessions_user_idx ON sessions (user_id)
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("DROP TABLE IF EXISTS sessions")
|
||||
op.execute("DROP TABLE IF EXISTS messages")
|
||||
op.execute("DROP INDEX IF EXISTS chat_members_user_idx")
|
||||
op.execute("DROP TABLE IF EXISTS chat_members")
|
||||
op.execute("DROP TABLE IF EXISTS chats")
|
||||
op.execute("DROP TABLE IF EXISTS users")
|
||||
@@ -0,0 +1,31 @@
|
||||
"""add lease_until to jobs
|
||||
|
||||
Revision ID: <hash>
|
||||
Revises: d60dfc3e075a
|
||||
Create Date: <date>
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "6980f998def1"
|
||||
down_revision = "d60dfc3e075a"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("""
|
||||
ALTER TABLE jobs
|
||||
ADD COLUMN IF NOT EXISTS lease_until TIMESTAMPTZ
|
||||
""")
|
||||
op.execute("""
|
||||
CREATE INDEX IF NOT EXISTS jobs_processing_expired_idx
|
||||
ON jobs (lease_until)
|
||||
WHERE status = 'processing'
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("DROP INDEX IF EXISTS jobs_processing_expired_idx")
|
||||
op.execute("ALTER TABLE jobs DROP COLUMN IF EXISTS lease_until")
|
||||
@@ -0,0 +1,57 @@
|
||||
"""add attachments table
|
||||
|
||||
Revision ID: c3f18af4ec56
|
||||
Revises: 2803f3a0e579
|
||||
Create Date: <оставь>
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "c3f18af4ec56"
|
||||
down_revision = "2803f3a0e579"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("""
|
||||
CREATE TABLE attachments (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
owner_id UUID NOT NULL REFERENCES users(id),
|
||||
filename TEXT NOT NULL,
|
||||
content_type TEXT NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
|
||||
size_bytes INTEGER NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
|
||||
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
s3_key TEXT NOT NULL UNIQUE,
|
||||
status TEXT NOT NULL DEFAULT 'ready',
|
||||
error_code TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
updated_at TIMESTAMPTZ NOT NULL
|
||||
)
|
||||
""")
|
||||
op.execute("""
|
||||
CREATE INDEX attachments_chat_idx ON attachments (chat_id)
|
||||
""")
|
||||
op.execute("""
|
||||
CREATE INDEX attachments_owner_idx ON attachments (owner_id)
|
||||
""")
|
||||
op.execute("""
|
||||
CREATE TABLE message_attachments (
|
||||
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
position INTEGER NOT NULL,
|
||||
PRIMARY KEY (message_id, attachment_id)
|
||||
)
|
||||
""")
|
||||
op.execute("""
|
||||
CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id)
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("DROP TABLE IF EXISTS message_attachments")
|
||||
op.execute("DROP INDEX IF EXISTS attachments_owner_idx")
|
||||
op.execute("DROP INDEX IF EXISTS attachments_chat_idx")
|
||||
op.execute("DROP TABLE IF EXISTS attachments")
|
||||
@@ -0,0 +1,25 @@
|
||||
"""add variants column to attachments
|
||||
|
||||
Revision ID: <hash>
|
||||
Revises: 25c48b226c14
|
||||
Create Date: <date>
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "d60dfc3e075a"
|
||||
down_revision = "25c48b226c14"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("""
|
||||
ALTER TABLE attachments
|
||||
ADD COLUMN IF NOT EXISTS variants JSONB NOT NULL DEFAULT '[]'::jsonb
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("ALTER TABLE attachments DROP COLUMN IF EXISTS variants")
|
||||
Reference in New Issue
Block a user