Sha256: 30397247087b00fc9a7626fec00ce857fbaa12b45e7d3315b653d7ea1d72576a

Contents?: true

Size: 1.7 KB

Versions: 9

Compression:

Stored size: 1.7 KB

Contents

module Lamby
  class ColdStartMetrics
    NAMESPACE = 'Lamby'

    @cold_start = true
    @cold_start_time = (Time.now.to_f * 1000).to_i

    class << self

      def instrument!
        return unless @cold_start
        @cold_start = false
        now = (Time.now.to_f * 1000).to_i
        proactive_init = (now - @cold_start_time) > 10_000
        new(proactive_init).instrument!
      end

      def clear!
        @cold_start = true
        @cold_start_time = (Time.now.to_f * 1000).to_i
      end

    end

    def initialize(proactive_init)
      @proactive_init = proactive_init
      @metrics = []
      @properties = {}
    end

    def instrument!
      name = @proactive_init ? 'ProactiveInit' : 'ColdStart'
      put_metric name, 1, 'Count'
      puts JSON.dump(message)
    end

    private

    def dimensions
      [{ AppName: rails_app_name }]
    end

    def put_metric(name, value, unit = nil)
      @metrics << { 'Name': name }.tap do |m|
        m['Unit'] = unit if unit
      end
      set_property name, value
    end

    def set_property(name, value)
      @properties[name] = value
      self
    end

    def message
      {
        '_aws': {
          'Timestamp': timestamp,
          'CloudWatchMetrics': [
            {
              'Namespace': NAMESPACE,
              'Dimensions': [dimensions.map(&:keys).flatten],
              'Metrics': @metrics
            }
          ]
        }
      }.tap do |m|
        dimensions.each { |d| m.merge!(d) }
        m.merge!(@properties)
      end
    end

    def timestamp
      Time.current.strftime('%s%3N').to_i
    end

    def rails_app_name
      Lamby.config.metrics_app_name ||
        Rails.application.class.name.split('::').first
    end

  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
lamby-6.0.1 lib/lamby/cold_start_metrics.rb
lamby-6.0.0 lib/lamby/cold_start_metrics.rb
lamby-5.2.2 lib/lamby/cold_start_metrics.rb
lamby-5.2.1 lib/lamby/cold_start_metrics.rb
lamby_updated-5.2.4 lib/lamby/cold_start_metrics.rb
lamby_updated-5.2.2 lib/lamby/cold_start_metrics.rb
lamby_updated-5.2.1 lib/lamby/cold_start_metrics.rb
lamby-5.2.0 lib/lamby/cold_start_metrics.rb
lamby-5.1.0 lib/lamby/cold_start_metrics.rb