sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chet...@apache.org
Subject svn commit: r1723047 - in /sling/whiteboard/chetanm/metrics: pom.xml src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java
Date Tue, 05 Jan 2016 11:47:10 GMT
Author: chetanm
Date: Tue Jan  5 11:47:09 2016
New Revision: 1723047

URL: http://svn.apache.org/viewvc?rev=1723047&view=rev
Log:
SLING-4080 - API to capture/measure application-level metrics

Add webconsole printer for MetricRegistry metrices

Added:
    sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java
  (with props)
Modified:
    sling/whiteboard/chetanm/metrics/pom.xml
    sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java

Modified: sling/whiteboard/chetanm/metrics/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/chetanm/metrics/pom.xml?rev=1723047&r1=1723046&r2=1723047&view=diff
==============================================================================
--- sling/whiteboard/chetanm/metrics/pom.xml (original)
+++ sling/whiteboard/chetanm/metrics/pom.xml Tue Jan  5 11:47:09 2016
@@ -98,6 +98,18 @@
     </dependency>
 
     <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.inventory</artifactId>
+      <version>1.0.2</version>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>2.2</version>
+    </dependency>
+
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <scope>test</scope>

Added: sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java?rev=1723047&view=auto
==============================================================================
--- sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java
(added)
+++ sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java
Tue Jan  5 11:47:09 2016
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sling.metrics.internal;
+
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import com.codahale.metrics.ConsoleReporter;
+import com.codahale.metrics.Metric;
+import com.codahale.metrics.MetricRegistry;
+import org.apache.commons.io.output.WriterOutputStream;
+import org.apache.felix.inventory.Format;
+import org.apache.felix.inventory.InventoryPrinter;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service(value = InventoryPrinter.class)
+@Properties({
+        @Property(name = InventoryPrinter.FORMAT, value = {"TEXT" }),
+        @Property(name = InventoryPrinter.NAME, value = "slingmetrics"),
+        @Property(name = InventoryPrinter.TITLE, value = "Sling Metrics"),
+        @Property(name = InventoryPrinter.WEBCONSOLE, boolValue = true)
+})
+public class MetricPrinter implements InventoryPrinter, ServiceTrackerCustomizer<MetricRegistry,
MetricRegistry>{
+    /**
+     * Service property name which stores the MetricRegistry name as a given OSGi
+     * ServiceRegistry might have multiple instances of MetricRegistry
+     */
+    public static final String METRIC_REGISTRY_NAME = "name";
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private BundleContext context;
+    private ServiceTracker<MetricRegistry, MetricRegistry> tracker;
+    private ConcurrentMap<ServiceReference, MetricRegistry> registries
+            = new ConcurrentHashMap<ServiceReference, MetricRegistry>();
+
+    @Activate
+    private void activate(BundleContext context){
+        this.context = context;
+        tracker = new ServiceTracker<MetricRegistry, MetricRegistry>(context, MetricRegistry.class,
this);
+        tracker.open();
+    }
+
+    @Deactivate
+    private void deactivate(BundleContext context){
+        tracker.close();
+    }
+
+    //~--------------------------------------------< InventoryPrinter >
+
+    @Override
+    public void print(PrintWriter printWriter, Format format, boolean isZip) {
+        if (format == Format.TEXT) {
+            MetricRegistry registry = getConsolidatedRegistry();
+            ConsoleReporter reporter = ConsoleReporter.forRegistry(registry)
+                    .outputTo(new PrintStream(new WriterOutputStream(printWriter)))
+                    .build();
+            reporter.report();
+            reporter.close();
+        }
+    }
+
+
+    //~---------------------------------------------< ServiceTracker >
+
+    @Override
+    public MetricRegistry addingService(ServiceReference<MetricRegistry> serviceReference)
{
+        MetricRegistry registry = context.getService(serviceReference);
+        registries.put(serviceReference, registry);
+        return registry;
+    }
+
+    @Override
+    public void modifiedService(ServiceReference<MetricRegistry> serviceReference,
MetricRegistry registry) {
+        registries.put(serviceReference, registry);
+    }
+
+    @Override
+    public void removedService(ServiceReference<MetricRegistry> serviceReference, MetricRegistry
registry) {
+        registries.remove(serviceReference);
+    }
+
+    //~----------------------------------------------< internal >
+
+    private MetricRegistry getConsolidatedRegistry() {
+        MetricRegistry registry = new MetricRegistry();
+        for (Map.Entry<ServiceReference, MetricRegistry> registryEntry : registries.entrySet()){
+            String metricRegistryName = (String) registryEntry.getKey().getProperty(METRIC_REGISTRY_NAME);
+            for (Map.Entry<String, Metric> metricEntry : registryEntry.getValue().getMetrics().entrySet()){
+                String metricName = metricEntry.getKey();
+                try{
+                    if (metricRegistryName != null){
+                        metricName = metricRegistryName + ":" + metricName;
+                    }
+                    registry.register(metricName, metricEntry.getValue());
+                }catch (IllegalArgumentException ex){
+                    log.warn("Duplicate Metric name found {}", metricName, ex);
+                }
+            }
+        }
+        return registry;
+    }
+}

Propchange: sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java?rev=1723047&r1=1723046&r2=1723047&view=diff
==============================================================================
--- sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java
(original)
+++ sling/whiteboard/chetanm/metrics/src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java
Tue Jan  5 11:47:09 2016
@@ -76,6 +76,7 @@ public class MetricsServiceImpl implemen
         final Dictionary<String, String> regProps = new Hashtable<String, String>();
         regProps.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Metrics Registry");
         regProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+        regProps.put("name", "sling");
         regs.add(context.registerService(MetricRegistry.class.getName(), registry, regProps));
     }
 



Mime
View raw message