From 656352882449b59fc2fae78eabf9364e099523e9 Mon Sep 17 00:00:00 2001 From: Steven Harman Date: Mon, 13 May 2024 05:36:18 -0400 Subject: [PATCH] Cleanup gem setup (#20) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Ignore Gemfile.lock from Git We don't really want to lock folks working on this gem into exact versions of the various dependencies. Especially for certain dependencies with native extensions, which might not install on different platforms. This also organizes the .gitignore. * Exclude dev-specific files from packed Gem We can keep the size of the packaged Gem smaller by no including specs, binstubs, git/github config, and other development-time-only files. * Upgrade Rake (b/c why not?) * Fix some spelling, grammar, and formatting * Test newer Rubies and Ubuntus too! * fixup ubuntu * Run tests on a new PR * Bring back Gemfile.lock and use latest Ruby 2.6 Even though Ruby 2.6 (and 2.7) are EoL'd, they are still supported by this Gem. So let's use that oldest one locally too. Also, it appears it is now recommended to include the Gemfile.lock in Git for Gems. The guidance from Bundler use to be _NOT_ to include the .lock file. But, time moves forward and things change. I'll adapt and learn too! 😅 https://bundler.io/guides/faq.html#using-gemfiles-inside-gems * Appease Rubocop 👮 --- .github/workflows/build.yml | 19 ++++++--- .gitignore | 25 ++++++++---- .tool-versions | 2 +- Gemfile | 2 +- Gemfile.lock | 70 ++++++++++++++++++--------------- README.md | 22 ++++++----- puma-plugin-telemetry.gemspec | 5 ++- spec/integration/plugin_spec.rb | 8 ++-- spec/support/server.rb | 4 +- 9 files changed, 95 insertions(+), 62 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 101a3ff..d782eaf 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,19 +1,28 @@ name: build -on: [push, create] +on: [push, create, pull_request] jobs: build_matrix: strategy: matrix: - os: ['ubuntu-18.04', 'ubuntu-20.04'] - ruby: ['2.6', '2.7', '3.0', '3.1'] + os: + - "ubuntu-20.04" + - "ubuntu-22.04" + - "ubuntu-latest" + ruby: + - "2.6" + - "2.7" + - "3.0" + - "3.1" + - "3.2" + - "3.3" runs-on: ${{ matrix.os }} steps: # Environment setup - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: @@ -45,7 +54,7 @@ jobs: if: github.event_name == 'create' && startsWith(github.ref, 'refs/tags/v') steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 - name: Create credentials diff --git a/.gitignore b/.gitignore index b04a8c8..be4e9bc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,22 @@ -/.bundle/ +# Ignore all logfiles and tempfiles +/tmp/ +/tags + +# Environment normalisation +/.rspec-local /.yardoc -/_yardoc/ -/coverage/ -/doc/ +/_yardoc +/lib/bundler/man/ + +# Gem packaging stuff +/*.gem /pkg/ -/spec/reports/ -/tmp/ -# rspec failure tracking +# Dependencies and such +/.bundle +/.ruby-version + +# Spec housekeeping +/coverage/ +/spec/reports .rspec_status diff --git a/.tool-versions b/.tool-versions index 1c44dd0..43e0f42 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -ruby 2.6.6 +ruby 2.6.10 diff --git a/Gemfile b/Gemfile index 5c6c633..0f12708 100644 --- a/Gemfile +++ b/Gemfile @@ -8,7 +8,7 @@ gemspec gem 'dogstatsd-ruby' gem 'rack' -gem 'rake', '~> 12.0' +gem 'rake', '~> 13.0' gem 'rspec', '~> 3.0' gem 'rubocop', '~> 1.5' gem 'rubocop-performance', '~> 1.9' diff --git a/Gemfile.lock b/Gemfile.lock index 5be8418..5b324fe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,60 +8,66 @@ GEM remote: https://rubygems.org/ specs: ast (2.4.2) - diff-lcs (1.5.0) - dogstatsd-ruby (5.5.0) - nio4r (2.5.8) - parallel (1.22.1) - parser (3.1.2.0) + diff-lcs (1.5.1) + dogstatsd-ruby (5.6.1) + json (2.7.2) + nio4r (2.7.1) + parallel (1.24.0) + parser (3.3.1.0) ast (~> 2.4.1) - puma (6.0.1) + racc + puma (6.4.2) nio4r (~> 2.0) - rack (2.2.3.1) + racc (1.7.3) + rack (3.0.10) rainbow (3.1.1) - rake (12.3.3) - regexp_parser (2.5.0) - rexml (3.2.5) - rspec (3.11.0) - rspec-core (~> 3.11.0) - rspec-expectations (~> 3.11.0) - rspec-mocks (~> 3.11.0) - rspec-core (3.11.0) - rspec-support (~> 3.11.0) - rspec-expectations (3.11.0) + rake (13.2.1) + regexp_parser (2.9.0) + rexml (3.2.6) + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.0) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.11.0) - rspec-mocks (3.11.1) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.11.0) - rspec-support (3.11.0) - rubocop (1.30.1) + rspec-support (~> 3.13.0) + rspec-support (3.13.1) + rubocop (1.50.2) + json (~> 2.3) parallel (~> 1.10) - parser (>= 3.1.0.0) + parser (>= 3.2.0.0) rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.18.0, < 2.0) + rubocop-ast (>= 1.28.0, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.18.0) - parser (>= 3.1.1.0) - rubocop-performance (1.14.2) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.30.0) + parser (>= 3.2.1.0) + rubocop-performance (1.17.1) rubocop (>= 1.7.0, < 2.0) rubocop-ast (>= 0.4.0) - ruby-progressbar (1.11.0) - unicode-display_width (2.1.0) + ruby-progressbar (1.13.0) + unicode-display_width (2.5.0) PLATFORMS + arm64-darwin-23 ruby + x86_64-linux DEPENDENCIES dogstatsd-ruby puma-plugin-telemetry! rack - rake (~> 12.0) + rake (~> 13.0) rspec (~> 3.0) rubocop (~> 1.5) rubocop-performance (~> 1.9) BUNDLED WITH - 2.3.12 + 2.4.22 diff --git a/README.md b/README.md index 9247eaa..d0b6f66 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Puma plugin which should be able to handle all your metric needs regarding your webserver: -- ability to publish basic puma statistics (like queue backlog) to both logs and datadog +- ability to publish basic puma statistics (like queue backlog) to both logs and Datadog - ability to add custom target whenever you need it - ability to monitor puma socket listen queue (!) - ability to report requests queue time via custom rack middleware - the time request spent between being accepted by Load Balancer and start of its processing by Puma worker @@ -17,11 +17,15 @@ gem "puma-plugin-telemetry" And then execute: - $ bundle install +```console +$ bundle install +``` Or install it yourself as: - $ gem install puma-plugin-telemetry +```console +$ gem install puma-plugin-telemetry +``` ## Usage @@ -39,15 +43,15 @@ end ### Basic -Output telemetry as JSON to STDOUT +Output telemetry as JSON to `STDOUT` ```ruby config.add_target :io ``` -### Datadog statsd target +### Datadog StatsD target -Given gem provides built in target for Datadog Statsd client, that uses batch operation to publish metrics. +Given gem provides built in target for Datadog StatsD client, that uses batch operation to publish metrics. **NOTE** Be sure to have `dogstatsd` gem installed. @@ -76,7 +80,7 @@ end ### Custom Targets -Target is a simple object that implements `call` methods that accepts `telemetry` hash object. This means it can be super simple `proc` or some sofisticated class calling some external API. +Target is a simple object that implements `call` methods that accepts `telemetry` hash object. This means it can be super simple `proc` or some sophisticated class calling some external API. Just be mindful that if the API takes long to call, it will slow down frequency with which telemetry will get reported. @@ -87,7 +91,7 @@ Just be mindful that if the API takes long to call, it will slow down frequency ## Extra middleware -This gems comes together with middleware for measuring request queue time, which will be reported in `request.env` and published to given statsd client. +This gems comes together with middleware for measuring request queue time, which will be reported in `request.env` and published to given StatsD client. Example configuration: @@ -110,7 +114,7 @@ This will provide proper metric in Datadog and in logs as well. Logs can be tran ## Development -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. +After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. diff --git a/puma-plugin-telemetry.gemspec b/puma-plugin-telemetry.gemspec index a9dfc68..a930dd4 100644 --- a/puma-plugin-telemetry.gemspec +++ b/puma-plugin-telemetry.gemspec @@ -33,7 +33,10 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(File.expand_path(__dir__)) do - `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } + `git ls-files -z`.split("\x0").reject do |f| + (File.expand_path(f) == __FILE__) || + f.start_with?(*%w[bin/ spec/ .git .github Gemfile]) + end end spec.bindir = 'exe' spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } diff --git a/spec/integration/plugin_spec.rb b/spec/integration/plugin_spec.rb index 207e701..81628c3 100644 --- a/spec/integration/plugin_spec.rb +++ b/spec/integration/plugin_spec.rb @@ -50,12 +50,12 @@ class Plugin end it 'executes the first target' do - true while (line = @server.next_line) !~ /target=01/ + true until (line = @server.next_line).include?('target=01') expect(line).to start_with "target=01 telemetry=#{expected_telemetry.inspect}" end it 'executes the second target' do - true while (line = @server.next_line) !~ /target=02/ + true until (line = @server.next_line).include?('target=02') expect(line).to start_with "target=02 telemetry=#{expected_telemetry.inspect}" end end @@ -67,7 +67,7 @@ class Plugin end it 'logs only selected telemetry' do - true while (line = @server.next_line) !~ /Puma::Plugin::Telemetry/ + true until (line = @server.next_line).include?('Puma::Plugin::Telemetry') expect(line).to start_with expected_telemetry end end @@ -87,7 +87,7 @@ class Plugin end it "doesn't crash" do - true while (line = @server.next_line) !~ /DEBUG -- : Statsd/ + true until (line = @server.next_line).include?('DEBUG -- : Statsd') lines = ([line.slice(/workers.*/)] + Array.new(6) { @server.next_line.strip }) diff --git a/spec/support/server.rb b/spec/support/server.rb index 8a1bdbb..d50f35d 100644 --- a/spec/support/server.rb +++ b/spec/support/server.rb @@ -12,10 +12,10 @@ def start @server = IO.popen("BIND_PATH=#{bind_path} bundle exec puma -C spec/fixtures/#{@config}.rb -v --debug", 'r') @server_pid = @server.pid - true while next_line !~ /PID:\ / + true until next_line.include?('PID: ') @puma_pid = @lines.last.split(': ').last.to_i - true while next_line !~ /Ctrl-C/ + true until next_line.include?('Ctrl-C') end def stop