monitoring-8.x-1.x-dev/modules/prometheus/monitoring_prometheus.module
modules/prometheus/monitoring_prometheus.module
<?php
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter() for \Drupal\monitoring\Form\MonitoringSettingsForm.
*/
function monitoring_prometheus_form_monitoring_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
$config = \Drupal::config('monitoring_prometheus.settings');
$allowed_ips = '';
if ($allowed_ips_config = $config->get('allowed_ips')) {
$allowed_ips = implode("\r\n", $allowed_ips_config);
}
$form['allowed_ips'] = [
'#type' => 'textarea',
'#title' => t('Allowed Prometheus IPs'),
'#default_value' => $allowed_ips,
'#description' => t("Specify IPs that are allowed to access the /metrics url that monitoring_prometheus provides. Enter one IP per line. This doesn't bypass the permission based access control."),
];
$custom_labels = '';
if ($custom_labels_config = $config->get('custom_labels')) {
foreach ($custom_labels_config as $label_name => $label_value) {
$custom_labels .= "$label_name=$label_value" . "\r\n";
}
}
$form['custom_labels'] = [
'#type' => 'textarea',
'#title' => t('Custom labels'),
'#default_value' => $custom_labels,
'#description' => t('Specify custom labels that will be applied to each Gauge. Enter one label per line using this example syntax: environment=production.'),
];
if (\Drupal::moduleHandler()->moduleExists('token')) {
$form['token_help'] = [
'#theme' => 'token_tree_link',
'#token_types' => ['file'],
];
$form['custom_labels']['#description'] .= ' ' . t('Tokens will be replaced.');
}
$form['#submit'][] = 'monitoring_prometheus_settings_form_submit';
}
/**
* Form submission handler for monitoring_prometheus_form_monitoring_settings_alter().
*
* @see motinoring_prometheus_form_monitoring_settings_alter()
*/
function monitoring_prometheus_settings_form_submit($form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()->getEditable('monitoring_prometheus.settings');
$allowed_ips = explode("\r\n", $form_state->getValue('allowed_ips'));
$config->set('allowed_ips', array_filter($allowed_ips));
$labels = [];
$label_lines = explode("\r\n", $form_state->getValue('custom_labels'));
foreach ($label_lines as $line) {
$parts = explode('=', $line);
if (count($parts) < 2) {
continue;
}
$labels[trim($parts[0])] = trim($parts[1]);
}
$config->set('custom_labels', array_filter($labels));
$config->save();
}
