Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
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
openSource
spring-cloud-netflix
Commits
892eea75
Commit
892eea75
authored
Mar 23, 2016
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lazily look up MetricsClientHttpRequestInterceptor in BPP.
fixes gh-912
parent
3f9e76b8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
20 deletions
+50
-20
MetricsInterceptorConfiguration.java
...loud/netflix/metrics/MetricsInterceptorConfiguration.java
+31
-15
MetricsRestTemplateTests.java
...ework/cloud/netflix/metrics/MetricsRestTemplateTests.java
+19
-5
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/metrics/MetricsInterceptorConfiguration.java
View file @
892eea75
...
...
@@ -14,12 +14,15 @@
package
org
.
springframework
.
cloud
.
netflix
.
metrics
;
import
org.aspectj.lang.JoinPoint
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.boot.actuate.metrics.reader.MetricReader
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.client.RestTemplate
;
...
...
@@ -73,24 +76,37 @@ public class MetricsInterceptorConfiguration {
}
@Bean
BeanPostProcessor
spectatorRestTemplateInterceptorPostProcessor
(
final
MetricsClientHttpRequestInterceptor
interceptor
)
{
return
new
BeanPostProcessor
()
{
@Override
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
beanName
)
{
return
bean
;
}
BeanPostProcessor
spectatorRestTemplateInterceptorPostProcessor
(
)
{
return
new
MetricsInterceptorPostProcessor
();
}
private
static
class
MetricsInterceptorPostProcessor
implements
BeanPostProcessor
,
ApplicationContextAware
{
private
ApplicationContext
context
;
private
MetricsClientHttpRequestInterceptor
interceptor
;
@Override
public
Object
postProcessAfterInitialization
(
Object
bean
,
String
beanName
)
{
if
(
bean
instanceof
RestTemplate
)
{
((
RestTemplate
)
bean
).
getInterceptors
().
add
(
interceptor
);
@Override
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
beanName
)
{
return
bean
;
}
@Override
public
Object
postProcessAfterInitialization
(
Object
bean
,
String
beanName
)
{
if
(
bean
instanceof
RestTemplate
)
{
if
(
this
.
interceptor
==
null
)
{
this
.
interceptor
=
this
.
context
.
getBean
(
MetricsClientHttpRequestInterceptor
.
class
);
}
return
bean
;
((
RestTemplate
)
bean
).
getInterceptors
().
add
(
interceptor
)
;
}
};
return
bean
;
}
@Override
public
void
setApplicationContext
(
ApplicationContext
context
)
throws
BeansException
{
this
.
context
=
context
;
}
}
}
}
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/metrics/MetricsRestTemplateTests.java
View file @
892eea75
...
...
@@ -16,6 +16,8 @@
package
org
.
springframework
.
cloud
.
netflix
.
metrics
;
import
java.util.List
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
...
...
@@ -23,12 +25,16 @@ import org.springframework.boot.test.WebIntegrationTest;
import
org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory
;
import
org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactoryTests
;
import
org.springframework.http.client.ClientHttpRequestFactory
;
import
org.springframework.http.client.ClientHttpRequestInterceptor
;
import
org.springframework.http.client.InterceptingClientHttpRequestFactory
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
hamcrest
.
Matchers
.
hasSize
;
import
static
org
.
hamcrest
.
Matchers
.
instanceOf
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* @author Spencer Gibb
...
...
@@ -44,15 +50,23 @@ public class MetricsRestTemplateTests extends RibbonClientHttpRequestFactoryTest
@Override
public
void
requestFactoryIsRibbon
()
{
ClientHttpRequestFactory
requestFactory
=
this
.
restTemplate
.
getRequestFactory
();
assertT
rue
(
"wrong RequestFactory type: "
+
requestFactory
.
getClass
(),
requestFactory
instanceof
InterceptingClientHttpRequestFactory
);
assertT
hat
(
"wrong RequestFactory type: "
+
requestFactory
.
getClass
(),
requestFactory
,
is
(
instanceOf
(
InterceptingClientHttpRequestFactory
.
class
))
);
InterceptingClientHttpRequestFactory
intercepting
=
(
InterceptingClientHttpRequestFactory
)
requestFactory
;
Object
interceptorsField
=
ReflectionTestUtils
.
getField
(
intercepting
,
"interceptors"
);
assertThat
(
"wrong interceptors type: "
+
interceptorsField
.
getClass
(),
interceptorsField
,
is
(
instanceOf
(
List
.
class
)));
@SuppressWarnings
(
"unchecked"
)
List
<
ClientHttpRequestInterceptor
>
interceptors
=
(
List
<
ClientHttpRequestInterceptor
>)
interceptorsField
;
assertThat
(
"interceptors is wrong size"
,
interceptors
,
hasSize
(
1
));
assertThat
(
"wrong interceptor type"
,
interceptors
.
get
(
0
),
is
(
instanceOf
(
MetricsClientHttpRequestInterceptor
.
class
)));
Object
realRequestFactory
=
ReflectionTestUtils
.
getField
(
intercepting
,
"requestFactory"
);
assertT
rue
(
"wrong RequestFactory type: "
+
realRequestFactory
.
getClass
(),
realRequestFactory
instanceof
RibbonClientHttpRequestFactory
);
assertT
hat
(
"wrong RequestFactory type: "
+
realRequestFactory
.
getClass
(),
realRequestFactory
,
is
(
instanceOf
(
RibbonClientHttpRequestFactory
.
class
))
);
}
}
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