Sha256: c9be3545435e12d9e0a78a4ce8d10a274e1c4d7467902e4d81148ae4357b933d

Contents?: true

Size: 1.67 KB

Versions: 25

Compression:

Stored size: 1.67 KB

Contents

# this struct uniquely defines a metric, optionally inside
# the call scope of another metric
class NewRelic::MetricSpec
  attr_accessor   :name
  attr_accessor   :scope

  MAX_LENGTH = 255
  # Need a "zero-arg" constructor so it can be instantiated from java (using
  # jruby) for sending responses to ruby agents from the java collector.
  #
  def initialize(metric_name = '', metric_scope = '')
    self.name = (metric_name || '') && metric_name[0...MAX_LENGTH]
    self.scope = metric_scope && metric_scope[0...MAX_LENGTH]
  end

  def truncate!
    self.name = name[0...MAX_LENGTH] if name && name.size > MAX_LENGTH
    self.scope = scope[0...MAX_LENGTH] if scope && scope.size > MAX_LENGTH
  end

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

  def eql? o
    self.class == o.class &&
    name.eql?(o.name) &&
    # coerce scope to a string and compare
     (scope || '') == (o.scope || '')
  end

  def hash
    h = name.hash
    h ^= scope.hash unless scope.nil?
    h
  end
  # return a new metric spec if the given regex
  # matches the name or scope.
  def sub(pattern, replacement, apply_to_scope = true)
    return nil if name !~ pattern &&
     (!apply_to_scope || scope.nil? || scope !~ pattern)
    new_name = name.sub(pattern, replacement)[0...MAX_LENGTH]

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

    self.class.new new_name, new_scope
  end

  def to_s
    "#{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

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
newrelic_rpm-3.0.1 lib/new_relic/metric_spec.rb
newrelic_rpm-3.0.0 lib/new_relic/metric_spec.rb
newrelic_rpm-3.0.0.beta2 lib/new_relic/metric_spec.rb
newrelic_rpm-3.0.0.beta1 lib/new_relic/metric_spec.rb
newrelic_rpm-2.14.1.logging1 lib/new_relic/metric_spec.rb
newrelic_rpm-2.14.1 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.4.rum6 lib/new_relic/metric_spec.rb
newrelic_rpm-2.14.0 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.4.rum5 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.6.beta2 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.6.beta1 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.4.rum4 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.5.beta4 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.5.beta3 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.5.beta2 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.5.beta1 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.4.eum3 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.4.eum2 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.4.eum1 lib/new_relic/metric_spec.rb
newrelic_rpm-2.13.4 lib/new_relic/metric_spec.rb