FIX: avoid shelling to get hostname

FIX: avoid shelling to get hostname

Previously we were using hostname to get hostname, this is slow and risks orphan processes.

This swaps it with Socket.gethostname which is both faster and more reliable.

We also now cache it on the PrometheusExporter instance

diff --git a/CHANGELOG b/CHANGELOG
index f850ec9..fb396b0 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
-0.5.0 - pending
+0.5.0 - 14-02-2019
 
 - Breaking change: listen only to localhost by default to prevent unintended insecure configuration
+- FIX: Avoid calling `hostname` aggressively, instead cache it on the exporter instance
 
 0.4.17 - 13-01-2019
 
diff --git a/lib/prometheus_exporter.rb b/lib/prometheus_exporter.rb
index ea8b628..ddcf7eb 100644
--- a/lib/prometheus_exporter.rb
+++ b/lib/prometheus_exporter.rb
@@ -20,6 +20,17 @@ module PrometheusExporter
     end
   end
 
+  def self.hostname
+    @hostname ||=
+      begin
+        require 'socket'
+        Socket.gethostname
+      rescue => e
+        STDERR.puts "Unable to lookup hostname #{e}"
+        "unknown-host"
+      end
+  end
+
   def self.detect_json_serializer(preferred)
     if preferred.nil?
       preferred = :oj if has_oj?
diff --git a/lib/prometheus_exporter/instrumentation/active_record.rb b/lib/prometheus_exporter/instrumentation/active_record.rb
index de59020..174f9a3 100644
--- a/lib/prometheus_exporter/instrumentation/active_record.rb
+++ b/lib/prometheus_exporter/instrumentation/active_record.rb
@@ -51,17 +51,6 @@ module PrometheusExporter::Instrumentation
     def initialize(metric_labels, config_labels)
       @metric_labels = metric_labels
       @config_labels = config_labels
-      @hostname = nil
-    end
-
-    def hostname
-      @hostname ||=
-        begin
-          `hostname`.strip
-        rescue => e
-          STDERR.puts "Unable to lookup hostname #{e}"
-          "unknown-host"
-        end
     end
 
     def collect
@@ -87,7 +76,7 @@ module PrometheusExporter::Instrumentation
         metric = {
           pid: pid,
           type: "active_record",
-          hostname: hostname,
+          hostname: ::PrometheusExporter.hostname,
           metric_labels: labels
         }
         metric.merge!(pool.stat)
diff --git a/lib/prometheus_exporter/instrumentation/process.rb b/lib/prometheus_exporter/instrumentation/process.rb
index 3d79002..bf891e3 100644
--- a/lib/prometheus_exporter/instrumentation/process.rb
+++ b/lib/prometheus_exporter/instrumentation/process.rb
@@ -42,24 +42,13 @@ module PrometheusExporter::Instrumentation
 
     def initialize(metric_labels)
       @metric_labels = metric_labels
-      @hostname = nil
-    end
-
-    def hostname
-      @hostname ||=
-        begin
-          `hostname`.strip
-        rescue => e
-          STDERR.puts "Unable to lookup hostname #{e}"
-          "unknown-host"
-        end
     end
 
     def collect
       metric = {}
       metric[:type] = "process"
       metric[:metric_labels] = @metric_labels
-      metric[:hostname] = hostname
+      metric[:hostname] = ::PrometheusExporter.hostname
       collect_gc_stats(metric)
       collect_v8_stats(metric)
       collect_process_stats(metric)
diff --git a/test/prometheus_exporter_test.rb b/test/prometheus_exporter_test.rb
index f1c42a2..7638b5c 100644
--- a/test/prometheus_exporter_test.rb
+++ b/test/prometheus_exporter_test.rb
@@ -6,4 +6,8 @@ class PrometheusExporterTest < Minitest::Test
   def test_that_it_has_a_version_number
     refute_nil ::PrometheusExporter::VERSION
   end
+
+  def test_it_can_get_hostname
+    assert_equal `hostname`.strip, ::PrometheusExporter.hostname
+  end
 end

GitHub sha: 45ff6535

1 Like