Sha256: 1ba429e79f9d7780a485413780254f5b485f2efb3947153824ec437480d5b472

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require 'set'

module HeimdallApm
  # Convert metrics hash from requests into an collection of points we want to
  # track, but without aggregations and percentiles/std_dev calculations of same
  # endpoints across multiples requests. These operations are deferred to
  # InfluxDB, in favor of more granular data.
  # This may change in the future if it proves non scalable.
  #
  class PointsCollection
    # Metrics we want to explicitely keep separated into measurements. Everything
    # else will be label as Ruby.
    ROOT_METRICS = ['Sql', 'Elastic', 'Redis'].map do |key|
      downcased = key.downcase
      [key, ["#{downcased}_time", "#{downcased}_count"]]
    end.to_h

    def initialize
      @points = []
    end

    def empty?
      @points.empty?
    end

    def to_a
      @points
    end

    def append(txn, metrics)
      timestamp   = txn.root_segment.stop_time
      series_name = txn.custom_series_name || (txn.web? ? 'app' : 'job')
      values      = Hash.new { |h, k| h[k] = 0 }

      tags = txn.tags || {}
      tags[:endpoint] = txn.scope

      metrics.each do |meta, stat|
        if ROOT_METRICS.key?(meta.type)
          time_key, count_key = ROOT_METRICS[meta.type]

          values[time_key]  += stat.total_exclusive_time
          values[count_key] += stat.call_count
        else
          values['ruby_time'] += stat.total_exclusive_time
        end

        values['total_time'] += stat.total_exclusive_time
      end

      # Segment time are in seconds, store them in milliseconds
      values.transform_values! { |v| v * 1000 }

      @points << {
        series: series_name,
        timestamp: (timestamp * 1000).to_i,
        tags: tags,
        values: values
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
heimdall_apm-0.1.1 lib/heimdall_apm/points_collection.rb