Skip to content

Commit 47dc708

Browse files
committed
Drop reevoocop and just use rubocop...
Make the syle more like the "comunity" guidelines to make the codebase more accessable.
1 parent 2b60ad3 commit 47dc708

File tree

13 files changed

+326
-284
lines changed

13 files changed

+326
-284
lines changed

.rubocop.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
AllCops:
2+
Exclude:
3+
- '*.gemspec'
4+
5+
Metrics/LineLength:
6+
Max: 120
7+
Exclude:
8+
- 'test/**/*'
9+
Metrics/ClassLength:
10+
Exclude:
11+
- 'test/**/*'
12+
Metrics/AbcSize:
13+
Exclude:
14+
- 'test/**/*'
15+
Metrics/MethodLength:
16+
Exclude:
17+
- 'test/**/*'

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2-
source "https://rubygems.org"
2+
3+
source 'https://rubygems.org'
34

45
gemspec

Rakefile

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# frozen_string_literal: true
2-
require "bundler/gem_tasks"
3-
require "rake/testtask"
4-
require "reevoocop/rake_task"
5-
require "fileutils"
62

7-
ReevooCop::RakeTask.new(:reevoocop)
3+
require 'bundler/gem_tasks'
4+
require 'rake/testtask'
5+
require 'rubocop/rake_task'
6+
require 'fileutils'
7+
8+
RuboCop::RakeTask.new(:rubocop)
89

910
Rake::TestTask.new(:test) do |t|
10-
t.test_files = Dir["test/**/test_*.rb"]
11+
t.test_files = Dir['test/**/test_*.rb']
1112
end
1213

13-
task default: "docker:test"
14-
task build: "docker:test"
15-
task default: :reevoocop
14+
task default: 'docker:test'
15+
task build: 'docker:test'
16+
task default: :rubocop
1617

1718
namespace :docker do
1819
distros = %i[ubuntu tdagent-ubuntu tdagent-centos]
@@ -22,10 +23,10 @@ namespace :docker do
2223
task distro do
2324
puts "testing on #{distro}"
2425
begin
25-
FileUtils.cp("test/docker/Dockerfile.#{distro}", "Dockerfile")
26-
sh "docker build ."
26+
FileUtils.cp("test/docker/Dockerfile.#{distro}", 'Dockerfile')
27+
sh 'docker build .'
2728
ensure
28-
FileUtils.rm("Dockerfile")
29+
FileUtils.rm('Dockerfile')
2930
end
3031
end
3132
end

fluent-plugin-systemd.gemspec

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
# -*- encoding: utf-8 -*-
22
# frozen_string_literal: true
33

4-
lib = File.expand_path("../lib", __FILE__)
4+
lib = File.expand_path('../lib', __FILE__)
55
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
66

77
Gem::Specification.new do |spec|
8-
spec.name = "fluent-plugin-systemd"
9-
spec.version = "0.3.1"
10-
spec.authors = ["Ed Robinson"]
11-
spec.email = ["[email protected]"]
8+
spec.name = 'fluent-plugin-systemd'
9+
spec.version = '0.3.1'
10+
spec.authors = ['Ed Robinson']
11+
spec.email = ['[email protected]']
1212

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

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

19-
spec.files = Dir["lib/**/**.rb", "README.md", "LICENCE"]
20-
spec.require_paths = ["lib"]
21+
spec.add_development_dependency 'bundler', '~> 1.10'
22+
spec.add_development_dependency 'rake'
23+
spec.add_development_dependency 'test-unit', '~> 2.5'
24+
spec.add_development_dependency 'rubocop', '~> 0.53.0'
2125

22-
spec.add_development_dependency "bundler", "~> 1.10"
23-
spec.add_development_dependency "rake"
24-
spec.add_development_dependency "test-unit", "~> 2.5"
25-
spec.add_development_dependency "reevoocop"
26-
27-
spec.add_runtime_dependency "fluentd", [">= 0.14.11", "< 2"]
28-
spec.add_runtime_dependency "systemd-journal", "~> 1.3"
26+
spec.add_runtime_dependency 'fluentd', ['>= 0.14.11', '< 2']
27+
spec.add_runtime_dependency 'systemd-journal', '~> 1.3'
2928
end

