Sha256: 1409fad133af76625a47a386533c66248f66e84d1ab48db6d5379024b4b902ba

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

# encoding: utf-8

module OneApm
  class MetricSpec
    attr_reader   :name, :scope

    # the maximum length of a metric name or metric scope
    OA_MAX_LENGTH = 255
    OA_LENGTH_RANGE = (0...OA_MAX_LENGTH)
    OA_EMPTY_SCOPE = ''.freeze

    def initialize(metric_name='', metric_scope=nil)
      if metric_name.to_s.length > OA_MAX_LENGTH
        @name = metric_name.to_s[OA_LENGTH_RANGE]
      else
        @name = metric_name.to_s
      end

      if metric_scope
        if metric_scope.to_s.length > OA_MAX_LENGTH
          @scope = metric_scope.to_s[OA_LENGTH_RANGE]
        else
          @scope = metric_scope.to_s
        end
      else
        @scope = OA_EMPTY_SCOPE
      end
    end

    def ==(o)
      self.eql?(o)
    end

    def eql? o
      @name == o.name && @scope == o.scope
    end

    def hash
      @name.hash ^ @scope.hash
    end

    # return a new metric spec if the given regex
    # matches the name or scope.
    def sub(pattern, replacement, apply_to_scope = true)
      OneApm::Manager.logger.warn("The sub method on metric specs is deprecated") rescue nil
      return nil if name !~ pattern &&
       (!apply_to_scope || scope.nil? || scope !~ pattern)
      new_name = name.sub(pattern, replacement)[OA_LENGTH_RANGE]

      if apply_to_scope
        new_scope = (scope && scope.sub(pattern, replacement)[OA_LENGTH_RANGE])
      else
        new_scope = scope
      end

      self.class.new new_name, new_scope
    end

    def to_s
      return name if scope.empty?
      "#{name}:#{scope}"
    end

    def inspect
      "#<OneApm::MetricSpec '#{name}':'#{scope}'>"
    end

    def to_json(*a)
      {'name' => name,
      'scope' => scope}.to_json(*a)
    end

    def <=>(o)
      namecmp = self.name <=> o.name
      return namecmp if namecmp != 0
      return (self.scope || '') <=> (o.scope || '')
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/metrics/metric_spec.rb
oneapm_rpm-1.4.1 lib/one_apm/metrics/metric_spec.rb
oneapm_rpm-1.4.0 lib/one_apm/metrics/metric_spec.rb
oneapm_rpm-1.3.7 lib/one_apm/metrics/metric_spec.rb
oneapm_rpm-1.3.7.rc1 lib/one_apm/metrics/metric_spec.rb