Skip to content

Commit

Permalink
Drop reevoocop and just use rubocop...
Browse files Browse the repository at this point in the history
Make the syle more like the "comunity" guidelines to
make the codebase more accessable.
  • Loading branch information
errm committed Apr 14, 2018
1 parent 2b60ad3 commit 47dc708
Show file tree
Hide file tree
Showing 13 changed files with 326 additions and 284 deletions.
17 changes: 17 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
AllCops:
Exclude:
- '*.gemspec'

Metrics/LineLength:
Max: 120
Exclude:
- 'test/**/*'
Metrics/ClassLength:
Exclude:
- 'test/**/*'
Metrics/AbcSize:
Exclude:
- 'test/**/*'
Metrics/MethodLength:
Exclude:
- 'test/**/*'
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
source "https://rubygems.org"

source 'https://rubygems.org'

gemspec
25 changes: 13 additions & 12 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rake/testtask"
require "reevoocop/rake_task"
require "fileutils"

ReevooCop::RakeTask.new(:reevoocop)
require 'bundler/gem_tasks'
require 'rake/testtask'
require 'rubocop/rake_task'
require 'fileutils'

RuboCop::RakeTask.new(:rubocop)

Rake::TestTask.new(:test) do |t|
t.test_files = Dir["test/**/test_*.rb"]
t.test_files = Dir['test/**/test_*.rb']
end

task default: "docker:test"
task build: "docker:test"
task default: :reevoocop
task default: 'docker:test'
task build: 'docker:test'
task default: :rubocop

namespace :docker do
distros = %i[ubuntu tdagent-ubuntu tdagent-centos]
Expand All @@ -22,10 +23,10 @@ namespace :docker do
task distro do
puts "testing on #{distro}"
begin
FileUtils.cp("test/docker/Dockerfile.#{distro}", "Dockerfile")
sh "docker build ."
FileUtils.cp("test/docker/Dockerfile.#{distro}", 'Dockerfile')
sh 'docker build .'
ensure
FileUtils.rm("Dockerfile")
FileUtils.rm('Dockerfile')
end
end
end
Expand Down
35 changes: 17 additions & 18 deletions fluent-plugin-systemd.gemspec
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: true

lib = File.expand_path("../lib", __FILE__)
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "fluent-plugin-systemd"
spec.version = "0.3.1"
spec.authors = ["Ed Robinson"]
spec.email = ["[email protected]"]
spec.name = 'fluent-plugin-systemd'
spec.version = '0.3.1'
spec.authors = ['Ed Robinson']
spec.email = ['[email protected]']

spec.summary = "Input plugin to read from systemd journal."
spec.description = "This is a fluentd input plugin. It reads logs from the systemd journal."
spec.homepage = "https://github.com/reevoo/fluent-plugin-systemd"
spec.license = "MIT"
spec.summary = 'Input plugin to read from systemd journal.'
spec.description = 'This is a fluentd input plugin. It reads logs from the systemd journal.'
spec.homepage = 'https://github.com/reevoo/fluent-plugin-systemd'
spec.license = 'MIT'

spec.files = Dir['lib/**/**.rb', 'README.md', 'LICENCE']
spec.require_paths = ['lib']

spec.files = Dir["lib/**/**.rb", "README.md", "LICENCE"]
spec.require_paths = ["lib"]
spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'test-unit', '~> 2.5'
spec.add_development_dependency 'rubocop', '~> 0.53.0'

spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake"
spec.add_development_dependency "test-unit", "~> 2.5"
spec.add_development_dependency "reevoocop"

spec.add_runtime_dependency "fluentd", [">= 0.14.11", "< 2"]
spec.add_runtime_dependency "systemd-journal", "~> 1.3"
spec.add_runtime_dependency 'fluentd', ['>= 0.14.11', '< 2']
spec.add_runtime_dependency 'systemd-journal', '~> 1.3'
end
11 changes: 5 additions & 6 deletions lib/fluent/plugin/filter_systemd_entry.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true
require "fluent/plugin/filter"
require "fluent/plugin/systemd/entry_mutator"

require 'fluent/plugin/filter'
require 'fluent/plugin/systemd/entry_mutator'

module Fluent
module Plugin
# Fluentd systemd/journal filter plugin
class SystemdEntryFilter < Filter
Fluent::Plugin.register_filter("systemd_entry", self)
Fluent::Plugin.register_filter('systemd_entry', self)

