Class: Fluent::Plugin::PrometheusInput

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

Defined Under Namespace

Modules: AsyncWrapper

Instance Method Summary collapse

Constructor Details

#initializePrometheusInput

Returns a new instance of PrometheusInput.



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

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

Instance Method Details

#configure(conf) ⇒ Object



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

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
  @secure = @transport_config.protocol == :tls || (@ssl && @ssl['enable'])

  @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



119
120
121
122
123
124
125
# File 'lib/fluent/plugin/in_prometheus.rb', line 119

def shutdown
  if @webrick_server
    @webrick_server.shutdown
    @webrick_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
109
110
111
112
113
114
115
116
117
# File 'lib/fluent/plugin/in_prometheus.rb', line 67

def start
  super

  scheme = @secure ? 'https' : 'http'
  log.debug "listening prometheus http server on #{scheme}:://#{@bind}:#{@port}/#{@metrics_path} for worker#{fluentd_worker_id}"

  proto = @secure ? :tls : :tcp

  if @ssl && @ssl['enable'] && @ssl['extra_conf']
    start_webrick
    return
  end

  begin
    require 'async'
    require 'fluent/plugin/in_prometheus/async_wrapper'
    extend AsyncWrapper
  rescue LoadError => _
    # ignore
  end

  tls_opt = if @ssl && @ssl['enable']
              ssl_config = {}

              if (@ssl['certificate_path'] && @ssl['private_key_path'].nil?) || (@ssl['certificate_path'].nil? && @ssl['private_key_path'])
                raise Fluent::ConfigError.new('both certificate_path and private_key_path must be defined')
              end

              if @ssl['certificate_path']
                ssl_config['cert_path'] = @ssl['certificate_path']
              end

              if @ssl['private_key_path']
                ssl_config['private_key_path'] = @ssl['private_key_path']
              end

              if @ssl['ca_path']
                ssl_config['ca_path'] = @ssl['ca_path']
                # Only ca_path is insecure in fluentd
                # https://github.com/fluent/fluentd/blob/2236ad45197ba336fd9faf56f442252c8b226f25/lib/fluent/plugin_helper/cert_option.rb#L68
                ssl_config['insecure'] = true
              end

              ssl_config
            end

  http_server_create_http_server(:in_prometheus_server, addr: @bind, port: @port, logger: log, proto: proto, tls_opts: tls_opt) do |server|
    server.get(@metrics_path) { |_req| all_metrics }
    server.get(@aggregated_metrics_path) { |_req| all_workers_metrics }
  end
end