-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from stepio/feature/metrics
Feature/metrics
- Loading branch information
Showing
7 changed files
with
116 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/io/github/stepio/cache/metrics/CaffeineMetricsAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/io/github/stepio/cache/metrics/MetricsRegistrarProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |