BigW Consortium Gitlab

influx_sampler.rb 2.62 KB
Newer Older
1 2 3 4 5 6 7
module Gitlab
  module Metrics
    # Class that sends certain metrics to InfluxDB at a specific interval.
    #
    # This class is used to gather statistics that can't be directly associated
    # with a transaction such as system memory usage, garbage collection
    # statistics, etc.
8
    class InfluxSampler < BaseSampler
9
      # interval - The sampling interval in seconds.
10
      def initialize(interval = Metrics.settings[:sample_interval])
11 12
        super(interval)
        @last_step = nil
13 14

        @metrics = []
15 16 17

        @last_minor_gc = Delta.new(GC.stat[:minor_gc_count])
        @last_major_gc = Delta.new(GC.stat[:major_gc_count])
18 19 20 21 22 23

        if Gitlab::Metrics.mri?
          require 'allocations'

          Allocations.start
        end
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
      end

      def sample
        sample_memory_usage
        sample_file_descriptors
        sample_objects
        sample_gc

        flush
      ensure
        GC::Profiler.clear
        @metrics.clear
      end

      def flush
39
        Metrics.submit_metrics(@metrics.map(&:to_hash))
40 41 42
      end

      def sample_memory_usage
43
        add_metric('memory_usage', value: System.memory_usage)
44 45 46
      end

      def sample_file_descriptors
47
        add_metric('file_descriptors', value: System.file_descriptor_count)
48 49
      end

50 51 52 53
      if Metrics.mri?
        def sample_objects
          sample = Allocations.to_hash
          counts = sample.each_with_object({}) do |(klass, count), hash|
54 55 56 57 58
            name = klass.name

            next unless name

            hash[name] = count
59 60 61 62 63 64
          end

          # Symbols aren't allocated so we'll need to add those manually.
          counts['Symbol'] = Symbol.all_symbols.length

          counts.each do |name, count|
65
            add_metric('object_counts', { count: count }, type: name)
66 67 68 69
          end
        end
      else
        def sample_objects
70
        end
71 72 73
      end

      def sample_gc
74
        time = GC::Profiler.total_time * 1000.0
75 76 77 78 79 80 81 82 83 84 85 86
        stats = GC.stat.merge(total_time: time)

        # We want the difference of GC runs compared to the last sample, not the
        # total amount since the process started.
        stats[:minor_gc_count] =
          @last_minor_gc.compared_with(stats[:minor_gc_count])

        stats[:major_gc_count] =
          @last_major_gc.compared_with(stats[:major_gc_count])

        stats[:count] = stats[:minor_gc_count] + stats[:major_gc_count]

87 88 89 90 91 92 93 94 95 96 97
        add_metric('gc_statistics', stats)
      end

      def add_metric(series, values, tags = {})
        prefix = sidekiq? ? 'sidekiq_' : 'rails_'

        @metrics << Metric.new("#{prefix}#{series}", values, tags)
      end

      def sidekiq?
        Sidekiq.server?
98 99 100 101
      end
    end
  end
end