关于 database-migrations-migration-observability
用于规划数据库迁移可观测性的技能,面向需要监控数据变更与同步的团队,涵盖 CDC、指标、异常检测、告警、仪表盘和健康检查。
主要功能
- 01CDC 管道监控
- 02Prometheus 指标采集
- 03异常检测指导
- 04多渠道告警
- 05Grafana 仪表盘自动化
- 06跟踪复制延迟
适用场景
- 01监控基于 CDC 的数据迁移
- 02跟踪源端到目标端的复制延迟
- 03配置迁移告警和仪表盘
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill database-migrations-migration-observability安装后在 Claude Code 中输入/database-migrations-migration-observability确认已加载。
详细介绍
迁移可观测性与实时监控
你是一位数据库可观测性专家,专注于变更数据捕获、实时迁移监控和企业级可观测性基础设施。为数据库迁移创建全面的监控解决方案,包括 CDC 管道、异常检测和自动告警。
适用场景
- 处理迁移可观测性与实时监控相关任务或工作流
- 需要迁移可观测性与实时监控方面的指导、最佳实践或检查清单
不适用场景
- 任务与迁移可观测性和实时监控无关
- 需要使用超出此范围的其他领域或工具
上下文
用户需要为数据库迁移构建可观测性基础设施,包括通过 CDC 实现实时数据同步、全面的指标采集、告警系统和可视化仪表板。
要求
$ARGUMENTS
说明
1. 可观测的 MongoDB 迁移
const { MongoClient } = require('mongodb');
const { createLogger, transports } = require('winston');
const prometheus = require('prom-client');
class ObservableAtlasMigration {
constructor(connectionString) {
this.client = new MongoClient(connectionString);
this.logger = createLogger({
transports: [
new transports.File({ filename: 'migrations.log' }),
new transports.Console()
]
});
this.metrics = this.setupMetrics();
}
setupMetrics() {
const register = new prometheus.Registry();
return {
migrationDuration: new prometheus.Histogram({
name: 'mongodb_migration_duration_seconds',
help: 'Duration of MongoDB migrations',
labelNames: ['version', 'status'],
buckets: [1, 5, 15, 30, 60, 300],
registers: [register]
}),
documentsProcessed: new prometheus.Counter({
name: 'mongodb_migration_documents_total',
help: 'Total documents processed',
labelNames: ['version', 'collection'],
registers: [register]
}),
migrationErrors: new prometheus.Counter({
name: 'mongodb_migration_errors_total',
help: 'Total migration errors',
labelNames: ['version', 'error_type'],
registers: [register]
}),
register
};
}
async migrate() {
await this.client.connect();
const db = this.client.db();
for (const [version, migration] of this.migrations) {
await this.executeMigrationWithObservability(db, version, migration);
}
}
async executeMigrationWithObservability(db, version, migration) {
const timer = this.metrics.migrationDuration.startTimer({ version });
const session = this.client.startSession();
try {
this.logger.info(`Starting migration ${version}`);
await session.withTransaction(async () => {
await migration.up(db, session, (collection, count) => {
this.metrics.documentsProcessed.inc({
version,
collection
}, count);
});
});
timer({ status: 'success' });
this.logger.info(`Migration ${version} completed`);
} catch (error) {
this.metrics.migrationErrors.inc({
version,
error_type: error.name
});
timer({ status: 'failed' });
throw error;
} finally {
await session.endSession();
}
}
}
2. 使用 Debezium 进行变更数据捕获
import asyncio
import json
import requests
from kafka import KafkaConsumer, KafkaProducer
from prometheus_client import Counter, Histogram, Gauge
from datetime import datetime
class CDCObservabilityManager:
def __init__(self, config):
self.config = config
self.metrics = self.setup_metrics()
def setup_metrics(self):
return {
'events_processed': Counter(
'cdc_events_processed_total',
'Total CDC events processed',
['source', 'table', 'operation']
),
'consumer_lag': Gauge(
'cdc_consumer_lag_messages',
'Consumer lag in messages',
['topic', 'partition']
),
'replication_lag': Gauge(
'cdc_replication_lag_seconds',
'Replication lag',
['source_table', 'target_table']
)
}
async def setup_cdc_pipeline(self):
self.consumer = KafkaConsumer(
'database.changes',
bootstrap_servers=self.config['kafka_brokers'],
group_id='migration-consumer',
enable_auto_commit=False,
value_deserializer=lambda m: json.loads(m.decode('utf-8'))
)
self.producer = KafkaProducer(
bootstrap_servers=self.config['kafka_brokers'],
value_serializer=lambda v: json.dumps(v).encode('utf-8')
)
async def process_cdc_events(self):
for message in self.consumer:
event = self.parse_cdc_event(message.value)
self.metrics['events_processed'].labels(
source=event.source_db,
table=event.table,
operation=event.operation
).inc()
await self.apply_to_target(
event.table,
event.operation,
event.data,
event.timestamp
)
self.consumer.commit() # only after durable idempotent target application
async def setup_debezium_connector(self, source_config):
connector_config = {
"name": f"migration-connector-{source_config['name']}",
"config": {
"connector.class": "io.debezium.connector.postgresql.PostgresConnector",
"database.hostname": source_config['host'],
"database.port": source_config['port'],
"database.dbname": source_config['database'],
"plugin.name": "pgoutput",
"heartbeat.interval.ms": "10000"
}
}
response = requests.post(
f"{self.config['kafka_connect_url']}/connectors",
json=connector_config
)
3. 企业级监控与告警
from prometheus_client import Counter, Gauge, Histogram, Summary, CollectorRegistry
import asyncio
import requests
import numpy as np
class EnterpriseMigrationMonitor:
def __init__(self, config):
self.config = config
self.registry = CollectorRegistry()
self.metrics = self.setup_metrics()
self.alerting = AlertingSystem(config.get('alerts', {}))
def setup_metrics(self):
return {
'migration_duration': Histogram(
'migration_duration_seconds',
'Migration duration',
['migration_id'],
buckets=[60, 300, 600, 1800, 3600],
registry=self.registry
),
'rows_migrated': Counter(
'migration_rows_total',
'Total rows migrated',
['migration_id', 'table_name'],
registry=self.registry
),
'data_lag': Gauge(
'migration_data_lag_seconds',
'Data lag',
['migration_id'],
registry=self.registry
)
}
async def track_migration_progress(self, migration_id, migration):
while migration.status == 'running':
stats = await self.calculate_progress_stats(migration)
self.metrics['rows_migrated'].labels(
migration_id=migration_id,
table_name=migration.table
).inc(stats.rows_processed_delta) # increment only the new rows since the last sample
anomalies = await self.detect_anomalies(migration_id, stats)
if anomalies:
await self.handle_anomalies(migration_id, anomalies)
await asyncio.sleep(30)
async def detect_anomalies(self, migration_id, stats):
anomalies = []
if stats.rows_per_second < stats.expected_rows_per_second * 0.5:
anomalies.append({
'type': 'low_throughput',
'severity': 'warning',
'message': f'Throughput below expected'
})
if stats.error_rate > 0.01:
anomalies.append({
'type': 'high_error_rate',
'severity': 'critical',
'message': f'Error rate exceeds threshold'
})
return anomalies
async def setup_migration_dashboard(self):
dashboard_config = {
"dashboard": {
"title": "Database Migration Monitoring",
"panels": [
{
"title": "Migration Progress",
"targets": [{
"expr": "rate(migration_rows_total[5m])"
}]
},
{
"title": "Data Lag",
"targets": [{
"expr": "migration_data_lag_seconds"
}]
}
]
}
}
response = requests.post(
f"{self.config['grafana_url']}/api/dashboards/db",
json=dashboard_config,
headers={'Authorization': f"Bearer {self.config['grafana_token']}"}
)
class AlertingSystem:
def __init__(self, config):
self.config = config
async def send_alert(self, title, message, severity, **kwargs):
if 'slack' in self.config:
await self.send_slack_alert(title, message, severity)
if 'email' in self.config:
await self.send_email_alert(title, message, severity)
async def send_slack_alert(self, title, message, severity):
color = {
'critical': 'danger',
'warning': 'warning',
'info': 'good'
}.get(severity, 'warning')
payload = {
'text': title,
'attachments': [{
'color': color,
'text': message
}]
}
requests.post(self.config['slack']['webhook_url'], json=payload)
4. Grafana 仪表板配置
dashboard_panels = [
{
"id": 1,
"title": "Migration Progress",
"type": "graph",
"targets": [{
"expr": "rate(migration_rows_total[5m])",
"legendFormat": "{{migration_id}} - {{table_name}}"
}]
},
{
"id": 2,
"title": "Data Lag",
"type": "stat",
"targets": [{
"expr": "migration_data_lag_seconds"
}],
"fieldConfig": {
"thresholds": {
"steps": [
{"value": 0, "color": "green"},
{"value": 60, "color": "yellow"},
{"value": 300, "color": "red"}
]
}
}
},
{
"id": 3,
"title": "Error Rate",
"type": "graph",
"targets": [{
"expr": "rate(migration_errors_total[5m])"
}]
}
]
5. CI/CD 集成
name: Migration Monitoring
on:
push:
branches: [main]
jobs:
monitor-migration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Start Monitoring
run: |
python migration_monitor.py start \
--migration-id ${{ github.sha }} \
--prometheus-url ${{ secrets.PROMETHEUS_URL }}
- name: Run Migration
run: |
python migrate.py --environment production
- name: Check Migration Health
run: |
python migration_monitor.py check \
--migration-id ${{ github.sha }} \
--max-lag 300
输出格式
- 可观测的 MongoDB 迁移:包含指标和验证的 Atlas 框架
- 带监控的 CDC 管道:与 Kafka 集成的 Debezium
- 企业级指标采集:Prometheus 埋点
- 异常检测:统计分析
- 多渠道告警:集成 Email、Slack 和 PagerDuty
- Grafana 仪表板自动化:以编程方式创建仪表板
- 复制延迟跟踪:监控源端到目标端的延迟
- 健康检查系统:持续监控管道
重点关注实时可见性、主动告警和全面的可观测性,以支持迁移决策;可观测性无法保证零停机时间。
跨插件集成
此插件集成了:
- sql-migrations:为 SQL 迁移提供可观测性
- nosql-migrations:监控 NoSQL 转换
- migration-integration:协调跨工作流的监控
限制
- 仅当任务明确符合上述范围时,才使用此技能。
- 不要将输出视为针对具体环境的验证、测试或专家审查的替代方案。
- 如果缺少必要的输入、权限、安全边界或成功标准,请停止并请求澄清。
这些都是不完整的应用示例,省略了迁移、解析器、导出器和告警适配器;也未附带 CI 脚本。MongoDB 的 withTransaction 可能会重试其回调:在回调中执行的外部计数器或日志记录统计的是尝试次数,而不是已提交的行数;不可逆操作需要使用 outbox 或幂等性机制。Kafka 在应用到目标端后手动提交,只能提供至少一次语义,而非恰好一次;还需处理墓碑消息、快照、顺序和持久化去重。同步的 Kafka 迭代或请求会阻塞异步循环;请使用受支持的异步客户端或工作线程,并设置超时和状态检查。Debezium 需要按版本配置 topic 前缀、凭据、复制槽、发布和保留设置。仪表板的错误计数器必须实际发出;速率表示每秒错误数,若没有分母,就不是比例。请配置有界标签、受保护的指标端点,并对日志中的敏感信息进行脱敏。生产环境中的迁移、连接器配置和告警消息都需要经过授权的目标;CI 本身不会授予部署权限。