lib/splash/commands.rb in prometheus-splash-0.1.1 vs lib/splash/commands.rb in prometheus-splash-0.2.0

- old
+ new

@@ -5,22 +5,24 @@ include Splash::Config include Splash::Helpers include Splash::Backends include Splash::Exiter + + @@registry = Prometheus::Client.registry + @@metric_exitcode = Prometheus::Client::Gauge.new(:errorcode, docstring: 'SPLASH metric batch errorcode') + @@metric_time = Prometheus::Client::Gauge.new(:exectime, docstring: 'SPLASH metric batch execution time') + @@registry.register(@@metric_exitcode) + @@registry.register(@@metric_time) + def initialize(name) @config = get_config + @url = "http://#{@config.prometheus_pushgateway_host}:#{@config.prometheus_pushgateway_port}" @name = name unless @config.commands.keys.include? @name.to_sym then splash_exit case: :not_found, more: "command #{@name} is not defined in configuration" end - @registry = Prometheus::Client.registry - @url = "http://#{@config.prometheus_pushgateway_host}:#{@config.prometheus_pushgateway_port}" - @metric_exitcode = Prometheus::Client::Gauge.new(:errorcode, docstring: 'SPLASH metric batch errorcode') - @metric_time = Prometheus::Client::Gauge.new(:exectime, docstring: 'SPLASH metric batch execution time') - @registry.register(@metric_exitcode) - @registry.register(@metric_time) end def ack puts "Sending ack for command : '#{@name}'" notify(0,0) @@ -28,14 +30,14 @@ def notify(value,time) unless verify_service host: @config.prometheus_pushgateway_host ,port: @config.prometheus_pushgateway_port then return { :case => :service_dependence_missing, :more => "Prometheus Notification not send."} end - @metric_exitcode.set(value) - @metric_time.set(time) + @@metric_exitcode.set(value) + @@metric_time.set(time) hostname = Socket.gethostname - Prometheus::Client::Push.new(@name, hostname, @url).add(@registry) + Prometheus::Client::Push.new(@name, hostname, @url).add(@@registry) puts " * Prometheus Gateway notified." return { :case => :quiet_exit} end @@ -64,24 +66,23 @@ end time = Time.now - start tp = Template::new( list_token: @config.execution_template_tokens, template_file: @config.execution_template_path) - - tp.start_date = start_date - tp.end_date = DateTime.now.to_s - tp.cmd_name = @name - tp.cmd_line = @config.commands[@name.to_sym][:command] - tp.desc = @config.commands[@name.to_sym][:desc] - tp.status = status.to_s - tp.stdout = stdout - tp.stderr = stderr - tp.exec_time = time.to_s + data = Hash::new + data[:start_date] = start_date + data[:end_date] = DateTime.now.to_s + data[:cmd_name] = @name + data[:cmd_line] = @config.commands[@name.to_sym][:command] + data[:desc] = @config.commands[@name.to_sym][:desc] + data[:status] = status.to_s + data[:stdout] = stdout + data[:stderr] = stderr + data[:exec_time] = time.to_s backend = get_backend :execution_trace key = @name - backend.put key: key, value: tp.output + backend.put key: key, value: data.to_yaml exit_code = status.exitstatus - end puts " => exitcode #{exit_code}" if options[:notify] then acase = notify(exit_code,time.to_i)