Merge branch 'include_prometheus' into 'master'

Include prometheus

See merge request !1227
This commit is contained in:
Marin Jankovski 2017-01-16 03:15:40 +00:00
commit bef79732b2
34 changed files with 729 additions and 1 deletions

View File

@ -7,6 +7,7 @@ omnibus-gitlab repository.
- Update git to 2.10.2 27cde301
- Allow users to specify an initial shared runner registration token
- Include Prometheus and Node Exporter, off by default
8.15.0

View File

@ -114,6 +114,8 @@ dependency "gitlab-selinux"
dependency "gitlab-scripts"
dependency "gitlab-config-template"
dependency "mattermost"
dependency "node-exporter"
dependency "prometheus"
# version manifest file
dependency "version-manifest"

View File

@ -0,0 +1,36 @@
#
# Copyright:: Copyright (c) 2017 GitLab Inc.
#
# 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.
#
name 'node-exporter'
default_version 'v0.13.0'
license 'APACHE-2.0'
license_file 'LICENSE'
source git: 'https://github.com/prometheus/node_exporter.git'
relative_path 'src/github.com/prometheus/node_exporter'
build do
env = with_standard_compiler_flags(with_embedded_path)
env['GOPATH'] = "#{Omnibus::Config.source_dir}/node-exporter"
env['GO15VENDOREXPERIMENT'] = '1' # Build machines have go 1.5.x, use vendor directory
exporter_source_dir = "#{Omnibus::Config.source_dir}/node-exporter"
cwd = "#{exporter_source_dir}/src/github.com/prometheus/node_exporter"
command 'go build', env: env, cwd: cwd
copy 'node_exporter', "#{install_dir}/embedded/bin/"
end

View File

@ -0,0 +1,40 @@
#
## Copyright:: Copyright (c) 2014 GitLab.com
## License:: Apache License, Version 2.0
##
## 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.
##
#
name "prometheus"
default_version "v1.4.1"
license "APACHE-2.0"
license_file "LICENSE"
source git: "https://github.com/prometheus/prometheus.git"
relative_path "src/github.com/prometheus/prometheus"
build do
env = with_standard_compiler_flags(with_embedded_path)
env = {
'GOPATH' => "#{Omnibus::Config.source_dir}/prometheus",
'GO15VENDOREXPERIMENT' => '1' # Build machines have go 1.5.x, use vendor directory
}
exporter_source_dir = "#{Omnibus::Config.source_dir}/prometheus"
cwd = "#{exporter_source_dir}/src/github.com/prometheus/prometheus"
command "go build ./cmd/prometheus", env: env, cwd: cwd
copy "prometheus", "#{install_dir}/embedded/bin/"
end

View File

@ -8,6 +8,8 @@ Omnibus is a way to package different services and tools required to run GitLab,
- [Installation Requirements](https://docs.gitlab.com/ce/install/requirements.html)
- [GitLab CI](gitlab-ci/README.md) Set up the GitLab CI coordinator that ships with Omnibus GitLab package.
- [GitLab Mattermost](gitlab-mattermost/README.md) Set up the Mattermost messaging app that ships with Omnibus GitLab package.
- [GitLab Prometheus](gitlab-prometheus/README.md) Set up the Prometheus
monitoring included in the Omnibus GitLab package.
- [Docker](docker/README.md) Set up the GitLab in Docker container.
## Maintenance

View File

@ -0,0 +1,10 @@
# GitLab Prometheus
GitLab provides out of the box monitoring with
[Prometheus](https://prometheus.io/)
>**Note:**
Prometheus services are on by default starting with GitLab 9.0.
- [Prometheus](prometheus.md)
- [Node Exporter](node-exporter.md)

View File

@ -0,0 +1,13 @@
# Node Exporter
Node exporter allows you to measure various machine resources such as
memory, disk and CPU utilization.
To enable Node Exporter in your GitLab installation, in `/etc/gitlab/gitlab.rb`
uncomment and edit the following line:
```
node_exporter['enable'] = true
```
After saving the changes, run `sudo gitlab-ctl reconfigure`.

View File

@ -0,0 +1,9 @@
# Prometheus
To enable Prometheus in your GitLab installation, in `/etc/gitlab/gitlab.rb`
uncomment and edit the following line:
```
prometheus['enable'] = true
```
After saving the changes, run `sudo gitlab-ctl reconfigure`.

View File

@ -17,6 +17,8 @@ by default:
| Unicorn | Yes | Socket | Port (8080) | X |
| GitLab Workhorse | Yes | Socket | Port (8181) | X |
| Nginx status | Yes | Port | X | 8060 |
| Node exporter | No | Port | X | 9100 |
| Prometheus | No | Port | X | 9090 |
| Redis Sentinel | No | Port | X | 26379 |
| Incoming email | No | Port | X | 143 |
| Elastic search | No | Port | X | 9200 |

View File

@ -34,6 +34,7 @@ groupadd -g 996 gitlab-psql
# groupadd -g 995 gitlab-ci
groupadd -g 994 mattermost
groupadd -g 993 registry
groupadd -g 992 gitlab-prometheus
# Create accounts
useradd -m -u 998 -g git -m -s /bin/sh -d /var/opt/gitlab git
@ -43,3 +44,4 @@ useradd -m -u 996 -g gitlab-psql -m -s /bin/sh -d /var/opt/gitlab/postgresql git
# useradd -m -u 995 -g gitlab-ci -m -s /bin/sh -d /var/opt/gitlab/gitlab-ci gitlab-ci
useradd -m -u 994 -g mattermost -m -s /bin/sh -d /var/opt/gitlab/mattermost mattermost
useradd -m -u 993 -g registry -m -s /bin/sh -d /var/opt/gitlab/registry registry
useradd -m -u 992 -g gitlab-prometheus -m -s /bin/sh -d /var/opt/gitlab/prometheus gitlab-prometheus

View File

@ -1133,6 +1133,46 @@ external_url 'GENERATED_EXTERNAL_URL'
# registry_nginx['custom_gitlab_server_config'] = "location ^~ /foo-namespace/bar-project/raw/ {\n deny all;\n}\n"
################################################################################
## Prometheus
################################################################################
##! Docs: https://docs.gitlab.com/omnibus/gitlab-prometheus/prometheus.html
# prometheus['enable'] = false
# prometheus['username'] = 'gitlab-prometheus'
# prometheus['uid'] = nil
# prometheus['gid'] = nil
# prometheus['shell'] = '/bin/sh'
# prometheus['home'] = '/var/opt/gitlab/prometheus'
# prometheus['log_directory'] = '/var/log/gitlab/prometheus'
# prometheus['scrape_interval'] = 15
# prometheus['scrape_timeout'] = 15
# prometheus['flags'] = {
'storage.local.path' => "#{node['gitlab']['prometheus']['home']}/data",
'storage.local.memory-chunks' => '50000',
'storage.local.max-chunks-to-persist' => '40000',
'config.file' => "#{node['gitlab']['prometheus']['home']}/prometheus.yml"
}
##! Advanced settings. Should be changed only if absolutely needed.
# prometheus['listen_address'] = 'localhost:9090'
################################################################################
## Prometheus Node Exporter
################################################################################
##! Docs: https://docs.gitlab.com/omnibus/gitlab-prometheus/node-exporter.html
# node_exporter['enable'] = false
# node_exporter['home'] = '/var/opt/gitlab/node-exporter'
# node_exporter['log_directory'] = '/var/log/gitlab/node-exporter'
# node_exporter['flags'] = {
'collector.textfile.directory' => "#{node['gitlab']['node-exporter']['home']}/textfile_collector"
}
##! Advanced settings. Should be changed only if absolutely needed.
# node_exporter['listen_address'] = 'localhost:9100'
################################################################################
################################################################################

View File

@ -961,3 +961,34 @@ default['gitlab']['registry-nginx']['proxy_set_headers'] = {
"X-Forwarded-For" => "$proxy_add_x_forwarded_for",
"X-Forwarded-Proto" => "$scheme"
}
####
# Prometheus server
####
default['gitlab']['prometheus']['enable'] = false
default['gitlab']['prometheus']['username'] = 'gitlab-prometheus'
default['gitlab']['prometheus']['uid'] = nil
default['gitlab']['prometheus']['gid'] = nil
default['gitlab']['prometheus']['shell'] = '/bin/sh'
default['gitlab']['prometheus']['home'] = '/var/opt/gitlab/prometheus'
default['gitlab']['prometheus']['log_directory'] = '/var/log/gitlab/prometheus'
default['gitlab']['prometheus']['scrape_interval'] = 15
default['gitlab']['prometheus']['scrape_timeout'] = 15
default['gitlab']['prometheus']['listen_address'] = 'localhost:9090'
default['gitlab']['prometheus']['flags'] = {
'storage.local.path' => File.join(node['gitlab']['prometheus']['home'], 'data'),
'storage.local.memory-chunks' => '50000',
'storage.local.max-chunks-to-persist' => '40000',
'config.file' => File.join(node['gitlab']['prometheus']['home'], 'prometheus.yml')
}
####
# Prometheus Node Exporter
####
default['gitlab']['node-exporter']['enable'] = false
default['gitlab']['node-exporter']['home'] = '/var/opt/gitlab/node-exporter'
default['gitlab']['node-exporter']['log_directory'] = '/var/log/gitlab/node-exporter'
default['gitlab']['node-exporter']['flags'] = {
'collector.textfile.directory' => File.join(node['gitlab']['node-exporter']['home'], 'textfile_collector')
}
default['gitlab']['node-exporter']['listen_address'] = 'localhost:9100'

View File

@ -86,6 +86,14 @@ class AccountHelper
node['gitlab']['registry']['group']
end
def prometheus_user
node['gitlab']['prometheus']['username']
end
def prometheus_group
node['gitlab']['prometheus']['group']
end
def users
%W(
#{gitlab_user}
@ -96,6 +104,8 @@ class AccountHelper
#{ci_redis_user}
#{mattermost_user}
#{registry_user}
#{node_exporter_user}
#{prometheus_user}
)
end
@ -109,6 +119,8 @@ class AccountHelper
#{ci_redis_group}
#{mattermost_group}
#{registry_group}
#{node_exporter_group}
#{prometheus_group}
)
end
end

View File

@ -80,6 +80,8 @@ module Gitlab
mattermost Mash.new
gitlab_pages Mash.new
registry Mash.new
node_exporter Mash.new
prometheus Mash.new
sentinel Mash.new
node nil
external_url nil
@ -196,7 +198,9 @@ module Gitlab
"gitlab_pages",
"registry",
"sentinel",
"gitaly"
"gitaly",
"node_exporter",
"prometheus"
].each do |key|
rkey = key.gsub('_', '-')
results['gitlab'][rkey] = Gitlab[key]

View File

@ -50,6 +50,8 @@ module Logging
mattermost
gitlab-pages
registry
node_exporter
prometheus
}.each do |runit_sv|
Gitlab[runit_sv.gsub('-', '_')]['svlogd_prefix'] ||= "#{Gitlab['node']['hostname']} #{runit_sv}: "
end

View File

@ -0,0 +1,34 @@
#
# Copyright:: Copyright (c) 2017 GitLab Inc.
#
# 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.
#
class PrometheusHelper
attr_reader :node
def initialize(node)
@node = node
end
def flags(service)
config = []
node['gitlab'][service]['flags'].each do |flag_key, flag_value|
next if flag_value.empty?
config << "-#{flag_key}=#{flag_value}"
end
config.join(" ")
end
end

View File

@ -133,6 +133,9 @@ include_recipe "gitlab::logrotate_folders_and_configs"
end
end
# Recipe which handles all prometheus related services
include_recipe "gitlab::gitlab-prometheus"
# Deprecated in favor of gitlab-workhorse since 8.2
runit_service "gitlab-git-http-server" do
action :disable

View File

@ -0,0 +1,28 @@
#
# Copyright:: Copyright (c) 2017 GitLab Inc.
# License:: Apache License, Version 2.0
#
# 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.
#
# Configure Prometheus Services
[
"prometheus",
"node-exporter"
].each do |service|
if node["gitlab"][service]["enable"]
include_recipe "gitlab::#{service}"
else
include_recipe "gitlab::#{service}_disable"
end
end

View File

