Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unicorn_sampler.rb « samplers « metrics « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2fa324f3feab5e0933091ce760431943846df97a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

module Gitlab
  module Metrics
    module Samplers
      class UnicornSampler < BaseSampler
        DEFAULT_SAMPLING_INTERVAL_SECONDS = 5

        def metrics
          @metrics ||= init_metrics
        end

        def init_metrics
          {
            unicorn_active_connections: ::Gitlab::Metrics.gauge(:unicorn_active_connections, 'Unicorn active connections', {}, :max),
            unicorn_queued_connections: ::Gitlab::Metrics.gauge(:unicorn_queued_connections, 'Unicorn queued connections', {}, :max),
            unicorn_workers:            ::Gitlab::Metrics.gauge(:unicorn_workers, 'Unicorn workers')
          }
        end

        def enabled?
          # Raindrops::Linux.tcp_listener_stats is only present on Linux
          unicorn_with_listeners? && Raindrops::Linux.respond_to?(:tcp_listener_stats)
        end

        def sample
          Raindrops::Linux.tcp_listener_stats(tcp_listeners).each do |addr, stats|
            set_unicorn_connection_metrics('tcp', addr, stats)
          end
          Raindrops::Linux.unix_listener_stats(unix_listeners).each do |addr, stats|
            set_unicorn_connection_metrics('unix', addr, stats)
          end

          metrics[:unicorn_workers].set({}, unicorn_workers_count)
        end

        private

        def tcp_listeners
          @tcp_listeners ||= Unicorn.listener_names.grep(%r{\A[^/]+:\d+\z})
        end

        def set_unicorn_connection_metrics(type, addr, stats)
          labels = { socket_type: type, socket_address: addr }

          metrics[:unicorn_active_connections].set(labels, stats.active)
          metrics[:unicorn_queued_connections].set(labels, stats.queued)
        end

        def unix_listeners
          @unix_listeners ||= Unicorn.listener_names - tcp_listeners
        end

        def unicorn_with_listeners?
          defined?(Unicorn) && Unicorn.listener_names.any?
        end

        def unicorn_workers_count
          http_servers.sum(&:worker_processes)
        end

        # Traversal of ObjectSpace is expensive, on fully loaded application
        # it takes around 80ms. The instances of HttpServers are not a subject
        # to change so we can cache the list of servers.
        def http_servers
          return [] unless Gitlab::Runtime.unicorn?

          @http_servers ||= ObjectSpace.each_object(::Unicorn::HttpServer).to_a
        end
      end
    end
  end
end