use the opts pattern for custom quantiles too (#42)

use the opts pattern for custom quantiles too (#42)

From 86ffa70c9831c0ef36894421c684b10b12b8e0f3 Mon Sep 17 00:00:00 2001
From: Jonathan Stern <jonathan.a.stern@gmail.com>
Date: Mon, 22 Oct 2018 16:01:20 -0500
Subject: [PATCH] use the opts pattern for custom quantiles too (#42)


diff --git a/README.md b/README.md
index 06864f3..856fc42 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,7 @@ To learn more see [Instrumenting Rails with Prometheus](https://samsaffron.com/a
 * [Installation](#installation)
 * [Usage](#usage)
   * [Single process mode](#single-process-mode)
+    * [Custom quantiles and buckets](#custom-quantiles-and-buckets)
   * [Multi process mode](#multi-process-mode)
   * [Rails integration](#rails-integration)
     * [Per-process stats](#per-process-stats)
@@ -90,7 +91,7 @@ You can also choose custom quantiles for summaries and custom buckets for histog
 
 ```ruby
 
-summary = PrometheusExporter::Metric::Summary.new("load_time", "time to load page", [0.99, 0.75, 0.5, 0.25])
+summary = PrometheusExporter::Metric::Summary.new("load_time", "time to load page", quantiles: [0.99, 0.75, 0.5, 0.25])
 histogram = PrometheusExporter::Metric::Histogram.new("api_time", "time to call api", buckets: [0.1, 0.5, 1])
 

diff --git a/lib/prometheus_exporter/metric/summary.rb b/lib/prometheus_exporter/metric/summary.rb
index 5b4b27f…a550432 100644
— a/lib/prometheus_exporter/metric/summary.rb
+++ b/lib/prometheus_exporter/metric/summary.rb
@@ -3,19 +3,19 @@
module PrometheusExporter::Metric
class Summary < Base

  • QUANTILES = [0.99, 0.9, 0.5, 0.1, 0.01]
  • DEFAULT_QUANTILES = [0.99, 0.9, 0.5, 0.1, 0.01]
    ROTATE_AGE = 120

    attr_reader :estimators, :count, :total

  • def initialize(name, help, quantiles = QUANTILES)
  • def initialize(name, help, opts = {})
    super(name, help)
    @buffers = [{}, {}]
    @last_rotated = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    @current_buffer = 0
    @counts = {}
    @sums = {}
  •  @quantiles = quantiles
    
  •  @quantiles = opts[:quantiles] || DEFAULT_QUANTILES
    

    end

    def type
    diff --git a/test/metric/summary_test.rb b/test/metric/summary_test.rb
    index 37991ae…e8b3999 100644
    — a/test/metric/summary_test.rb
    +++ b/test/metric/summary_test.rb
    @@ -12,7 +12,7 @@ module PrometheusExporter::Metric
    end

    it “can correctly gather a summary with custom quantiles” do

  •  summary = Summary.new("custom", "custom summary", [0.4, 0.6])
    
  •  summary = Summary.new("custom", "custom summary", quantiles: [0.4, 0.6])
    
     (1..10).each do |i|
       summary.observe(i)
    
[<small>GitHub</small>](https://github.com/discourse/prometheus_exporter/commit/86ffa70c9831c0ef36894421c684b10b12b8e0f3)