Add Hutch support (#43)

Add Hutch support (#43)

From 322d5729522941ba542d71d4bc3aafbb04a01d23 Mon Sep 17 00:00:00 2001
From: andrew morton <drewish@katherinehouse.com>
Date: Sun, 4 Nov 2018 19:54:07 -0700
Subject: [PATCH] Add Hutch support (#43)


diff --git a/README.md b/README.md
index 856fc42..7f12b05 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,7 @@ To learn more see [Instrumenting Rails with Prometheus](https://samsaffron.com/a
     * [Per-process stats](#per-process-stats)
     * [Sidekiq metrics](#sidekiq-metrics)
     * [Delayed Job plugin](#delayed-job-plugin)
+    * [Hutch metrics](#hutch-message-processing-tracer)
   * [Puma metrics](#puma-metrics)
   * [Custom type collectors](#custom-type-collectors)
   * [Multi process mode with custom collector](#multi-process-mode-with-custom-collector)
@@ -218,6 +219,17 @@ unless Rails.env == "test"
 end

+#### Hutch Message Processing Tracer
+
+Capture Hutch metrics (how many jobs ran? how many failed? how long did they take?)
+
+```ruby
+unless Rails.env == ‚Äútest‚ÄĚ

  • require ‚Äėprometheus_exporter/instrumentation‚Äô
  • Hutch::Config.set(:tracer, PrometheusExporter::Instrumentation::Hutch)
    +end
    +```

Instrumenting Request Queueing Time

Request Queueing is defined as the time it takes for a request to reach your application (instrumented by this prometheus_exporter) from farther upstream (as your load balancer). A high queueing time usually means that your backend cannot handle all the incoming requests in time, so they queue up (= you should see if you need to add more capacity).
diff --git a/lib/prometheus_exporter/instrumentation.rb b/lib/prometheus_exporter/instrumentation.rb
index 285517f…e032422 100644
‚ÄĒ a/lib/prometheus_exporter/instrumentation.rb
+++ b/lib/prometheus_exporter/instrumentation.rb
@@ -4,3 +4,4 @@ require_relative ‚Äúinstrumentation/sidekiq‚ÄĚ
require_relative ‚Äúinstrumentation/delayed_job‚ÄĚ
require_relative ‚Äúinstrumentation/global‚ÄĚ
require_relative ‚Äúinstrumentation/puma‚ÄĚ
+require_relative ‚Äúinstrumentation/hutch‚ÄĚ
diff --git a/lib/prometheus_exporter/instrumentation/hutch.rb b/lib/prometheus_exporter/instrumentation/hutch.rb
new file mode 100644
index 0000000…8f8240f
‚ÄĒ /dev/null
+++ b/lib/prometheus_exporter/instrumentation/hutch.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+module PrometheusExporter::Instrumentation

  • class Hutch

  • def initialize(klass)

  •  @klass = klass
    
  •  @client = PrometheusExporter::Client.default
    
  • end

  • def handle(message)

  •  success = false
    
  •  start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
    
  •  result = @klass.process(message)
    
  •  success = true
    
  •  result
    
  • ensure

  •  duration = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start
    
  •  @client.send_json(
    
  •    type: "hutch",
    
  •    name: @klass.class.to_s,
    
  •    success: success,
    
  •    duration: duration
    
  •  )
    
  • end

  • end
    +end
    diff --git a/lib/prometheus_exporter/server.rb b/lib/prometheus_exporter/server.rb
    index e564007…48b03a4 100644
    ‚ÄĒ a/lib/prometheus_exporter/server.rb
    +++ b/lib/prometheus_exporter/server.rb
    @@ -9,3 +9,4 @@ require_relative ‚Äúserver/collector‚ÄĚ
    require_relative ‚Äúserver/web_server‚ÄĚ
    require_relative ‚Äúserver/runner‚ÄĚ
    require_relative ‚Äúserver/puma_collector‚ÄĚ
    +require_relative ‚Äúserver/hutch_collector‚ÄĚ
    diff --git a/lib/prometheus_exporter/server/collector.rb b/lib/prometheus_exporter/server/collector.rb
    index d251e4a…37b3b85 100644
    ‚ÄĒ a/lib/prometheus_exporter/server/collector.rb
    +++ b/lib/prometheus_exporter/server/collector.rb
    @@ -31,6 +31,7 @@ module PrometheusExporter::Server
    register_collector(SidekiqCollector.new)
    register_collector(DelayedJobCollector.new)
    register_collector(PumaCollector.new)

  •  register_collector(HutchCollector.new)
    

    end

    def register_collector(collector)
    diff --git a/lib/prometheus_exporter/server/hutch_collector.rb b/lib/prometheus_exporter/server/hutch_collector.rb
    new file mode 100644
    index 0000000…e4c8eef
    ‚ÄĒ /dev/null
    +++ b/lib/prometheus_exporter/server/hutch_collector.rb
    @@ -0,0 +1,43 @@
    +module PrometheusExporter::Server

  • class HutchCollector < TypeCollector

  • def type

  •  "hutch"
    
  • end

  • def collect(obj)

  •  default_labels = { job_name: obj['name'] }
    
  •  custom_labels = obj['custom_labels']
    
  •  labels = custom_labels.nil? ? default_labels : default_labels.merge(custom_labels)
    
  •  ensure_hutch_metrics
    
  •  @hutch_job_duration_seconds.observe(obj["duration"], labels)
    
  •  @hutch_jobs_total.observe(1, labels)
    
  •  @hutch_failed_jobs_total.observe(1, labels) if !obj["success"]
    
  • end

  • def metrics

  •  if @hutch_jobs_total
    
  •    [@hutch_job_duration_seconds, @hutch_jobs_total, @hutch_failed_jobs_total]
    
  •  else
    
  •    []
    
  •  end
    
  • end

  • protected

  • def ensure_hutch_metrics

  •  if !@hutch_jobs_total
    
  •    @hutch_job_duration_seconds = PrometheusExporter::Metric::Counter.new(
    
  •      "hutch_job_duration_seconds", "Total time spent in hutch jobs.")
    
  •    @hutch_jobs_total = PrometheusExporter::Metric::Counter.new(
    
  •      "hutch_jobs_total", "Total number of hutch jobs executed.")
    
  •    @hutch_failed_jobs_total = PrometheusExporter::Metric::Counter.new(
    
  •      "hutch_failed_jobs_total", "Total number failed hutch jobs executed.")
    
  •  end
    
  • end

  • end
    +end

[<small>GitHub</small>](https://github.com/discourse/prometheus_exporter/commit/322d5729522941ba542d71d4bc3aafbb04a01d23)