This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-0.0.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 51f39e45c45850c896a08e5691d3bca9b7278eea Author: Chetan Mehrotra <[email protected]> AuthorDate: Tue Jan 5 11:47:09 2016 +0000 SLING-4080 - API to capture/measure application-level metrics Add webconsole printer for MetricRegistry metrices git-svn-id: https://svn.apache.org/repos/asf/sling/whiteboard/chetanm/metrics@1723047 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 12 ++ .../sling/metrics/internal/MetricPrinter.java | 133 +++++++++++++++++++++ .../sling/metrics/internal/MetricsServiceImpl.java | 1 + 3 files changed, 146 insertions(+) diff --git a/pom.xml b/pom.xml index 6c87f50..7a4d955 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java b/src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java new file mode 100644 index 0000000..7ac78bd --- /dev/null +++ b/src/main/java/org/apache/sling/metrics/internal/MetricPrinter.java @@ -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; + } +} diff --git a/src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java b/src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java index 3933e78..dea3ea2 100644 --- a/src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java +++ b/src/main/java/org/apache/sling/metrics/internal/MetricsServiceImpl.java @@ -76,6 +76,7 @@ public class MetricsServiceImpl implements MetricsService{ 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)); } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
