From 88e5cb236027c1f5f07c686ce49a9b4a1d7c6276 Mon Sep 17 00:00:00 2001 From: Rustam Ibragimov <33183529+0exp@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:07:40 +0300 Subject: [PATCH] [middelware-autoset] An ability to setup Rails middleware usage manually (#46) * middleware-autoset * linter * chanlog,readme,version * readme * debump * audit - bump dev gems * no more old rubies * empty-commit * new rubocop --- .github/workflows/test.yml | 2 +- .rubocop.yml | 2 +- Changelog.md | 6 ++ Gemfile.lock | 178 ++++++++++++++++++------------------- README.md | 13 +++ lamian.gemspec | 2 +- lib/lamian.rb | 6 +- lib/lamian/config.rb | 10 ++- lib/lamian/engine.rb | 4 +- lib/lamian/logger.rb | 2 +- spec/spec_helper.rb | 2 +- 11 files changed, 128 insertions(+), 99 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7f8619c..231cadb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -44,7 +44,7 @@ jobs: strategy: fail-fast: false matrix: - ruby: ["3.0", "3.1", "3.2"] + ruby: ["3.1", "3.2", "3.3"] steps: - uses: actions/checkout@v2 diff --git a/.rubocop.yml b/.rubocop.yml index 26ee005..65a6c16 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,7 +3,7 @@ inherit_gem: AllCops: DisplayCopNames: true - TargetRubyVersion: 2.7 + TargetRubyVersion: 3.1 SuggestExtensions: false RSpec/EmptyLineAfterHook: diff --git a/Changelog.md b/Changelog.md index 48b4a7a..878fd91 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,9 @@ +## 1.10.0 + +* Add an ability to insert Lamian's middleware inside the rails initialization process manually (`Lamian.config.middleware_autoset = true/false`) +* Drop EOL rubies from CI; +* Minimal Ruby version: 3.1; + ## 1.9.0 * Add `max_log_lines` config option to limit number of most recent log lines stored in the log device diff --git a/Gemfile.lock b/Gemfile.lock index 8e1ba96..3bb4ebf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,80 +7,76 @@ PATH GEM remote: https://rubygems.org/ specs: - actioncable (7.1.3.4) - actionpack (= 7.1.3.4) - activesupport (= 7.1.3.4) + actioncable (7.2.0) + actionpack (= 7.2.0) + activesupport (= 7.2.0) nio4r (~> 2.0) websocket-driver (>= 0.6.1) zeitwerk (~> 2.6) - actionmailbox (7.1.3.4) - actionpack (= 7.1.3.4) - activejob (= 7.1.3.4) - activerecord (= 7.1.3.4) - activestorage (= 7.1.3.4) - activesupport (= 7.1.3.4) - mail (>= 2.7.1) - net-imap - net-pop - net-smtp - actionmailer (7.1.3.4) - actionpack (= 7.1.3.4) - actionview (= 7.1.3.4) - activejob (= 7.1.3.4) - activesupport (= 7.1.3.4) - mail (~> 2.5, >= 2.5.4) - net-imap - net-pop - net-smtp + actionmailbox (7.2.0) + actionpack (= 7.2.0) + activejob (= 7.2.0) + activerecord (= 7.2.0) + activestorage (= 7.2.0) + activesupport (= 7.2.0) + mail (>= 2.8.0) + actionmailer (7.2.0) + actionpack (= 7.2.0) + actionview (= 7.2.0) + activejob (= 7.2.0) + activesupport (= 7.2.0) + mail (>= 2.8.0) rails-dom-testing (~> 2.2) - actionpack (7.1.3.4) - actionview (= 7.1.3.4) - activesupport (= 7.1.3.4) + actionpack (7.2.0) + actionview (= 7.2.0) + activesupport (= 7.2.0) nokogiri (>= 1.8.5) racc - rack (>= 2.2.4) + rack (>= 2.2.4, < 3.2) rack-session (>= 1.0.1) rack-test (>= 0.6.3) rails-dom-testing (~> 2.2) rails-html-sanitizer (~> 1.6) - actiontext (7.1.3.4) - actionpack (= 7.1.3.4) - activerecord (= 7.1.3.4) - activestorage (= 7.1.3.4) - activesupport (= 7.1.3.4) + useragent (~> 0.16) + actiontext (7.2.0) + actionpack (= 7.2.0) + activerecord (= 7.2.0) + activestorage (= 7.2.0) + activesupport (= 7.2.0) globalid (>= 0.6.0) nokogiri (>= 1.8.5) - actionview (7.1.3.4) - activesupport (= 7.1.3.4) + actionview (7.2.0) + activesupport (= 7.2.0) builder (~> 3.1) erubi (~> 1.11) rails-dom-testing (~> 2.2) rails-html-sanitizer (~> 1.6) - activejob (7.1.3.4) - activesupport (= 7.1.3.4) + activejob (7.2.0) + activesupport (= 7.2.0) globalid (>= 0.3.6) - activemodel (7.1.3.4) - activesupport (= 7.1.3.4) - activerecord (7.1.3.4) - activemodel (= 7.1.3.4) - activesupport (= 7.1.3.4) + activemodel (7.2.0) + activesupport (= 7.2.0) + activerecord (7.2.0) + activemodel (= 7.2.0) + activesupport (= 7.2.0) timeout (>= 0.4.0) - activestorage (7.1.3.4) - actionpack (= 7.1.3.4) - activejob (= 7.1.3.4) - activerecord (= 7.1.3.4) - activesupport (= 7.1.3.4) + activestorage (7.2.0) + actionpack (= 7.2.0) + activejob (= 7.2.0) + activerecord (= 7.2.0) + activesupport (= 7.2.0) marcel (~> 1.0) - activesupport (7.1.3.4) + activesupport (7.2.0) base64 bigdecimal - concurrent-ruby (~> 1.0, >= 1.0.2) + concurrent-ruby (~> 1.0, >= 1.3.1) connection_pool (>= 2.2.5) drb i18n (>= 1.6, < 2) + logger (>= 1.4.2) minitest (>= 5.1) - mutex_m - tzinfo (~> 2.0) + securerandom (>= 0.3) + tzinfo (~> 2.0, >= 2.0.5) addressable (2.8.7) public_suffix (>= 2.0.2, < 7.0) ast (2.4.2) @@ -90,32 +86,34 @@ GEM bundler-audit (0.9.1) bundler (>= 1.2.0, < 3) thor (~> 1.0) - childprocess (5.0.0) + childprocess (5.1.0) + logger (~> 1.5) ci-helper (0.6.0) colorize (~> 1.1) dry-inflector (~> 1.0) umbrellio-sequel-plugins (~> 0.14) coderay (1.1.3) colorize (1.1.0) - concurrent-ruby (1.3.3) + concurrent-ruby (1.3.4) connection_pool (2.4.1) crass (1.0.6) date (3.3.4) diff-lcs (1.5.1) - docile (1.4.0) + docile (1.4.1) drb (2.2.1) - dry-inflector (1.0.0) + dry-inflector (1.1.0) erubi (1.13.0) - faraday (2.9.2) + faraday (2.10.1) faraday-net_http (>= 2.0, < 3.2) - faraday-net_http (3.1.0) + logger + faraday-net_http (3.1.1) net-http globalid (1.2.1) activesupport (>= 6.1) i18n (1.14.5) concurrent-ruby (~> 1.0) io-console (0.7.2) - irb (1.13.2) + irb (1.14.0) rdoc (>= 4.0.0) reline (>= 0.4.2) json (2.7.2) @@ -123,6 +121,7 @@ GEM launchy (3.0.1) addressable (~> 2.8) childprocess (~> 5.0) + logger (1.6.0) loofah (2.22.0) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -135,8 +134,7 @@ GEM method_source (1.1.0) mini_mime (1.1.5) mini_portile2 (2.8.7) - minitest (5.24.0) - mutex_m (0.2.0) + minitest (5.25.0) net-http (0.4.1) uri net-imap (0.4.14) @@ -149,15 +147,15 @@ GEM net-smtp (0.5.0) net-protocol nio4r (2.7.3) - nokogiri (1.16.6) + nokogiri (1.16.7) mini_portile2 (~> 2.8.2) racc (~> 1.4) - nokogiri (1.16.6-x86_64-darwin) + nokogiri (1.16.7-x86_64-darwin) racc (~> 1.4) - nokogiri (1.16.6-x86_64-linux) + nokogiri (1.16.7-x86_64-linux) racc (~> 1.4) - parallel (1.25.1) - parser (3.3.3.0) + parallel (1.26.2) + parser (3.3.4.2) ast (~> 2.4.1) racc pry (0.14.2) @@ -165,9 +163,9 @@ GEM method_source (~> 1.0) psych (5.1.2) stringio - public_suffix (6.0.0) - racc (1.8.0) - rack (3.1.4) + public_suffix (6.0.1) + racc (1.8.1) + rack (3.1.7) rack-session (2.0.0) rack (>= 3.0.0) rack-test (2.1.0) @@ -175,20 +173,20 @@ GEM rackup (2.1.0) rack (>= 3) webrick (~> 1.8) - rails (7.1.3.4) - actioncable (= 7.1.3.4) - actionmailbox (= 7.1.3.4) - actionmailer (= 7.1.3.4) - actionpack (= 7.1.3.4) - actiontext (= 7.1.3.4) - actionview (= 7.1.3.4) - activejob (= 7.1.3.4) - activemodel (= 7.1.3.4) - activerecord (= 7.1.3.4) - activestorage (= 7.1.3.4) - activesupport (= 7.1.3.4) + rails (7.2.0) + actioncable (= 7.2.0) + actionmailbox (= 7.2.0) + actionmailer (= 7.2.0) + actionpack (= 7.2.0) + actiontext (= 7.2.0) + actionview (= 7.2.0) + activejob (= 7.2.0) + activemodel (= 7.2.0) + activerecord (= 7.2.0) + activestorage (= 7.2.0) + activesupport (= 7.2.0) bundler (>= 1.15.0) - railties (= 7.1.3.4) + railties (= 7.2.0) rails-dom-testing (2.2.0) activesupport (>= 5.0.0) minitest @@ -196,10 +194,10 @@ GEM rails-html-sanitizer (1.6.0) loofah (~> 2.21) nokogiri (~> 1.14) - railties (7.1.3.4) - actionpack (= 7.1.3.4) - activesupport (= 7.1.3.4) - irb + railties (7.2.0) + actionpack (= 7.2.0) + activesupport (= 7.2.0) + irb (~> 1.13) rackup (>= 1.0.0) rake (>= 12.2) thor (~> 1.0, >= 1.2.2) @@ -211,7 +209,7 @@ GEM regexp_parser (2.9.2) reline (0.5.9) io-console (~> 0.5) - rexml (3.3.1) + rexml (3.3.5) strscan rspec (3.13.0) rspec-core (~> 3.13.0) @@ -237,7 +235,7 @@ GEM rubocop-ast (>= 1.31.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.31.3) + rubocop-ast (1.32.0) parser (>= 3.3.1.0) rubocop-capybara (2.21.0) rubocop (~> 1.41) @@ -270,14 +268,15 @@ GEM rubocop-sequel (0.3.4) rubocop (~> 1.0) ruby-progressbar (1.13.0) - semantic_logger (4.15.0) + securerandom (0.3.1) + semantic_logger (4.16.0) concurrent-ruby (~> 1.0) sentry-raven (3.1.2) faraday (>= 1.0) - sentry-ruby (5.18.0) + sentry-ruby (5.19.0) bigdecimal concurrent-ruby (~> 1.0, >= 1.0.2) - sequel (5.81.0) + sequel (5.83.1) bigdecimal simplecov (0.22.0) docile (~> 1.1) @@ -298,12 +297,13 @@ GEM symbiont-ruby unicode-display_width (2.5.0) uri (0.13.0) + useragent (0.16.10) webrick (1.8.1) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) yard (0.9.36) - zeitwerk (2.6.16) + zeitwerk (2.6.17) PLATFORMS ruby diff --git a/README.md b/README.md index b8075e0..d512ec6 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,19 @@ It automatically redefines `Sentry.configuration.before_send` callback if Sentry initialization is completed. If `before_send` is already defined it wraps custom callback. +If you work with rails the `Lamian::Engine` will be automatically initialized, that additionally +adds `Lamian::Middleware` in the first position in your Rails Application middleware stack. + +If you need to manually configure the middleware stack use `Lamian.config.middleware_autoset=false. + +```ruby +Lamian.configure do |config| + config.middleware_autoset = false +end + +Rails.configuration.middleware.use_after(YourSignificantMiddlewareInStack, Lamian::Middleware) +``` + ### Usage with Sidekiq You should add Lamian middleware to the Sidekiq initializer like this: diff --git a/lamian.gemspec b/lamian.gemspec index 35675ac..2c37423 100644 --- a/lamian.gemspec +++ b/lamian.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |spec| spec.homepage = "https://github.com/umbrellio/lamian" spec.license = "MIT" - spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0") + spec.required_ruby_version = Gem::Requirement.new(">= 3.1.0") spec.files = `git ls-files -z`.split("\x0").reject { |f| f.include?("spec") } spec.require_paths = ["lib"] diff --git a/lib/lamian.rb b/lib/lamian.rb index 091fc13..5b1e5ab 100644 --- a/lib/lamian.rb +++ b/lib/lamian.rb @@ -52,8 +52,8 @@ def logger end # Collects logs sent inside block - def run(&block) - logger.run(&block) + def run(&) + logger.run(&) end # Dumps log collected in this run @@ -63,7 +63,7 @@ def run(&block) # value given (for now) # @return formatted log (String by default) def dump(format: nil) - logger.dump(format: format) + logger.dump(format:) end # Truncates the collected log to the specified limit and dumps it. diff --git a/lib/lamian/config.rb b/lib/lamian/config.rb index 695b3e9..f1e4618 100644 --- a/lib/lamian/config.rb +++ b/lib/lamian/config.rb @@ -8,11 +8,19 @@ module Lamian # max number of most recent log lines to store, defaults to 5000 # @attr raven_log_size_limit [Integer] # size limit when sending lamian log to sentry, defaults to +500_000+ - Config = Struct.new(:formatter, :max_log_lines, :raven_log_size_limit) do + # @attr middleware_autoset [BOolean] + # automatically setup a middleware module during rails initialization process + Config = Struct.new( + :formatter, + :max_log_lines, + :raven_log_size_limit, + :middleware_autoset, + ) do def initialize self.formatter = ::Logger::Formatter.new self.max_log_lines = 5000 self.raven_log_size_limit = 500_000 + self.middleware_autoset = true end end end diff --git a/lib/lamian/engine.rb b/lib/lamian/engine.rb index 7121c7f..6e8f79d 100644 --- a/lib/lamian/engine.rb +++ b/lib/lamian/engine.rb @@ -11,7 +11,9 @@ class Engine < ::Rails::Engine initializer "lamian.use_rack_middleware" do |app| # :nocov: - app.config.middleware.unshift(Lamian::Middleware) + if Lamian.config.middleware_autoset + app.config.middleware.unshift(Lamian::Middleware) + end # :nocov: end diff --git a/lib/lamian/logger.rb b/lib/lamian/logger.rb index 3009188..c91e4de 100644 --- a/lib/lamian/logger.rb +++ b/lib/lamian/logger.rb @@ -31,7 +31,7 @@ def run # Part of Logger api, entry point for all logs # extened to run on each log device in stack - def add(*args, &block) + def add(*args, &) @formatter = Lamian.config.formatter logdevs.each do |logdev| diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f1c50ff..0813e15 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,7 +26,7 @@ require "lamian" -Dir[File.join(__dir__, "support/**/*.rb")].sort.each { |x| require(x) } +Dir[File.join(__dir__, "support/**/*.rb")].each { |x| require(x) } RSpec.configure do |config| config.expect_with(:rspec) { |c| c.syntax = :expect }