mirror of
https://github.com/cmur2/dyndnsd.git
synced 2026-11-14 12:55:58 +01:00
hitime is only needed for Metriks::Timer and requires native extensions. Those do not compile with default settings under Ubuntu 26.04 or Alpine 3.24.
63 lines
1.4 KiB
Ruby
63 lines
1.4 KiB
Ruby
# Vendored from metriks 0.9.9.8 — https://github.com/eric/metriks
|
|
# Copyright (c) 2012 Eric Lindvall, MIT licensed.
|
|
# Trimmed to the subset dyndnsd uses (meters + registry + reporters).
|
|
|
|
module Metriks::Reporter
|
|
class ProcTitle
|
|
def initialize(options = {})
|
|
@rounding = options[:rounding] || 1
|
|
@prefix = options[:prefix] || $0.dup
|
|
|
|
@interval = options[:interval] || 5
|
|
@on_error = options[:on_error] || proc { |ex| }
|
|
|
|
@metrics = []
|
|
end
|
|
|
|
def add(name, suffix = nil, &block)
|
|
@metrics << [ name, suffix, block ]
|
|
end
|
|
|
|
def empty?
|
|
@metrics.empty?
|
|
end
|
|
|
|
def start
|
|
@thread ||= Thread.new do
|
|
loop do
|
|
begin
|
|
unless @metrics.empty?
|
|
title = generate_title
|
|
if title && !title.empty?
|
|
$0 = "#{@prefix} #{title}"
|
|
end
|
|
end
|
|
rescue Exception => ex
|
|
@on_error[ex] rescue nil
|
|
end
|
|
sleep @interval
|
|
end
|
|
end
|
|
end
|
|
|
|
def stop
|
|
@thread.kill if @thread
|
|
@thread = nil
|
|
end
|
|
|
|
def restart
|
|
stop
|
|
start
|
|
end
|
|
|
|
protected
|
|
def generate_title
|
|
@metrics.collect do |name, suffix, block|
|
|
val = block.call
|
|
val = "%.#{@rounding}f" % val if val.is_a?(Float)
|
|
|
|
"#{name}: #{val}#{suffix}"
|
|
end.join(' ')
|
|
end
|
|
end
|
|
end |