Skip to content

Implement CloudWatchCollector as built-in metrics collector #119

@vlavrynovych

Description

@vlavrynovych

Overview

Implement CloudWatchCollector as a built-in metrics collector in MSR core, integrating with AWS CloudWatch for production monitoring.

Current State

CloudWatchCollector is currently documented as a custom collector example in the documentation (docs/customization/metrics/custom-collectors.md), but not implemented as a built-in collector.

Proposed Implementation

Add CloudWatchCollector to src/metrics/collectors/ using the AWS SDK:

import { IMetricsCollector } from '../../interface/IMetricsCollector';
import { CloudWatchClient, PutMetricDataCommand } from '@aws-sdk/client-cloudwatch';

export interface CloudWatchCollectorConfig {
  region: string;
  namespace?: string;  // default: 'MSR/Migrations'
  dimensions?: Array<{ Name: string; Value: string }>;
}

export class CloudWatchCollector implements IMetricsCollector {
  private client: CloudWatchClient;
  private namespace: string;
  private dimensions: Array<{ Name: string; Value: string }>;

  constructor(config: CloudWatchCollectorConfig) {
    this.client = new CloudWatchClient({ region: config.region });
    this.namespace = config.namespace || 'MSR/Migrations';
    this.dimensions = config.dimensions || [
      { Name: 'Environment', Value: process.env.NODE_ENV || 'production' }
    ];
  }

  async recordScriptComplete(script, duration) {
    await this.client.send(new PutMetricDataCommand({
      Namespace: this.namespace,
      MetricData: [
        {
          MetricName: 'MigrationDuration',
          Value: duration,
          Unit: 'Milliseconds',
          Dimensions: [...this.dimensions, { Name: 'ScriptName', Value: script.name }],
          Timestamp: new Date()
        },
        {
          MetricName: 'MigrationSuccess',
          Value: 1,
          Unit: 'Count',
          Dimensions: this.dimensions,
          Timestamp: new Date()
        }
      ]
    }));
  }

  async recordScriptError(script, error) {
    await this.client.send(new PutMetricDataCommand({
      Namespace: this.namespace,
      MetricData: [{
        MetricName: 'MigrationFailures',
        Value: 1,
        Unit: 'Count',
        Dimensions: [
          ...this.dimensions,
          { Name: 'ScriptName', Value: script.name },
          { Name: 'ErrorType', Value: error.constructor.name }
        ],
        Timestamp: new Date()
      }]
    }));
  }

  async recordRollback(strategy, success, duration) {
    // Implementation for rollback metrics
  }
}

Requirements

  • Implement CloudWatchCollector in src/metrics/collectors/CloudWatchCollector.ts
  • Add @aws-sdk/client-cloudwatch as peer dependency
  • Export from src/metrics/collectors/index.ts
  • Write comprehensive unit tests (mock AWS SDK client)
  • Update documentation to reflect built-in status
  • Add integration example in docs
  • Add to metrics overview documentation

Dependencies

  • @aws-sdk/client-cloudwatch (peer dependency) - AWS CloudWatch client

Related

Acceptance Criteria

  • CloudWatchCollector implemented with full IMetricsCollector interface
  • 100% test coverage maintained
  • Peer dependency added to package.json
  • Documentation updated
  • Example usage in docs
  • Works with CloudWatch dashboards and alarms

Metadata

Metadata

Assignees

Labels

⚡ Priority: HighImportant tasks requiring quick attention✨ Type: FeatureNew functionality or enhancement🟡 Test: MediumModerate testing effort required

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions