Skip to content

Commit

Permalink
added provider name to DEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
matjaz99 committed Jun 15, 2023
1 parent 1c37d3f commit eabc5ec
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [ENHANCEMENT] Added link to active alerts directly from providers
* [ENHANCEMENT] Added POJO support in MongoDB client
* [ENHANCEMENT] Added configuration option for `ALERTMONITOR_MONGODB_CONNECT_TIMEOUT_SEC` and `ALERTMONITOR_MONGODB_READ_TIMEOUT_SEC`
* [ENHANCEMENT] Added `provider` attribute to alerts

## 2.4.4-SNAPSHOT

Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ services:
- ALERTMONITOR_PROMETHEUS_ID_LABELS="cluster, region, monitor"
- ALERTMONITOR_MONGODB_ENABLED=false
- ALERTMONITOR_MONGODB_CONNECTION_STRING="mongodb://admin:mongodbpassword@mongodb:27017/?authSource=admin"
- ALERTMONITOR_MONGODB_CONNECT_TIMEOUT_SEC="5"
- ALERTMONITOR_MONGODB_READ_TIMEOUT_SEC="30"
volumes:
- /var/log:/opt/alertmonitor/log
deploy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,16 @@ public String getAlertsPerSecondInLastHour() {
}

@Override
public DEvent getEvent(String id) {
logger.info("MongoDbDataManager: getEvent id=" + id);
public DEvent getEvent(String uid) {
logger.info("MongoDbDataManager: getEvent id=" + uid);

try {
MongoDatabase db = mongoClient.getDatabase(AmProps.ALERTMONITOR_MONGODB_DB_NAME);
MongoCollection<Document> collection = db.getCollection("journal");
// MongoCollection<Document> collection = db.getCollection("journal");
MongoCollection<DEvent> collection = db.getCollection("journal", DEvent.class);

Document doc = collection.find(Filters.eq("uid", id)).first();
DEvent event = convertToDEvent(doc);
DEvent event = collection.find(Filters.eq("uid", uid)).first();
// DEvent event = convertToDEvent(doc);
DAO.getInstance().removeWarningFromAllProviders("mongo");
AmMetrics.alertmonitor_db_queries_total.labels("journal").inc();
return event;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/si/matjazcerkvenik/alertmonitor/model/DEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public class DEvent implements Cloneable {
/** Labels that identify the prometheus */
@BsonProperty(value = "prometheusId")
private String prometheusId;

/** Provider name who sent alert */
@BsonProperty(value = "provider")
private String provider;

/** Timestamp of first occurrence */
@BsonProperty(value = "timestamp")
Expand Down Expand Up @@ -196,6 +200,14 @@ public void setPrometheusId(String prometheusId) {
this.prometheusId = prometheusId;
}

public String getProvider() {
return provider;
}

public void setProvider(String provider) {
this.provider = provider;
}

public long getTimestamp() {
return timestamp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.gson.GsonBuilder;
import si.matjazcerkvenik.alertmonitor.model.DEvent;
import si.matjazcerkvenik.alertmonitor.model.DSeverity;
import si.matjazcerkvenik.alertmonitor.model.config.ProviderConfig;
import si.matjazcerkvenik.alertmonitor.model.config.YamlConfig;
import si.matjazcerkvenik.alertmonitor.util.*;
import si.matjazcerkvenik.alertmonitor.util.Formatter;
import si.matjazcerkvenik.alertmonitor.web.WebhookMessage;
Expand Down Expand Up @@ -82,6 +84,18 @@ public static List<DEvent> convertToDevent(WebhookMessage m, AmAlertMessage am)
s = s.substring(0, s.length()-2) + "}";
e.setPrometheusId(s);

// set provider name
if (AmProps.yamlConfig != null) {
for (ProviderConfig pc : AmProps.yamlConfig.getProviders()) {
if (pc.getUri().equalsIgnoreCase(m.getRequestUri())) {
e.setProvider(pc.getName());
break;
}
}
} else {
e.setProvider(AmProps.ALERTMONITOR_DATAPROVIDERS_DEFAULT_PROVIDER_NAME);
}

// set all other labels
e.setOtherLabels(a.getLabels());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package si.matjazcerkvenik.alertmonitor.model.eventlogger;

public class EEvent {
public class ElEvent {

private String alarmId;
private String alarmSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public boolean synchronizeAlerts(List<DEvent> alertList, boolean sync) {
e.setToBeDeleted(true);
}

List<DEvent> newAlerts = new ArrayList<>();
List<DEvent> newAlertsList = new ArrayList<>();
int newAlertsCount = 0;

for (DEvent e : alertList) {
Expand All @@ -151,7 +151,7 @@ public boolean synchronizeAlerts(List<DEvent> alertList, boolean sync) {
e.setFirstTimestamp(e.getTimestamp());
e.setLastTimestamp(e.getTimestamp());
addActiveAlert(e);
newAlerts.add(e);
newAlertsList.add(e);
newAlertsCount++;
}
}
Expand All @@ -167,7 +167,7 @@ public boolean synchronizeAlerts(List<DEvent> alertList, boolean sync) {
logger.info("SYNC[" + providerConfig.getName() + "]: removing active alert: {cid=" + cid + "}");
removeActiveAlert(activeAlerts.get(cid));
}
addToJournal(newAlerts);
addToJournal(newAlertsList);

logger.info("SYNC[" + providerConfig.getName() + "]: total sync alerts count: " + alertList.size());
logger.info("SYNC[" + providerConfig.getName() + "]: new alerts count: " + newAlertsCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import si.matjazcerkvenik.alertmonitor.model.DEvent;
import si.matjazcerkvenik.alertmonitor.model.DTarget;
import si.matjazcerkvenik.alertmonitor.model.DWarning;
import si.matjazcerkvenik.alertmonitor.model.alertmanager.AlertmanagerProcessor;
import si.matjazcerkvenik.alertmonitor.model.alertmanager.AmAlertMessage;
import si.matjazcerkvenik.alertmonitor.model.eventlogger.EEvent;
import si.matjazcerkvenik.alertmonitor.model.eventlogger.ElEvent;
import si.matjazcerkvenik.alertmonitor.util.AmMetrics;
import si.matjazcerkvenik.alertmonitor.util.Formatter;
import si.matjazcerkvenik.alertmonitor.util.LogFactory;
Expand All @@ -49,7 +47,7 @@ public void processIncomingEvent(WebhookMessage m) {
try {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
EEvent am = gson.fromJson(m.getBody(), EEvent.class);
ElEvent am = gson.fromJson(m.getBody(), ElEvent.class);

DEvent e = new DEvent();
e.setTimestamp(System.currentTimeMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void run() {
}

// all alerts retrieved by sync
List<DEvent> syncAlerts = new ArrayList<>();
List<DEvent> syncAlertsList = new ArrayList<>();

for (PAlert alert : activeAlerts) {
logger.debug(alert.toString());
Expand Down Expand Up @@ -97,6 +97,9 @@ public void run() {
s = s.substring(0, s.length()-2) + "}";
e.setPrometheusId(s);

// set provider name
e.setProvider(dataProvider.providerConfig.getName());

// set all other labels
e.setOtherLabels(alert.getLabels());

Expand Down Expand Up @@ -127,11 +130,11 @@ public void run() {
e.generateCID();

logger.debug("SYNCTASK[" + dataProvider.getProviderConfig().getName() + "]: " + e.toString());
syncAlerts.add(e);
syncAlertsList.add(e);

} // for each alert

dataProvider.synchronizeAlerts(syncAlerts, true);
dataProvider.synchronizeAlerts(syncAlertsList, true);

dataProvider.syncSuccessCount++;
AmMetrics.alertmonitor_sync_success.labels(dataProvider.providerConfig.getName()).set(1);
Expand Down
7 changes: 5 additions & 2 deletions src/main/webapp/alert/alert.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<f:param name="uid" value="#{uiAlertBean.event.clearUid}" />
</h:link>

<h:outputText value="prometheusId" />
<h:outputText value="#{uiAlertBean.event.prometheusId}" />
<h:outputText value="provider" />
<h:outputText value="#{uiAlertBean.event.provider}" />

<h:outputText value="severity" />
<am:singletag value="#{uiAlertBean.getTagObject(uiAlertBean.event.severity)}">
Expand Down Expand Up @@ -99,6 +99,9 @@
<h:outputText value="otherLabels" />
<h:outputText value="#{uiAlertBean.event.otherLabelsString}" />

<h:outputText value="prometheusId" />
<h:outputText value="#{uiAlertBean.event.prometheusId}" />

<h:outputText value="generatorUrl" />
<h:outputText value="#{uiAlertBean.event.generatorUrl}" />

Expand Down

0 comments on commit eabc5ec

Please sign in to comment.