Sha256: 451188f1d33681c69d824e68f94ac1892490261dc827b20c17e032be2d0f5244

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require 'set'
module NewRelic
  module Binding
    class Metric
      attr_reader :component, :name, :value, :count, :min, :max, :sum_of_squares

      def initialize(component, name, input_value, options = {} )
        value = input_value.to_f
        @component = component
        @name = name
        @value = value
        if options_has_required_keys(options)
          @count = options[:count].to_i
          @min = options[:min].to_f
          @max = options[:max].to_f
          @sum_of_squares = options[:sum_of_squares].to_f
        else
          PlatformLogger.warn("Metric #{@name} count, min, max, and sum_of_squares are all required if one is set, falling back to value only") unless options.size == 0
          @count = 1
          @min = value
          @max = value
          @sum_of_squares = (value * value)
        end
      end

      def aggregate(metric)
        @value += metric.value
        @count += metric.count
        @min = [@min, metric.min].min
        @max = [@max, metric.max].max
        @sum_of_squares += metric.sum_of_squares
      end

      def to_hash
        {
          name => [
            @value, @count, @min, @max, @sum_of_squares
          ]
        }
      end

      private

      def options_has_required_keys(options)
        options.keys.to_set.superset?(Set.new([:count, :min, :max, :sum_of_squares]))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
newrelic_plugin-1.3.1 lib/newrelic_platform_binding/metric.rb
newrelic_plugin-1.3.0 lib/newrelic_platform_binding/metric.rb