You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2022/02/11 17:25:04 UTC

[sling-org-apache-sling-event-dea] 01/01: SLING-11139 maintain metrics for the DEA

This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch improvement/SLING-11139-metrics-for-DEA
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-event-dea.git

commit 505114f925854873f5569479471fe90873c3fa12
Author: Jörg Hoh <jo...@joerghoh.de>
AuthorDate: Fri Feb 11 18:24:47 2022 +0100

    SLING-11139 maintain metrics for the DEA
---
 pom.xml                                                 |  6 ++++++
 .../sling/event/dea/impl/DistributedEventAdminImpl.java | 17 +++++++++++++++++
 .../sling/event/dea/impl/DistributedEventReceiver.java  | 16 ++++++++++++++++
 .../sling/event/dea/impl/DistributedEventSender.java    | 12 ++++++++++++
 4 files changed, 51 insertions(+)

diff --git a/pom.xml b/pom.xml
index 7e89b28..8d531e3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -109,6 +109,12 @@
             <version>1.2.0</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.metrics</artifactId>
+            <version>1.2.10</version>
+            <scope>provided</scope>
+        </dependency>
       <!-- Testing -->
         <dependency>
             <groupId>junit</groupId>
diff --git a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventAdminImpl.java b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventAdminImpl.java
index 809b0ba..fe3a70a 100644
--- a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventAdminImpl.java
+++ b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventAdminImpl.java
@@ -21,13 +21,18 @@ package org.apache.sling.event.dea.impl;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.serviceusermapping.ServiceUserMapped;
 import org.apache.sling.settings.SlingSettingsService;
+import org.apache.sling.commons.metrics.Gauge;
+import org.apache.sling.commons.metrics.MetricsService;
 import org.osgi.framework.BundleContext;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Deactivate;
 import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.osgi.service.event.EventAdmin;
 
+
+
 /**
  * This service wraps the configuration of the distributed event admin
  * and starts the different parts.
@@ -59,6 +64,9 @@ public class DistributedEventAdminImpl {
     @Reference
     private ServiceUserMapped serviceUserMapped;
 
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL)
+    MetricsService metrics;
+    
     /** Default repository path. */
     public static final String DEFAULT_REPOSITORY_PATH = "/var/eventing/distribution";
 
@@ -84,6 +92,15 @@ public class DistributedEventAdminImpl {
                               props.repository_path(),
                               ownRootPath,
                               this.resourceResolverFactory, this.eventAdmin);
+        
+        if (metrics != null) {
+            // It's easier to use a gauge here instead of passing the (optional) MetricsRegistry into 
+            // each of these 2 services, so they can register counters themselves.
+            metrics.gauge("org.apache.sling.event.dea.receiver.successCounter", receiver::getSuccessCounter);
+            metrics.gauge("org.apache.sling.event.dea.receiver.failureCounter", receiver::getFailureCounter);
+            metrics.gauge("org.apache.sling.event.dea.sender.postedEventsCounter", sender::getPostedEventCounter);
+        }
+        
     }
 
     @Deactivate
diff --git a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventReceiver.java b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventReceiver.java
index 6fe6302..c8ea420 100644
--- a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventReceiver.java
+++ b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventReceiver.java
@@ -28,6 +28,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.sling.api.resource.LoginException;
@@ -98,6 +99,10 @@ public class DistributedEventReceiver
 
     /** The service registration. */
     private volatile ServiceRegistration<?> serviceRegistration;
+    
+    // counters can be simple ints, as there's only 1 thread
+    private volatile int successCounter = 0;
+    private volatile int failureCounter = 0;
 
     public DistributedEventReceiver(final BundleContext bundleContext,
             final String rootPath,
@@ -202,8 +207,10 @@ public class DistributedEventReceiver
             if ( event != null && this.running ) {
                 try {
                     this.writeEvent(event);
+                    successCounter++;
                 } catch (final Exception e) {
                     this.logger.error("Exception during writing the event to the resource tree.", e);
+                    failureCounter++;
                 }
             }
         }
@@ -440,5 +447,14 @@ public class DistributedEventReceiver
             }
         }
     }
+    
+    
+    public int getSuccessCounter() {
+        return successCounter;
+    }
+    
+    public int getFailureCounter() {
+        return failureCounter;
+    }
 }
 
diff --git a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventSender.java b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventSender.java
index 3159931..950da29 100644
--- a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventSender.java
+++ b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventSender.java
@@ -71,6 +71,8 @@ public class DistributedEventSender
     private final String ownRootPathWithSlash;
 
     private volatile ServiceRegistration<ResourceChangeListener> serviceRegistration;
+    
+    private volatile int postedEventCounter = 0;
 
     public DistributedEventSender(final BundleContext bundleContext,
             final String rootPath,
@@ -202,6 +204,7 @@ public class DistributedEventSender
                             final EventAdmin localEA = this.eventAdmin;
                             if ( localEA != null ) {
                                 localEA.postEvent(e);
+                                postedEventCounter++;
                             } else {
                                 this.logger.error("Unable to post event as no event admin is available.");
                             }
@@ -244,4 +247,13 @@ public class DistributedEventSender
             this.logger.debug("Ignored exception " + e.getMessage(), e);
         }
     }
+    
+    /**
+     * Return the number of events which have been posted to the local eventAdmin
+     * @return number of events
+     */
+    public int getPostedEventCounter() {
+        return postedEventCounter;
+    }
+    
 }