<commit_before>/**
 * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
 *
 * 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 */
package io.pravega.shared.metrics;

import com.google.common.base.Preconditions;
import io.micrometer.core.instrument.Meter;
import io.pravega.common.Exceptions;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * Base class for a Metric Proxy.
 *
 * @param <T> Type of Metric.
 */
@Slf4j
abstract class MetricProxy<T extends Metric> implements AutoCloseable {
    private final AtomicReference<T> instance = new AtomicReference<>();
    private final AtomicBoolean closed = new AtomicBoolean();

    @Getter
    private final String proxyName;
    private final Consumer<String> closeCallback;

    /**
     * Creates a new instance of the MetricProxy class.
     *
     * @param instance      The initial Metric Instance.
     * @param proxyName     The name of the MetricProxy. This may be different from the name of the Metric's instance.
     * @param closeCallback A Consumer that will be invoked when this Proxy is closed.
     */
    MetricProxy(T instance, String proxyName, Consumer<String> closeCallback) {
        this.closeCallback = Preconditions.checkNotNull(closeCallback, "closeCallback");
        this.proxyName = Exceptions.checkNotNullOrEmpty(proxyName, "name");
        updateInstance(instance);
    }

    @Override
    public void close() {
        if (!closed.getAndSet(true)) {
            T i = this.instance.get();
            if (i != null) {
                i.close();
                this.closeCallback.accept(this.proxyName);
            }
        }
    }

    /**
     * Gets the id of the underlying metric.
     *
     * @return The id of the underlying metric.
     */
    public Meter.Id getId() {
        return getInstance().getId();
    }

    /**
     * Updates the underlying Metric instance with the given one, and closes out the previous one.
     *
     * @param instance The instance to update to.
     */
    void updateInstance(T instance) {
        T oldInstance = this.instance.getAndSet(Preconditions.checkNotNull(instance, "instance"));
        if (oldInstance != null && oldInstance != instance) {
            oldInstance.close();
        }
    }

    protected T getInstance() {
        if (!closed.get()) {
            log.warn("This MetricProxy ({}) has already been closed. Further updates to this Metric will not be seen by a MeterRegistry.", proxyName);
        }
        return this.instance.get();
    }
}<commit_msg>Issue 5478: Removed log message from MetricsProxy (#5479)<commit_after>/**
 * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
 *
 * 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 */
package io.pravega.shared.metrics;

import com.google.common.base.Preconditions;
import io.micrometer.core.instrument.Meter;
import io.pravega.common.Exceptions;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * Base class for a Metric Proxy.
 *
 * @param <T> Type of Metric.
 */
@Slf4j
abstract class MetricProxy<T extends Metric> implements AutoCloseable {
    private final AtomicReference<T> instance = new AtomicReference<>();
    private final AtomicBoolean closed = new AtomicBoolean();

    @Getter
    private final String proxyName;
    private final Consumer<String> closeCallback;

    /**
     * Creates a new instance of the MetricProxy class.
     *
     * @param instance      The initial Metric Instance.
     * @param proxyName     The name of the MetricProxy. This may be different from the name of the Metric's instance.
     * @param closeCallback A Consumer that will be invoked when this Proxy is closed.
     */
    MetricProxy(T instance, String proxyName, Consumer<String> closeCallback) {
        this.closeCallback = Preconditions.checkNotNull(closeCallback, "closeCallback");
        this.proxyName = Exceptions.checkNotNullOrEmpty(proxyName, "name");
        updateInstance(instance);
    }

    @Override
    public void close() {
        if (!closed.getAndSet(true)) {
            T i = this.instance.get();
            if (i != null) {
                i.close();
                this.closeCallback.accept(this.proxyName);
            }
        }
    }

    /**
     * Gets the id of the underlying metric.
     *
     * @return The id of the underlying metric.
     */
    public Meter.Id getId() {
        return getInstance().getId();
    }

    /**
     * Updates the underlying Metric instance with the given one, and closes out the previous one.
     *
     * @param instance The instance to update to.
     */
    void updateInstance(T instance) {
        T oldInstance = this.instance.getAndSet(Preconditions.checkNotNull(instance, "instance"));
        if (oldInstance != null && oldInstance != instance) {
            oldInstance.close();
        }
    }

    protected T getInstance() {
        return this.instance.get();
    }
}
{'fraction_non_alphanumeric': 0.10046728971962617, 'fraction_numerical': 0.003953989935298346, 'mean_word_length': 2.75, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5937646', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1531, 'n_words': 490}