@ -0,0 +1,48 @@
#
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# Copyright:: Copyright (c) 2016 GitLab Inc.
# License:: Apache License, Version 2.0
#
# 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.
#
account_helper = AccountHelper.new(node)
prometheus_user = account_helper.prometheus_user
node_exporter_log_dir = node['gitlab']['node-exporter']['log_directory']
textfile_dir = File.join(node['gitlab']['node-exporter']['home'], 'textfile_collector')
# node-exporter runs under the prometheus user account. If prometheus is
# disabled, it's up to this recipe to create the account
include_recipe 'gitlab::prometheus_user'
directory node_exporter_log_dir do
owner prometheus_user
mode '0700'
recursive true
end
directory textfile_dir do
owner prometheus_user
mode '0755'
recursive true
end
runtime_flags = PrometheusHelper.new(node).flags('node-exporter')
runit_service 'node-exporter' do
options({
log_directory: node_exporter_log_dir,
flags: runtime_flags
}.merge(params))
log_options node['gitlab']['logging'].to_hash.merge(
node['gitlab']['node-exporter'].to_hash
)
end

View File

@ -0,0 +1,21 @@
#
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# Copyright:: Copyright (c) 2014 GitLab.com
# License:: Apache License, Version 2.0
#
# 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.
#
runit_service "node-exporter" do
action :disable
end

View File

@ -0,0 +1,54 @@
#
# Copyright:: Copyright (c) 2017 GitLab Inc.
#
# 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.
#
account_helper = AccountHelper.new(node)
prometheus_user = account_helper.prometheus_user
prometheus_log_dir = node['gitlab']['prometheus']['log_directory']
prometheus_dir = node['gitlab']['prometheus']['home']
include_recipe 'gitlab::prometheus_user'
directory prometheus_dir do
owner prometheus_user
mode '0750'
recursive true
end
directory prometheus_log_dir do
owner prometheus_user
mode '0700'
recursive true
end
template 'Prometheus template' do
path File.join(prometheus_dir, 'prometheus.yml')
source 'prometheus.yml.erb'
owner prometheus_user
mode '0644'
variables node['gitlab']['prometheus'].to_hash
notifies :restart, 'service[prometheus]'
end
runtime_flags = PrometheusHelper.new(node).flags('prometheus')
runit_service 'prometheus' do
options({
log_directory: prometheus_log_dir,
flags: runtime_flags
}.merge(params))
log_options node['gitlab']['logging'].to_hash.merge(
node['gitlab']['prometheus'].to_hash
)
end

View File

@ -0,0 +1,19 @@
#
# Copyright:: Copyright (c) 2017 GitLab Inc.
#
# 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.
#
runit_service 'prometheus' do
action :disable
end

View File

@ -0,0 +1,30 @@
#
# Copyright:: Copyright (c) 2017 GitLab Inc.
#
# 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.
#
account_helper = AccountHelper.new(node)
prometheus_user = account_helper.prometheus_user
prometheus_dir = node['gitlab']['prometheus']['home']
account "Prometheus user and group" do
username prometheus_user
uid node['gitlab']['prometheus']['uid']
ugid prometheus_user
groupname prometheus_user
home prometheus_dir
gid node['gitlab']['prometheus']['gid']
shell node['gitlab']['prometheus']['shell']
manage node['gitlab']['manage-accounts']['enable']
end

View File

@ -0,0 +1,13 @@
global:
scrape_interval: <%= @scrape_interval %>s
scrape_timeout: <%= @scrape_timeout %>s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['<%= @listen_address %>']
<% if node['gitlab']['node-exporter']['enable'] %>
- job_name: node
static_configs:
- targets: ['<%= node['gitlab']['node-exporter']['listen_address'] %>']
<% end -%>

View File

@ -0,0 +1,6 @@
<%= "s#@svlogd_size" if @svlogd_size %>
<%= "n#@svlogd_num" if @svlogd_num %>
<%= "t#@svlogd_timeout" if @svlogd_timeout %>
<%= "!#@svlogd_filter" if @svlogd_filter %>
<%= "u#@svlogd_udp" if @svlogd_udp %>
<%= "p#@svlogd_prefix" if @svlogd_prefix %>

View File

@ -0,0 +1,2 @@
#!/bin/sh
exec svlogd -tt <%= @options[:log_directory] %>

View File

@ -0,0 +1,5 @@
#!/bin/sh
exec 2>&1
<%= render("mount_point_check.erb") %>
umask 077
exec chpst -P -U <%= node['gitlab']['prometheus']['username'] %> -u <%= node['gitlab']['prometheus']['username'] %> /opt/gitlab/embedded/bin/node_exporter <%= @options[:flags] %>

