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/03/24 18:37:52 UTC

[sling-org-apache-sling-event-dea] branch master updated: SLING-11139 maintain metrics for the DEA (#3)

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

joerghoh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-event-dea.git


The following commit(s) were added to refs/heads/master by this push:
     new c5a7768  SLING-11139 maintain metrics for the DEA (#3)
c5a7768 is described below

commit c5a77688dd6176c27dd563d75962f62b8177daa1
Author: Jörg Hoh <jo...@users.noreply.github.com>
AuthorDate: Thu Mar 24 19:37:47 2022 +0100

    SLING-11139 maintain metrics for the DEA (#3)
    
    * SLING-11139 maintain metrics for the DEA
---
 pom.xml                                                  |  6 ++++++
 .../sling/event/dea/impl/DistributedEventAdminImpl.java  | 16 ++++++++++++++++
 .../sling/event/dea/impl/DistributedEventReceiver.java   | 16 ++++++++++++++++
 .../sling/event/dea/impl/DistributedEventSender.java     | 13 +++++++++++++
 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..0c60655 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,17 @@ 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.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 +63,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 +91,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..0b5ff27 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
+    private AtomicInteger successCounter = new AtomicInteger();
+    private AtomicInteger failureCounter = new AtomicInteger();
 
     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.incrementAndGet();
                 } catch (final Exception e) {
                     this.logger.error("Exception during writing the event to the resource tree.", e);
+                    failureCounter.incrementAndGet();
                 }
             }
         }
@@ -440,5 +447,14 @@ public class DistributedEventReceiver
             }
         }
     }
+    
+    
+    public int getSuccessCounter() {
+        return successCounter.get();
+    }
+    
+    public int getFailureCounter() {
+        return failureCounter.get();
+    }
 }
 
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..b017caf 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
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.Resource;
@@ -71,6 +72,8 @@ public class DistributedEventSender
     private final String ownRootPathWithSlash;
 
     private volatile ServiceRegistration<ResourceChangeListener> serviceRegistration;
+    
+    private AtomicInteger postedEventCounter = new AtomicInteger();
 
     public DistributedEventSender(final BundleContext bundleContext,
             final String rootPath,
@@ -202,6 +205,7 @@ public class DistributedEventSender
                             final EventAdmin localEA = this.eventAdmin;
                             if ( localEA != null ) {
                                 localEA.postEvent(e);
+                                postedEventCounter.incrementAndGet();
                             } else {
                                 this.logger.error("Unable to post event as no event admin is available.");
                             }
@@ -244,4 +248,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.get();
+    }
+    
 }