[java-support] branch dev/JSPT-111 updated: Push metric helper for reloadable services down into java-support.
Scott Cantor
cantor.2 at osu.edu
Thu Jun 16 15:23:15 UTC 2022
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch dev/JSPT-111
in repository java-support.
View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=94534a97dc53e1753fbdcea33a9fde390910ee23
The following commit(s) were added to refs/heads/dev/JSPT-111 by this push:
new 94534a9 Push metric helper for reloadable services down into java-support.
94534a9 is described below
commit 94534a97dc53e1753fbdcea33a9fde390910ee23
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Jun 16 11:23:09 2022 -0400
Push metric helper for reloadable services down into java-support.
---
pom.xml | 6 +
.../support/service/ReloadableServiceGaugeSet.java | 176 +++++++++++++++++++++
2 files changed, 182 insertions(+)
diff --git a/pom.xml b/pom.xml
index 0a5bbcc..62e628f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -123,6 +123,12 @@
<artifactId>bcpkix-jdk15on</artifactId>
<optional>true</optional>
</dependency>
+ <dependency>
+ <!-- Required if using the metrics service class. -->
+ <groupId>io.dropwizard.metrics</groupId>
+ <artifactId>metrics-core</artifactId>
+ <optional>true</optional>
+ </dependency>
<!-- Provided Dependencies -->
<dependency>
diff --git a/src/main/java/net/shibboleth/utilities/java/support/service/ReloadableServiceGaugeSet.java b/src/main/java/net/shibboleth/utilities/java/support/service/ReloadableServiceGaugeSet.java
new file mode 100644
index 0000000..62030eb
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/service/ReloadableServiceGaugeSet.java
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.service;
+
+import com.codahale.metrics.Gauge;
+import com.codahale.metrics.Metric;
+import com.codahale.metrics.MetricFilter;
+import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.MetricSet;
+
+import net.shibboleth.utilities.java.support.annotation.ParameterName;
+import net.shibboleth.utilities.java.support.annotation.constraint.Live;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.component.AbstractInitializableComponent;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.component.ComponentSupport;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
+
+import java.time.Instant;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+
+/**
+ * A set of gauges for a reloadable service.
+ * @param <T> Type of service we are monitoring
+ */
+public class ReloadableServiceGaugeSet<T> extends AbstractInitializableComponent implements MetricSet, MetricFilter {
+
+ /** Default prefix for metrics. */
+ @Nonnull @NotEmpty protected static final String DEFAULT_METRIC_NAME = "net.shibboleth.idp";
+
+ /** The map of gauges. */
+ @NonnullAfterInit @NonnullElements private Map<String,Metric> gauges;
+
+ /** The service to report on. */
+ @NonnullAfterInit private ReloadableService<T> service;
+
+ /** The metric Prefix. */
+ @Nonnull @NotEmpty private final String metricPrefix;
+
+ /** The default metric name. */
+ @Nonnull @NotEmpty private String defaultMetricName;
+
+ /**
+ * Constructor.
+ *
+ * @param metricName name to include in metric names produced by this set
+ */
+ public ReloadableServiceGaugeSet(@Nonnull @NotEmpty @ParameterName(name="metricName") final String metricName) {
+ metricPrefix = Constraint.isNotEmpty(metricName, "Metric name cannot be null or empty");
+ }
+
+ /**
+ * Get the default metric name.
+ *
+ * @return default metric name
+ */
+ @NonnullAfterInit @NotEmpty public String getDefaultMetricName() {
+ return defaultMetricName;
+ }
+
+ /**
+ * Set the default metric name.
+ *
+ * @param name the name
+ */
+ public void setDefaultMetricName(@Nonnull @NotEmpty final String name) {
+ ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+ defaultMetricName = Constraint.isNotNull(StringSupport.trimOrNull(name),
+ "Default metric name cannot be null or empty");
+ }
+
+ /**
+ * Get the service to report on.
+ *
+ * @return service to report on
+ */
+ @NonnullAfterInit public ReloadableService<T> getService() {
+ return service;
+ }
+
+ /**
+ * Set the service to report on.
+ *
+ * @param svc service instance
+ */
+ public void setService(@Nonnull final ReloadableService<T> svc) {
+ ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+ service = Constraint.isNotNull(svc, "ReloadableService cannot be null");
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected void doInitialize() throws ComponentInitializationException {
+ if (service == null) {
+ throw new ComponentInitializationException("Injected ReloadableService cannot be null");
+ }
+
+ gauges = new HashMap<>();
+
+ gauges.put(
+ MetricRegistry.name(defaultMetricName, metricPrefix, "reload", "success"),
+ new Gauge<Instant>() {
+ public Instant getValue() {
+ return service.getLastSuccessfulReloadInstant();
+ }
+ });
+
+ gauges.put(
+ MetricRegistry.name(defaultMetricName, metricPrefix, "reload", "attempt"),
+ new Gauge<Instant>() {
+ public Instant getValue() {
+ return service.getLastReloadAttemptInstant();
+ }
+ });
+
+ gauges.put(
+ MetricRegistry.name(defaultMetricName, metricPrefix, "reload", "error"),
+ new Gauge<String>() {
+ public String getValue() {
+ return service.getReloadFailureCause() != null
+ ? service.getReloadFailureCause().getMessage() : null;
+ }
+ });
+
+ super.doInitialize();
+ }
+
+ /** {@inheritDoc} */
+ public Map<String,Metric> getMetrics() {
+ return Collections.unmodifiableMap(gauges);
+ }
+
+ /** {@inheritDoc} */
+ public boolean matches(final String name, final Metric metric) {
+ return gauges.containsKey(name);
+ }
+
+ /**
+ * Get the underlying map of metrics.
+ *
+ * @return map of metrics
+ */
+ @Nonnull @NonnullElements @Live protected Map<String,Metric> getMetricMap() {
+ return gauges;
+ }
+
+ /** Get the log prefix.
+ * @return the log prefix (usually the metric name).
+ */
+ @Nonnull @NotEmpty protected final String getLogPrefix() {
+ return metricPrefix;
+ }
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list