Sha256: 1c1545d5339edbb617cc68cd3d00a381593933a64a6e3fc94c0a169f40e8f756

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

require 'thread'
require 'heimdall_apm/points_collection'

module HeimdallApm
  # Keeps in RAM one or more minute's worth of metrics.
  # When informed to by the background thread, it pushes the in-RAM metrics off
  # to InfluxDB.
  class Vault
    def initialize(context)
      @context  = context
      @lock     = Mutex.new
      @spans    = Hash.new { |h, k| h[k] = Span.new(k, @context) }
    end

    def current_span
      @spans[current_timestamp]
    end

    def retrieve_and_delete_previous_span
      timestamp = current_timestamp - 60
      @lock.synchronize { @spans.delete(timestamp) }
    end

    def store_transaction_metrics(txn, metrics)
      @lock.synchronize { current_span.add_point(txn, metrics) }
    end

    def current_timestamp
      time = Time.now.utc
      time.to_i - time.sec
    end
  end

  # One span of storage
  class Span
    attr_reader :points_collection

    def initialize(timestamp, context)
      @timestamp = timestamp
      @context   = context

      @points_collection = ::HeimdallApm::PointsCollection.new
    end

    def add_point(txn, metrics)
      @points_collection.append(txn, metrics)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
heimdall_apm-0.1.3 lib/heimdall_apm/vault.rb
heimdall_apm-0.1.2 lib/heimdall_apm/vault.rb
heimdall_apm-0.1.1 lib/heimdall_apm/vault.rb
heimdall_apm-0.1.0 lib/heimdall_apm/vault.rb