Sha256: 6fdf6c5351a1573bb941db4cf7632516399e1d3839890449dbad33b3fd94aafe

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module Nexaas
  module Auditor
    module StatisticsTrackers
      class Base

        def track_count(metric:, value: nil)
          value ||= 1
          track(:count, metric, value)
        end

        def track_value(metric:, value:)
          track(:value, metric, value)
        end

        private

        def track(type, name, value)
          validate_value!(value, type)
          full_name = full_metric_name(name)
          validate_name!(name, full_name)

          send_track(type, full_name, value)
        end

        def send_track(type, full_name, value)
          raise "Not Implemented, override in subclass."
        end

        def full_metric_name(name)
          if @namespace.to_s == ''
            name
          else
            "#{@namespace}.#{name}"
          end
        end

        # allowed chars: a-z, A-Z, `.`, `-` and `_`
        def validate_name!(name, full_name)
          if (name.to_s == '') || !(full_name =~ /\A[a-zA-Z0-9\.\-_]+\Z/)
            raise ArgumentError, "unsuported metric name: '#{name}'"
          end
        end

        # allowed values: Numeric (Integer, Float, Decimal, etc)
        def validate_value!(value, type)
          raise ArgumentError, "unsuported value: #{value} (#{value.class})" unless value.is_a?(Numeric)
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nexaas-auditor-1.0.3 lib/nexaas/auditor/statistics_trackers/base.rb
nexaas-auditor-1.0.2 lib/nexaas/auditor/statistics_trackers/base.rb
nexaas-auditor-1.0.1 lib/nexaas/auditor/statistics_trackers/base.rb