lib/fluent/plugin/filter_systemd_entry.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# frozen_string_literal: true
2-
require "fluent/plugin/filter"
3-
require "fluent/plugin/systemd/entry_mutator"
2+
3+
require 'fluent/plugin/filter'
4+
require 'fluent/plugin/systemd/entry_mutator'
45

56
module Fluent
67
module Plugin
78
# Fluentd systemd/journal filter plugin
89
class SystemdEntryFilter < Filter
9-
Fluent::Plugin.register_filter("systemd_entry", self)
10+
Fluent::Plugin.register_filter('systemd_entry', self)
1011

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

2423
def filter(_tag, _time, entry)

lib/fluent/plugin/in_systemd.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
# frozen_string_literal: true
2-
require "systemd/journal"
3-
require "fluent/plugin/input"
4-
require "fluent/plugin/systemd/pos_writer"
5-
require "fluent/plugin/systemd/entry_mutator"
2+
3+
require 'systemd/journal'
4+
require 'fluent/plugin/input'
5+
require 'fluent/plugin/systemd/pos_writer'
6+
require 'fluent/plugin/systemd/entry_mutator'
67

78
module Fluent
89
module Plugin
10+
# Fluentd plugin for reading from the systemd journal
911
class SystemdInput < Input # rubocop:disable Metrics/ClassLength
10-
Fluent::Plugin.register_input("systemd", self)
12+
Fluent::Plugin.register_input('systemd', self)
1113

1214
helpers :timer, :storage
1315

14-
DEFAULT_STORAGE_TYPE = "local"
16+
DEFAULT_STORAGE_TYPE = 'local'
1517

16-
config_param :path, :string, default: "/var/log/journal"
18+
config_param :path, :string, default: '/var/log/journal'
1719
config_param :filters, :array, default: []
1820
config_param :pos_file, :string, default: nil, deprecated: "Use <storage> section with `persistent: true' instead"
1921
config_param :read_from_head, :bool, default: false
20-
config_param :strip_underscores, :bool, default: false, deprecated: "Use <entry> section or `systemd_entry` " \
21-
"filter plugin instead"
22+
config_param :strip_underscores, :bool, default: false, deprecated: 'Use <entry> section or `systemd_entry` ' \
23+
'filter plugin instead'
2224
config_param :tag, :string
2325

2426
config_section :storage do
25-
config_set_default :usage, "positions"
27+
config_set_default :usage, 'positions'
2628
config_set_default :@type, DEFAULT_STORAGE_TYPE
2729
config_set_default :persistent, false
2830
end
2931

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

5050
def start
@@ -81,7 +81,7 @@ def seek
8181
rescue Systemd::JournalError
8282
log.warn(
8383
"Could not seek to cursor #{cursor} found in pos file: #{@pos_storage.path}, " \
84-
"falling back to reading from #{read_from}",
84+
"falling back to reading from #{read_from}"
8585
)
8686
seek_to(read_from)
8787
end
@@ -120,7 +120,7 @@ def emit(entry)
120120
retries += 1
121121
sleep 1.5**retries + rand(0..3)
122122
retry
123-
rescue => e
123+
rescue => e # rubocop:disable Style/RescueStandardError
124124
log.error("Exception emitting record: #{e}")
125125
end
126126

lib/fluent/plugin/systemd/entry_mutator.rb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
2-
require "fluent/config/error"
2+
3+
require 'fluent/config/error'
34

45
module Fluent
56
module Plugin
@@ -19,12 +20,11 @@ module Plugin
1920
# "<new_field2>" => ["<source_field2>"]
2021
# }
2122
class SystemdEntryMutator
22-
2323
Options = Struct.new(
2424
:field_map,
2525
:field_map_strict,
2626
:fields_lowercase,
27-
:fields_strip_underscores,
27+
:fields_strip_underscores
2828
)
2929

3030
def self.default_opts
@@ -56,6 +56,10 @@ def method_missing(sym, *args)
5656
super
5757
end
5858

59+
def respond_to_missing?(sym, include_private = false)
60+
@opts.members.include?(sym) || super
61+
end
62+
5963
# The main run method that performs all configured mutations, if any,
6064
# against a single journal entry. Returns the mutated entry hash.
6165
# entry - hash or `Systemd::Journal:Entry`
@@ -91,16 +95,21 @@ def format_fields(entry, mapped = nil)
9195
end
9296
end
9397

98+
def warnings
99+
return [] unless field_map_strict && field_map.empty?
100+
'`field_map_strict` set to true with empty `field_map`, expect no fields'
101+
end
102+
94103
private
95104

96105
def join_if_needed(values)
97106
values.compact!
98107
return values.first if values.length == 1
99-
values.join(" ")
108+
values.join(' ')
100109
end
101110

102111
def format_field_name(name)
103-
name = name.gsub(/\A_+/, "") if @opts.fields_strip_underscores
112+
name = name.gsub(/\A_+/, '') if @opts.fields_strip_underscores
104113
name = name.downcase if @opts.fields_lowercase
105114
name
106115
end
@@ -116,8 +125,8 @@ def options_from_hash(opts)
116125
end
117126

118127
def validate_options(opts)
119-
validate_all_strings opts[:field_map].keys, "`field_map` keys must be strings"
120-
validate_all_strings opts[:field_map].values, "`field_map` values must be strings or an array of strings", true
128+
validate_all_strings opts[:field_map].keys, '`field_map` keys must be strings'
129+
validate_all_strings opts[:field_map].values, '`field_map` values must be strings or an array of strings', true
121130
%i[field_map_strict fields_strip_underscores fields_lowercase].each do |opt|
122131
validate_boolean opts[opt], opt
123132
end
@@ -127,21 +136,21 @@ def validate_all_strings(arr, message, allow_nesting = false)
127136
valid = arr.all? do |value|
128137
value.is_a?(String) || allow_nesting && value.is_a?(Array) && value.all? { |key| key.is_a?(String) }
129138
end
130-
fail Fluent::ConfigError, message unless valid
139+
raise Fluent::ConfigError, message unless valid
131140
end
132141

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

137-
# Compute the inverse of a human friendly field map `fm` which is what
146+
# Compute the inverse of a human friendly field map `field_map` which is what
138147
# the mutator uses for the actual mapping. The resulting structure for
139148
# the inverse field map hash is:
140149
# {"<new_field_name>" => ["<source_field_name>", ...], ...}
141-
def invert_field_map(fm)
150+
def invert_field_map(field_map)
142151
invs = {}
143-
fm.values.flatten.uniq.each do |cstm|
144-
sysds = fm.select { |_, v| (v == cstm || v.include?(cstm)) }
152+
field_map.values.flatten.uniq.each do |cstm|
153+
sysds = field_map.select { |_, v| (v == cstm || v.include?(cstm)) }
145154
invs[cstm] = sysds.keys
146155
end
147156
invs

lib/fluent/plugin/systemd/pos_writer.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
# frozen_string_literal: true
2-
require "fluent/plugin/input"
2+
3+
require 'fluent/plugin/input'
34

45
module Fluent
56
module Plugin
67
class SystemdInput < Input
8+
# This is used to write the systemd cursor to the configured storage
9+
# We do this periodicly in a thread so as to not contend on resources
10+
# that might be needed for more important tasks.
11+
#
12+
# When signaled to shutdown we ensure that the most recent cursor
13+
# has been written.
14+
#
15+
# If fluentd stops runnning without cleanly shutting down PosWriter
16+
# the cursor could be up to 1 second stale
717
class PosWriter
818
def initialize(pos_file, storage)
919
@path = pos_file
@@ -76,7 +86,7 @@ def work
7686
def write_pos
7787
@lock.synchronize do
7888
if @written_cursor != @cursor
79-
file = File.open(@path, "w+", 0o644)
89+
file = File.open(@path, 'w+', 0o644)
8090
file.print @cursor
8191
file.close
8292
@written_cursor = @cursor

test/helper.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2-
require "test/unit"
3-
require "fluent/test"
4-
require "fluent/test/helpers"
2+
3+
require 'test/unit'
4+
require 'fluent/test'
5+
require 'fluent/test/helpers'

0 commit comments

Comments
 (0)