You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:22:50 UTC
[sling-org-apache-sling-commons-metrics] annotated tag
org.apache.sling.commons.metrics-1.2.0 created (now 2ca43b2)
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a change to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git.
at 2ca43b2 (tag)
tagging cb8ec0a133d15d939d870f721d343abc42bf389c (commit)
by Chetan Mehrotra
on Mon Nov 28 09:31:47 2016 +0000
- Log -----------------------------------------------------------------
org.apache.sling.commons.metrics-1.2.0
-----------------------------------------------------------------------
This annotated tag includes the following new commits:
new a22642b SLING-4080 - API to capture/measure application-level metrics
new 80a2f48 SLING-4080 - API to capture/measure application-level metrics
new 2f36180 SLING-4080 - API to capture/measure application-level metrics
new 66282a3 SLING-4080 - API to capture/measure application-level metrics
new 51f39e4 SLING-4080 - API to capture/measure application-level metrics
new a41c22d SLING-4080 - API to capture/measure application-level metrics
new 1cb754c SLING-4080 - API to capture/measure application-level metrics
new 6168de6 SLING-4080 - API to capture/measure application-level metrics
new e2c7c10 SLING-4080 - API to capture/measure application-level metrics
new 6bd68f8 SLING-4080 - API to capture/measure application-level metrics Moving to trunk from sandbox
new b67d410 SLING-4080 - API to capture/measure application-level metrics
new 79e3480 SLING-4080 - API to capture/measure application-level metrics
new bbd4906 SLING-4080 - API to capture/measure application-level metrics
new eb6c21b SLING-4080 - API to capture/measure application-level metrics
new 2c1237f SLING-4080 - API to capture/measure application-level metrics
new f332c2e SLING-4080 - API to capture/measure application-level metrics
new a523422 SLING-4080 - API to capture/measure application-level metrics
new f6846d7 SLING-4080 - API to capture/measure application-level metrics
new 8b19b8e SLING-4080 - API to capture/measure application-level metrics
new e75be94 SLING-4080 - API to capture/measure application-level metrics
new 97878b9 SLING-4080 - API to capture/measure application-level metrics
new f54222d [maven-release-plugin] prepare release org.apache.sling.commons.metrics-0.0.2
new 5e06af8 [maven-release-plugin] prepare for next development iteration
new 15ce3aa SLING-4080 - API to capture/measure application-level metrics
new 1889f9b svn:ignore
new 71d7fff [maven-release-plugin] prepare release org.apache.sling.commons.metrics-1.0.0
new e5d38fb [maven-release-plugin] prepare for next development iteration
new 8ca6065 SLING-5424 - MBeanServer reference in MetricServiceImpl should be made optional
new 2871fc7 SLING-5693 - Add support for exporting MetricRegistry data as JSON
new 91f51c8 SLING-5899 - "Guages" Typo in Sling metrics console
new 8d6a1a2 Update to parent pom 29
new 3ca6f09 SLING-5443 - Review the naming conventions used by JMXReporter to register the Metric MBeans
new f9f9947 SLING-5443 - Review the naming conventions used by JMXReporter to register the Metric MBeans
new 8c9ec74 SLING-5443 - Review the naming conventions used by JMXReporter to register the Metric MBeans
new 090aa7b SLING-5443 - Review the naming conventions used by JMXReporter to register the Metric MBeans
new fa6a655 SLING-5966 - Add Gauge support to metrics
new 709afe3 SLING-6297 - Switch to standard OSGi annotation in Metrics
new c504247 [maven-release-plugin] prepare release org.apache.sling.commons.metrics-1.2.0
new cb8ec0a [maven-release-plugin] copy for tag org.apache.sling.commons.metrics-1.2.0
The 39 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
--
To stop receiving notification emails like this one, please contact
['"commits@sling.apache.org" <co...@sling.apache.org>'].
[sling-org-apache-sling-commons-metrics] 12/13:
[maven-release-plugin] prepare release
org.apache.sling.commons.metrics-1.2.0
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit c5042470b3db4e5838c30b98a0127e3a499266a7
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Mon Nov 28 09:31:33 2016 +0000
[maven-release-plugin] prepare release org.apache.sling.commons.metrics-1.2.0
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1771692 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/pom.xml b/pom.xml
index 31cfea9..3275dd5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -30,7 +30,7 @@
<artifactId>org.apache.sling.commons.metrics</artifactId>
<packaging>bundle</packaging>
- <version>1.0.1-SNAPSHOT</version>
+ <version>1.2.0</version>
<name>Apache Sling Metrics</name>
<description>
@@ -40,9 +40,9 @@
</description>
<scm>
- <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics</connection>
- <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics</developerConnection>
- <url>http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics</url>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/sling/tags/org.apache.sling.commons.metrics-1.2.0</connection>
+ <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.commons.metrics-1.2.0</developerConnection>
+ <url>http://svn.apache.org/viewvc/sling/tags/org.apache.sling.commons.metrics-1.2.0</url>
</scm>
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 02/13: SLING-5424 -
MBeanServer reference in MetricServiceImpl should be made optional
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 8ca6065c655fca0f476661b07542d78b2dbffd4d
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Wed Jan 13 04:58:38 2016 +0000
SLING-5424 - MBeanServer reference in MetricServiceImpl should be made optional
-- Added test to check for MBean registration - Due to missing start call on JMXReporter
Mbeans were not getting registered
-- Made reference optional - If not set default PlatformMBeanServer is used
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1724354 13f79535-47bb-0310-9956-ffa450edef68
---
.../metrics/internal/MetricsServiceImpl.java | 29 ++++++++++++++-------
.../metrics/internal/MetricServiceTest.java | 30 ++++++++++++++++++++--
2 files changed, 48 insertions(+), 11 deletions(-)
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
index a0e0520..d2f2728 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
@@ -20,6 +20,7 @@
package org.apache.sling.commons.metrics.internal;
import java.io.IOException;
+import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
@@ -36,6 +37,7 @@ 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.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.sling.commons.metrics.Meter;
import org.apache.sling.commons.metrics.MetricsService;
import org.apache.sling.commons.metrics.Timer;
@@ -52,21 +54,14 @@ public class MetricsServiceImpl implements MetricsService {
private final ConcurrentMap<String, Metric> metrics = new ConcurrentHashMap<String, Metric>();
private final MetricRegistry registry = new MetricRegistry();
- @Reference
+ @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
private MBeanServer server;
private JmxReporter reporter;
@Activate
private void activate(BundleContext context, Map<String, Object> config) {
- //TODO Domain name should be based on calling bundle
- //For that we can register ServiceFactory and make use of calling
- //bundle symbolic name to determine the mapping
-
- reporter = JmxReporter.forRegistry(registry)
- .inDomain("org.apache.sling")
- .registerWith(server)
- .build();
+ enableJMXReporter();
final Dictionary<String, String> svcProps = new Hashtable<String, String>();
svcProps.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Metrics Service");
@@ -205,4 +200,20 @@ public class MetricsServiceImpl implements MetricsService {
boolean isInstance(Metric metric);
}
+
+ private void enableJMXReporter() {
+ //TODO Domain name should be based on calling bundle
+ //For that we can register ServiceFactory and make use of calling
+ //bundle symbolic name to determine the mapping
+
+ if (server == null){
+ server = ManagementFactory.getPlatformMBeanServer();
+ }
+
+ reporter = JmxReporter.forRegistry(registry)
+ .inDomain("org.apache.sling")
+ .registerWith(server)
+ .build();
+ reporter.start();
+ }
}
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
index a028abf..5885426 100644
--- a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
@@ -21,9 +21,14 @@ package org.apache.sling.commons.metrics.internal;
import java.lang.management.ManagementFactory;
import java.util.Collections;
+import java.util.Set;
import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.Query;
+import javax.management.QueryExp;
+import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import org.apache.sling.commons.metrics.Counter;
import org.apache.sling.commons.metrics.Histogram;
@@ -36,9 +41,13 @@ import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class MetricServiceTest {
@@ -48,8 +57,8 @@ public class MetricServiceTest {
private MetricsServiceImpl service = new MetricsServiceImpl();
@After
- public void registerMBeanServer() {
- context.registerService(MBeanServer.class, ManagementFactory.getPlatformMBeanServer());
+ public void deactivate(){
+ MockOsgi.deactivate(service);
}
@Test
@@ -118,6 +127,23 @@ public class MetricServiceTest {
service.timer("test");
}
+ @Test
+ public void jmxRegistration() throws Exception{
+ MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+ activate();
+ Meter meter = service.meter("test");
+ assertNotNull(meter);
+ QueryExp q = Query.isInstanceOf(Query.value(JmxReporter.JmxMeterMBean.class.getName()));
+ Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.sling:name=*"), q);
+ assertThat(names, is(not(empty())));
+
+ MockOsgi.deactivate(service);
+
+ names = server.queryNames(new ObjectName("org.apache.sling:name=*"), q);
+ assertThat(names, is(empty()));
+
+ }
+
private MetricRegistry getRegistry(){
return context.getService(MetricRegistry.class);
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 06/13: SLING-5443 - Review
the naming conventions used by JMXReporter to register the Metric MBeans
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 3ca6f09f9f26bfd505e1e9a472f9a81a33ad0c33
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Thu Nov 17 05:08:12 2016 +0000
SLING-5443 - Review the naming conventions used by JMXReporter to register the Metric MBeans
Update the osgi-mock version to make use of ServiceFactory support
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1770118 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 10 +++++-----
.../sling/commons/metrics/internal/MetricServiceTest.java | 6 +++---
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/pom.xml b/pom.xml
index 8ca2ac8..3a45b75 100644
--- a/pom.xml
+++ b/pom.xml
@@ -90,14 +90,14 @@
</dependency>
<dependency>
<groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
- <version>4.3.1</version>
+ <artifactId>osgi.core</artifactId>
+ <version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
- <artifactId>org.osgi.compendium</artifactId>
- <version>4.3.1</version>
+ <artifactId>osgi.cmpn</artifactId>
+ <version>6.0.0</version>
<scope>provided</scope>
</dependency>
@@ -134,7 +134,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.osgi-mock</artifactId>
- <version>1.3.0</version>
+ <version>2.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
index 5885426..ca76690 100644
--- a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
@@ -58,7 +58,7 @@ public class MetricServiceTest {
@After
public void deactivate(){
- MockOsgi.deactivate(service);
+ MockOsgi.deactivate(service, context.bundleContext());
}
@Test
@@ -70,7 +70,7 @@ public class MetricServiceTest {
assertNotNull(service.adaptTo(MetricRegistry.class));
- MockOsgi.deactivate(service);
+ MockOsgi.deactivate(service, context.bundleContext());
assertNull(context.getService(MetricRegistry.class));
assertNull(context.getService(MetricsService.class));
@@ -137,7 +137,7 @@ public class MetricServiceTest {
Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.sling:name=*"), q);
assertThat(names, is(not(empty())));
- MockOsgi.deactivate(service);
+ MockOsgi.deactivate(service, context.bundleContext());
names = server.queryNames(new ObjectName("org.apache.sling:name=*"), q);
assertThat(names, is(empty()));
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 13/13:
[maven-release-plugin] copy for tag org.apache.sling.commons.metrics-1.2.0
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit cb8ec0a133d15d939d870f721d343abc42bf389c
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Mon Nov 28 09:31:47 2016 +0000
[maven-release-plugin] copy for tag org.apache.sling.commons.metrics-1.2.0
git-svn-id: https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.commons.metrics-1.2.0@1771693 13f79535-47bb-0310-9956-ffa450edef68
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 01/13:
[maven-release-plugin] prepare for next development iteration
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit e5d38fbcaad6087d9a7c5cca8e7e7cd6e61987fd
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Tue Jan 12 05:04:08 2016 +0000
[maven-release-plugin] prepare for next development iteration
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1724155 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/pom.xml b/pom.xml
index 3e038d8..c76858f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -30,7 +30,7 @@
<artifactId>org.apache.sling.commons.metrics</artifactId>
<packaging>bundle</packaging>
- <version>1.0.0</version>
+ <version>1.0.1-SNAPSHOT</version>
<name>Apache Sling Metrics</name>
<description>
@@ -40,9 +40,9 @@
</description>
<scm>
- <connection>scm:svn:http://svn.apache.org/repos/asf/sling/tags/org.apache.sling.commons.metrics-1.0.0</connection>
- <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.commons.metrics-1.0.0</developerConnection>
- <url>http://svn.apache.org/viewvc/sling/tags/org.apache.sling.commons.metrics-1.0.0</url>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics</connection>
+ <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics</developerConnection>
+ <url>http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics</url>
</scm>
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 07/13: SLING-5443 - Review
the naming conventions used by JMXReporter to register the Metric MBeans
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit f9f9947db0c951969f3bd82fa97ddefdd6f4fefb
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Thu Nov 17 05:08:27 2016 +0000
SLING-5443 - Review the naming conventions used by JMXReporter to register the Metric MBeans
Switch to ServiceFactory for MetricService and use Bundle to determine JMX domain name for metrics created from using bundle.
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1770119 13f79535-47bb-0310-9956-ffa450edef68
---
.../metrics/internal/BundleMetricsMapper.java | 102 +++++++++++++++++++++
.../metrics/internal/MetricsServiceFactory.java | 102 +++++++++++++++++++++
.../metrics/internal/MetricsServiceImpl.java | 23 +++--
.../metrics/internal/BundleMetricsMapperTest.java | 77 ++++++++++++++++
.../internal/MetricsServiceFactoryTest.java | 100 ++++++++++++++++++++
5 files changed, 394 insertions(+), 10 deletions(-)
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java b/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java
new file mode 100644
index 0000000..9a74a1e
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java
@@ -0,0 +1,102 @@
+/*
+ * 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.commons.metrics.internal;
+
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import javax.management.ObjectName;
+
+import com.codahale.metrics.DefaultObjectNameFactory;
+import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.ObjectNameFactory;
+import org.osgi.framework.Bundle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class BundleMetricsMapper implements ObjectNameFactory{
+ public static final String HEADER_DOMAIN_NAME = "Sling-Metrics-Domain";
+ public static final String DEFAULT_DOMAIN_NAME = "org.apache.sling";
+ private final Logger log = LoggerFactory.getLogger(getClass());
+ private final ConcurrentMap<String, Bundle> metricToBundleMapping = new ConcurrentHashMap<>();
+ private final MetricRegistry registry;
+ private final ObjectNameFactory defaultFactory = new DefaultObjectNameFactory();
+
+ BundleMetricsMapper(MetricRegistry registry) {
+ this.registry = registry;
+ }
+
+ public void addMapping(String name, Bundle bundle) {
+ metricToBundleMapping.putIfAbsent(name, bundle);
+ }
+
+ public void unregister(Set<String> registeredNames) {
+ for (String name : registeredNames){
+ registry.remove(name);
+ metricToBundleMapping.remove(name);
+ }
+ log.debug("Removed metrics for {}", registeredNames);
+ }
+
+ @Override
+ public ObjectName createName(String type, String domain, String name) {
+ String mappedDomainName = safeDomainName(getDomainName(name));
+ if (mappedDomainName == null) {
+ mappedDomainName = domain;
+ }
+ return defaultFactory.createName(type, mappedDomainName, name);
+ }
+
+ private String getDomainName(String name) {
+ Bundle bundle = metricToBundleMapping.get(name);
+ return getDomainName(bundle);
+ }
+
+ private String getDomainName(Bundle bundle) {
+ if (bundle == null){
+ return null;
+ }
+
+ String domainNameHeader = bundle.getHeaders().get(HEADER_DOMAIN_NAME);
+ if (domainNameHeader != null){
+ return domainNameHeader;
+ }
+
+ //Fallback to symbolic name
+ return bundle.getSymbolicName();
+ }
+
+ static String safeDomainName(String name){
+ if (name == null){
+ return null;
+ }
+
+ name = name.trim();
+
+ //Taken from javax.management.ObjectName.isDomain()
+ //Following are special chars in domain name
+ name = name.replace(':', '_');
+ name = name.replace('*', '_');
+ name = name.replace('?', '_');
+ name = name.replace('\n', '_');
+ return name;
+ }
+}
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceFactory.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceFactory.java
new file mode 100644
index 0000000..b9d8bb6
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceFactory.java
@@ -0,0 +1,102 @@
+/*
+ * 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.commons.metrics.internal;
+
+import java.util.Collections;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.sling.commons.metrics.Counter;
+import org.apache.sling.commons.metrics.Histogram;
+import org.apache.sling.commons.metrics.Meter;
+import org.apache.sling.commons.metrics.MetricsService;
+import org.apache.sling.commons.metrics.Timer;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+
+class MetricsServiceFactory implements ServiceFactory<MetricsService> {
+ private final MetricsService delegate;
+ private final BundleMetricsMapper metricsMapper;
+
+ public MetricsServiceFactory(MetricsService delegate, BundleMetricsMapper metricsMapper) {
+ this.delegate = delegate;
+ this.metricsMapper = metricsMapper;
+ }
+
+ @Override
+ public MetricsService getService(Bundle bundle, ServiceRegistration<MetricsService> registration) {
+ return new BundleMetricService(bundle);
+ }
+
+ @Override
+ public void ungetService(Bundle bundle, ServiceRegistration<MetricsService> registration, MetricsService service) {
+ if (service instanceof BundleMetricService){
+ ((BundleMetricService) service).unregister();
+ }
+ }
+
+ private class BundleMetricService implements MetricsService {
+ private final Bundle bundle;
+ private Set<String> registeredNames = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
+
+ public BundleMetricService(Bundle bundle) {
+ this.bundle = bundle;
+ }
+
+ @Override
+ public Timer timer(String name) {
+ addMapping(name);
+ return delegate.timer(name);
+ }
+
+ @Override
+ public Histogram histogram(String name) {
+ addMapping(name);
+ return delegate.histogram(name);
+ }
+
+ @Override
+ public Counter counter(String name) {
+ addMapping(name);
+ return delegate.counter(name);
+ }
+
+ @Override
+ public Meter meter(String name) {
+ addMapping(name);
+ return delegate.meter(name);
+ }
+
+ @Override
+ public <A> A adaptTo(Class<A> type) {
+ return delegate.adaptTo(type);
+ }
+
+ void unregister(){
+ metricsMapper.unregister(registeredNames);
+ }
+
+ private void addMapping(String name) {
+ metricsMapper.addMapping(name, bundle);
+ registeredNames.add(name);
+ }
+ }
+}
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
index d2f2728..03efcee 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
@@ -50,9 +50,10 @@ import org.osgi.framework.ServiceRegistration;
@Component
public class MetricsServiceImpl implements MetricsService {
- private final List<ServiceRegistration> regs = new ArrayList<ServiceRegistration>();
- private final ConcurrentMap<String, Metric> metrics = new ConcurrentHashMap<String, Metric>();
+ private final List<ServiceRegistration> regs = new ArrayList<>();
+ private final ConcurrentMap<String, Metric> metrics = new ConcurrentHashMap<>();
private final MetricRegistry registry = new MetricRegistry();
+ private final BundleMetricsMapper metricsMapper = new BundleMetricsMapper(registry);
@Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
private MBeanServer server;
@@ -63,12 +64,13 @@ public class MetricsServiceImpl implements MetricsService {
private void activate(BundleContext context, Map<String, Object> config) {
enableJMXReporter();
- final Dictionary<String, String> svcProps = new Hashtable<String, String>();
+ final Dictionary<String, String> svcProps = new Hashtable<>();
svcProps.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Metrics Service");
svcProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
- regs.add(context.registerService(MetricsService.class.getName(), this, svcProps));
+ regs.add(context.registerService(MetricsService.class.getName(),
+ new MetricsServiceFactory(this, metricsMapper), svcProps));
- final Dictionary<String, String> regProps = new Hashtable<String, String>();
+ final Dictionary<String, String> regProps = new Hashtable<>();
regProps.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Metrics Registry");
regProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
regProps.put("name", "sling");
@@ -118,6 +120,10 @@ public class MetricsServiceImpl implements MetricsService {
return null;
}
+ MetricRegistry getRegistry() {
+ return registry;
+ }
+
@SuppressWarnings("unchecked")
private <T extends Metric> T getOrAdd(String name, MetricBuilder<T> builder) {
final Metric metric = metrics.get(name);
@@ -202,16 +208,13 @@ public class MetricsServiceImpl implements MetricsService {
}
private void enableJMXReporter() {
- //TODO Domain name should be based on calling bundle
- //For that we can register ServiceFactory and make use of calling
- //bundle symbolic name to determine the mapping
-
if (server == null){
server = ManagementFactory.getPlatformMBeanServer();
}
reporter = JmxReporter.forRegistry(registry)
- .inDomain("org.apache.sling")
+ .inDomain(BundleMetricsMapper.DEFAULT_DOMAIN_NAME)
+ .createsObjectNamesWith(metricsMapper)
.registerWith(server)
.build();
reporter.start();
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapperTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapperTest.java
new file mode 100644
index 0000000..d34357a
--- /dev/null
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapperTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.commons.metrics.internal;
+
+import javax.management.ObjectName;
+
+import com.codahale.metrics.MetricRegistry;
+import com.google.common.collect.ImmutableMap;
+import org.apache.sling.testing.mock.osgi.MockBundle;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class BundleMetricsMapperTest {
+ @Rule
+ public final OsgiContext context = new OsgiContext();
+ private MetricRegistry registry = new MetricRegistry();
+
+ private BundleMetricsMapper mapper = new BundleMetricsMapper(registry);
+
+ @Test
+ public void defaultDomainName() throws Exception{
+ ObjectName name = mapper.createName("counter", "foo", "bar");
+ assertEquals("foo", name.getDomain());
+ }
+
+ @Test
+ public void mappedName_SymbolicName() throws Exception{
+ MockBundle bundle = new MockBundle(context.bundleContext());
+ bundle.setSymbolicName("com.example");
+
+ mapper.addMapping("bar", bundle);
+
+ ObjectName name = mapper.createName("counter", "foo", "bar");
+ assertEquals("com.example", name.getDomain());
+ }
+
+ @Test
+ public void mappedName_Header() throws Exception{
+ MockBundle bundle = new MockBundle(context.bundleContext());
+ bundle.setSymbolicName("com.example");
+ bundle.setHeaders(ImmutableMap.of(BundleMetricsMapper.HEADER_DOMAIN_NAME, "com.test"));
+
+ mapper.addMapping("bar", bundle);
+
+ ObjectName name = mapper.createName("counter", "foo", "bar");
+ assertEquals("com.test", name.getDomain());
+ }
+
+ @Test
+ public void safeDomainName() throws Exception{
+ assertEquals("com.foo", BundleMetricsMapper.safeDomainName("com.foo"));
+ assertEquals("com_foo", BundleMetricsMapper.safeDomainName("com:foo"));
+ assertEquals("com_foo", BundleMetricsMapper.safeDomainName("com?foo"));
+ assertEquals("com_foo", BundleMetricsMapper.safeDomainName("com*foo"));
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/MetricsServiceFactoryTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/MetricsServiceFactoryTest.java
new file mode 100644
index 0000000..71d7fe4
--- /dev/null
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/MetricsServiceFactoryTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.commons.metrics.internal;
+
+import java.util.Collections;
+
+import javax.management.ObjectName;
+
+import com.codahale.metrics.MetricRegistry;
+import org.apache.sling.commons.metrics.MetricsService;
+import org.apache.sling.testing.mock.osgi.MockBundle;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceRegistration;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+
+public class MetricsServiceFactoryTest {
+ @Rule
+ public final OsgiContext context = new OsgiContext();
+ private MetricsServiceImpl serviceImpl = new MetricsServiceImpl();
+ private MetricRegistry registry = serviceImpl.getRegistry();
+ private BundleMetricsMapper mapper = new BundleMetricsMapper(registry);
+ private MetricsServiceFactory srvFactory = new MetricsServiceFactory(serviceImpl, mapper);
+ private ServiceRegistration<MetricsService> reg = mock(ServiceRegistration.class);
+
+ @Test
+ public void basicWorking() throws Exception{
+ MetricsService service = srvFactory.getService(cb("foo"), reg);
+ service.meter("m1");
+ service.timer("t1");
+ service.histogram("h1");
+ service.counter("c1");
+ assertTrue(registry.getMeters().containsKey("m1"));
+ assertTrue(registry.getTimers().containsKey("t1"));
+ assertTrue(registry.getHistograms().containsKey("h1"));
+ assertTrue(registry.getCounters().containsKey("c1"));
+
+ ObjectName name = mapper.createName("meter", "com.foo", "m1");
+
+ //Domain name should be bundle symbolic name
+ assertEquals("foo", name.getDomain());
+ }
+
+ @Test
+ public void unRegistration() throws Exception{
+ Bundle foo = cb("foo");
+ Bundle bar = cb("bar");
+ MetricsService srv1 = srvFactory.getService(foo, reg);
+ MetricsService srv2 = srvFactory.getService(bar, reg);
+
+ srv1.meter("m1");
+ srv1.counter("c1");
+
+ srv2.meter("m2");
+ assertTrue(registry.getMeters().containsKey("m1"));
+ assertTrue(registry.getMeters().containsKey("m2"));
+ assertTrue(registry.getCounters().containsKey("c1"));
+
+ srvFactory.ungetService(foo, reg, srv1);
+
+ //Metrics from 'foo' bundle i.e. m1 and c1 must be removed
+ assertFalse(registry.getMeters().containsKey("m1"));
+ assertFalse(registry.getCounters().containsKey("c1"));
+
+ //Metrics from 'bar' bundle should be present
+ assertTrue(registry.getMeters().containsKey("m2"));
+ }
+
+ private Bundle cb(String name){
+ MockBundle bundle = new MockBundle(context.bundleContext());
+ bundle.setSymbolicName(name);
+ return bundle;
+ }
+
+
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 03/13: SLING-5693 - Add
support for exporting MetricRegistry data as JSON
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 2871fc71e38d9064cf5dae647e47c998ee2f1969
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Tue Apr 26 06:25:04 2016 +0000
SLING-5693 - Add support for exporting MetricRegistry data as JSON
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1740952 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 6 +
.../commons/metrics/internal/JSONReporter.java | 298 +++++++++++++++++++++
.../metrics/internal/MetricWebConsolePlugin.java | 9 +-
.../commons/metrics/internal/JSONReporterTest.java | 87 ++++++
4 files changed, 399 insertions(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index c76858f..7b9fef1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -113,6 +113,12 @@
<version>2.2</version>
<optional>true</optional>
</dependency>
+ <dependency>
+ <groupId>org.json</groupId>
+ <artifactId>json</artifactId>
+ <version>20090211</version>
+ <optional>true</optional>
+ </dependency>
<dependency>
<groupId>org.hamcrest</groupId>
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java b/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java
new file mode 100644
index 0000000..9c631f7
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java
@@ -0,0 +1,298 @@
+/*
+ * 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.commons.metrics.internal;
+
+import java.io.Closeable;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.Locale;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.concurrent.TimeUnit;
+
+import com.codahale.metrics.ConsoleReporter;
+import com.codahale.metrics.Counter;
+import com.codahale.metrics.Gauge;
+import com.codahale.metrics.Histogram;
+import com.codahale.metrics.Meter;
+import com.codahale.metrics.MetricFilter;
+import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.Reporter;
+import com.codahale.metrics.Snapshot;
+import com.codahale.metrics.Timer;
+import org.json.JSONException;
+import org.json.JSONWriter;
+
+class JSONReporter implements Reporter, Closeable {
+
+ public static JSONReporter.Builder forRegistry(MetricRegistry registry) {
+ return new JSONReporter.Builder(registry);
+ }
+
+ public static class Builder {
+ private final MetricRegistry registry;
+ private PrintStream output;
+ private MetricFilter filter;
+ private TimeUnit rateUnit;
+ private TimeUnit durationUnit;
+
+ private Builder(MetricRegistry registry) {
+ this.registry = registry;
+ this.output = System.out;
+ this.filter = MetricFilter.ALL;
+ this.rateUnit = TimeUnit.SECONDS;
+ this.durationUnit = TimeUnit.MILLISECONDS;
+ }
+
+ /**
+ * Write to the given {@link PrintStream}.
+ *
+ * @param output a {@link PrintStream} instance.
+ * @return {@code this}
+ */
+ public Builder outputTo(PrintStream output) {
+ this.output = output;
+ return this;
+ }
+
+ /**
+ * Convert rates to the given time unit.
+ *
+ * @param rateUnit a unit of time
+ * @return {@code this}
+ */
+ public Builder convertRatesTo(TimeUnit rateUnit) {
+ this.rateUnit = rateUnit;
+ return this;
+ }
+
+ /**
+ * Convert durations to the given time unit.
+ *
+ * @param durationUnit a unit of time
+ * @return {@code this}
+ */
+ public Builder convertDurationsTo(TimeUnit durationUnit) {
+ this.durationUnit = durationUnit;
+ return this;
+ }
+
+ /**
+ * Only report metrics which match the given filter.
+ *
+ * @param filter a {@link MetricFilter}
+ * @return {@code this}
+ */
+ public Builder filter(MetricFilter filter) {
+ this.filter = filter;
+ return this;
+ }
+
+ /**
+ * Builds a {@link ConsoleReporter} with the given properties.
+ *
+ * @return a {@link ConsoleReporter}
+ */
+ public JSONReporter build() {
+ return new JSONReporter(registry,
+ output,
+ rateUnit,
+ durationUnit,
+ filter);
+ }
+ }
+
+ private final MetricRegistry registry;
+ private final MetricFilter filter;
+ private final double durationFactor;
+ private final String durationUnit;
+ private final double rateFactor;
+ private final String rateUnit;
+ private final JSONWriter json;
+ private final PrintWriter pw;
+
+ private JSONReporter(MetricRegistry registry,
+ PrintStream output, TimeUnit rateUnit, TimeUnit durationUnit, MetricFilter filter){
+ this.registry = registry;
+ this.filter = filter;
+ this.pw = new PrintWriter(output);
+ this.json = new JSONWriter(pw);
+ this.rateFactor = rateUnit.toSeconds(1);
+ this.rateUnit = calculateRateUnit(rateUnit);
+ this.durationFactor = 1.0 / durationUnit.toNanos(1);
+ this.durationUnit = durationUnit.toString().toLowerCase(Locale.US);
+ }
+
+ public void report() {
+ try {
+ report(registry.getGauges(filter),
+ registry.getCounters(filter),
+ registry.getHistograms(filter),
+ registry.getMeters(filter),
+ registry.getTimers(filter));
+ } catch (JSONException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void close(){
+ pw.close();
+ }
+
+ private void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
+ SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
+ SortedMap<String, Timer> timers) throws JSONException {
+ json.object();
+ if (!gauges.isEmpty()) {
+ json.key("guages").object();
+ for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
+ printGauge(entry);
+ }
+ json.endObject();
+ }
+
+ if (!counters.isEmpty()) {
+ json.key("counters").object();
+ for (Map.Entry<String, Counter> entry : counters.entrySet()) {
+ printCounter(entry);
+ }
+ json.endObject();
+ }
+
+ if (!histograms.isEmpty()) {
+ json.key("histograms").object();
+ for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
+ printHistogram(entry);
+ }
+ json.endObject();
+ }
+
+ if (!meters.isEmpty()) {
+ json.key("meters").object();
+ for (Map.Entry<String, Meter> entry : meters.entrySet()) {
+ printMeter(entry);
+ }
+ json.endObject();
+ }
+
+ if (!timers.isEmpty()) {
+ json.key("timers").object();
+ for (Map.Entry<String, Timer> entry : timers.entrySet()) {
+ printTimer(entry);
+ }
+ json.endObject();
+ }
+
+ json.endObject();
+
+ }
+
+ private void printTimer(Map.Entry<String, Timer> e) throws JSONException {
+ json.key(e.getKey()).object();
+ Timer timer = e.getValue();
+ Snapshot snapshot = timer.getSnapshot();
+
+ json.key("count").value(timer.getCount());
+ json.key("max").value(snapshot.getMax() * durationFactor);
+ json.key("mean").value(snapshot.getMean() * durationFactor);
+ json.key("min").value(snapshot.getMin() * durationFactor);
+
+ json.key("p50").value(snapshot.getMedian() * durationFactor);
+ json.key("p75").value(snapshot.get75thPercentile() * durationFactor);
+ json.key("p95").value(snapshot.get95thPercentile() * durationFactor);
+ json.key("p98").value(snapshot.get98thPercentile() * durationFactor);
+ json.key("p99").value(snapshot.get99thPercentile() * durationFactor);
+ json.key("p999").value(snapshot.get999thPercentile() * durationFactor);
+
+ json.key("stddev").value(snapshot.getStdDev() * durationFactor);
+ json.key("m1_rate").value(timer.getOneMinuteRate() * rateFactor);
+ json.key("m5_rate").value(timer.getFiveMinuteRate() * rateFactor);
+ json.key("m15_rate").value(timer.getFifteenMinuteRate() * rateFactor);
+ json.key("mean_rate").value(timer.getMeanRate() * rateFactor);
+ json.key("duration_units").value(durationUnit);
+ json.key("rate_units").value(rateUnit);
+
+ json.endObject();
+ }
+
+ private void printMeter(Map.Entry<String, Meter> e) throws JSONException {
+ json.key(e.getKey()).object();
+ Meter meter = e.getValue();
+ json.key("count").value(e.getValue().getCount());
+ json.key("m1_rate").value(meter.getOneMinuteRate() * rateFactor);
+ json.key("m5_rate").value(meter.getFiveMinuteRate() * rateFactor);
+ json.key("m15_rate").value(meter.getFifteenMinuteRate() * rateFactor);
+ json.key("mean_rate").value(meter.getMeanRate() * rateFactor);
+ json.key("units").value(rateUnit);
+ json.endObject();
+ }
+
+ private void printHistogram(Map.Entry<String, Histogram> e) throws JSONException {
+ json.key(e.getKey()).object();
+ json.key("count").value(e.getValue().getCount());
+
+ Snapshot snapshot = e.getValue().getSnapshot();
+ json.key("max").value(snapshot.getMax());
+ json.key("mean").value(snapshot.getMean());
+ json.key("min").value(snapshot.getMin());
+ json.key("p50").value(snapshot.getMedian());
+ json.key("p75").value(snapshot.get75thPercentile());
+ json.key("p95").value(snapshot.get95thPercentile());
+ json.key("p98").value(snapshot.get98thPercentile());
+ json.key("p99").value(snapshot.get99thPercentile());
+ json.key("p999").value(snapshot.get999thPercentile());
+ json.key("stddev").value(snapshot.getStdDev());
+
+ json.endObject();
+ }
+
+ private void printCounter(Map.Entry<String, Counter> e) throws JSONException {
+ json.key(e.getKey()).object();
+ json.key("count").value(e.getValue().getCount());
+ json.endObject();
+ }
+
+ private void printGauge(Map.Entry<String, Gauge> e) throws JSONException {
+ json.key(e.getKey()).object();
+ Object v = e.getValue().getValue();
+ json.key("value").value(jsonSafeValue(v));
+ json.endObject();
+ }
+
+ private static Object jsonSafeValue(Object v){
+ //Json does not allow NaN or infinite doubles. So take care of that
+ if (v instanceof Number){
+ if (v instanceof Double){
+ Double d = (Double) v;
+ if (d.isInfinite() || d.isNaN()){
+ return d.toString();
+ }
+ }
+ }
+ return v;
+ }
+
+ private static String calculateRateUnit(TimeUnit unit) {
+ final String s = unit.toString().toLowerCase(Locale.US);
+ return s.substring(0, s.length() - 1);
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
index f5261c5..c33eb18 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
@@ -66,7 +66,7 @@ import org.slf4j.LoggerFactory;
@Property(name = "felix.webconsole.label", value = "slingmetrics"),
@Property(name = "felix.webconsole.title", value = "Metrics"),
@Property(name = "felix.webconsole.category", value = "Sling"),
- @Property(name = InventoryPrinter.FORMAT, value = {"TEXT" }),
+ @Property(name = InventoryPrinter.FORMAT, value = {"TEXT" , "JSON"}),
@Property(name = InventoryPrinter.NAME, value = "slingmetrics"),
@Property(name = InventoryPrinter.TITLE, value = "Sling Metrics"),
@Property(name = InventoryPrinter.WEBCONSOLE, boolValue = true)
@@ -114,6 +114,13 @@ public class MetricWebConsolePlugin extends HttpServlet implements
.build();
reporter.report();
reporter.close();
+ } else if (format == Format.JSON) {
+ MetricRegistry registry = getConsolidatedRegistry();
+ JSONReporter reporter = JSONReporter.forRegistry(registry)
+ .outputTo(new PrintStream(new WriterOutputStream(printWriter)))
+ .build();
+ reporter.report();
+ reporter.close();
}
}
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/JSONReporterTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/JSONReporterTest.java
new file mode 100644
index 0000000..1f13872
--- /dev/null
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/JSONReporterTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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.commons.metrics.internal;
+
+import java.io.PrintStream;
+import java.io.StringWriter;
+
+import com.codahale.metrics.Gauge;
+import com.codahale.metrics.JvmAttributeGaugeSet;
+import com.codahale.metrics.MetricRegistry;
+import org.apache.commons.io.output.WriterOutputStream;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+public class JSONReporterTest {
+
+ @Test
+ public void jsonOutput() throws Exception {
+ MetricRegistry registry = new MetricRegistry();
+ registry.meter("test1").mark(5);
+ registry.timer("test2").time().close();
+ registry.histogram("test3").update(743);
+ registry.counter("test4").inc(9);
+ registry.registerAll(new JvmAttributeGaugeSet());
+
+ JSONObject json = getJSON(registry);
+
+ assertTrue(json.has("meters"));
+ assertTrue(json.has("guages"));
+ assertTrue(json.has("timers"));
+ assertTrue(json.has("counters"));
+ assertTrue(json.has("histograms"));
+ assertTrue(json.has("meters"));
+
+ assertTrue(json.getJSONObject("meters").has("test1"));
+ assertTrue(json.getJSONObject("timers").has("test2"));
+ assertTrue(json.getJSONObject("counters").has("test4"));
+ assertTrue(json.getJSONObject("histograms").has("test3"));
+ }
+
+ @Test
+ public void nan_value() throws Exception{
+ MetricRegistry registry = new MetricRegistry();
+
+ registry.register("test", new Gauge<Double>() {
+ @Override
+ public Double getValue() {
+ return Double.POSITIVE_INFINITY;
+ }
+ });
+
+
+ JSONObject json = getJSON(registry);
+ assertTrue(json.getJSONObject("guages").has("test"));
+ }
+
+ private static JSONObject getJSON(MetricRegistry registry) throws JSONException {
+ StringWriter sw = new StringWriter();
+ JSONReporter reporter = JSONReporter.forRegistry(registry)
+ .outputTo(new PrintStream(new WriterOutputStream(sw)))
+ .build();
+ reporter.report();
+ reporter.close();
+ return new JSONObject(sw.toString());
+ }
+
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 08/13: SLING-5443 - Review
the naming conventions used by JMXReporter to register the Metric MBeans
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 8c9ec741370ed263d973a6a54d353b8cace8cfef
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Thu Nov 17 05:08:41 2016 +0000
SLING-5443 - Review the naming conventions used by JMXReporter to register the Metric MBeans
Specify type for JMX ObjectNames created for Metrics
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1770120 13f79535-47bb-0310-9956-ffa450edef68
---
.../metrics/internal/BundleMetricsMapper.java | 31 ++++----
.../sling/commons/metrics/internal/JmxUtil.java | 89 ++++++++++++++++++++++
.../metrics/internal/BundleMetricsMapperTest.java | 7 --
.../commons/metrics/internal/JmxUtilTest.java | 51 +++++++++++++
.../metrics/internal/MetricServiceTest.java | 3 +-
5 files changed, 156 insertions(+), 25 deletions(-)
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java b/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java
index 9a74a1e..8bdbd45 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java
@@ -19,10 +19,12 @@
package org.apache.sling.commons.metrics.internal;
+import java.util.Hashtable;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
+import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import com.codahale.metrics.DefaultObjectNameFactory;
@@ -35,6 +37,7 @@ import org.slf4j.LoggerFactory;
class BundleMetricsMapper implements ObjectNameFactory{
public static final String HEADER_DOMAIN_NAME = "Sling-Metrics-Domain";
public static final String DEFAULT_DOMAIN_NAME = "org.apache.sling";
+ static final String JMX_TYPE_METRICS = "Metrics";
private final Logger log = LoggerFactory.getLogger(getClass());
private final ConcurrentMap<String, Bundle> metricToBundleMapping = new ConcurrentHashMap<>();
private final MetricRegistry registry;
@@ -58,11 +61,20 @@ class BundleMetricsMapper implements ObjectNameFactory{
@Override
public ObjectName createName(String type, String domain, String name) {
- String mappedDomainName = safeDomainName(getDomainName(name));
+ String mappedDomainName = JmxUtil.safeDomainName(getDomainName(name));
if (mappedDomainName == null) {
mappedDomainName = domain;
}
- return defaultFactory.createName(type, mappedDomainName, name);
+
+ Hashtable<String, String> table = new Hashtable<String, String>();
+ table.put("type", JMX_TYPE_METRICS);
+ table.put("name", JmxUtil.quoteValueIfRequired(name));
+ try {
+ return new ObjectName(mappedDomainName, table);
+ } catch (MalformedObjectNameException e) {
+ log.warn("Unable to register {} {}", type, name, e);
+ throw new RuntimeException(e);
+ }
}
private String getDomainName(String name) {
@@ -84,19 +96,4 @@ class BundleMetricsMapper implements ObjectNameFactory{
return bundle.getSymbolicName();
}
- static String safeDomainName(String name){
- if (name == null){
- return null;
- }
-
- name = name.trim();
-
- //Taken from javax.management.ObjectName.isDomain()
- //Following are special chars in domain name
- name = name.replace(':', '_');
- name = name.replace('*', '_');
- name = name.replace('?', '_');
- name = name.replace('\n', '_');
- return name;
- }
}
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/JmxUtil.java b/src/main/java/org/apache/sling/commons/metrics/internal/JmxUtil.java
new file mode 100644
index 0000000..9938631
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/JmxUtil.java
@@ -0,0 +1,89 @@
+/*
+ * 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.commons.metrics.internal;
+
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+/**
+ * Utility methods related to JMX
+ */
+final class JmxUtil {
+
+ /**
+ * Checks if the passed value string can be used as is as part of
+ * JMX {@link javax.management.ObjectName} If it cannot be used then
+ * it would return a quoted string which is then safe to be used
+ * as part of ObjectName.
+ *
+ * <p>This is meant to avoid unnecessary quoting of value</p>
+ *
+ * @param unquotedValue to quote if required
+ * @return passed value or quoted value if required
+ */
+ public static String quoteValueIfRequired(String unquotedValue) {
+ String result;
+ String quotedValue = ObjectName.quote(unquotedValue);
+
+ //Check if some chars are escaped or not. In that case
+ //length of quoted string (excluding quotes) would differ
+ if (quotedValue.substring(1, quotedValue.length() - 1).equals(unquotedValue)) {
+ ObjectName on = null;
+ try {
+ //Quoting logic in ObjectName does not escape ',', '='
+ //etc. So try now by constructing ObjectName. If that
+ //passes then value can be used as safely
+
+ //Also we cannot just rely on ObjectName as it treats
+ //*, ? as pattern chars and which should ideally be escaped
+ on = new ObjectName("dummy", "dummy", unquotedValue);
+ } catch (MalformedObjectNameException ignore) {
+ //ignore
+ }
+
+ if (on != null){
+ result = unquotedValue;
+ } else {
+ result = quotedValue;
+ }
+ } else {
+ //Some escaping done. So do quote
+ result = quotedValue;
+ }
+ return result;
+ }
+
+
+ public static String safeDomainName(String name){
+ if (name == null){
+ return null;
+ }
+
+ name = name.trim();
+
+ //Taken from javax.management.ObjectName.isDomain()
+ //Following are special chars in domain name
+ name = name.replace(':', '_');
+ name = name.replace('*', '_');
+ name = name.replace('?', '_');
+ name = name.replace('\n', '_');
+ return name;
+ }
+}
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapperTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapperTest.java
index d34357a..ae08a05 100644
--- a/src/test/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapperTest.java
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapperTest.java
@@ -66,12 +66,5 @@ public class BundleMetricsMapperTest {
assertEquals("com.test", name.getDomain());
}
- @Test
- public void safeDomainName() throws Exception{
- assertEquals("com.foo", BundleMetricsMapper.safeDomainName("com.foo"));
- assertEquals("com_foo", BundleMetricsMapper.safeDomainName("com:foo"));
- assertEquals("com_foo", BundleMetricsMapper.safeDomainName("com?foo"));
- assertEquals("com_foo", BundleMetricsMapper.safeDomainName("com*foo"));
- }
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/JmxUtilTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/JmxUtilTest.java
new file mode 100644
index 0000000..9db25c7
--- /dev/null
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/JmxUtilTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.commons.metrics.internal;
+
+import junit.framework.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class JmxUtilTest {
+
+ @Test
+ public void quotation() throws Exception{
+ assertEquals("text", JmxUtil.quoteValueIfRequired("text"));
+ TestCase.assertEquals("", JmxUtil.quoteValueIfRequired(""));
+ assertTrue(JmxUtil.quoteValueIfRequired("text*with?chars").startsWith("\""));
+ }
+
+ @Test
+ public void quoteAndComma() throws Exception{
+ assertTrue(JmxUtil.quoteValueIfRequired("text,withComma").startsWith("\""));
+ assertTrue(JmxUtil.quoteValueIfRequired("text=withEqual").startsWith("\""));
+ }
+
+ @Test
+ public void safeDomainName() throws Exception{
+ assertEquals("com.foo", JmxUtil.safeDomainName("com.foo"));
+ assertEquals("com_foo", JmxUtil.safeDomainName("com:foo"));
+ assertEquals("com_foo", JmxUtil.safeDomainName("com?foo"));
+ assertEquals("com_foo", JmxUtil.safeDomainName("com*foo"));
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
index ca76690..72a8e02 100644
--- a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
@@ -41,6 +41,7 @@ import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
+import static org.apache.sling.commons.metrics.internal.BundleMetricsMapper.JMX_TYPE_METRICS;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
@@ -134,7 +135,7 @@ public class MetricServiceTest {
Meter meter = service.meter("test");
assertNotNull(meter);
QueryExp q = Query.isInstanceOf(Query.value(JmxReporter.JmxMeterMBean.class.getName()));
- Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.sling:name=*"), q);
+ Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.sling:name=*,type="+ JMX_TYPE_METRICS), q);
assertThat(names, is(not(empty())));
MockOsgi.deactivate(service, context.bundleContext());
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 05/13: Update to parent
pom 29
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 8d6a1a25a55ecb834a81a53dd96d089d356cce51
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Oct 21 06:15:56 2016 +0000
Update to parent pom 29
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1765929 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index 7b9fef1..8ca2ac8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,7 +25,7 @@
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling</artifactId>
- <version>26</version>
+ <version>29</version>
</parent>
<artifactId>org.apache.sling.commons.metrics</artifactId>
@@ -85,7 +85,7 @@
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
+ <artifactId>javax.servlet-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 04/13: SLING-5899 -
"Guages" Typo in Sling metrics console
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 91f51c84b9926c2d133ea116e55cea9ffac02f50
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Mon Jul 25 14:12:02 2016 +0000
SLING-5899 - "Guages" Typo in Sling metrics console
Typo fix, thanks to Ana Vinatoru for spotting.
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1754008 13f79535-47bb-0310-9956-ffa450edef68
---
.../apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
index c33eb18..d0321ef 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
@@ -399,7 +399,7 @@ public class MetricWebConsolePlugin extends HttpServlet implements
pw.println("<br>");
pw.println("<div class='table'>");
- pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>Guages</div>");
+ pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>Gauges</div>");
pw.println("<table class='nicetable' id='data-guages'>");
pw.println("<thead>");
pw.println("<tr>");
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 11/13: SLING-6297 - Switch
to standard OSGi annotation in Metrics
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 709afe39cfbc2b8d17ef91b6c019cc94752a8101
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Thu Nov 17 06:56:52 2016 +0000
SLING-6297 - Switch to standard OSGi annotation in Metrics
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1770126 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 23 +++++++++-----
.../org/apache/sling/commons/metrics/Counter.java | 3 +-
.../org/apache/sling/commons/metrics/Counting.java | 3 +-
.../org/apache/sling/commons/metrics/Gauge.java | 3 +-
.../apache/sling/commons/metrics/Histogram.java | 3 +-
.../org/apache/sling/commons/metrics/Meter.java | 3 +-
.../org/apache/sling/commons/metrics/Metric.java | 3 +-
.../sling/commons/metrics/MetricsService.java | 3 +-
.../org/apache/sling/commons/metrics/Timer.java | 3 +-
.../metrics/internal/MetricWebConsolePlugin.java | 35 ++++++++++------------
.../metrics/internal/MetricsServiceImpl.java | 14 ++++-----
.../apache/sling/commons/metrics/package-info.java | 4 +--
12 files changed, 55 insertions(+), 45 deletions(-)
diff --git a/pom.xml b/pom.xml
index 3a45b75..31cfea9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -64,11 +64,21 @@
org.apache.felix.inventory;version="[1.0.0,2)"
</DynamicImport-Package>
</instructions>
+ <!-- Export SCR metadata to classpath to have them available in unit tests -->
+ <exportScr>true</exportScr>
</configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-scr-plugin</artifactId>
+ <executions>
+ <!-- Configure extra execution of 'manifest' in process-classes phase to make sure SCR metadata is generated before unit test runs -->
+ <execution>
+ <id>scr-metadata</id>
+ <goals>
+ <goal>manifest</goal>
+ </goals>
+ <configuration>
+ <supportIncrementalBuild>true</supportIncrementalBuild>
+ </configuration>
+ </execution>
+ </executions>
</plugin>
</plugins>
</build>
@@ -91,14 +101,10 @@
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
- <version>6.0.0</version>
- <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.cmpn</artifactId>
- <version>6.0.0</version>
- <scope>provided</scope>
</dependency>
<dependency>
@@ -141,6 +147,7 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
+ <scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
diff --git a/src/main/java/org/apache/sling/commons/metrics/Counter.java b/src/main/java/org/apache/sling/commons/metrics/Counter.java
index 8269d5b..64730dd 100644
--- a/src/main/java/org/apache/sling/commons/metrics/Counter.java
+++ b/src/main/java/org/apache/sling/commons/metrics/Counter.java
@@ -19,7 +19,8 @@
package org.apache.sling.commons.metrics;
-import aQute.bnd.annotation.ProviderType;
+
+import org.osgi.annotation.versioning.ProviderType;
/**
* An incrementing and decrementing counter metric.
diff --git a/src/main/java/org/apache/sling/commons/metrics/Counting.java b/src/main/java/org/apache/sling/commons/metrics/Counting.java
index 890d650..f974ea6 100644
--- a/src/main/java/org/apache/sling/commons/metrics/Counting.java
+++ b/src/main/java/org/apache/sling/commons/metrics/Counting.java
@@ -19,7 +19,8 @@
package org.apache.sling.commons.metrics;
-import aQute.bnd.annotation.ProviderType;
+
+import org.osgi.annotation.versioning.ProviderType;
@ProviderType
public interface Counting {
diff --git a/src/main/java/org/apache/sling/commons/metrics/Gauge.java b/src/main/java/org/apache/sling/commons/metrics/Gauge.java
index ac723a6..9813120 100644
--- a/src/main/java/org/apache/sling/commons/metrics/Gauge.java
+++ b/src/main/java/org/apache/sling/commons/metrics/Gauge.java
@@ -19,7 +19,8 @@
package org.apache.sling.commons.metrics;
-import aQute.bnd.annotation.ConsumerType;
+
+import org.osgi.annotation.versioning.ConsumerType;
/**
* A gauge metric is an instantaneous reading of a particular value. To instrument a queue's depth,
diff --git a/src/main/java/org/apache/sling/commons/metrics/Histogram.java b/src/main/java/org/apache/sling/commons/metrics/Histogram.java
index 7f87cdc..d78f3a8 100644
--- a/src/main/java/org/apache/sling/commons/metrics/Histogram.java
+++ b/src/main/java/org/apache/sling/commons/metrics/Histogram.java
@@ -19,7 +19,8 @@
package org.apache.sling.commons.metrics;
-import aQute.bnd.annotation.ProviderType;
+
+import org.osgi.annotation.versioning.ProviderType;
/**
* A metric which calculates the distribution of a value.
diff --git a/src/main/java/org/apache/sling/commons/metrics/Meter.java b/src/main/java/org/apache/sling/commons/metrics/Meter.java
index 3fef3f8..6674fbf 100644
--- a/src/main/java/org/apache/sling/commons/metrics/Meter.java
+++ b/src/main/java/org/apache/sling/commons/metrics/Meter.java
@@ -19,7 +19,8 @@
package org.apache.sling.commons.metrics;
-import aQute.bnd.annotation.ProviderType;
+
+import org.osgi.annotation.versioning.ProviderType;
/**
* A meter metric which measures mean throughput
diff --git a/src/main/java/org/apache/sling/commons/metrics/Metric.java b/src/main/java/org/apache/sling/commons/metrics/Metric.java
index b8a12bb..bf07b69 100644
--- a/src/main/java/org/apache/sling/commons/metrics/Metric.java
+++ b/src/main/java/org/apache/sling/commons/metrics/Metric.java
@@ -19,7 +19,8 @@
package org.apache.sling.commons.metrics;
-import aQute.bnd.annotation.ProviderType;
+
+import org.osgi.annotation.versioning.ProviderType;
@ProviderType
public interface Metric {
diff --git a/src/main/java/org/apache/sling/commons/metrics/MetricsService.java b/src/main/java/org/apache/sling/commons/metrics/MetricsService.java
index 56e87e6..5bf4eae 100644
--- a/src/main/java/org/apache/sling/commons/metrics/MetricsService.java
+++ b/src/main/java/org/apache/sling/commons/metrics/MetricsService.java
@@ -19,7 +19,8 @@
package org.apache.sling.commons.metrics;
-import aQute.bnd.annotation.ProviderType;
+
+import org.osgi.annotation.versioning.ProviderType;
/**
* The {@code MetricsService} enables creation of various types of {@code Metric}.
diff --git a/src/main/java/org/apache/sling/commons/metrics/Timer.java b/src/main/java/org/apache/sling/commons/metrics/Timer.java
index 8de1707..f5d79cb 100644
--- a/src/main/java/org/apache/sling/commons/metrics/Timer.java
+++ b/src/main/java/org/apache/sling/commons/metrics/Timer.java
@@ -22,7 +22,8 @@ package org.apache.sling.commons.metrics;
import java.io.Closeable;
import java.util.concurrent.TimeUnit;
-import aQute.bnd.annotation.ProviderType;
+import org.osgi.annotation.versioning.ProviderType;
+
/**
* A timer metric which aggregates timing durations and provides duration statistics.
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
index d0321ef..0ebc173 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
@@ -47,30 +47,27 @@ import com.codahale.metrics.Timer;
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.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
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, Servlet.class})
-@Properties({
- @Property(name = "felix.webconsole.label", value = "slingmetrics"),
- @Property(name = "felix.webconsole.title", value = "Metrics"),
- @Property(name = "felix.webconsole.category", value = "Sling"),
- @Property(name = InventoryPrinter.FORMAT, value = {"TEXT" , "JSON"}),
- @Property(name = InventoryPrinter.NAME, value = "slingmetrics"),
- @Property(name = InventoryPrinter.TITLE, value = "Sling Metrics"),
- @Property(name = InventoryPrinter.WEBCONSOLE, boolValue = true)
-})
+@Component(service = {InventoryPrinter.class, Servlet.class},
+ property = {
+ "felix.webconsole.label=slingmetrics",
+ "felix.webconsole.title=Metrics",
+ "felix.webconsole.category=Sling",
+ InventoryPrinter.FORMAT + "=TEXT",
+ InventoryPrinter.FORMAT + "=JSON",
+ InventoryPrinter.TITLE + "=Sling Metrics",
+ InventoryPrinter.NAME + "=slingmetrics"
+ }
+)
public class MetricWebConsolePlugin extends HttpServlet implements
InventoryPrinter, ServiceTrackerCustomizer<MetricRegistry, MetricRegistry>{
/**
@@ -82,7 +79,7 @@ public class MetricWebConsolePlugin extends HttpServlet implements
private BundleContext context;
private ServiceTracker<MetricRegistry, MetricRegistry> tracker;
private ConcurrentMap<ServiceReference, MetricRegistry> registries
- = new ConcurrentHashMap<ServiceReference, MetricRegistry>();
+ = new ConcurrentHashMap<>();
private TimeUnit rateUnit = TimeUnit.SECONDS;
private TimeUnit durationUnit = TimeUnit.MILLISECONDS;
@@ -94,7 +91,7 @@ public class MetricWebConsolePlugin extends HttpServlet implements
private void activate(BundleContext context){
this.context = context;
this.timeUnit = new MetricTimeUnits(rateUnit, durationUnit, specificRateUnits, specificDurationUnits);
- tracker = new ServiceTracker<MetricRegistry, MetricRegistry>(context, MetricRegistry.class, this);
+ tracker = new ServiceTracker<>(context, MetricRegistry.class, this);
tracker.open();
}
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
index 1bc6514..cec64bb 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
@@ -33,11 +33,6 @@ import javax.management.MBeanServer;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
-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.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.sling.commons.metrics.Meter;
import org.apache.sling.commons.metrics.MetricsService;
import org.apache.sling.commons.metrics.Timer;
@@ -47,8 +42,13 @@ import org.apache.sling.commons.metrics.Metric;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
+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;
-@Component
+@Component(service = {}, immediate = true)
public class MetricsServiceImpl implements MetricsService {
private final List<ServiceRegistration> regs = new ArrayList<>();
private final ConcurrentMap<String, Metric> metrics = new ConcurrentHashMap<>();
@@ -56,7 +56,7 @@ public class MetricsServiceImpl implements MetricsService {
private final BundleMetricsMapper metricsMapper = new BundleMetricsMapper(registry);
private GaugeManager gaugeManager;
- @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
+ @Reference(cardinality = ReferenceCardinality.OPTIONAL)
private MBeanServer server;
private JmxReporter reporter;
diff --git a/src/main/java/org/apache/sling/commons/metrics/package-info.java b/src/main/java/org/apache/sling/commons/metrics/package-info.java
index 5b24541..3d6a101 100644
--- a/src/main/java/org/apache/sling/commons/metrics/package-info.java
+++ b/src/main/java/org/apache/sling/commons/metrics/package-info.java
@@ -23,9 +23,7 @@
* @version 1.0
*/
@Version("1.1.0")
-@Export(optional = "provide:=true")
package org.apache.sling.commons.metrics;
-import aQute.bnd.annotation.Export;
-import aQute.bnd.annotation.Version;
+import org.osgi.annotation.versioning.Version;
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 09/13: SLING-5443 - Review
the naming conventions used by JMXReporter to register the Metric MBeans
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 090aa7b09640d6ca06eb7063f4978d2ec0225b04
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Thu Nov 17 05:11:17 2016 +0000
SLING-5443 - Review the naming conventions used by JMXReporter to register the Metric MBeans
Remove unused instance
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1770121 13f79535-47bb-0310-9956-ffa450edef68
---
.../apache/sling/commons/metrics/internal/BundleMetricsMapper.java | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java b/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java
index 8bdbd45..a4ec010 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/BundleMetricsMapper.java
@@ -27,7 +27,6 @@ import java.util.concurrent.ConcurrentMap;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
-import com.codahale.metrics.DefaultObjectNameFactory;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ObjectNameFactory;
import org.osgi.framework.Bundle;
@@ -41,7 +40,6 @@ class BundleMetricsMapper implements ObjectNameFactory{
private final Logger log = LoggerFactory.getLogger(getClass());
private final ConcurrentMap<String, Bundle> metricToBundleMapping = new ConcurrentHashMap<>();
private final MetricRegistry registry;
- private final ObjectNameFactory defaultFactory = new DefaultObjectNameFactory();
BundleMetricsMapper(MetricRegistry registry) {
this.registry = registry;
@@ -66,7 +64,7 @@ class BundleMetricsMapper implements ObjectNameFactory{
mappedDomainName = domain;
}
- Hashtable<String, String> table = new Hashtable<String, String>();
+ Hashtable<String, String> table = new Hashtable<>();
table.put("type", JMX_TYPE_METRICS);
table.put("name", JmxUtil.quoteValueIfRequired(name));
try {
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.
[sling-org-apache-sling-commons-metrics] 10/13: SLING-5966 - Add
Gauge support to metrics
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit fa6a655f8ec0d3278b6337da9eb6d8f7baba5754
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Thu Nov 17 06:06:36 2016 +0000
SLING-5966 - Add Gauge support to metrics
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1770122 13f79535-47bb-0310-9956-ffa450edef68
---
.../org/apache/sling/commons/metrics/Gauge.java | 54 ++++++++++
.../commons/metrics/internal/GaugeManager.java | 119 +++++++++++++++++++++
.../metrics/internal/MetricsServiceImpl.java | 5 +
.../apache/sling/commons/metrics/package-info.java | 2 +-
.../metrics/internal/MetricServiceTest.java | 31 ++++++
5 files changed, 210 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/sling/commons/metrics/Gauge.java b/src/main/java/org/apache/sling/commons/metrics/Gauge.java
new file mode 100644
index 0000000..ac723a6
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/metrics/Gauge.java
@@ -0,0 +1,54 @@
+/*
+ * 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.commons.metrics;
+
+import aQute.bnd.annotation.ConsumerType;
+
+/**
+ * A gauge metric is an instantaneous reading of a particular value. To instrument a queue's depth,
+ * for example:<br>
+ * <pre><code>
+ * final Queue<String> queue = new ConcurrentLinkedQueue<String>();
+ * final Gauge<Integer> queueDepth = new Gauge<Integer>() {
+ * public Integer getValue() {
+ * return queue.size();
+ * }
+ * };
+ * </code></pre>
+ *
+ * <p> A Gauge instance should be registered with OSGi ServiceRegistry with {@code Gauge#NAME} set
+ * to Gauge name. Then the Gauge instance would be registered with MetricService via whiteboard
+ * pattern
+ *
+ * @param <T> the type of the metric's value
+ */
+@ConsumerType
+public interface Gauge<T> {
+ /**
+ * Service property name which determines the name of the Gauge
+ */
+ String NAME = "name";
+ /**
+ * Returns the metric's current value.
+ *
+ * @return the metric's current value
+ */
+ T getValue();
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/GaugeManager.java b/src/main/java/org/apache/sling/commons/metrics/internal/GaugeManager.java
new file mode 100644
index 0000000..bc48066
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/GaugeManager.java
@@ -0,0 +1,119 @@
+/*
+ * 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.commons.metrics.internal;
+
+import java.io.Closeable;
+import java.util.Collections;
+
+import com.codahale.metrics.MetricRegistry;
+import org.apache.sling.commons.metrics.Gauge;
+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;
+
+class GaugeManager implements ServiceTrackerCustomizer<Gauge, GaugeManager.GaugeImpl>, Closeable {
+ private final Logger log = LoggerFactory.getLogger(getClass());
+ private final MetricRegistry registry;
+ private final BundleMetricsMapper mapper;
+ private final BundleContext bundleContext;
+ private ServiceTracker<Gauge, GaugeImpl> tracker;
+
+ public GaugeManager(BundleContext context, MetricRegistry registry, BundleMetricsMapper mapper) {
+ this.registry = registry;
+ this.mapper = mapper;
+ this.bundleContext = context;
+ this.tracker = new ServiceTracker<>(context, Gauge.class, this);
+ tracker.open();
+ }
+
+ //~-------------------------------------< ServiceTrackerCustomizer >
+
+ @Override
+ public GaugeImpl addingService(ServiceReference<Gauge> reference) {
+ String name = (String) reference.getProperty(Gauge.NAME);
+ if (name == null){
+ log.warn("A {} service is registered without [{}] property. This Gauge would not be " +
+ "registered with MetricsRegistry", reference, Gauge.NAME);
+ return null;
+ }
+
+ Gauge gauge = bundleContext.getService(reference);
+ GaugeImpl gaugeImpl = new GaugeImpl(name, gauge);
+ register(reference, gaugeImpl);
+ return gaugeImpl;
+ }
+
+ @Override
+ public void modifiedService(ServiceReference<Gauge> reference, GaugeImpl service) {
+ String name = (String) reference.getProperty(Gauge.NAME);
+ if (name == null){
+ return;
+ }
+
+ if (!name.equals(service.name)){
+ unregister(service);
+ service.name = name;
+ register(reference, service);
+ }
+ }
+
+ @Override
+ public void removedService(ServiceReference<Gauge> reference, GaugeImpl service) {
+ unregister(service);
+ }
+
+ //~------------------------------------< Closeable >
+
+ @Override
+ public void close() {
+ tracker.close();
+ }
+
+ //~-------------------------------------< Internal >
+
+ private void unregister(GaugeImpl service) {
+ mapper.unregister(Collections.singleton(service.name));
+ }
+
+ private void register(ServiceReference<Gauge> reference, GaugeImpl gaugeImpl) {
+ mapper.addMapping(gaugeImpl.name, reference.getBundle());
+ registry.register(gaugeImpl.name, gaugeImpl);
+ }
+
+ //~--------------------------------------< GaugeImpl >
+
+ public static class GaugeImpl implements com.codahale.metrics.Gauge {
+ String name;
+ final Gauge gauge;
+
+ public GaugeImpl(String name, Gauge gauge) {
+ this.name = name;
+ this.gauge = gauge;
+ }
+
+ @Override
+ public Object getValue() {
+ return gauge.getValue();
+ }
+ }
+}
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
index 03efcee..1bc6514 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
@@ -54,6 +54,7 @@ public class MetricsServiceImpl implements MetricsService {
private final ConcurrentMap<String, Metric> metrics = new ConcurrentHashMap<>();
private final MetricRegistry registry = new MetricRegistry();
private final BundleMetricsMapper metricsMapper = new BundleMetricsMapper(registry);
+ private GaugeManager gaugeManager;
@Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
private MBeanServer server;
@@ -64,6 +65,8 @@ public class MetricsServiceImpl implements MetricsService {
private void activate(BundleContext context, Map<String, Object> config) {
enableJMXReporter();
+ gaugeManager = new GaugeManager(context, registry, metricsMapper);
+
final Dictionary<String, String> svcProps = new Hashtable<>();
svcProps.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Metrics Service");
svcProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
@@ -84,6 +87,8 @@ public class MetricsServiceImpl implements MetricsService {
}
regs.clear();
+ gaugeManager.close();
+
metrics.clear();
if (reporter != null) {
diff --git a/src/main/java/org/apache/sling/commons/metrics/package-info.java b/src/main/java/org/apache/sling/commons/metrics/package-info.java
index aa5623b..5b24541 100644
--- a/src/main/java/org/apache/sling/commons/metrics/package-info.java
+++ b/src/main/java/org/apache/sling/commons/metrics/package-info.java
@@ -22,7 +22,7 @@
*
* @version 1.0
*/
-@Version("1.0")
+@Version("1.1.0")
@Export(optional = "provide:=true")
package org.apache.sling.commons.metrics;
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
index 72a8e02..a8af34a 100644
--- a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
@@ -31,20 +31,25 @@ import javax.management.QueryExp;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import org.apache.sling.commons.metrics.Counter;
+import org.apache.sling.commons.metrics.Gauge;
import org.apache.sling.commons.metrics.Histogram;
import org.apache.sling.commons.metrics.Meter;
import org.apache.sling.commons.metrics.MetricsService;
import org.apache.sling.commons.metrics.Timer;
+import org.apache.sling.testing.mock.osgi.MapUtil;
import org.apache.sling.testing.mock.osgi.MockOsgi;
import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
+import org.osgi.framework.ServiceRegistration;
import static org.apache.sling.commons.metrics.internal.BundleMetricsMapper.JMX_TYPE_METRICS;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
@@ -145,6 +150,19 @@ public class MetricServiceTest {
}
+ @Test
+ public void gaugeRegistration() throws Exception{
+ activate();
+ ServiceRegistration<Gauge> reg = context.bundleContext().registerService(Gauge.class, new TestGauge(42),
+ MapUtil.toDictionary(Gauge.NAME, "foo"));
+
+ assertTrue(getRegistry().getGauges().containsKey("foo"));
+ assertEquals(42, getRegistry().getGauges().get("foo").getValue());
+
+ reg.unregister();
+ assertFalse(getRegistry().getGauges().containsKey("foo"));
+ }
+
private MetricRegistry getRegistry(){
return context.getService(MetricRegistry.class);
}
@@ -153,4 +171,17 @@ public class MetricServiceTest {
MockOsgi.activate(service, context.bundleContext(), Collections.<String, Object>emptyMap());
}
+ private static class TestGauge implements Gauge {
+ int value;
+
+ public TestGauge(int value){
+ this.value = value;
+ }
+
+ @Override
+ public Object getValue() {
+ return value;
+ }
+ }
+
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.