All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sirius.kernel.health.MemoryBasedHealthMonitor Maven / Gradle / Ivy

Go to download

Provides common core classes and the microkernel powering all Sirius applications

There is a newer version: 12.9.1
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.kernel.health;

import sirius.kernel.commons.Strings;
import sirius.kernel.di.std.ConfigValue;
import sirius.kernel.di.std.Register;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Provides a in-memory store for logs and exceptions.
 * 

* This will be inherently limited in size but should always contain the most recent logs and errors. */ @Register(classes = {MemoryBasedHealthMonitor.class, LogTap.class, ExceptionHandler.class}) public class MemoryBasedHealthMonitor implements ExceptionHandler, LogTap { protected final List incidents = Collections.synchronizedList(new ArrayList()); protected final List messages = Collections.synchronizedList(new ArrayList()); @ConfigValue("health.memory.max-errors") private int maxErrors; @ConfigValue("health.memory.max-logs") private int maxMsg; private final Counter numIncidents = new Counter(); private final Counter numUniqueIncidents = new Counter(); private final Counter numLogMessages = new Counter(); @Override public void handle(Incident incident) throws Exception { synchronized (incidents) { boolean unique = true; Iterator iter = incidents.iterator(); while (iter.hasNext()) { if (Strings.areEqual(iter.next().getLocation(), incident.getLocation())) { iter.remove(); unique = false; } } incidents.add(0, incident); numIncidents.inc(); if (unique) { numUniqueIncidents.inc(); } while (incidents.size() > maxErrors) { incidents.remove(incidents.size() - 1); } } } @Override public void handleLogMessage(LogMessage msg) { if (msg.isReceiverWouldLog()) { synchronized (messages) { messages.add(0, msg); numLogMessages.inc(); while (messages.size() > maxMsg) { messages.remove(messages.size() - 1); } } } } /** * Contains all recorded incidents. * * @return all recorded incidents */ public List getIncidents() { return Collections.unmodifiableList(incidents); } /** * Contains all recorded log messages. * * @return all recorded messages */ public List getMessages() { return Collections.unmodifiableList(messages); } /** * Returns the total number of log messages encountered. * * @return the total number of log messages so far. */ public long getNumLogMessages() { return numLogMessages.getCount(); } /** * Returns the total number of incidents (exceptions) encountered. * * @return the total number of incidents so far. */ public long getNumIncidents() { return numIncidents.getCount(); } /** * Returns the total number of unique incidents (with different locations) encountered. * * @return the total number of unique incidents so far. */ public long getNumUniqueIncidents() { return numUniqueIncidents.getCount(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy