/**
 * Copyright 2017 Pivotal Software, Inc.
 * <p>
 * Licensed 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
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 io.micrometer.core.instrument.internal;

import io.micrometer.core.instrument.AbstractMeter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.util.MeterEquivalence;
import io.micrometer.core.lang.Nullable;

import java.lang.ref.WeakReference;
import java.util.function.ToDoubleFunction;

public class DefaultGauge<T> extends AbstractMeter implements Gauge {
    private final WeakReference<T> ref;
    private final ToDoubleFunction<T> value;

    public DefaultGauge(Meter.Id id, @Nullable T obj, ToDoubleFunction<T> value) {
        super(id);
        this.ref = new WeakReference<>(obj);
        this.value = value;
    }

    @Override
    public double value() {
        T obj = ref.get();
        return obj != null ? value.applyAsDouble(ref.get()) : Double.NaN;
    }

    @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
    @Override
    public boolean equals(Object o) {
        return MeterEquivalence.equals(this, o);
    }

    @Override
    public int hashCode() {
        return MeterEquivalence.hashCode(this);
    }
}

@@ -981,16 +981,151 @@
 ction;%0A%0A
+/**%0A * Default implementation for %7B@link Gauge%7D.%0A *%0A * @param %3CT%3E reference type%0A *%0A * @author Jon Schneider%0A * @author Johnny Lim%0A */%0A
 public c
@@ -1531,23 +1531,20 @@
         
-return 
+if (
 obj != n
@@ -1550,43 +1550,172 @@
 null
- ? value.applyAsDouble(ref.get()) :
+) %7B%0A            try %7B%0A                return value.applyAsDouble(ref.get());%0A            %7D%0A            catch (Throwable ex) %7B%0A            %7D%0A        %7D%0A        return
  Dou

Make value function defensive for exceptions in DefaultGauge (#1582)