View File

@ -0,0 +1,6 @@
<%= "s#@svlogd_size" if @svlogd_size %>
<%= "n#@svlogd_num" if @svlogd_num %>
<%= "t#@svlogd_timeout" if @svlogd_timeout %>
<%= "!#@svlogd_filter" if @svlogd_filter %>
<%= "u#@svlogd_udp" if @svlogd_udp %>
<%= "p#@svlogd_prefix" if @svlogd_prefix %>

View File

@ -0,0 +1,2 @@
#!/bin/sh
exec svlogd -tt <%= @options[:log_directory] %>

View File

@ -0,0 +1,5 @@
#!/bin/sh
exec 2>&1
<%= render("mount_point_check.erb") %>
umask 077
exec chpst -P -U <%= node['gitlab']['prometheus']['username'] %> -u <%= node['gitlab']['prometheus']['username'] %> /opt/gitlab/embedded/bin/prometheus <%= @options[:flags] %>

View File

@ -77,4 +77,19 @@ describe 'gitlab::default' do
expect(chef_run).to_not create_directory('/etc/gitlab')
end
end
context 'prometheus is disabled default' do
it 'includes the prometheus_disable recipe' do
expect(chef_run).to include_recipe('gitlab::prometheus_disable')
expect(chef_run).to_not include_recipe('gitlab::prometheus')
end
end
context 'with prometheus enabled' do
before { stub_gitlab_rb(prometheus: { enable: true }) }
it 'includes the prometheus recipe' do
expect(chef_run).to include_recipe('gitlab::prometheus')
expect(chef_run).to_not include_recipe('gitlab::prometheus_disable')
end
end
end

View File

@ -0,0 +1,76 @@
require 'chef_helper'
describe 'gitlab::node-exporter' do
let(:chef_run) { ChefSpec::SoloRunner.converge('gitlab::default') }
before do
allow(Gitlab).to receive(:[]).and_call_original
end
context 'when node-exporter is enabled' do
let(:config_template) { chef_run.template('/var/log/gitlab/node-exporter/config') }
before do
stub_gitlab_rb(
node_exporter: { enable: true }
)
end
it_behaves_like 'enabled runit service', 'node-exporter', 'root', 'root'
it 'populates the files with expected configuration' do
expect(config_template).to notify('ruby_block[reload node-exporter svlogd configuration]')
expect(chef_run).to render_file('/opt/gitlab/sv/node-exporter/run')
.with_content { |content|
expect(content).to match(/exec chpst -P/)
expect(content).to match(/\/opt\/gitlab\/embedded\/bin\/node_exporter/)
expect(content).to match(/\/textfile_collector/)
}
expect(chef_run).to render_file('/opt/gitlab/sv/node-exporter/log/run')
.with_content(/exec svlogd -tt \/var\/log\/gitlab\/node-exporter/)
end
it 'creates default set of directories' do
expect(chef_run).to create_directory('/var/log/gitlab/node-exporter').with(
owner: 'gitlab-prometheus',
group: nil,
mode: '0700'
)
expect(chef_run).to create_directory('/var/opt/gitlab/node-exporter/textfile_collector').with(
owner: 'gitlab-prometheus',
group: nil,
mode: '0755'
)
end
end
context 'when node-exporter is enabled and prometheus is disabled' do
before do
stub_gitlab_rb(
prometheus: { enable: false },
node_exporter: { enable: true }
)
end
it 'should create the gitlab-prometheus account if prometheus is disabled' do
expect(chef_run).to create_user('gitlab-prometheus')
end
end
context 'when log dir is changed' do
before do
stub_gitlab_rb(
node_exporter: {
log_directory: 'foo',
enable: true
}
)
end
it 'populates the files with expected configuration' do
expect(chef_run).to render_file('/opt/gitlab/sv/node-exporter/log/run')
.with_content(/exec svlogd -tt foo/)
end
end
end

View File

