Skip to content

Commit

Permalink
fixed metric counter
Browse files Browse the repository at this point in the history
Issue #204
  • Loading branch information
rsoika committed Feb 9, 2024
1 parent 328cf47 commit e2109b7
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 99 deletions.
10 changes: 5 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
// - Linux: $HOME/.config/Code/User/settings.json
// - Mac: $HOME/Library/Application Support/Code/User/settings.json
{
"editor.formatOnSave": true,
// Organize Java Imports
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"java.checkstyle.version": "8.44",
"java.checkstyle.autocheck": true,
// Checkstile
"java.checkstyle.configuration": "https://raw.githubusercontent.com/imixs/imixs-workflow/master/imixs-checkstyle-8.44.xml",
"java.configuration.updateBuildConfiguration": "automatic"
}
"editor.formatOnSave": true
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.eclipse.microprofile.metrics.Counter;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.annotation.RegistryType;
import org.eclipse.microprofile.metrics.annotation.RegistryScope;
import org.imixs.archive.backup.util.LogController;

import jakarta.enterprise.context.RequestScoped;
Expand Down Expand Up @@ -45,7 +45,7 @@ public class BackupController implements Serializable {
String metricsEndpoint;

@Inject
@RegistryType(type = MetricRegistry.Type.APPLICATION)
@RegistryScope(scope = MetricRegistry.APPLICATION_SCOPE)
MetricRegistry metricRegistry;

@Inject
Expand Down Expand Up @@ -147,18 +147,16 @@ public void stop() {
* @return
*/
public long getCounterByName(String name) {

// find counter by name
SortedMap<MetricID, Counter> allCounters = metricRegistry.getCounters();

for (Map.Entry<MetricID, Counter> entry : allCounters.entrySet()) {

MetricID metricID = entry.getKey();
if (metricID.getName().endsWith(name)) {
return entry.getValue().getCount();
}
}
logger.warning("Metric Counter : " + name + " not found!");
logger.fine("Metric Counter : " + name + " not found!");
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import java.util.logging.Logger;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.metrics.Counter;
import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.annotation.RegistryScope;
import org.imixs.archive.backup.util.FTPConnector;
import org.imixs.archive.backup.util.LogController;
import org.imixs.archive.backup.util.RestClientHelper;
Expand Down Expand Up @@ -75,6 +79,9 @@ public class BackupService {
@Inject
LogController logController;

public static final String METRIC_EVENTS_PROCESSED = "backup_events_processed";
public static final String METRIC_EVENTS_ERRORS = "backup_events_errors";

// timeout interval in ms
@Inject
@ConfigProperty(name = BackupApi.WORKFLOW_SYNC_INTERVAL, defaultValue = "10000")
Expand Down Expand Up @@ -121,6 +128,10 @@ public class BackupService {
@Inject
BackupStatusHandler backupStatusHandler;

@Inject
@RegistryScope(scope = MetricRegistry.APPLICATION_SCOPE)
MetricRegistry metricRegistry;

@PostConstruct
public void init() {
// init timer....
Expand Down Expand Up @@ -201,12 +212,17 @@ public void onTimeout(jakarta.ejb.Timer _timer) {
// finally remove the event log entry...
eventLogClient.deleteEventLogEntry(id);
success++;

countMetric(METRIC_EVENTS_PROCESSED);

} catch (InvalidAccessException | EJBException | BackupException | RestAPIException e) {
// we also catch EJBExceptions here because we do not want to cancel the
// ManagedScheduledExecutorService
logController.warning(BackupApi.TOPIC_BACKUP,
"SnapshotEvent " + id + " backup failed: " + e.getMessage());
errors++;
countMetric(METRIC_EVENTS_ERRORS);

}
}

Expand All @@ -230,6 +246,23 @@ public void onTimeout(jakarta.ejb.Timer _timer) {
}
}

/**
* Helper method to create a counter and inc the coutner
*
* @param name
*/
private void countMetric(String name) {

try {
Metadata metadata = Metadata.builder().withName(name)
.withDescription("Imixs-Backup Service - processed backup events").build();
Counter counter = metricRegistry.counter(metadata);
counter.inc();
} catch (Exception e) {
logger.severe("Unable to create metrics for ' " + name + "'");
}
}

/**
* Asynchronous method to release dead locks
*
Expand Down
179 changes: 90 additions & 89 deletions imixs-archive-backup/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
@@ -1,116 +1,117 @@
<ui:composition template="/WEB-INF/templates/layout.xhtml"
xmlns:faces="jakarta.faces" xmlns:f="jakarta.faces.core"
<ui:composition template="/WEB-INF/templates/layout.xhtml" xmlns:faces="jakarta.faces" xmlns:f="jakarta.faces.core"
xmlns:h="jakarta.faces.html" xmlns:ui="jakarta.faces.facelets">

<ui:define name="content">
<f:view>

<h:form id="connect_form_id">

<section class="" style="">
<div class="card" style="grid-column-start: span 12;min-height: 0px;">
<h1 style="line-height: 0;">
<span class="typcn typcn-cloud-storage-outline"></span> Imixs Backup Service
</h1>
</div>
</section>

<section class="" style="">
<div class="card" style="grid-column-start: span 12;">


<p class="help">
<span class="typcn typcn-lightbulb"></span> Imixs-Workflow API
Endpoint Configuration.
</p>

<div class="imixs-form-section" style="grid-column-start: span 12;">


<table class="imixsdatatable"
style="grid-column-start: span 12; margin: 10px 0;">
<tr>
<td style="width: 25%;">Imixs-Workflow Instance:</td>
<td>#{backupController.getInstanceEndpoint()}</td>
</tr>

<tr>
<td>Backup Status:</td>
<td>#{backupController.getStatus()} <ui:fragment
rendered="#{backupController.isConnected()}">
<span class="typcn typcn-media-record"
style="font-size: 1.3em; color: #5c9e5c;"></span>
</ui:fragment> <ui:fragment rendered="#{!backupController.isConnected()}">
<span class="typcn typcn-media-record"
style="font-size: 1.3em; color: #e84848;"></span>
</ui:fragment>

</td>
</tr>

<tr>
<td>Interval:</td>
<td>#{backupController.getInterval()} ms</td>
</tr>

<tr>
<td>Next Timeout:</td>
<td>#{backupController.getNextTimeout()}</td>
</tr>


<tr>
<td>FTP Host:</td>
<td>#{backupController.ftpServer}:#{backupController.ftpPort}</td>
</tr>

<tr>
<td>Backup Space:</td>
<td>#{backupController.ftpPath}</td>
</tr>

</table>
<section class="" style="">
<div class="card" style="grid-column-start: span 12;min-height: 0px;">
<h1 style="line-height: 0;">
<span class="typcn typcn-cloud-storage-outline"></span> Imixs Backup Service
</h1>
</div>
</section>

<div class="imixs-form-section">

<span style="grid-column-start: span 8;"> <h:commandButton
value="Refresh" action="/" /> <h:commandButton value="Start"
style="background:#4dc24f;"
actionListener="#{backupController.start()}"
rendered="#{!backupController.connected}" /> <h:commandButton
value="Stop" style="background: #e84848;"
rendered="#{backupController.connected}"
actionListener="#{backupController.stop()}" />
</span>
<section class="" style="">
<div class="card" style="grid-column-start: span 12;">


<p class="help">
<span class="typcn typcn-lightbulb"></span> Imixs-Workflow API
Endpoint Configuration.
</p>

<div class="imixs-form-section" style="grid-column-start: span 12;">


<table class="imixsdatatable" style="grid-column-start: span 12; margin: 10px 0;">
<tr>
<td style="width: 25%;">Imixs-Workflow Instance:</td>
<td>#{backupController.getInstanceEndpoint()}</td>
</tr>

<tr>
<td>Backup Status:</td>
<td>#{backupController.getStatus()} <ui:fragment
rendered="#{backupController.isConnected()}">
<span class="typcn typcn-media-record"
style="font-size: 1.3em; color: #5c9e5c;"></span>
</ui:fragment>
<ui:fragment rendered="#{!backupController.isConnected()}">
<span class="typcn typcn-media-record"
style="font-size: 1.3em; color: #e84848;"></span>
</ui:fragment>

</td>
</tr>

<tr>
<td>Interval:</td>
<td>#{backupController.getInterval()} ms</td>
</tr>

<tr>
<td>Next Timeout:</td>
<td>#{backupController.getNextTimeout()}</td>
</tr>


<tr>
<td>FTP Host:</td>
<td>#{backupController.ftpServer}:#{backupController.ftpPort}</td>
</tr>

<tr>
<td>Backup Space:</td>
<td>#{backupController.ftpPath}</td>
</tr>

</table>
</div>

<div class="imixs-form-section">

<span style="grid-column-start: span 8;">
<h:commandButton value="Refresh" action="/" />
<h:commandButton value="Start" style="background:#4dc24f;"
actionListener="#{backupController.start()}"
rendered="#{!backupController.connected}" />
<h:commandButton value="Stop" style="background: #e84848;"
rendered="#{backupController.connected}"
actionListener="#{backupController.stop()}" />
</span>
</div>
</div>
</div>

</section>

<!-- Metrics -->



<section class="" style="">
<div class="card" style="grid-column-start: span 7;">

<h1 class="lead">
#{exportController.getCounterByName('eventProcessed')} Events
#{backupController.getCounterByName('backup_events_processed')} Events
</h1>
<p class="lead">
Number of processed export events since last start.</p>
Total Number of processed backup events since last start.</p>



</div>
<div class="card" style="grid-column-start: span 5;">
<h1 class="lead">
<span style="#{(exportController.getCounterByName('errors')>0)?'color: #e84848;':''}">#{exportController.getCounterByName('errors')} Errors</span>
<span
style="#{(backupController.getCounterByName('backup_events_errors')>0)?'color: #e84848;':''}">#{backupController.getCounterByName('backup_events_errors')}
Errors</span>
</h1>
<p class="lead">
Number of processed export errors since last start.</p>
Total backup errros since last start.</p>

</div>
</section>

Expand All @@ -130,4 +131,4 @@
</h:form>
</f:view>
</ui:define>
</ui:composition>
</ui:composition>

0 comments on commit e2109b7

Please sign in to comment.