Class: Fluent::AnalyzeConfigFilter

Inherits:
Filter
  • Object
show all
Includes:
selfself::Constants, Config
Defined in:
lib/fluent/plugin/filter_analyze_config.rb

Overview

Fluentd filter plugin to analyze configuration usage.

For documentation on inspecting parsed configuration elements, see https://www.rubydoc.info/github/fluent/fluentd/Fluent/Config/Element

Defined Under Namespace

Modules: Constants

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/fluent/plugin/filter_analyze_config.rb', line 188

def configure(conf)
  super
  if File.file?(@google_fluentd_config_path) &&
     File.file?(@google_fluentd_baseline_config_path)
    @log.info(
      'google-fluentd configuration file found at' \
      " #{@google_fluentd_config_path}. " \
      'google-fluentd baseline configuration file found at' \
      " #{@google_fluentd_baseline_config_path}. " \
      'google-fluentd Analyzing configuration.')

    # TODO: Add OpenCensus support.
    registry = Monitoring::MonitoringRegistryFactory.create(
      Monitoring::PrometheusMonitoringRegistry.name, nil, nil, nil)

    plugin_usage = registry.counter(
      :stackdriver_enabled_plugins,
      [:plugin_name, :is_default_plugin, :has_default_value],
      'Enabled plugins')
    config_usage = registry.counter(
      :stackdriver_config_usage,
      [:plugin_name, :param, :is_present, :has_default_value],
      'Parameter usage for Google Cloud plugins')
    config_bool_values = registry.counter(
      :stackdriver_config_bool_values,
      [:plugin_name, :param, :value],
      'Values for bool parameters in Google Cloud plugins')

    config = parse_config(@google_fluentd_config_path)
    baseline_config = parse_config(@google_fluentd_baseline_config_path)

    # Create hash of all baseline elements by their plugin names.
    baseline_elements = Hash[baseline_config.elements.collect do |e|
                               [default_plugin_name(e), e]
                             end]
    baseline_google_element = baseline_config.elements.find do |e|
      e['@type'] == 'google_cloud'
    end

    # Look at each top-level config element and see whether it
    # matches the baseline value.
    #
    # Note on custom configurations: If the plugin has a custom
    # value (e.g. if a tail plugin has pos_file
    # /var/lib/google-fluentd/pos/my-custom-value.pos), then the
    # default_plugin_name (e.g. source/tail/my-custom-value) won't
    # be a key in baseline_elements below, so it won't be
    # used.  Instead it will use the custom_plugin_name
    # (e.g. source/tail).
    config.elements.each do |e|
      plugin_name = default_plugin_name(e)
      if baseline_elements.key?(plugin_name)
        is_default_plugin = true
        has_default_value = (baseline_elements[plugin_name] == e)
      else
        plugin_name = custom_plugin_name(e)
        is_default_plugin = false
        has_default_value = false
      end
      plugin_usage.increment(
        labels: {
          plugin_name: plugin_name,
          is_default_plugin: is_default_plugin,
          has_default_value: has_default_value,
          has_ruby_snippet: embedded_ruby?(e)
        },
        by: 1)

      # Additional metric for Google plugins (google_cloud and
      # detect_exceptions).
      next unless GOOGLE_PLUGIN_PARAMS.key?(e['@type'])
      GOOGLE_PLUGIN_PARAMS[e['@type']].each do |p|
        config_usage.increment(
          labels: {
            plugin_name: e['@type'],
            param: p,
            is_present: e.key?(p),
            has_default_value: (e.key?(p) &&
                                baseline_google_element.key?(p) &&
                                e[p] == baseline_google_element[p])
          },
          by: 1)
        next unless e.key?(p) && %w(true false).include?(e[p])
        config_bool_values.increment(
          labels: {
            plugin_name: e['@type'],
            param: p,
            value: e[p] == 'true'
          },
          by: 1)
      end
    end
  else
    @log.info(
      'google-fluentd configuration file does not exist at' \
      " #{@google_fluentd_config_path} or " \
      'google-fluentd baseline configuration file does not exist at' \
      " #{@google_fluentd_baseline_config_path} or " \
      '. Skipping configuration analysis.')
  end
rescue => e
  # Do not crash the agent due to configuration analysis failures.
  @log.warn(
    'Failed to optionally analyze the google-fluentd configuration' \
    " file. Proceeding anyway. Error: #{e}")
end

#custom_plugin_name(e) ⇒ Object

Returns a name for identifying plugins not in our default config. This should not contain arbitrary user-supplied data.



173
174
175
176
177
178
179
180
# File 'lib/fluent/plugin/filter_analyze_config.rb', line 173

def custom_plugin_name(e)
  if KNOWN_PLUGINS.key?(e.name) &&
     KNOWN_PLUGINS[e.name].include?(e['@type'])
    "#{e.name}/#{e['@type']}"
  else
    e.name.to_s
  end
end

#default_plugin_name(e) ⇒ Object

Returns a name for identifying plugins we ship by default.



160
161
162
163
164
165
166
167
168
169
# File 'lib/fluent/plugin/filter_analyze_config.rb', line 160

def default_plugin_name(e)
  case e['@type']
  when 'syslog'
    "#{e.name}/syslog/#{e['protocol_type']}"
  when 'tail'
    "#{e.name}/tail/#{File.basename(e['pos_file'], '.pos')}"
  else
    "#{e.name}/#{e['@type']}"
  end
end

#embedded_ruby?(e) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
186
# File 'lib/fluent/plugin/filter_analyze_config.rb', line 182

def embedded_ruby?(e)
  (e.arg.include?('#{') ||
   e.any? { |_, v| v.include?('#{') } ||
   e.elements.any? { |ee| embedded_ruby?(ee) })
end

#filter(tag, time, record) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



300
301
302
303
# File 'lib/fluent/plugin/filter_analyze_config.rb', line 300

def filter(tag, time, record)
  # Skip the actual filtering process.
  record
end

#parse_config(path) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fluent/plugin/filter_analyze_config.rb', line 144

def parse_config(path)
  data = File.open(path, 'r', &:read)
  fname = File.basename(path)
  basepath = File.dirname(path)
  eval_context = Kernel.binding
  # Override instance_eval so that LiteralParser does not actually
  # evaluate the embedded Ruby, but instead just returns the
  # source string.  See
  # https://github.com/fluent/fluentd/blob/master/lib/fluent/config/literal_parser.rb
  def eval_context.instance_eval(code)
    code
  end
  Fluent::Config::V1Parser.parse(data, fname, basepath, eval_context)
end

#shutdown ⇒ Object



295
296
297
# File 'lib/fluent/plugin/filter_analyze_config.rb', line 295

def shutdown
  super
end

#start ⇒ Object



136
137
138
139
140
141
142
# File 'lib/fluent/plugin/filter_analyze_config.rb', line 136

def start
  super
  @log = $log # rubocop:disable Style/GlobalVars

  # Initialize the insertID.
  @log.info 'Started the analyze_config plugin to analyze configuration.'
end