Skip to content

Commit

Permalink
Merge pull request #39 from stepio/feature/metrics
Browse files Browse the repository at this point in the history
Feature/metrics
  • Loading branch information
stepio authored Jan 13, 2020
2 parents 31a0bff + 8af47f3 commit d4f137b
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 15 deletions.
11 changes: 7 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>io.github.stepio.coffee-boots</groupId>
<artifactId>coffee-boots</artifactId>
<version>2.0.1-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
<inceptionYear>2018</inceptionYear>

<name>Coffee Boots</name>
Expand Down Expand Up @@ -66,16 +66,19 @@
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/github/stepio/cache/CacheCustomizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.stepio.cache;

import org.springframework.cache.Cache;

/**
* Allows additional post-processing for newly created {@link Cache} instances.
*
* @author Igor Stepanov
*/
public interface CacheCustomizer {

/**
* Invoked upon creating a {@link Cache} instance.
* @param name the name of the cache
* @param cache recently created Cache instance
* @see io.github.stepio.cache.caffeine.MultiConfigurationCacheManager#createCaffeineCache(String)
*/
void onCreate(String name, Cache cache);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,28 @@
package io.github.stepio.cache.caffeine;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
* AutoConfiguration to create all the necessary beans.
*
* @author Igor Stepanov
*/
@Configuration
@ConditionalOnClass({CacheManager.class, Caffeine.class})
@ConditionalOnClass({CaffeineCacheManager.class, Caffeine.class})
@ConditionalOnMissingBean(
value = {CacheManager.class},
name = {"cacheResolver"}
value = {MultiConfigurationCacheManager.class},
name = {"multiCaffeineManager"}
)
@AutoConfigureBefore({CacheAutoConfiguration.class})
public class CaffeineSpecSpringAutoConfiguration {
@AutoConfigureAfter(CacheAutoConfiguration.class)
public class CaffeineSpecAutoConfiguration {

/**
* Create CaffeineSupplier bean if it's not available in the context.
Expand All @@ -55,8 +56,9 @@ public CaffeineSupplier caffeineSupplier() {
* @return the MultiConfigurationCacheManager instance
*/
@Bean
@Primary
@ConditionalOnMissingBean
public MultiConfigurationCacheManager cacheManager(CaffeineSupplier caffeineSupplier) {
public MultiConfigurationCacheManager multiCaffeineManager(CaffeineSupplier caffeineSupplier) {
MultiConfigurationCacheManager cacheManager = new MultiConfigurationCacheManager();
cacheManager.setCacheBuilderSupplier(caffeineSupplier);
return cacheManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import io.github.stepio.cache.CacheCustomizer;
import org.springframework.cache.caffeine.CaffeineCacheManager;

import java.util.List;
import java.util.function.Function;

/**
Expand All @@ -31,7 +33,8 @@
*/
public class MultiConfigurationCacheManager extends CaffeineCacheManager {

private Function<String, Caffeine<Object, Object>> cacheBuilderSupplier;
protected Function<String, Caffeine<Object, Object>> cacheBuilderSupplier;
protected List<CacheCustomizer> customizers;

/**
* Set the {@link Function} to produce {@link Caffeine} for building each individual {@link Cache} instance.
Expand All @@ -43,12 +46,29 @@ public void setCacheBuilderSupplier(Function<String, Caffeine<Object, Object>> s
this.cacheBuilderSupplier = supplier;
}

/**
* Set list of {@link CacheCustomizer} instances to be invoked upon creation of each Cache instance.
* @param customizers list of CacheCustomizer instances to be invoked
* @see CacheCustomizer#onCreate(String, org.springframework.cache.Cache)
*/
public void setCustomizers(List<CacheCustomizer> customizers) {
this.customizers = customizers;
}

/**
* Accessor for the underlying {@link Caffeine} producer.
* @return exact implementation of function for instantiating the Cache with the specified name
*/
public Function<String, Caffeine<Object, Object>> getCacheBuilderSupplier() {
return cacheBuilderSupplier;
return this.cacheBuilderSupplier;
}

protected org.springframework.cache.Cache createCaffeineCache(String name) {
org.springframework.cache.Cache cache = super.createCaffeineCache(name);
if (this.customizers != null) {
this.customizers.forEach(customizer -> customizer.onCreate(name, cache));
}
return cache;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.stepio.cache.metrics;

import io.github.stepio.cache.CacheCustomizer;
import io.github.stepio.cache.caffeine.MultiConfigurationCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.cache.CacheMetricsRegistrar;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

/**
* Binds {@link CacheMetricsRegistrar} to {@link MultiConfigurationCacheManager}.
* Allows configuring metrics for dynamic caches.
*
* @author Igor Stepanov
*/
@Configuration
@ConditionalOnClass({CacheMetricsRegistrar.class})
public class CaffeineMetricsAutoConfiguration {

/**
* Binds {@link CacheMetricsRegistrar} to {@link MultiConfigurationCacheManager} using {@link CacheCustomizer}.
* @param cacheManager instance of MultiConfigurationCacheManager bean
* @param metricsRegistrar instance of CacheMetricsRegistrar bean
*/
@Autowired
public void bind(MultiConfigurationCacheManager cacheManager, CacheMetricsRegistrar metricsRegistrar) {
CacheCustomizer customizer = new MetricsRegistrarProxy(metricsRegistrar);
cacheManager.setCustomizers(Collections.singletonList(customizer));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.stepio.cache.metrics;

import io.github.stepio.cache.CacheCustomizer;
import io.micrometer.core.instrument.Tag;
import org.springframework.boot.actuate.metrics.cache.CacheMetricsRegistrar;
import org.springframework.cache.Cache;

/**
* Implementation of {@link CacheCustomizer}, which is used to bind newly created caches to metrics registry.
*/
public class MetricsRegistrarProxy implements CacheCustomizer {

private CacheMetricsRegistrar cacheMetricsRegistrar;

public MetricsRegistrarProxy(CacheMetricsRegistrar cacheMetricsRegistrar) {
this.cacheMetricsRegistrar = cacheMetricsRegistrar;
}

@Override
public void onCreate(String name, Cache cache) {
Tag cacheManagerTag = Tag.of("cacheManager", "multiCaffeineManager");
this.cacheMetricsRegistrar.bindCacheToRegistry(cache, cacheManagerTag);
}
}
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.github.stepio.cache.caffeine.CaffeineSpecSpringAutoConfiguration
io.github.stepio.cache.caffeine.CaffeineSpecAutoConfiguration,\
io.github.stepio.cache.metrics.CaffeineMetricsAutoConfiguration

0 comments on commit d4f137b

Please sign in to comment.