All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micrometer.spring.autoconfigure.cache.CacheMetricsRegistrarConfiguration Maven / Gradle / Ivy

There is a newer version: 1.3.20
Show newest version
/*
 * Copyright 2012-2019 the original author or authors.
 *
 * 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
 *
 *      https://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 io.micrometer.spring.autoconfigure.cache;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.spring.cache.CacheMeterBinderProvider;
import io.micrometer.spring.cache.CacheMetricsRegistrar;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.Map;

/**
 * Configure a {@link CacheMetricsRegistrar} and register all available {@link Cache
 * caches}.
 *
 * @author Stephane Nicoll
 * @author Johnny Lim
 */
@Configuration
@Conditional(CacheMetricsRegistrarConfiguration.CacheMetricsRegistrarConditionalOnBeans.class)
class CacheMetricsRegistrarConfiguration {

    private static final String CACHE_MANAGER_SUFFIX = "cacheManager";

    private final MeterRegistry registry;

    private final CacheMetricsRegistrar cacheMetricsRegistrar;

    private final Map cacheManagers;

    CacheMetricsRegistrarConfiguration(MeterRegistry registry,
                                       Collection> binderProviders,
                                       Map cacheManagers) {
        this.registry = registry;
        this.cacheManagers = cacheManagers;
        this.cacheMetricsRegistrar = new CacheMetricsRegistrar(this.registry,
                binderProviders);
    }

    @Bean
    public CacheMetricsRegistrar cacheMetricsRegistrar() {
        return this.cacheMetricsRegistrar;
    }

    @PostConstruct
    public void bindCachesToRegistry() {
        this.cacheManagers.forEach(this::bindCacheManagerToRegistry);
    }

    private void bindCacheManagerToRegistry(String beanName, CacheManager cacheManager) {
        cacheManager.getCacheNames().forEach((cacheName) -> bindCacheToRegistry(beanName,
                cacheManager.getCache(cacheName)));
    }

    private void bindCacheToRegistry(String beanName, Cache cache) {
        Tag cacheManagerTag = Tag.of("cacheManager", getCacheManagerName(beanName));
        this.cacheMetricsRegistrar.bindCacheToRegistry(cache, cacheManagerTag);
    }

    /**
     * Get the name of a {@link CacheManager} based on its {@code beanName}.
     *
     * @param beanName the name of the {@link CacheManager} bean
     * @return a name for the given cache manager
     */
    private String getCacheManagerName(String beanName) {
        if (beanName.length() > CACHE_MANAGER_SUFFIX.length()
                && StringUtils.endsWithIgnoreCase(beanName, CACHE_MANAGER_SUFFIX)) {
            return beanName.substring(0,
                    beanName.length() - CACHE_MANAGER_SUFFIX.length());
        }
        return beanName;
    }

    static class CacheMetricsRegistrarConditionalOnBeans extends AllNestedConditions {

        CacheMetricsRegistrarConditionalOnBeans() {
            super(ConfigurationPhase.REGISTER_BEAN);
        }

        @ConditionalOnBean(MeterRegistry.class)
        static class ConditionalOnMeterRegistryBean {
        }

        @ConditionalOnBean(CacheMeterBinderProvider.class)
        static class ConditionalOnCacheMeterBinderProviderBean {
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy