Sha256: 97bd193675517a89f76b6ca607c110e38328914ee1bdd6cbaa6c72522659e331

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

# A Hash-descended class for storing metric data in the NewRelic Agent.
#
# Keys are NewRelic::MetricSpec objects.
# Values are NewRelic::Agent::Stats objects.
#
# Missing keys will be automatically created as empty NewRelic::Agent::Stats
# instances, so use has_key? explicitly to check for key existence.
#
# This class makes no provisions for safe usage from multiple threads, such
# measures should be externally provided.
module NewRelic
  module Agent
    class StatsHash < ::Hash
      def initialize
        super { |hash, key| hash[key] = NewRelic::Agent::Stats.new }
      end

      def marshal_dump
        Hash[self]
      end

      def marshal_load(hash)
        self.merge!(hash)
      end

      def ==(other)
        Hash[self] == Hash[other]
      end

      def record(metric_specs, value=nil)
        Array(metric_specs).each do |metric_spec|
          stats = self[metric_spec]
          if block_given?
            yield stats
          else
            case value
            when Numeric
              stats.record_data_point(value)
            when NewRelic::Agent::Stats
              stats.merge!(value)
            end
          end
        end
      end

      def merge!(other)
        other.each do |key,val|
          if self.has_key?(key)
            self[key].merge!(val)
          else
            self[key] = val
          end
        end
        self
      end

      def resolve_scopes(resolved_scope)
        new_stats = self.class.new
        self.each do |spec, stats|
          if spec.scope != '' &&
              spec.scope.to_sym == StatsEngine::SCOPE_PLACEHOLDER
            spec.scope = resolved_scope
          end
          new_stats[spec] = stats
        end
        return new_stats
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
newrelic_rpm-3.6.4.122 lib/new_relic/agent/stats_engine/stats_hash.rb
newrelic_rpm-3.6.4.113.beta lib/new_relic/agent/stats_engine/stats_hash.rb