Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configure service for embedded prometheus exporter #10

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 41 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* `solr::config`: Setup and configure Solr.
* `solr::customplugins`: Manages custom plugins for Solr.
* `solr::install`: Installs and prepares the Solr application.
* `solr::prometheus_exporter`: Configure the embedded prometheus exporter as a service.
* `solr::service`: Installs and configures the Solr service.

### Defined types
Expand All @@ -35,16 +36,24 @@ The following parameters are available in the `solr` class:
* [`custom_plugins`](#custom_plugins)
* [`custom_plugins_dir`](#custom_plugins_dir)
* [`custom_plugins_id`](#custom_plugins_id)
* [`enable_prometheus_exporter`](#enable_prometheus_exporter)
* [`enable_remote_jmx`](#enable_remote_jmx)
* [`extract_dir`](#extract_dir)
* [`gc_log_opts`](#gc_log_opts)
* [`gc_tune`](#gc_tune)
* [`java_home`](#java_home)
* [`java_mem`](#java_mem)
* [`limit_file_max`](#limit_file_max)
* [`limit_proc_max`](#limit_proc_max)
* [`log_dir`](#log_dir)
* [`manage_custom_plugins`](#manage_custom_plugins)
* [`manage_service_limits`](#manage_service_limits)
* [`mirror`](#mirror)
* [`service_name`](#service_name)
* [`prometheus_exporter_user`](#prometheus_exporter_user)
* [`prometheus_exporter_env_vars`](#prometheus_exporter_env_vars)
* [`prometheus_exporter_extra_options`](#prometheus_exporter_extra_options)
* [`prometheus_exporter_service_name`](#prometheus_exporter_service_name)
* [`solr_base`](#solr_base)
* [`solr_home`](#solr_home)
* [`solr_host`](#solr_host)
Expand Down Expand Up @@ -84,6 +93,12 @@ Data type: `String`

Sets the Solr config option that is used to configure custom plugins.

##### <a name="enable_prometheus_exporter"></a>`enable_prometheus_exporter`

Data type: `Boolean`

Determines whether to enable embedded prometheus exporter as a service.

##### <a name="enable_remote_jmx"></a>`enable_remote_jmx`

Data type: `Boolean`
Expand Down Expand Up @@ -148,7 +163,8 @@ Determines whether to enable support for custom plugins.

Data type: `Boolean`

Determines whether to set resource limits for the Solr service. The service is NOT restarted when limits are changed.
Determines whether to set resource limits for the Solr service. The service
is NOT restarted when limits are changed.

##### <a name="mirror"></a>`mirror`

Expand All @@ -162,6 +178,30 @@ Data type: `String`

Sets the name of the system service that should be setup.

##### <a name="prometheus_exporter_user"></a>`prometheus_exporter_user`

Data type: `String`

Sets the user running the solr-exporter binary.

##### <a name="prometheus_exporter_env_vars"></a>`prometheus_exporter_env_vars`

Data type: `Optional[Hash]`

Sets solr-exporter environment variables in service file (see https://solr.apache.org/guide/solr/latest/deployment-guide/monitoring-with-prometheus-and-grafana.html#environment-variable-options).

##### <a name="prometheus_exporter_extra_options"></a>`prometheus_exporter_extra_options`

Data type: `Optional[String]`

Sets solr-exporter custom command line options (see https://solr.apache.org/guide/solr/latest/deployment-guide/monitoring-with-prometheus-and-grafana.html#command-line-parameters).

##### <a name="prometheus_exporter_service_name"></a>`prometheus_exporter_service_name`

Data type: `String`

Sets the name of the prometheus exporter system service that should be setup.

##### <a name="solr_base"></a>`solr_base`

Data type: `Stdlib::Compat::Absolute_path`
Expand Down
5 changes: 5 additions & 0 deletions data/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ solr::cloud: false
solr::custom_plugins: []
solr::custom_plugins_dir: "%{lookup('solr::var_dir')}/libs"
solr::custom_plugins_id: 'solr.custom_plugins.dir'
solr::enable_prometheus_exporter: false
solr::enable_remote_jmx: false
solr::extract_dir: '/opt'
solr::gc_log_opts:
Expand Down Expand Up @@ -31,6 +32,10 @@ solr::log_dir: '/var/log/solr'
solr::manage_custom_plugins: false
solr::manage_service_limits: true
solr::mirror: 'https://dlcdn.apache.org/solr/solr'
solr::prometheus_exporter_env_vars: {}
solr::prometheus_exporter_extra_options: ~
solr::prometheus_exporter_user: "%{lookup('solr::solr_user')}"
solr::prometheus_exporter_service_name: 'solr-exporter'
solr::service_name: 'solr'
solr::solr_base: "%{lookup('solr::extract_dir')}/%{lookup('solr::service_name')}"
solr::solr_home: "%{lookup('solr::var_dir')}/data"
Expand Down
26 changes: 26 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# @param custom_plugins_id
# Sets the Solr config option that is used to configure custom plugins.
#
# @param enable_prometheus_exporter
# Determines whether to enable embedded prometheus exporter as a service.
#
# @param enable_remote_jmx
# Determines whether to enable remote JMX support.
#
Expand Down Expand Up @@ -53,6 +56,18 @@
# @param service_name
# Sets the name of the system service that should be setup.
#
# @param prometheus_exporter_user
# Sets the user running the solr-exporter binary.
#
# @param prometheus_exporter_env_vars
# Sets solr-exporter environment variables in service file (see https://solr.apache.org/guide/solr/latest/deployment-guide/monitoring-with-prometheus-and-grafana.html#environment-variable-options).
#
# @param prometheus_exporter_extra_options
# Sets solr-exporter custom command line options (see https://solr.apache.org/guide/solr/latest/deployment-guide/monitoring-with-prometheus-and-grafana.html#command-line-parameters).
#
# @param prometheus_exporter_service_name
# Sets the name of the prometheus exporter system service that should be setup.
#
# @param solr_base
# Internal module parameter, SHOULD NOT BE CHANGED! Specifies a symlink that is created by the Solr installer.
#
Expand Down Expand Up @@ -112,6 +127,7 @@
Integer $zk_timeout,
String $solr_host,
String $solr_time,
Boolean $enable_prometheus_exporter,
Boolean $enable_remote_jmx,
String $service_name,
Stdlib::Compat::Absolute_path $solr_base,
Expand All @@ -123,6 +139,10 @@
Boolean $manage_service_limits,
Integer $limit_file_max,
Integer $limit_proc_max,
String $prometheus_exporter_service_name,
String $prometheus_exporter_user,
Optional[Hash] $prometheus_exporter_env_vars,
Optional[String] $prometheus_exporter_extra_options,
Optional[Array] $gc_log_opts,
Optional[Array] $gc_tune,
Optional[Stdlib::Compat::Absolute_path] $java_home,
Expand All @@ -133,4 +153,10 @@
-> Class { 'solr::customplugins': }
-> Class { 'solr::service': }
-> Class['solr']

if $enable_prometheus_exporter {
Class { 'solr::prometheus_exporter':
require => Class['solr'],
}
}
}
48 changes: 48 additions & 0 deletions manifests/prometheus_exporter.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# @summary
# Configure the embedded prometheus exporter as a service.
#
# @api private
class solr::prometheus_exporter {
assert_private()

$solr_dir = "${solr::extract_dir}/${solr::service_name}"
if versioncmp($solr::version, '9.0.0') >= 0 {
$exporter_dir = "${solr_dir}/prometheus-exporter"
} elsif versioncmp($solr::version, '7.3.0') >= 0 {
$exporter_dir = "${solr_dir}/contrib/prometheus-exporter"
} else {
fail("The provided version ${solr::version} does not contain the embedded exporter (min 7.3.0)")
}

$bin_path = "${exporter_dir}/bin/solr-exporter"
# File mode is 0644 by default, set it to executable
file { $bin_path:
ensure => 'file',
mode => '0755',
}

if $solr::prometheus_exporter_extra_options {
$command = "${bin_path} ${solr::prometheus_exporter_extra_options}"
} else {
$command = $bin_path
}

include 'systemd'

systemd::unit_file { "${solr::prometheus_exporter_service_name}.service":
content => epp('solr/solr-exporter.systemd.epp', {
'exporter_dir' => $exporter_dir,
'user' => $solr::prometheus_exporter_user,
'env_vars' => $solr::prometheus_exporter_env_vars,
'command' => $command,
}),
require => File[$bin_path],
notify => Service[$solr::prometheus_exporter_service_name],
}

service { $solr::prometheus_exporter_service_name:
ensure => 'running',
enable => true,
provider => 'systemd',
}
}
40 changes: 40 additions & 0 deletions spec/acceptance/class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,44 @@ class { 'solr':
}
end
end
context 'with exporter' do
describe 'work idempotently with no errors' do
let(:pp) do
<<-MANIFEST
class { 'solr':
version => '9.0.0',
# Use 'localhost' for acceptance tests.
solr_host => 'localhost',
# Use Apache Archive, because "old" releases get removed
# very quickly from the official mirrors.
# mirror => 'https://archive.apache.org/dist/lucene/solr',
enable_prometheus_exporter => true,
}
MANIFEST
end

it 'runs successfully' do
apply_manifest(pp, catch_failures: true)
end

it 'is idempotent' do
apply_manifest(pp, catch_changes: true)
end
end

describe service('solr-exporter') do
it { is_expected.to be_enabled }
it { is_expected.to be_running }
end

describe port(8989) do
it {
# Solr-exporter may take a while to start up
sleep(10)
# Depending on the host system, this may be either IPv4-
# or IPv6-only, so both needs to work.
is_expected.to be_listening
}
end
end
end
64 changes: 64 additions & 0 deletions spec/classes/solr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,70 @@
it { is_expected.not_to contain_limits__limits('solr/nofile') }
it { is_expected.not_to contain_limits__limits('solr/nproc') }
end

context 'solr class when enable_prometheus_exporter is set to true' do
let(:params) do
{
enable_prometheus_exporter: true,
version: '9.0.0',
}
end

it { is_expected.to compile.with_all_deps }

it { is_expected.to contain_class('solr::prometheus_exporter').that_requires('Class[solr]') }

it { is_expected.to contain_file('/opt/solr/prometheus-exporter/bin/solr-exporter').with('mode' => '0755') }
it { is_expected.to contain_systemd__unit_file('solr-exporter.service').that_requires('File[/opt/solr/prometheus-exporter/bin/solr-exporter]').that_notifies('Service[solr-exporter]') }
it { is_expected.to contain_service('solr-exporter').with('ensure' => 'running', 'enable' => true) }
end

context 'solr class when enable_prometheus_exporter is set to true with version < 9.0.0' do
let(:params) do
{
enable_prometheus_exporter: true,
version: '8.11.2',
}
end

it { is_expected.to compile.with_all_deps }

it { is_expected.to contain_class('solr::prometheus_exporter').that_requires('Class[solr]') }

it { is_expected.to contain_file('/opt/solr/contrib/prometheus-exporter/bin/solr-exporter').with('mode' => '0755') }
it {
is_expected.to contain_systemd__unit_file('solr-exporter.service')
.that_requires('File[/opt/solr/contrib/prometheus-exporter/bin/solr-exporter]')
.that_notifies('Service[solr-exporter]')
}
it { is_expected.to contain_service('solr-exporter').with('ensure' => 'running', 'enable' => true) }
end

context 'solr class fails when enable_prometheus_exporter is set to true with version < 7.3.0' do
let(:params) do
{
enable_prometheus_exporter: true,
version: '7.2.1',
mirror: 'https://archive.apache.org/dist/lucene/solr/',
}
end

it { is_expected.to compile.with_all_deps.and_raise_error(%r{The provided version 7\.2\.1 does not contain the embedded exporter \(min 7\.3\.0\)}) }
end

context 'solr class when enable_prometheus_exporter is set to true and env_vars are provided' do
let(:params) do
{
enable_prometheus_exporter: true,
prometheus_exporter_env_vars: { 'JAVA_HEAP': '128m' },
version: '9.0.0',
}
end

it { is_expected.to compile.with_all_deps }

it { is_expected.to contain_systemd__unit_file('solr-exporter.service').with_content(%r{Environment="JAVA_HEAP=128m"}) }
end
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions templates/solr-exporter.systemd.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%- | String $exporter_dir,
String $user,
String $command,
Hash $env_vars
| -%>
# THIS FILE IS MANAGED BY PUPPET
[Unit]
Description=Solr Prometheus Exporter
Wants=network-online.target
After=network-online.target

[Service]
WorkingDirectory=<%= $exporter_dir %>
User=<%= $user %>
<% $env_vars.each |$key, $value| { -%>
Environment="<%= $key %>=<%= $value %>"
<% } -%>
ExecStart=<%= $command %>

Type=simple
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=always

[Install]
WantedBy=multi-user.target