config_param :field_map, :hash, default: {}
config_param :field_map_strict, :bool, default: false
Expand All @@ -16,9 +17,7 @@ class SystemdEntryFilter < Filter
def configure(conf)
super
@mutator = SystemdEntryMutator.new(**@config_root_section.to_h)
if @mutator.field_map_strict && @mutator.field_map.empty?
log.warn("`field_map_strict` set to true with empty `field_map`, expect no fields")
end
@mutator.warnings.each { |warning| log.warn(warning) }
end

def filter(_tag, _time, entry)
Expand Down
34 changes: 17 additions & 17 deletions lib/fluent/plugin/in_systemd.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
# frozen_string_literal: true
require "systemd/journal"
require "fluent/plugin/input"
require "fluent/plugin/systemd/pos_writer"
require "fluent/plugin/systemd/entry_mutator"

require 'systemd/journal'
require 'fluent/plugin/input'
require 'fluent/plugin/systemd/pos_writer'
require 'fluent/plugin/systemd/entry_mutator'

module Fluent
module Plugin
# Fluentd plugin for reading from the systemd journal
class SystemdInput < Input # rubocop:disable Metrics/ClassLength
Fluent::Plugin.register_input("systemd", self)
Fluent::Plugin.register_input('systemd', self)

helpers :timer, :storage

DEFAULT_STORAGE_TYPE = "local"
DEFAULT_STORAGE_TYPE = 'local'

config_param :path, :string, default: "/var/log/journal"
config_param :path, :string, default: '/var/log/journal'
config_param :filters, :array, default: []
config_param :pos_file, :string, default: nil, deprecated: "Use <storage> section with `persistent: true' instead"
config_param :read_from_head, :bool, default: false
config_param :strip_underscores, :bool, default: false, deprecated: "Use <entry> section or `systemd_entry` " \
"filter plugin instead"
config_param :strip_underscores, :bool, default: false, deprecated: 'Use <entry> section or `systemd_entry` ' \
'filter plugin instead'
config_param :tag, :string

config_section :storage do
config_set_default :usage, "positions"
config_set_default :usage, 'positions'
config_set_default :@type, DEFAULT_STORAGE_TYPE
config_set_default :persistent, false
end

config_section :entry, param_name: "entry_opts", required: false, multi: false do
config_section :entry, param_name: 'entry_opts', required: false, multi: false do
config_param :field_map, :hash, default: {}
config_param :field_map_strict, :bool, default: false
config_param :fields_strip_underscores, :bool, default: false
Expand All @@ -37,14 +39,12 @@ class SystemdInput < Input # rubocop:disable Metrics/ClassLength
def configure(conf)
super
@journal = nil
@pos_storage = PosWriter.new(@pos_file, storage_create(usage: "positions"))
@pos_storage = PosWriter.new(@pos_file, storage_create(usage: 'positions'))
# legacy strip_underscores backwards compatibility (legacy takes
# precedence and is mutually exclusive with the entry block)
mut_opts = @strip_underscores ? { fields_strip_underscores: true } : @entry_opts.to_h
@mutator = SystemdEntryMutator.new(**mut_opts)
if @mutator.field_map_strict && @mutator.field_map.empty?
log.warn("`field_map_strict` set to true with empty `field_map`, expect no fields")
end
@mutator.warnings.each { |warning| log.warn(warning) }
end

def start
Expand Down Expand Up @@ -81,7 +81,7 @@ def seek
rescue Systemd::JournalError
log.warn(
"Could not seek to cursor #{cursor} found in pos file: #{@pos_storage.path}, " \
"falling back to reading from #{read_from}",
"falling back to reading from #{read_from}"
)
seek_to(read_from)
end
Expand Down Expand Up @@ -120,7 +120,7 @@ def emit(entry)
retries += 1
sleep 1.5**retries + rand(0..3)
retry
rescue => e
rescue => e # rubocop:disable Style/RescueStandardError
log.error("Exception emitting record: #{e}")
end

Expand Down
35 changes: 22 additions & 13 deletions lib/fluent/plugin/systemd/entry_mutator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require "fluent/config/error"

require 'fluent/config/error'

module Fluent
module Plugin
Expand All @@ -19,12 +20,11 @@ module Plugin
# "<new_field2>" => ["<source_field2>"]
# }
class SystemdEntryMutator

