Class: Fluent::Plugin::PrometheusInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_prometheus.rb

Defined Under Namespace

Classes: MonitorServlet, MonitorServletAll

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrometheusInput

Returns a new instance of PrometheusInput.



41
42
43
44
# File 'lib/fluent/plugin/in_prometheus.rb', line 41

def initialize
  super
  @registry = ::Prometheus::Client.registry
end

Instance Attribute Details

#base_portObject (readonly)

Returns the value of attribute base_port.



38
39
40
# File 'lib/fluent/plugin/in_prometheus.rb', line 38

def base_port
  @base_port
end

#metrics_pathObject (readonly)

Returns the value of attribute metrics_path.



39
40
41
# File 'lib/fluent/plugin/in_prometheus.rb', line 39

def metrics_path
  @metrics_path
end

#num_workersObject (readonly)

Returns the value of attribute num_workers.



37
38
39
# File 'lib/fluent/plugin/in_prometheus.rb', line 37

def num_workers
  @num_workers
end

#registryObject (readonly)

Returns the value of attribute registry.



35
36
37
# File 'lib/fluent/plugin/in_prometheus.rb', line 35

def registry
  @registry
end

Instance Method Details

#configure(conf) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fluent/plugin/in_prometheus.rb', line 46

def configure(conf)
  super

  # Get how many workers we have
  sysconf = if self.respond_to?(:owner) && owner.respond_to?(:system_config)
              owner.system_config
            elsif self.respond_to?(:system_config)
              self.system_config
            else
              nil
            end
  @num_workers = sysconf && sysconf.workers ? sysconf.workers : 1

  @base_port = @port
  @port += fluentd_worker_id
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/fluent/plugin/in_prometheus.rb', line 63

def multi_workers_ready?
  true
end

#shutdownObject



110
111
112
113
114
115
116
# File 'lib/fluent/plugin/in_prometheus.rb', line 110

def shutdown
  if @server
    @server.shutdown
    @server = nil
  end
  super
end

#startObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fluent/plugin/in_prometheus.rb', line 67

def start
  super
  log.debug "listening prometheus http server on http://#{@bind}:#{@port}/#{@metrics_path} for worker#{fluentd_worker_id}"
  config = {
    BindAddress: @bind,
    Port: @port,
    MaxClients: 5,
    Logger: WEBrick::Log.new(STDERR, WEBrick::Log::FATAL),
    AccessLog: [],
  }
  unless @ssl.nil? || !@ssl['enable']
    require 'webrick/https'
    require 'openssl'
    if (@ssl['certificate_path'] && @ssl['private_key_path'].nil?) || (@ssl['certificate_path'].nil? && @ssl['private_key_path'])
        raise RuntimeError.new("certificate_path and private_key_path most both be defined")
    end
    ssl_config = {
        SSLEnable: true
    }
    if @ssl['certificate_path']
      cert = OpenSSL::X509::Certificate.new(File.read(@ssl['certificate_path']))
      ssl_config[:SSLCertificate] = cert
    end
    if @ssl['private_key_path']
      key = OpenSSL::PKey::RSA.new(File.read(@ssl['private_key_path']))
      ssl_config[:SSLPrivateKey] = key
    end
    ssl_config[:SSLCACertificateFile] = @ssl['ca_path'] if @ssl['ca_path']
    ssl_config = ssl_config.merge(@ssl['extra_conf'])
    config = ssl_config.merge(config)
  end
  @log.on_debug do
    @log.debug("WEBrick conf: #{config}")
  end

  @server = WEBrick::HTTPServer.new(config)
  @server.mount(@metrics_path, MonitorServlet, self)
  @server.mount(@aggregated_metrics_path, MonitorServletAll, self)
  thread_create(:in_prometheus) do
    @server.start
  end
end