@ -0,0 +1,110 @@
require 'chef_helper'
describe 'gitlab::prometheus' do
let(:chef_run) { ChefSpec::SoloRunner.converge('gitlab::default') }
before do
allow(Gitlab).to receive(:[]).and_call_original
end
context 'when prometheus is enabled' do
let(:config_template) { chef_run.template('/var/log/gitlab/prometheus/config') }
before do
stub_gitlab_rb(
prometheus: {
enable: true
}
)
end
it_behaves_like 'enabled runit service', 'prometheus', 'root', 'root'
it 'populates the files with expected configuration' do
expect(config_template).to notify('ruby_block[reload prometheus svlogd configuration]')
expect(chef_run).to render_file('/opt/gitlab/sv/prometheus/run')
.with_content { |content|
expect(content).to match(/exec chpst -P/)
expect(content).to match(/\/opt\/gitlab\/embedded\/bin\/prometheus/)
expect(content).to match(/prometheus.yml/)
}
expect(chef_run).to render_file('/var/opt/gitlab/prometheus/prometheus.yml')
.with_content { |content|
expect(content).to match(/scrape_interval: 15s/)
expect(content).to match(/scrape_timeout: 15s/)
}
expect(chef_run).to render_file('/opt/gitlab/sv/prometheus/log/run')
.with_content(/exec svlogd -tt \/var\/log\/gitlab\/prometheus/)
end
it 'creates default set of directories' do
expect(chef_run).to create_directory('/var/log/gitlab/prometheus').with(
owner: 'gitlab-prometheus',
group: nil,
mode: '0700'
)
expect(chef_run).to create_directory('/var/opt/gitlab/prometheus').with(
owner: 'gitlab-prometheus',
group: nil,
mode: '0750'
)
end
it 'should create a gitlab-prometheus user account' do
expect(chef_run).to create_user('gitlab-prometheus')
end
end
context 'when storage path is changed' do
before do
stub_gitlab_rb(
prometheus: {
flags: { 'storage.local.path' => 'foo' },
enable: true
}
)
end
it 'populates the files with expected configuration' do
expect(chef_run).to render_file('/opt/gitlab/sv/prometheus/run')
.with_content(/storage.local.path=foo/)
end
end
context 'when scrape_interval is changed' do
before do
stub_gitlab_rb(
prometheus: {
scrape_interval: 9999,
enable: true
}
)
end
it 'renders prometheus.yml with the non-default value' do
expect(chef_run).to render_file('/var/opt/gitlab/prometheus/prometheus.yml')
.with_content(/scrape_interval: 9999s/)
end
end
context 'when scrape_timeout is changed' do
before do
stub_gitlab_rb(
prometheus: {
scrape_timeout: 8888,
scrape_interval: 11,
enable: true
}
)
end
it 'renders prometheus.yml with the non-default value' do
expect(chef_run).to render_file('/var/opt/gitlab/prometheus/prometheus.yml')
.with_content(/scrape_timeout: 8888s/)
expect(chef_run).to render_file('/var/opt/gitlab/prometheus/prometheus.yml')
.with_content(/scrape_interval: 11/)
end
end
end

View File

@ -0,0 +1,45 @@
#
# Copyright:: Copyright (c) 2017 GitLab Inc.
#
# 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.
#
require_relative '../../files/gitlab-cookbooks/gitlab/libraries/prometheus_helper.rb'
require 'chef_helper'
describe PrometheusHelper do
let(:chef_run) { ChefSpec::SoloRunner.new }
subject { described_class.new(chef_run.node) }
context 'flags for prometheus' do
context 'with default options' do
it 'returns the correct default config string' do
chef_run.converge('gitlab::default')
expect(subject.flags('prometheus')).to eq(
'-storage.local.path=/var/opt/gitlab/prometheus/data -storage.local.memory-chunks=50000 -storage.local.max-chunks-to-persist=40000 -config.file=/var/opt/gitlab/prometheus/prometheus.yml')
end
end
context 'with custom options' do
before { allow(Gitlab).to receive(:[]).and_call_original }
it 'does not return the correct string if any attributes have been changed' do
chef_run.node.set['gitlab']['prometheus']['home'] = '/fake/dir'
chef_run.converge('gitlab::default')
expect(subject.flags('prometheus')).to eq(
'-storage.local.path=/fake/dir/data -storage.local.memory-chunks=50000 -storage.local.max-chunks-to-persist=40000 -config.file=/fake/dir/prometheus.yml')
end
end
end
end