Sha256: 63d86f65ab2c520585e3ee95a9f54fe94de3e3852e82e8ab924ed0e06d5a1a59

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

module PgHero
  module Methods
    module System
      def cpu_usage
        rds_stats("CPUUtilization")
      end

      def connection_stats
        rds_stats("DatabaseConnections")
      end

      def replication_lag_stats
        rds_stats("ReplicaLag")
      end

      def read_iops_stats
        rds_stats("ReadIOPS")
      end

      def write_iops_stats
        rds_stats("WriteIOPS")
      end

      def rds_stats(metric_name)
        if system_stats_enabled?
          client =
            if defined?(Aws)
              Aws::CloudWatch::Client.new(access_key_id: access_key_id, secret_access_key: secret_access_key, region: region)
            else
              AWS::CloudWatch.new(access_key_id: access_key_id, secret_access_key: secret_access_key, region: region).client
            end

          now = Time.now
          resp = client.get_metric_statistics(
            namespace: "AWS/RDS",
            metric_name: metric_name,
            dimensions: [{name: "DBInstanceIdentifier", value: db_instance_identifier}],
            start_time: (now - 1 * 3600).iso8601,
            end_time: now.iso8601,
            period: 60,
            statistics: ["Average"]
          )
          data = {}
          resp[:datapoints].sort_by { |d| d[:timestamp] }.each do |d|
            data[d[:timestamp]] = d[:average]
          end
          data
        else
          {}
        end
      end

      def system_stats_enabled?
        !!((defined?(Aws) || defined?(AWS)) && access_key_id && secret_access_key && db_instance_identifier)
      end

      def access_key_id
        ENV["PGHERO_ACCESS_KEY_ID"] || ENV["AWS_ACCESS_KEY_ID"]
      end

      def secret_access_key
        ENV["PGHERO_SECRET_ACCESS_KEY"] || ENV["AWS_SECRET_ACCESS_KEY"]
      end

      def region
        ENV["PGHERO_REGION"] || ENV["AWS_REGION"] || (defined?(Aws) && Aws.config[:region]) || "us-east-1"
      end

      def db_instance_identifier
        databases[current_database].db_instance_identifier
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pghero-1.4.1 lib/pghero/methods/system.rb
pghero-1.4.0 lib/pghero/methods/system.rb
pghero-1.3.2 lib/pghero/methods/system.rb
pghero-1.3.1 lib/pghero/methods/system.rb
pghero-1.3.0 lib/pghero/methods/system.rb