Commit 55ed6e1c authored by Yorick Peterse's avatar Yorick Peterse

Cache InfluxDB settings after the first use

This ensures we don't need to load anything from either PostgreSQL or
the Rails cache whenever creating new InfluxDB connections.
parent a6c60127
...@@ -6,16 +6,21 @@ module Gitlab ...@@ -6,16 +6,21 @@ module Gitlab
METRICS_ROOT = Rails.root.join('lib', 'gitlab', 'metrics').to_s METRICS_ROOT = Rails.root.join('lib', 'gitlab', 'metrics').to_s
PATH_REGEX = /^#{RAILS_ROOT}\/?/ PATH_REGEX = /^#{RAILS_ROOT}\/?/
def self.pool_size def self.settings
current_application_settings[:metrics_pool_size] || 16 @settings ||= {
end enabled: current_application_settings[:metrics_enabled],
pool_size: current_application_settings[:metrics_pool_size],
def self.timeout timeout: current_application_settings[:metrics_timeout],
current_application_settings[:metrics_timeout] || 10 method_call_threshold: current_application_settings[:metrics_method_call_threshold],
host: current_application_settings[:metrics_host],
username: current_application_settings[:metrics_username],
password: current_application_settings[:metrics_password],
port: current_application_settings[:metrics_port]
}
end end
def self.enabled? def self.enabled?
current_application_settings[:metrics_enabled] || false settings[:enabled] || false
end end
def self.mri? def self.mri?
...@@ -26,8 +31,7 @@ module Gitlab ...@@ -26,8 +31,7 @@ module Gitlab
# This is memoized since this method is called for every instrumented # This is memoized since this method is called for every instrumented
# method. Loading data from an external cache on every method call slows # method. Loading data from an external cache on every method call slows
# things down too much. # things down too much.
@method_call_threshold ||= @method_call_threshold ||= settings[:method_call_threshold]
(current_application_settings[:metrics_method_call_threshold] || 10)
end end
def self.pool def self.pool
...@@ -90,11 +94,11 @@ module Gitlab ...@@ -90,11 +94,11 @@ module Gitlab
# When enabled this should be set before being used as the usual pattern # When enabled this should be set before being used as the usual pattern
# "@foo ||= bar" is _not_ thread-safe. # "@foo ||= bar" is _not_ thread-safe.
if enabled? if enabled?
@pool = ConnectionPool.new(size: pool_size, timeout: timeout) do @pool = ConnectionPool.new(size: settings[:pool_size], timeout: settings[:timeout]) do
host = current_application_settings[:metrics_host] host = settings[:host]
user = current_application_settings[:metrics_username] user = settings[:username]
pw = current_application_settings[:metrics_password] pw = settings[:password]
port = current_application_settings[:metrics_port] port = settings[:port]
InfluxDB::Client. InfluxDB::Client.
new(udp: { host: host, port: port }, username: user, password: pw) new(udp: { host: host, port: port }, username: user, password: pw)
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Metrics do describe Gitlab::Metrics do
describe '.pool_size' do describe '.settings' do
it 'returns a Fixnum' do it 'returns a Hash' do
expect(described_class.pool_size).to be_an_instance_of(Fixnum) expect(described_class.settings).to be_an_instance_of(Hash)
end
end
describe '.timeout' do
it 'returns a Fixnum' do
expect(described_class.timeout).to be_an_instance_of(Fixnum)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment