Sha256: c90eb220270f12b8ed863973e6696346aaa4f6db6c9864da574f5df36f81ef1b

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

module PassengerMetrics
  class Cloudwatch
    DEFAULT_NAMESPACE = "Passenger"
    DEFAULT_METRIC_NAME = "MetricName"
    DEFAULT_DIMENSION_NAME = "InstanceId"
    DEFAULT_DIMENSION_VALUE = "InstanceId"

    METRIC_UNITS = {
      nil => "None",
      :count => "Count"
    }

    attr_accessor :metric

    def initialize(metric = nil)
      self.metric = metric
    end

    def put
      client.put_metric_data(put_metric_data_payload)
    end

    private

    def put_metric_data_payload
      {:namespace => namespace, :metric_data => metric_data}
    end

    def metric_data
      [
        {
          :metric_name => metric_name,
          :dimensions => dimensions,
          :timestamp => timestamp,
          :value => value,
          :unit => unit
        }
      ]
    end

    def namespace
      ENV["AWS_CLOUDWATCH_NAMESPACE"] || DEFAULT_NAMESPACE
    end

    def metric_name
      ENV["AWS_CLOUDWATCH_METRIC_NAME"] || DEFAULT_METRIC_NAME
    end

    def dimension_name
      ENV["AWS_CLOUDWATCH_DIMENSION_NAME"] || DEFAULT_DIMENSION_NAME
    end

    def dimension_value
      ENV["AWS_CLOUDWATCH_DIMENSION_VALUE"] || DEFAULT_DIMENSION_VALUE
    end

    def dimensions
      (ENV["AWS_CLOUDWATCH_DIMENSIONS"] && JSON.parse(ENV["AWS_CLOUDWATCH_DIMENSIONS"])) || default_dimensions
    end

    def default_dimensions
      [{:name => dimension_name, :value => dimension_value}]
    end

    def timestamp
      Time.now
    end

    def value
      metric.value
    end

    def unit
      METRIC_UNITS[metric.unit]
    end

    def client
      @client ||= ::Aws::CloudWatch::Client.new region: 'us-east-1'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
passenger-metrics-0.0.1 lib/passenger-metrics/cloudwatch.rb
passenger-metrics-0.0.0 lib/passenger-metrics/cloudwatch.rb