Sha256: 36eb22aae1a24a2a9dc51a53b255c9ebcf9a5426d56472cbc52cbc96bef8425c

Contents?: true

Size: 1.87 KB

Versions: 38

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Appsignal
  module Probes
    module Helpers
      private

      def gauge_delta_cache
        @gauge_delta_cache ||= {}
      end

      # Calculate the delta of two values for a gauge metric.
      #
      # When this method is called, the given value is stored in a cache
      # under the given cache key.
      #
      # A block must be passed to this method. The first time the method
      # is called for a given cache key, the block will not be yielded to.
      # In subsequent calls, the delta between the previously stored value
      # in the cache for that key and the value given in this invocation
      # will be yielded to the block.
      #
      # This is used for absolute counter values which we want to track as
      # gauges.
      #
      # @example
      #   gauge_delta :with_block, 10 do |delta|
      #     puts "this block will not be yielded to"
      #   end
      #   gauge_delta :with_block, 15 do |delta|
      #     # `delta` has a value of `5`
      #     puts "this block will be yielded to with delta = #{delta}"
      #   end
      #
      def gauge_delta(cache_key, value)
        previous_value = gauge_delta_cache[cache_key]
        gauge_delta_cache[cache_key] = value
        return unless previous_value

        yield value - previous_value
      end

      def hostname
        return @hostname if defined?(@hostname)

        config = @appsignal.config
        # Auto detect hostname as fallback. May be inaccurate.
        @hostname =
          config[:hostname] || Socket.gethostname
        Appsignal.internal_logger.debug "Probe helper: Using hostname config " \
          "option '#{@hostname.inspect}' as hostname"

        @hostname
      end

      def set_gauge_with_hostname(metric, value, tags = {})
        @appsignal.set_gauge(metric, value, { :hostname => hostname }.merge(tags))
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
appsignal-3.10.0-java lib/appsignal/probes/helpers.rb
appsignal-3.10.0 lib/appsignal/probes/helpers.rb
appsignal-3.9.3-java lib/appsignal/probes/helpers.rb
appsignal-3.9.3 lib/appsignal/probes/helpers.rb
appsignal-3.9.2-java lib/appsignal/probes/helpers.rb
appsignal-3.9.2 lib/appsignal/probes/helpers.rb
appsignal-3.9.1-java lib/appsignal/probes/helpers.rb
appsignal-3.9.1 lib/appsignal/probes/helpers.rb
appsignal-3.9.0-java lib/appsignal/probes/helpers.rb
appsignal-3.9.0 lib/appsignal/probes/helpers.rb
appsignal-3.8.1-java lib/appsignal/probes/helpers.rb
appsignal-3.8.1 lib/appsignal/probes/helpers.rb
appsignal-3.8.0-java lib/appsignal/probes/helpers.rb
appsignal-3.8.0 lib/appsignal/probes/helpers.rb
appsignal-3.7.6-java lib/appsignal/probes/helpers.rb
appsignal-3.7.6 lib/appsignal/probes/helpers.rb
appsignal-3.7.2-java lib/appsignal/probes/helpers.rb
appsignal-3.7.2 lib/appsignal/probes/helpers.rb
appsignal-3.7.1-java lib/appsignal/probes/helpers.rb
appsignal-3.7.1 lib/appsignal/probes/helpers.rb