Make stats interval into config parameter (#830)

diff --git a/rel/overlay/etc/default.ini b/rel/overlay/etc/default.ini
index 653131e..43a40fb 100644
--- a/rel/overlay/etc/default.ini
+++ b/rel/overlay/etc/default.ini
@@ -541,3 +541,7 @@
 ; syslog_port = 514
 ; syslog_appid = couchdb
 ; syslog_facility = local2
+
+[stats]
+; Stats collection interval in seconds. Default 10 seconds.
+;interval = 10
diff --git a/src/couch_stats/src/couch_stats.app.src b/src/couch_stats/src/couch_stats.app.src
index d60ce1c..6339a0f 100644
--- a/src/couch_stats/src/couch_stats.app.src
+++ b/src/couch_stats/src/couch_stats.app.src
@@ -16,7 +16,5 @@
     {registered, [couch_stats_aggregator, couch_stats_process_tracker]},
     {applications, [kernel, stdlib, folsom, couch_log]},
     {mod, {couch_stats_app, []}},
-    {env, [
-        {collection_interval, 10}
-    ]}
+    {env, []}
 ]}.
diff --git a/src/couch_stats/src/couch_stats.erl b/src/couch_stats/src/couch_stats.erl
index e02da29..59175f7 100644
--- a/src/couch_stats/src/couch_stats.erl
+++ b/src/couch_stats/src/couch_stats.erl
@@ -29,6 +29,10 @@
     update_gauge/2
 ]).
 
+
+-include("couch_stats.hrl").
+
+
 -type response() :: ok | {error, unknown_metric}.
 -type stat() :: {any(), [{atom(), any()}]}.
 
@@ -56,7 +60,7 @@
         {error, Name, metric_already_exists} -> {error, metric_exists}
     end;
 new(histogram, Name) ->
-    {ok, Time} = application:get_env(couch_stats, collection_interval),
+    Time = config:get_integer("stats", "interval", ?DEFAULT_INTERVAL),
     case folsom_metrics:new_histogram(Name, slide_uniform, {Time, 1024}) of
         ok -> ok;
         {error, Name, metric_already_exists} -> {error, metric_exists}
diff --git a/src/couch_stats/src/couch_stats.hrl b/src/couch_stats/src/couch_stats.hrl
new file mode 100644
index 0000000..3cffe99
--- /dev/null
+++ b/src/couch_stats/src/couch_stats.hrl
@@ -0,0 +1,14 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-define(DEFAULT_INTERVAL, 10).
+-define(RELOAD_INTERVAL, 600).
diff --git a/src/couch_stats/src/couch_stats_aggregator.erl b/src/couch_stats/src/couch_stats_aggregator.erl
index 0f6c9dd..17bd6fc 100644
--- a/src/couch_stats/src/couch_stats_aggregator.erl
+++ b/src/couch_stats/src/couch_stats_aggregator.erl
@@ -30,6 +30,9 @@
     terminate/2
 ]).
 
+
+-include("couch_stats.hrl").
+
 -record(st, {
     descriptions,
     stats,
@@ -52,11 +55,9 @@
 
 init([]) ->
     {ok, Descs} = reload_metrics(),
-    Interval = case application:get_env(couch_stats, collection_interval) of
-        {ok, I} -> I * 1000
-    end,
-    {ok, CT} = timer:send_interval(Interval, self(), collect),
-    {ok, RT} = timer:send_interval(600000, self(), reload),
+    Interval = config:get_integer("stats", "interval", ?DEFAULT_INTERVAL),
+    {ok, CT} = timer:send_interval(Interval * 1000, self(), collect),
+    {ok, RT} = timer:send_interval(?RELOAD_INTERVAL * 1000, self(), reload),
     {ok, #st{descriptions=Descs, stats=[], collect_timer=CT, reload_timer=RT}}.
 
 handle_call(fetch, _from, #st{stats = Stats}=State) ->