# HG changeset patch
# User Ben Kochie <bjk@gitlab.com>
# Date 1550164907 -3600
#      Thu Feb 14 18:21:47 2019 +0100
# Node ID b4a489dc91929404257235292237f9127586e9fa
# Parent  f7e0b6f801364c2dc3b1b5f32bdd7d2b6428557d
Refactor Prometheus rails scrape config

* Rename `gitlab-unicorn` to `gitlab-rails`.
* Collect metrics from Puma if it's enabled instead of Unicorn.

diff --git a/changelogs/unreleased/3046-refactor-rails-monitoring.yml b/changelogs/unreleased/3046-refactor-rails-monitoring.yml
new file mode 100644
--- /dev/null
+++ b/changelogs/unreleased/3046-refactor-rails-monitoring.yml
@@ -0,0 +1,5 @@
+---
+title: Refactor Prometheus rails scrape config
+merge_request: 3046
+author: Ben Kochie <bjk@gitlab.com>
+type: added
diff --git a/files/gitlab-cookbooks/gitlab/libraries/prometheus.rb b/files/gitlab-cookbooks/gitlab/libraries/prometheus.rb
--- a/files/gitlab-cookbooks/gitlab/libraries/prometheus.rb
+++ b/files/gitlab-cookbooks/gitlab/libraries/prometheus.rb
@@ -220,7 +220,7 @@
       gitlab_monitor_scrape_configs
       registry_scrape_config
       sidekiq_scrape_config
-      unicorn_scrape_configs
+      rails_scrape_configs
       workhorse_scrape_config
       exporter_scrape_config('node')
       exporter_scrape_config('postgres')
@@ -349,19 +349,24 @@
       Gitlab['prometheus']['scrape_configs'] = default_scrape_configs.compact.flatten
     end
 
-    def unicorn_scrape_configs
-      # Don't parse if unicorn is explicitly disabled
-      return unless Services.enabled?('unicorn')
-
-      default_config = Gitlab['node']['gitlab']['unicorn'].to_hash
-      user_config = Gitlab['unicorn']
+    def rails_scrape_configs
+      if Services.enabled?('unicorn')
+        default_config = Gitlab['node']['gitlab']['unicorn'].to_hash
+        user_config = Gitlab['unicorn']
+      elsif Services.enabled?('puma')
+        default_config = Gitlab['node']['gitlab']['puma'].to_hash
+        user_config = Gitlab['puma']
+      else
+        # Don't add scrape config if puma and unicorn explicitly disabled
+        return
+      end
 
       listen_address = user_config['listen'] || default_config['listen']
       listen_port = user_config['port'] || default_config['port']
       prometheus_target = [listen_address, listen_port].join(':')
 
       scrape_config = {
-        'job_name' => 'gitlab-unicorn',
+        'job_name' => 'gitlab-rails',
         'metrics_path' => '/-/metrics',
         'static_configs' => [
           'targets' => [prometheus_target],
diff --git a/spec/chef/recipes/prometheus_spec.rb b/spec/chef/recipes/prometheus_spec.rb
--- a/spec/chef/recipes/prometheus_spec.rb
+++ b/spec/chef/recipes/prometheus_spec.rb
@@ -34,7 +34,7 @@
     static_configs:
     - targets:
       - localhost:9229
-  - job_name: gitlab-unicorn
+  - job_name: gitlab-rails
     metrics_path: "/-/metrics"
     static_configs:
     - targets:
@@ -248,6 +248,26 @@
     end
   end
 
+  context 'with puma' do
+    context 'with user provided settings' do
+      before do
+        stub_gitlab_rb(
+          puma: {
+            enable: 'true'
+          },
+          unicorn: {
+            enable: 'false'
+          }
+        )
+      end
+
+      it 'configures puma job' do
+        expect(chef_run).to render_file('/var/opt/gitlab/prometheus/prometheus.yml')
+          .with_content(%r{- job_name: gitlab-rails\s+metrics_path: "/-/metrics"\s+static_configs:\s+- targets:\s+- 127.0.0.1:8080})
+      end
+    end
+  end
+
   context 'with Prometheus version 1' do
     before { allow(PrometheusHelper).to receive(:is_version_1?).and_return(true) }
     context 'with user provided settings' do