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



134
135
136
137
138
139
140
# File 'lib/fluent/plugin/in_prometheus.rb', line 134

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fluent/plugin/in_prometheus.rb', line 67

def start
  super

  # Normalize bind address: strip brackets if present (for consistency)
  # Brackets are only for URI formatting, not for socket binding
  @bind = @bind[1..-2] if @bind.start_with?('[') && @bind.end_with?(']')

  scheme = @secure ? 'https' : 'http'
  # Format bind address properly for URLs (add brackets for IPv6)
  bind_display = @bind.include?(':') ? "[#{@bind}]" : @bind
  log.debug "listening prometheus http server on #{scheme}://#{bind_display}:#{@port}/#{@metrics_path} for worker#{fluentd_worker_id}"

  proto = @secure ? :tls : :tcp

  # IPv6 + TLS combination is not currently supported
  if @bind.include?(':') && @secure
    raise Fluent::ConfigError, 'IPv6 with <transport tls> is not currently supported. Use bind 0.0.0.0 with TLS, or bind ::1 without TLS.'
  end

  # Use webrick for IPv6 or SSL extra_conf
  # The http_server helper has issues with IPv6 URI construction
  if (@ssl && @ssl['enable'] && @ssl['extra_conf']) || @bind.include?(':')
    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

  # Use raw bind address for socket binding (no brackets)
  # Brackets are only for URL/URI formatting, not for bind()
  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