Skip to content

Commit

Permalink
Implemented service reload for Prometheus
Browse files Browse the repository at this point in the history
With this commit all the configuration files that can be reload on-the-fly
like prometheus.yml and alerts rules do not trigger a full service restart
like they did til now.
I left the possibility to choose if a change to a command-line flag should
or not restart prometheus (thus applying the change on puppet run) with
the already existing  flag (still true by default).
  • Loading branch information
vide committed Jun 20, 2017
1 parent f945188 commit 0ae4c30
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
16 changes: 15 additions & 1 deletion manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@

if $prometheus::init_style {

# the vast majority of files here are init-files
# so any change there should trigger a full service restart
if $::prometheus::restart_on_change {
File {
notify => [Class['::prometheus::run_service']]
}
$systemd_notify = [Exec['prometheus-systemd-reload'], Class['::prometheus::run_service']]
} else {
$systemd_notify = Exec['prometheus-systemd-reload']
}

case $prometheus::init_style {
'upstart' : {
file { '/etc/init/prometheus.conf':
Expand All @@ -31,8 +42,10 @@
mode => '0644',
owner => 'root',
group => 'root',
notify => $systemd_notify,
content => template('prometheus/prometheus.systemd.erb'),
}~>
}

exec { 'prometheus-systemd-reload':
command => 'systemctl daemon-reload',
path => [ '/usr/bin', '/bin', '/usr/sbin' ],
Expand Down Expand Up @@ -90,6 +103,7 @@
owner => $prometheus::user,
group => $prometheus::group,
mode => $prometheus::config_mode,
notify => Class['::prometheus::service_reload'],
content => template($config_template),
}

Expand Down
9 changes: 3 additions & 6 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
#
# [*restart_on_change*]
# Should puppet restart prometheus on configuration change? (default true)
# Note: this applies only to command-line options changes. Configuration
# options are always *reloaded* without restarting.
#
# [*init_style*]
# Service startup scripts style (e.g. rc, upstart or systemd)
Expand Down Expand Up @@ -158,10 +160,6 @@

$config_hash_real = deep_merge($config_defaults, $config_hash)
validate_hash($config_hash_real)
$notify_service = $restart_on_change ? {
true => Class['::prometheus::run_service'],
default => undef,
}

anchor {'prometheus_first': }
->
Expand All @@ -171,14 +169,13 @@
rule_files => $rule_files,
scrape_configs => $scrape_configs,
purge => $purge_config_dir,
notify => $notify_service,
config_template => $config_template,
} ->
class { '::prometheus::alerts':
location => $config_dir,
alerts => $alerts,
notify => $notify_service,
} ->
class { '::prometheus::run_service': } ->
class { '::prometheus::service_reload': } ->
anchor {'prometheus_last': }
}
6 changes: 3 additions & 3 deletions manifests/run_service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

if $prometheus::manage_service == true {
service { 'prometheus':
ensure => $prometheus::service_ensure,
name => $init_selector,
enable => $prometheus::service_enable,
ensure => $prometheus::service_ensure,
name => $init_selector,
enable => $prometheus::service_enable,
}
}
}
23 changes: 23 additions & 0 deletions manifests/service_reload.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This class implements prometheus service reload
# without restarting the whole service when a config
# changes
class prometheus::service_reload() {

if $prometheus::manage_service == true {
$init_selector = $prometheus::run_service::init_selector

$prometheus_reload = $prometheus::init_style ? {
'systemd' => "systemctl reload ${init_selector}",
'upstart' => "upstart reload ${init_selector}",
'sysv' => "/etc/init.d/${init_selector} reload",
'sles' => "/etc/init.d/${init_selector} reload",
'debian' => "/etc/init.d/${init_selector} reload",
'launchd' => "launchctl stop ${init_selector} && launchctl start ${init_selector}",
}

exec { 'prometheus-reload':
command => $prometheus_reload,
refreshonly => true,
}
}
}

0 comments on commit 0ae4c30

Please sign in to comment.