From ca0eaf416e414e17600e2e11592607e8d14abbcd Mon Sep 17 00:00:00 2001 From: Salahutdinov Dmitry Date: Mon, 1 Apr 2019 23:13:59 +0500 Subject: [PATCH] Rename to yabeda-puma-plugin, because yabeda-puma has already taken --- README.md | 2 +- lib/puma/plugin/yabeda.rb | 12 +++---- lib/yabeda/puma.rb | 10 ------ lib/yabeda/puma/plugin.rb | 12 +++++++ lib/yabeda/puma/plugin/statistics.rb | 10 ++++++ lib/yabeda/puma/plugin/statistics/fetcher.rb | 21 +++++++++++ lib/yabeda/puma/plugin/statistics/parser.rb | 36 +++++++++++++++++++ lib/yabeda/puma/plugin/version.rb | 7 ++++ lib/yabeda/puma/statistics.rb | 8 ----- lib/yabeda/puma/statistics/fetcher.rb | 19 ---------- lib/yabeda/puma/statistics/parser.rb | 34 ------------------ lib/yabeda/puma/version.rb | 5 --- spec/integration/clustered_spec.rb | 7 +--- spec/spec_helper.rb | 3 +- .../{ => plugin}/statistics/parser_spec.rb | 4 +-- spec/yabeda/puma/plugin_spec.rb | 7 ++++ spec/yabeda/puma/puma_spec.rb | 7 ---- ...puma.gemspec => yabeda-puma-plugin.gemspec | 11 +++--- 18 files changed, 109 insertions(+), 106 deletions(-) delete mode 100644 lib/yabeda/puma.rb create mode 100644 lib/yabeda/puma/plugin.rb create mode 100644 lib/yabeda/puma/plugin/statistics.rb create mode 100644 lib/yabeda/puma/plugin/statistics/fetcher.rb create mode 100644 lib/yabeda/puma/plugin/statistics/parser.rb create mode 100644 lib/yabeda/puma/plugin/version.rb delete mode 100644 lib/yabeda/puma/statistics.rb delete mode 100644 lib/yabeda/puma/statistics/fetcher.rb delete mode 100644 lib/yabeda/puma/statistics/parser.rb delete mode 100644 lib/yabeda/puma/version.rb rename spec/yabeda/puma/{ => plugin}/statistics/parser_spec.rb (95%) create mode 100644 spec/yabeda/puma/plugin_spec.rb delete mode 100644 spec/yabeda/puma/puma_spec.rb rename yabeda-puma.gemspec => yabeda-puma-plugin.gemspec (75%) diff --git a/README.md b/README.md index 096832f..c10b707 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ alt="Sponsored by Amplifr" src="https://amplifr-direct.s3-eu-west-1.amazonaws.com/social_images/image/37b580d9-3668-4005-8d5a-137de3a3e77c.png" /> -# Yabeda::Puma +# Yabeda::Puma::Plugin Built-in metrics for [Puma](https://github.com/puma/puma) web server monitoring out of the box! Part of the [yabeda](https://github.com/yabeda-rb/yabeda) suite. diff --git a/lib/puma/plugin/yabeda.rb b/lib/puma/plugin/yabeda.rb index 5b2985f..f2e6b9f 100644 --- a/lib/puma/plugin/yabeda.rb +++ b/lib/puma/plugin/yabeda.rb @@ -1,4 +1,4 @@ -require 'yabeda/puma.rb' +require 'yabeda/puma/plugin.rb' Puma::Plugin.create do def start(launcher) @@ -7,7 +7,7 @@ def start(launcher) control_url = launcher.options[:control_url] raise StandardError, "Puma control app is not activated" if control_url == nil - Yabeda::Puma.tap do |puma| + Yabeda::Puma::Plugin.tap do |puma| puma.control_url = control_url puma.control_auth_token = launcher.options[:control_auth_token] end @@ -27,10 +27,10 @@ def start(launcher) end collect do - require 'yabeda/puma/statistics/fetcher' - stats = Yabeda::Puma::Statistics::Fetcher.call - require 'yabeda/puma/statistics/parser' - Yabeda::Puma::Statistics::Parser.new(clustered: clustered, data: stats).call.each do |item| + require 'yabeda/puma/plugin/statistics/fetcher' + stats = Yabeda::Puma::Plugin::Statistics::Fetcher.call + require 'yabeda/puma/plugin/statistics/parser' + Yabeda::Puma::Plugin::Statistics::Parser.new(clustered: clustered, data: stats).call.each do |item| send("puma_#{item[:name]}").set(item[:labels], item[:value]) end end diff --git a/lib/yabeda/puma.rb b/lib/yabeda/puma.rb deleted file mode 100644 index 5e48a47..0000000 --- a/lib/yabeda/puma.rb +++ /dev/null @@ -1,10 +0,0 @@ -require "yabeda/puma/version" -require 'yabeda' - -module Yabeda - module Puma - class << self - attr_accessor :control_url, :control_auth_token - end - end -end diff --git a/lib/yabeda/puma/plugin.rb b/lib/yabeda/puma/plugin.rb new file mode 100644 index 0000000..fa62e75 --- /dev/null +++ b/lib/yabeda/puma/plugin.rb @@ -0,0 +1,12 @@ +require "yabeda/puma/plugin/version" +require 'yabeda' + +module Yabeda + module Puma + module Plugin + class << self + attr_accessor :control_url, :control_auth_token + end + end + end +end diff --git a/lib/yabeda/puma/plugin/statistics.rb b/lib/yabeda/puma/plugin/statistics.rb new file mode 100644 index 0000000..7198754 --- /dev/null +++ b/lib/yabeda/puma/plugin/statistics.rb @@ -0,0 +1,10 @@ +module Yabeda + module Puma + module Plugin + module Statistics + METRICS = [:backlog, :running, :pool_capacity, :max_threads, :workers] + CLUSTERED_METRICS = [:booted_workers, :old_workers] + end + end + end +end diff --git a/lib/yabeda/puma/plugin/statistics/fetcher.rb b/lib/yabeda/puma/plugin/statistics/fetcher.rb new file mode 100644 index 0000000..0a90f16 --- /dev/null +++ b/lib/yabeda/puma/plugin/statistics/fetcher.rb @@ -0,0 +1,21 @@ +require 'yabeda/puma/plugin/statistics' +require 'json' + +module Yabeda + module Puma + module Plugin + module Statistics + class Fetcher + def self.call + body = Socket.unix(Yabeda::Puma::Plugin.control_url.gsub("unix://", '')) do |socket| + socket << "GET /stats?token=#{Yabeda::Puma::Plugin.control_auth_token} HTTP/1.0\r\n\r\n" + socket.read + end + + JSON.parse(body.split("\n").last) + end + end + end + end + end +end diff --git a/lib/yabeda/puma/plugin/statistics/parser.rb b/lib/yabeda/puma/plugin/statistics/parser.rb new file mode 100644 index 0000000..92fffd9 --- /dev/null +++ b/lib/yabeda/puma/plugin/statistics/parser.rb @@ -0,0 +1,36 @@ +require 'yabeda/puma/plugin/statistics' + +module Yabeda + module Puma + module Plugin + module Statistics + class Parser + attr_reader :clustered, :data + + def initialize(clustered:, data:) + @clustered = clustered + @data = data + end + + def call + Array.new.tap { |result| parse(data, result) } + end + + private + + def parse(stats, labels = {}, result) + stats.each do |key, value| + value.each { |s| parse(s, labels.merge(index: s['index']), result) } if key == 'worker_status' + parse(value, labels, result) if key == 'last_status' + result << {name: key, value: value, labels: labels} if metric?(key) + end + end + + def metric?(name) + Statistics::METRICS.include?(name.to_sym) || (Statistics::CLUSTERED_METRICS.include?(name.to_sym) && clustered) + end + end + end + end + end +end diff --git a/lib/yabeda/puma/plugin/version.rb b/lib/yabeda/puma/plugin/version.rb new file mode 100644 index 0000000..315b6ba --- /dev/null +++ b/lib/yabeda/puma/plugin/version.rb @@ -0,0 +1,7 @@ +module Yabeda + module Puma + module Plugin + VERSION = "0.1.0" + end + end +end diff --git a/lib/yabeda/puma/statistics.rb b/lib/yabeda/puma/statistics.rb deleted file mode 100644 index ebca03b..0000000 --- a/lib/yabeda/puma/statistics.rb +++ /dev/null @@ -1,8 +0,0 @@ -module Yabeda - module Puma - module Statistics - METRICS = [:backlog, :running, :pool_capacity, :max_threads, :workers] - CLUSTERED_METRICS = [:booted_workers, :old_workers] - end - end -end diff --git a/lib/yabeda/puma/statistics/fetcher.rb b/lib/yabeda/puma/statistics/fetcher.rb deleted file mode 100644 index f37511a..0000000 --- a/lib/yabeda/puma/statistics/fetcher.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'yabeda/puma/statistics' -require 'json' - -module Yabeda - module Puma - module Statistics - class Fetcher - def self.call - body = Socket.unix(Yabeda::Puma.control_url.gsub("unix://", '')) do |socket| - socket << "GET /stats?token=#{Yabeda::Puma.control_auth_token} HTTP/1.0\r\n\r\n" - socket.read - end - - JSON.parse(body.split("\n").last) - end - end - end - end -end diff --git a/lib/yabeda/puma/statistics/parser.rb b/lib/yabeda/puma/statistics/parser.rb deleted file mode 100644 index 6f6065b..0000000 --- a/lib/yabeda/puma/statistics/parser.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'yabeda/puma/statistics' - -module Yabeda - module Puma - module Statistics - class Parser - attr_reader :clustered, :data - - def initialize(clustered:, data:) - @clustered = clustered - @data = data - end - - def call - Array.new.tap { |result| parse(data, result) } - end - - private - - def parse(stats, labels = {}, result) - stats.each do |key, value| - value.each { |s| parse(s, labels.merge(index: s['index']), result) } if key == 'worker_status' - parse(value, labels, result) if key == 'last_status' - result << {name: key, value: value, labels: labels} if metric?(key) - end - end - - def metric?(name) - Statistics::METRICS.include?(name.to_sym) || (Statistics::CLUSTERED_METRICS.include?(name.to_sym) && clustered) - end - end - end - end -end diff --git a/lib/yabeda/puma/version.rb b/lib/yabeda/puma/version.rb deleted file mode 100644 index b80e5ec..0000000 --- a/lib/yabeda/puma/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Yabeda - module Puma - VERSION = "0.1.0" - end -end diff --git a/spec/integration/clustered_spec.rb b/spec/integration/clustered_spec.rb index e9285c3..e7a21ed 100644 --- a/spec/integration/clustered_spec.rb +++ b/spec/integration/clustered_spec.rb @@ -6,13 +6,8 @@ require "puma/detect" require "puma/cli" require 'rack' -require 'byebug' -RSpec.describe Yabeda::Puma do -# it "has a version number" do -# expect(Yabeda::Puma::VERSION).not_to be nil -# end - # +RSpec.describe Yabeda::Puma::Plugin do def next_port(incr = 1) @next_port = 9000 if @next_port == nil @next_port += incr diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4f77e89..e44bd74 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,5 @@ require "bundler/setup" -require "yabeda/puma" -require 'byebug' +require "yabeda/puma/plugin" RSpec.configure do |config| # Enable flags like --only-failures and --next-failure diff --git a/spec/yabeda/puma/statistics/parser_spec.rb b/spec/yabeda/puma/plugin/statistics/parser_spec.rb similarity index 95% rename from spec/yabeda/puma/statistics/parser_spec.rb rename to spec/yabeda/puma/plugin/statistics/parser_spec.rb index 4f4c008..e83af17 100644 --- a/spec/yabeda/puma/statistics/parser_spec.rb +++ b/spec/yabeda/puma/plugin/statistics/parser_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' -require 'yabeda/puma/statistics/parser' +require 'yabeda/puma/plugin/statistics/parser' -RSpec.describe Yabeda::Puma::Statistics::Parser do +RSpec.describe Yabeda::Puma::Plugin::Statistics::Parser do describe '#parse' do subject(:statistics) { described_class.new(clustered: clustered, data: data).call } diff --git a/spec/yabeda/puma/plugin_spec.rb b/spec/yabeda/puma/plugin_spec.rb new file mode 100644 index 0000000..b59d38d --- /dev/null +++ b/spec/yabeda/puma/plugin_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +RSpec.describe Yabeda::Puma::Plugin do + it "has a version number" do + expect(Yabeda::Puma::Plugin::VERSION).not_to be nil + end +end diff --git a/spec/yabeda/puma/puma_spec.rb b/spec/yabeda/puma/puma_spec.rb deleted file mode 100644 index 0f7bcad..0000000 --- a/spec/yabeda/puma/puma_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'spec_helper' - -RSpec.describe Yabeda::Puma do - it "has a version number" do - expect(Yabeda::Puma::VERSION).not_to be nil - end -end diff --git a/yabeda-puma.gemspec b/yabeda-puma-plugin.gemspec similarity index 75% rename from yabeda-puma.gemspec rename to yabeda-puma-plugin.gemspec index 0366a2e..c0fb6d6 100644 --- a/yabeda-puma.gemspec +++ b/yabeda-puma-plugin.gemspec @@ -1,17 +1,17 @@ lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require "yabeda/puma/version" +require "yabeda/puma/plugin/version" Gem::Specification.new do |spec| - spec.name = "yabeda-puma" - spec.version = Yabeda::Puma::VERSION + spec.name = "yabeda-puma-plugin" + spec.version = Yabeda::Puma::Plugin::VERSION spec.authors = ["Salahutdinov Dmitry"] spec.email = ["dsalahutdinov@gmail.com"] - spec.summary = %q{Collecting metrics of the puma web server.} + spec.summary = %q{Puma web server plugin for collecting puma metrics with Yabeda framework.} spec.description = %q{Extends Yabeda metrics with puma web server values by using puma plugin} - spec.homepage = "http://github.com/yabeda-rb/yabeda-puma" + spec.homepage = "http://github.com/yabeda-rb/yabeda-puma-plugin" spec.license = "MIT" spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do @@ -28,6 +28,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" - spec.add_development_dependency "byebug" spec.add_development_dependency "rack" end