File

src/app/shared/services/prometheus-alert.service.ts

Index

Properties
Methods

Constructor

constructor(alertFormatter: PrometheusAlertFormatter, prometheusService: PrometheusService)
Parameters :
Name Type Optional
alertFormatter PrometheusAlertFormatter No
prometheusService PrometheusService No

Methods

Private getChangedAlerts
getChangedAlerts(alerts: PrometheusCustomAlert[], oldAlerts: PrometheusCustomAlert[])
Parameters :
Name Type Optional
alerts PrometheusCustomAlert[] No
oldAlerts PrometheusCustomAlert[] No
Returns : any
Private getVanishedAlerts
getVanishedAlerts(alerts: PrometheusCustomAlert[], oldAlerts: PrometheusCustomAlert[])
Parameters :
Name Type Optional
alerts PrometheusCustomAlert[] No
oldAlerts PrometheusCustomAlert[] No
Returns : any
Private handleAlerts
handleAlerts(alerts: PrometheusAlert[])
Parameters :
Name Type Optional
alerts PrometheusAlert[] No
Returns : void
Private notifyOnAlertChanges
notifyOnAlertChanges(alerts: PrometheusAlert[], oldAlerts: PrometheusAlert[])
Parameters :
Name Type Optional
alerts PrometheusAlert[] No
oldAlerts PrometheusAlert[] No
Returns : void
refresh
refresh()
Returns : void

Properties

alerts
Type : PrometheusAlert[]
Default value : []
Private canAlertsBeNotified
Default value : false
Private connected
Default value : true
import { Injectable } from '@angular/core';

import * as _ from 'lodash';

import { PrometheusService } from '../api/prometheus.service';
import { PrometheusAlert, PrometheusCustomAlert } from '../models/prometheus-alerts';
import { PrometheusAlertFormatter } from './prometheus-alert-formatter';
import { ServicesModule } from './services.module';

@Injectable({
  providedIn: ServicesModule
})
export class PrometheusAlertService {
  private canAlertsBeNotified = false;
  private connected = true;
  alerts: PrometheusAlert[] = [];

  constructor(
    private alertFormatter: PrometheusAlertFormatter,
    private prometheusService: PrometheusService
  ) {}

  refresh() {
    this.prometheusService.ifAlertmanagerConfigured((url) => {
      if (this.connected) {
        this.prometheusService.list().subscribe(
          (alerts) => this.handleAlerts(alerts),
          (resp) => {
            const errorMsg = `Please check if <a target="_blank" href="${url}">Prometheus Alertmanager</a> is still running`;
            resp['application'] = 'Prometheus';
            if (resp.status === 500) {
              this.connected = false;
              resp.error.detail = errorMsg;
            }
          }
        );
      }
    });
  }

  private handleAlerts(alerts: PrometheusAlert[]) {
    if (this.canAlertsBeNotified) {
      this.notifyOnAlertChanges(alerts, this.alerts);
    }
    this.alerts = alerts;
    this.canAlertsBeNotified = true;
  }

  private notifyOnAlertChanges(alerts: PrometheusAlert[], oldAlerts: PrometheusAlert[]) {
    const changedAlerts = this.getChangedAlerts(
      this.alertFormatter.convertToCustomAlerts(alerts),
      this.alertFormatter.convertToCustomAlerts(oldAlerts)
    );
    const notifications = changedAlerts.map((alert) =>
      this.alertFormatter.convertAlertToNotification(alert)
    );
    this.alertFormatter.sendNotifications(notifications);
  }

  private getChangedAlerts(alerts: PrometheusCustomAlert[], oldAlerts: PrometheusCustomAlert[]) {
    const updatedAndNew = _.differenceWith(alerts, oldAlerts, _.isEqual);
    return updatedAndNew.concat(this.getVanishedAlerts(alerts, oldAlerts));
  }

  private getVanishedAlerts(alerts: PrometheusCustomAlert[], oldAlerts: PrometheusCustomAlert[]) {
    return _.differenceWith(oldAlerts, alerts, (a, b) => a.fingerprint === b.fingerprint).map(
      (alert) => {
        alert.status = 'resolved';
        return alert;
      }
    );
  }
}

result-matching ""

    No results matching ""