Sha256: aedc47232958444eb6d7d87eb0debb4e5b7b6d0e90d83806321e824c2bed8be1

Contents?: true

Size: 1.31 KB

Versions: 32

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Appsignal
  class Heartbeat
    class << self
      # @api private
      def transmitter
        @transmitter ||= Appsignal::Transmitter.new(
          "#{Appsignal.config[:logging_endpoint]}/heartbeats/json"
        )
      end
    end

    attr_reader :name, :id

    def initialize(name:)
      @name = name
      @id = SecureRandom.hex(8)
    end

    def start
      transmit_event("start")
    end

    def finish
      transmit_event("finish")
    end

    private

    def event(kind)
      {
        :name => name,
        :id => @id,
        :kind => kind,
        :timestamp => Time.now.utc.to_i
      }
    end

    def transmit_event(kind)
      unless Appsignal.active?
        Appsignal.internal_logger.debug("AppSignal not active, not transmitting heartbeat event")
        return
      end

      response = self.class.transmitter.transmit(event(kind))

      if response.code.to_i >= 200 && response.code.to_i < 300
        Appsignal.internal_logger.debug("Transmitted heartbeat `#{name}` (#{id}) #{kind} event")
      else
        Appsignal.internal_logger.error(
          "Failed to transmit heartbeat event: status code was #{response.code}"
        )
      end
    rescue => e
      Appsignal.internal_logger.error("Failed to transmit heartbeat event: #{e}")
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
appsignal-3.12.6-java lib/appsignal/heartbeat.rb
appsignal-3.12.6 lib/appsignal/heartbeat.rb
appsignal-3.12.5-java lib/appsignal/heartbeat.rb
appsignal-3.12.5 lib/appsignal/heartbeat.rb
appsignal-3.12.4-java lib/appsignal/heartbeat.rb
appsignal-3.12.4 lib/appsignal/heartbeat.rb
appsignal-3.12.3-java lib/appsignal/heartbeat.rb
appsignal-3.12.3 lib/appsignal/heartbeat.rb
appsignal-3.12.2-java lib/appsignal/heartbeat.rb
appsignal-3.12.2 lib/appsignal/heartbeat.rb
appsignal-3.12.1-java lib/appsignal/heartbeat.rb
appsignal-3.12.1 lib/appsignal/heartbeat.rb
appsignal-3.12.0-java lib/appsignal/heartbeat.rb
appsignal-3.12.0 lib/appsignal/heartbeat.rb
appsignal-3.11.0-java lib/appsignal/heartbeat.rb
appsignal-3.11.0 lib/appsignal/heartbeat.rb
appsignal-3.10.0-java lib/appsignal/heartbeat.rb
appsignal-3.10.0 lib/appsignal/heartbeat.rb
appsignal-3.9.3-java lib/appsignal/heartbeat.rb
appsignal-3.9.3 lib/appsignal/heartbeat.rb