File: client.rb

package info (click to toggle)
ruby-prometheus-client-mmap 1.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 700 kB
  • sloc: ruby: 3,149; sh: 54; makefile: 21
file content (58 lines) | stat: -rw-r--r-- 1,749 bytes parent folder | download | duplicates (2)
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
require 'prometheus/client/registry'
require 'prometheus/client/configuration'
require 'prometheus/client/mmaped_value'

module Prometheus
  # Client is a ruby implementation for a Prometheus compatible client.
  module Client
    class << self
      attr_writer :configuration

      def configuration
        @configuration ||= Configuration.new
      end

      def configure
        yield(configuration)
      end

      # Returns a default registry object
      def registry
        @registry ||= Registry.new
      end

      def logger
        configuration.logger
      end

      def pid
        configuration.pid_provider.call
      end

      # Resets the registry and reinitializes all metrics files.
      # Use case: clean up everything in specs `before` block,
      # to prevent leaking the state between specs which are updating metrics.
      def reset!
        @registry = nil
        ::Prometheus::Client::MmapedValue.reset_and_reinitialize
      end

      def cleanup!
        Dir.glob("#{configuration.multiprocess_files_dir}/*.db").each { |f| File.unlink(f) if File.exist?(f) }
      end

      # With `force: false`: reinitializes metric files only for processes with the changed PID.
      # With `force: true`: reinitializes all metrics files.
      # Always keeps the registry.
      # Use case  (`force: false`): pick up new metric files on each worker start,
      # without resetting already registered files for the master or previously initialized workers.
      def reinitialize_on_pid_change(force: false)
        if force
          ::Prometheus::Client::MmapedValue.reset_and_reinitialize
        else
          ::Prometheus::Client::MmapedValue.reinitialize_on_pid_change
        end
      end
    end
  end
end