Options = Struct.new(
:field_map,
:field_map_strict,
:fields_lowercase,
:fields_strip_underscores,
:fields_strip_underscores
)

def self.default_opts
Expand Down Expand Up @@ -56,6 +56,10 @@ def method_missing(sym, *args)
super
end

def respond_to_missing?(sym, include_private = false)
@opts.members.include?(sym) || super
end

# The main run method that performs all configured mutations, if any,
# against a single journal entry. Returns the mutated entry hash.
# entry - hash or `Systemd::Journal:Entry`
Expand Down Expand Up @@ -91,16 +95,21 @@ def format_fields(entry, mapped = nil)
end
end

def warnings
return [] unless field_map_strict && field_map.empty?
'`field_map_strict` set to true with empty `field_map`, expect no fields'
end

private

def join_if_needed(values)
values.compact!
return values.first if values.length == 1
values.join(" ")
values.join(' ')
end

def format_field_name(name)
name = name.gsub(/\A_+/, "") if @opts.fields_strip_underscores
name = name.gsub(/\A_+/, '') if @opts.fields_strip_underscores
name = name.downcase if @opts.fields_lowercase
name
end
Expand All @@ -116,8 +125,8 @@ def options_from_hash(opts)
end

def validate_options(opts)
validate_all_strings opts[:field_map].keys, "`field_map` keys must be strings"
validate_all_strings opts[:field_map].values, "`field_map` values must be strings or an array of strings", true
validate_all_strings opts[:field_map].keys, '`field_map` keys must be strings'
validate_all_strings opts[:field_map].values, '`field_map` values must be strings or an array of strings', true
%i[field_map_strict fields_strip_underscores fields_lowercase].each do |opt|
validate_boolean opts[opt], opt
end
Expand All @@ -127,21 +136,21 @@ def validate_all_strings(arr, message, allow_nesting = false)
valid = arr.all? do |value|
value.is_a?(String) || allow_nesting && value.is_a?(Array) && value.all? { |key| key.is_a?(String) }
end
fail Fluent::ConfigError, message unless valid
raise Fluent::ConfigError, message unless valid
end

def validate_boolean(value, name)
fail Fluent::ConfigError, "`#{name}` must be boolean" unless [true, false].include?(value)
raise Fluent::ConfigError, "`#{name}` must be boolean" unless [true, false].include?(value)
end

# Compute the inverse of a human friendly field map `fm` which is what
# Compute the inverse of a human friendly field map `field_map` which is what
# the mutator uses for the actual mapping. The resulting structure for
# the inverse field map hash is:
# {"<new_field_name>" => ["<source_field_name>", ...], ...}
def invert_field_map(fm)
def invert_field_map(field_map)
invs = {}
fm.values.flatten.uniq.each do |cstm|
sysds = fm.select { |_, v| (v == cstm || v.include?(cstm)) }
field_map.values.flatten.uniq.each do |cstm|
sysds = field_map.select { |_, v| (v == cstm || v.include?(cstm)) }
invs[cstm] = sysds.keys
end
invs
Expand Down
14 changes: 12 additions & 2 deletions lib/fluent/plugin/systemd/pos_writer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# frozen_string_literal: true
require "fluent/plugin/input"

require 'fluent/plugin/input'

module Fluent
module Plugin
class SystemdInput < Input
# This is used to write the systemd cursor to the configured storage
# We do this periodicly in a thread so as to not contend on resources
# that might be needed for more important tasks.
#
# When signaled to shutdown we ensure that the most recent cursor
# has been written.
#
# If fluentd stops runnning without cleanly shutting down PosWriter
# the cursor could be up to 1 second stale
class PosWriter
def initialize(pos_file, storage)
@path = pos_file
Expand Down Expand Up @@ -76,7 +86,7 @@ def work
def write_pos
@lock.synchronize do
if @written_cursor != @cursor
file = File.open(@path, "w+", 0o644)
file = File.open(@path, 'w+', 0o644)
file.print @cursor
file.close
@written_cursor = @cursor
Expand Down
7 changes: 4 additions & 3 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
require "test/unit"
require "fluent/test"
require "fluent/test/helpers"

require 'test/unit'
require 'fluent/test'
require 'fluent/test/helpers'
Loading

0 comments on commit 47dc708

Please sign in to comment.