Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
0c41384e
Commit
0c41384e
authored
Jul 25, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.0.x'
parents
e524adb2
a25b6bd4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
1 deletion
+39
-1
CacheMetricsRegistrar.java
...ork/boot/actuate/metrics/cache/CacheMetricsRegistrar.java
+28
-1
CacheMetricsRegistrarTests.java
...oot/actuate/metrics/cache/CacheMetricsRegistrarTests.java
+11
-0
No files found.
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java
View file @
0c41384e
...
@@ -26,6 +26,8 @@ import io.micrometer.core.instrument.binder.MeterBinder;
...
@@ -26,6 +26,8 @@ import io.micrometer.core.instrument.binder.MeterBinder;
import
org.springframework.boot.util.LambdaSafe
;
import
org.springframework.boot.util.LambdaSafe
;
import
org.springframework.cache.Cache
;
import
org.springframework.cache.Cache
;
import
org.springframework.cache.transaction.TransactionAwareCacheDecorator
;
import
org.springframework.util.ClassUtils
;
/**
/**
* Register supported {@link Cache} to a {@link MeterRegistry}.
* Register supported {@link Cache} to a {@link MeterRegistry}.
...
@@ -59,7 +61,7 @@ public class CacheMetricsRegistrar {
...
@@ -59,7 +61,7 @@ public class CacheMetricsRegistrar {
* @return {@code true} if the {@code cache} is supported and was registered
* @return {@code true} if the {@code cache} is supported and was registered
*/
*/
public
boolean
bindCacheToRegistry
(
Cache
cache
,
Tag
...
tags
)
{
public
boolean
bindCacheToRegistry
(
Cache
cache
,
Tag
...
tags
)
{
MeterBinder
meterBinder
=
getMeterBinder
(
cache
,
Tags
.
of
(
tags
));
MeterBinder
meterBinder
=
getMeterBinder
(
unwrapIfNecessary
(
cache
)
,
Tags
.
of
(
tags
));
if
(
meterBinder
!=
null
)
{
if
(
meterBinder
!=
null
)
{
meterBinder
.
bindTo
(
this
.
registry
);
meterBinder
.
bindTo
(
this
.
registry
);
return
true
;
return
true
;
...
@@ -87,4 +89,29 @@ public class CacheMetricsRegistrar {
...
@@ -87,4 +89,29 @@ public class CacheMetricsRegistrar {
return
Tags
.
of
(
"name"
,
cache
.
getName
());
return
Tags
.
of
(
"name"
,
cache
.
getName
());
}
}
private
Cache
unwrapIfNecessary
(
Cache
cache
)
{
if
(
ClassUtils
.
isPresent
(
"org.springframework.cache.transaction.TransactionAwareCacheDecorator"
,
getClass
().
getClassLoader
()))
{
return
TransactionAwareCacheDecoratorHandler
.
unwrapIfNecessary
(
cache
);
}
return
cache
;
}
private
static
class
TransactionAwareCacheDecoratorHandler
{
private
static
Cache
unwrapIfNecessary
(
Cache
cache
)
{
try
{
if
(
cache
instanceof
TransactionAwareCacheDecorator
)
{
return
((
TransactionAwareCacheDecorator
)
cache
).
getTargetCache
();
}
}
catch
(
NoClassDefFoundError
ex
)
{
// Ignore
}
return
cache
;
}
}
}
}
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrarTests.java
View file @
0c41384e
...
@@ -24,6 +24,7 @@ import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
...
@@ -24,6 +24,7 @@ import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.cache.caffeine.CaffeineCache
;
import
org.springframework.cache.caffeine.CaffeineCache
;
import
org.springframework.cache.transaction.TransactionAwareCacheDecorator
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
@@ -46,6 +47,16 @@ public class CacheMetricsRegistrarTests {
...
@@ -46,6 +47,16 @@ public class CacheMetricsRegistrarTests {
.
isNotNull
();
.
isNotNull
();
}
}
@Test
public
void
bindToSupportedCacheWrappedInTransactionProxy
()
{
CacheMetricsRegistrar
registrar
=
new
CacheMetricsRegistrar
(
this
.
meterRegistry
,
Collections
.
singleton
(
new
CaffeineCacheMeterBinderProvider
()));
assertThat
(
registrar
.
bindCacheToRegistry
(
new
TransactionAwareCacheDecorator
(
new
CaffeineCache
(
"test"
,
Caffeine
.
newBuilder
().
build
())))).
isTrue
();
assertThat
(
this
.
meterRegistry
.
get
(
"cache.gets"
).
tags
(
"name"
,
"test"
).
meter
())
.
isNotNull
();
}
@Test
@Test
public
void
bindToUnsupportedCache
()
{
public
void
bindToUnsupportedCache
()
{
CacheMetricsRegistrar
registrar
=
new
CacheMetricsRegistrar
(
this
.
meterRegistry
,
CacheMetricsRegistrar
registrar
=
new
CacheMetricsRegistrar
(
this
.
meterRegistry
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment