Sha256: 323957eef9411aeb8d0faf065209fe3ca8ac78b4398ea4cd1cc655063f735a58

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Appsignal
  class Heartbeat
    class << self
      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.trace("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

  def self.heartbeat(name)
    heartbeat = Appsignal::Heartbeat.new(:name => name)
    output = nil

    if block_given?
      heartbeat.start
      output = yield
    end

    heartbeat.finish
    output
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
appsignal-3.7.2-java lib/appsignal/heartbeat.rb
appsignal-3.7.2 lib/appsignal/heartbeat.rb
appsignal-3.7.1-java lib/appsignal/heartbeat.rb
appsignal-3.7.1 lib/appsignal/heartbeat.rb
appsignal-3.7.0-java lib/appsignal/heartbeat.rb
appsignal-3.7.0 lib/appsignal/heartbeat.rb