Skip to content

Commit

Permalink
Removes code duplication, introduces rubocop (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia authored Apr 24, 2019
1 parent d6c7d52 commit 9e6d121
Show file tree
Hide file tree
Showing 25 changed files with 294 additions and 146 deletions.
22 changes: 22 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
inherit_from: .rubocop_todo.yml

require:
- rubocop-performance

AllCops:
DisplayCopNames: true
DisplayStyleGuide: true
TargetRubyVersion: 2.3

Metrics/BlockLength:
Exclude:
- spec/**/*.rb

Metrics/LineLength:
Max: 120
Exclude:
- spec/**/*.rb

Style/Documentation:
Exclude:
- 'spec/**/*'
41 changes: 41 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-04-20 13:23:53 +0200 using RuboCop version 0.67.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Lint/DuplicateMethods:
Exclude:
- 'lib/sidekiq/logstash/configuration.rb'

# Offense count: 4
Metrics/AbcSize:
Max: 31

# Offense count: 1
Metrics/CyclomaticComplexity:
Max: 13

# Offense count: 6
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 28

# Offense count: 1
Metrics/PerceivedComplexity:
Max: 14

# Offense count: 7
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'lib/sidekiq/logging/argument_filter.rb'
- 'lib/sidekiq/logging/logstash_formatter.rb'
- 'lib/sidekiq/logging/shared.rb'
- 'lib/sidekiq/logstash.rb'
- 'lib/sidekiq/logstash/configuration.rb'
- 'lib/sidekiq/logstash_job_logger.rb'
- 'lib/sidekiq/middleware/server/logstash_logging.rb'
14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
language: ruby
cache: bundler
rvm:
- 2.2.2
- 2.3.0
- 2.4.0
before_install: gem install bundler -v 1.11.2
- 2.3
- 2.4
- 2.5
- 2.6
before_install:
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
- gem install bundler -v '~> 1.17'
gemfile:
- gemfiles/sidekiq3.gemfile
- gemfiles/sidekiq4.gemfile
Expand All @@ -16,5 +20,5 @@ deploy:
on:
tags: true
repo: iMacTia/sidekiq-logstash
rvm: 2.4.0
rvm: 2.6

2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in sidekiq-logstash.gemspec
Expand Down
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'sidekiq/logstash'
Expand Down
1 change: 1 addition & 0 deletions bin/test_console
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'sidekiq/logstash'
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/sidekiq3.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'sidekiq', '~> 3.0'
Expand Down
4 changes: 3 additions & 1 deletion gemfiles/sidekiq4.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'sidekiq', '~> 4.0'

gemspec path: '../'
gemspec path: '../'
4 changes: 3 additions & 1 deletion gemfiles/sidekiq5.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'sidekiq', '~> 5.0'

gemspec path: '../'
gemspec path: '../'
38 changes: 27 additions & 11 deletions lib/sidekiq/logging/argument_filter.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# frozen_string_literal: true

# This implementation is taken directly from https://github.com/rails/rails/blob/52ce6ece8c8f74064bb64e0a0b1ddd83092718e1/actionpack/lib/action_dispatch/http/parameter_filter.rb
# Adding actionpack to the gem dependencies would have been too heavy, so here is just what we need.

module Sidekiq
module Logging
# Class that allows to filter-out sensible arguments.
class ArgumentFilter
FILTERED = '[FILTERED]'.freeze
# String used to replace sensible arguments.
FILTERED = '[FILTERED]'

def initialize(filters = [])
@filters = filters
end

# Filters argument by using the filters provided upon initialization.
# @param args [Array] the list of arguments.
def filter(args)
compiled_filter.call(args)
end
Expand All @@ -22,8 +27,11 @@ def compiled_filter

class CompiledFilter # :nodoc:
def self.compile(filters)
return lambda { |args| args.dup } if filters.empty?
strings, regexps, blocks = [], [], []
return ->(args) { args.dup } if filters.empty?

strings = []
regexps = []
blocks = []
filters.each do |item|
case item
when Proc
Expand All @@ -34,10 +42,10 @@ def self.compile(filters)
strings << Regexp.escape(item.to_s)
end
end
deep_regexps, regexps = regexps.partition { |r| r.to_s.include?("\\.".freeze) }
deep_strings, strings = strings.partition { |s| s.include?("\\.".freeze) }
regexps << Regexp.new(strings.join('|'.freeze), true) unless strings.empty?
deep_regexps << Regexp.new(deep_strings.join('|'.freeze), true) unless deep_strings.empty?
deep_regexps, regexps = regexps.partition { |r| r.to_s.include?('\\.') }
deep_strings, strings = strings.partition { |s| s.include?('\\.') }
regexps << Regexp.new(strings.join('|'), true) unless strings.empty?
deep_regexps << Regexp.new(deep_strings.join('|'), true) unless deep_strings.empty?
new regexps, deep_regexps, blocks
end

Expand All @@ -62,8 +70,16 @@ def call(original_args, parents = [])
elsif value.is_a?(Array)
value = value.map { |v| v.is_a?(Hash) ? call(v, parents) : v }
elsif blocks.any?
key = key.dup rescue key
value = value.dup rescue value
key = begin
key.dup
rescue StandardError
key
end
value = begin
value.dup
rescue StandardError
value
end
blocks.each { |b| b.call(key, value) }
end
parents.pop if deep_regexps
Expand All @@ -74,4 +90,4 @@ def call(original_args, parents = [])
end
end
end
end
end
13 changes: 11 additions & 2 deletions lib/sidekiq/logging/logstash_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true

require 'logstash-event'

module Sidekiq
module Logging
# Class that takes a log payload and format it to be Logstash-compatible.
class LogstashFormatter
def call(severity, time, progname, data)
def call(severity, _time, _progname, data)
json_data = { severity: severity }

if data.is_a? Hash
Expand All @@ -13,12 +16,18 @@ def call(severity, time, progname, data)
end

# Merge custom_options to provide customization
custom_options.call(json_data) if custom_options rescue nil
begin
custom_options&.call(json_data)
rescue StandardError
nil
end
event = LogStash::Event.new(json_data)

"#{event.to_json}\n"
end

private

def custom_options
Sidekiq::Logstash.configuration.custom_options
end
Expand Down
Loading

0 comments on commit 9e6d121

Please sign in to comment.