From dbb07b6c1c422e8a2caa6add84484db5dcc044c3 Mon Sep 17 00:00:00 2001 From: Andrew Le Date: Tue, 31 Dec 2019 11:06:34 -0800 Subject: [PATCH 1/2] Convert to stdout output for librato stats collection --- lib/puma/plugin/statsd.rb | 43 ++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/lib/puma/plugin/statsd.rb b/lib/puma/plugin/statsd.rb index 66d73b3..7306b92 100644 --- a/lib/puma/plugin/statsd.rb +++ b/lib/puma/plugin/statsd.rb @@ -5,31 +5,22 @@ require 'socket' class StatsdConnector - ENV_NAME = "STATSD_HOST" - STATSD_TYPES = { count: 'c', gauge: 'g' } - - attr_reader :host, :port - def initialize - @host = ENV.fetch(ENV_NAME, nil) - @port = ENV.fetch("STATSD_PORT", 8125) + @data = [] end def enabled? - !!host + true end def send(metric_name:, value:, type:, tags: {}) - data = "#{metric_name}:#{value}|#{STATSD_TYPES.fetch(type)}" - if tags.any? - tag_str = tags.map { |k,v| "#{k}:#{v}" }.join(",") - data = "#{data}|##{tag_str}" - end + @data << "#{type}##{metric_name}=#{value}" + end - socket = UDPSocket.new - socket.send(data, 0, host, port) - ensure - socket.close + def submit(&block) + block.call(self) + STDOUT.puts @data.join(" ") + @data.clear end end @@ -96,7 +87,7 @@ def start(launcher) @statsd = ::StatsdConnector.new if @statsd.enabled? - @launcher.events.debug "statsd: enabled (host: #{@statsd.host})" + @launcher.events.debug "statsd: enabled" register_hooks else @launcher.events.debug "statsd: not enabled (no #{StatsdConnector::ENV_NAME} env var found)" @@ -126,17 +117,19 @@ def tags # Send data to statsd every few seconds def stats_loop - sleep 5 + sleep 30 loop do @launcher.events.debug "statsd: notify statsd" begin stats = ::PumaStats.new(fetch_stats) - @statsd.send(metric_name: "puma.workers", value: stats.workers, type: :gauge, tags: tags) - @statsd.send(metric_name: "puma.booted_workers", value: stats.booted_workers, type: :gauge, tags: tags) - @statsd.send(metric_name: "puma.running", value: stats.running, type: :gauge, tags: tags) - @statsd.send(metric_name: "puma.backlog", value: stats.backlog, type: :gauge, tags: tags) - @statsd.send(metric_name: "puma.pool_capacity", value: stats.pool_capacity, type: :gauge, tags: tags) - @statsd.send(metric_name: "puma.max_threads", value: stats.max_threads, type: :gauge, tags: tags) + @statsd.submit do |s| + s.send(metric_name: "puma.workers", value: stats.workers, type: :measure, tags: tags) + s.send(metric_name: "puma.booted_workers", value: stats.booted_workers, type: :measure, tags: tags) + s.send(metric_name: "puma.running", value: stats.running, type: :measure, tags: tags) + s.send(metric_name: "puma.backlog", value: stats.backlog, type: :measure, tags: tags) + s.send(metric_name: "puma.pool_capacity", value: stats.pool_capacity, type: :measure, tags: tags) + s.send(metric_name: "puma.max_threads", value: stats.max_threads, type: :measure, tags: tags) + end rescue StandardError => e @launcher.events.error "! statsd: notify stats failed:\n #{e.to_s}\n #{e.backtrace.join("\n ")}" ensure From f579e73c1c6eb187a2fdec468b752a90fc7d0bd6 Mon Sep 17 00:00:00 2001 From: Andrew Le Date: Tue, 31 Dec 2019 11:17:17 -0800 Subject: [PATCH 2/2] Change reporting interval --- lib/puma/plugin/statsd.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/puma/plugin/statsd.rb b/lib/puma/plugin/statsd.rb index 7306b92..75ea8f2 100644 --- a/lib/puma/plugin/statsd.rb +++ b/lib/puma/plugin/statsd.rb @@ -117,7 +117,7 @@ def tags # Send data to statsd every few seconds def stats_loop - sleep 30 + sleep 5 loop do @launcher.events.debug "statsd: notify statsd" begin @@ -133,7 +133,7 @@ def stats_loop rescue StandardError => e @launcher.events.error "! statsd: notify stats failed:\n #{e.to_s}\n #{e.backtrace.join("\n ")}" ensure - sleep 2 + sleep 30 end end end