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 2020/03/16 11:41:13 UTC

[sling-whiteboard] branch master updated: metrics-osgi: add dropwizard-metrics consumer

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4601e5c  metrics-osgi: add dropwizard-metrics consumer
4601e5c is described below

commit 4601e5c1009b5c6b5ac0cc8cd0065dc36b889f2e
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Mon Mar 16 12:40:49 2020 +0100

    metrics-osgi: add dropwizard-metrics consumer
---
 osgi-metrics/consumers/pom.xml                     |  6 +++
 .../impl/dropwizard/SlingMetricsListener.java      | 50 ++++++++++++++++++++++
 2 files changed, 56 insertions(+)

diff --git a/osgi-metrics/consumers/pom.xml b/osgi-metrics/consumers/pom.xml
index f258352..5881884 100644
--- a/osgi-metrics/consumers/pom.xml
+++ b/osgi-metrics/consumers/pom.xml
@@ -70,6 +70,12 @@
             <artifactId>slf4j-simple</artifactId>
         </dependency>
         <dependency>
+            <groupId>io.dropwizard.metrics</groupId>
+            <artifactId>metrics-core</artifactId>
+            <version>3.2.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.metrics.osgi.collector</artifactId>
             <version>0.1.0-SNAPSHOT</version>
diff --git a/osgi-metrics/consumers/src/main/java/org/apache/sling/metrics/osgi/consumers/impl/dropwizard/SlingMetricsListener.java b/osgi-metrics/consumers/src/main/java/org/apache/sling/metrics/osgi/consumers/impl/dropwizard/SlingMetricsListener.java
new file mode 100644
index 0000000..5628a0e
--- /dev/null
+++ b/osgi-metrics/consumers/src/main/java/org/apache/sling/metrics/osgi/consumers/impl/dropwizard/SlingMetricsListener.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.metrics.osgi.consumers.impl.dropwizard;
+
+import org.apache.sling.metrics.osgi.StartupMetrics;
+import org.apache.sling.metrics.osgi.StartupMetricsListener;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+import com.codahale.metrics.Gauge;
+import com.codahale.metrics.MetricRegistry;
+
+@Component
+public class SlingMetricsListener implements StartupMetricsListener{
+
+    private static final String APPLICATION_STARTUP_GAUGE_NAME = "osgi.application_startup_time_millis";
+    private static final String BUNDLE_STARTUP_GAUGE_NAME_PREFIX = "osgi.bundle_startup_time_millis.";
+    private static final String SERVICE_RESTART_GAUGE_NAME_PREFIX = "osgi.service_restarts_count.";
+    
+    private static final long SERVICE_RESTART_THRESOLD = 3;
+    private static final long SLOW_BUNDLE_THRESHOLD_MILLIS = 50;
+    
+    @Reference
+    private MetricRegistry registry;
+    
+    @Override
+    public void onStartupComplete(StartupMetrics event) {
+        registry.register(APPLICATION_STARTUP_GAUGE_NAME, (Gauge<Long>) () -> event.getStartupTime().toMillis() );
+        event.getBundleStartDurations().stream()
+            .filter( bsd -> bsd.getStartedAfter().toMillis() >= SLOW_BUNDLE_THRESHOLD_MILLIS )
+            .forEach( bsd -> registry.register(BUNDLE_STARTUP_GAUGE_NAME_PREFIX + bsd.getSymbolicName(), (Gauge<Long>) () -> bsd.getStartedAfter().toMillis()));
+        event.getServiceRestarts().stream()
+            .filter( src -> src.getServiceRestarts() >= SERVICE_RESTART_THRESOLD )
+            .forEach( src -> registry.register(SERVICE_RESTART_GAUGE_NAME_PREFIX + src.getServiceIdentifier(), (Gauge<Integer>) src::getServiceRestarts) );
+    }
+}