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
4634811c
Commit
4634811c
authored
Aug 31, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
7bee9dfc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
48 deletions
+72
-48
RestTemplateMetricsConfigurationTests.java
...ics/web/client/RestTemplateMetricsConfigurationTests.java
+23
-15
WebClientMetricsConfigurationTests.java
...etrics/web/client/WebClientMetricsConfigurationTests.java
+49
-33
No files found.
spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateMetricsConfigurationTests.java
View file @
4634811c
...
@@ -55,9 +55,8 @@ public class RestTemplateMetricsConfigurationTests {
...
@@ -55,9 +55,8 @@ public class RestTemplateMetricsConfigurationTests {
public
void
restTemplateCreatedWithBuilderIsInstrumented
()
{
public
void
restTemplateCreatedWithBuilderIsInstrumented
()
{
this
.
contextRunner
.
run
((
context
)
->
{
this
.
contextRunner
.
run
((
context
)
->
{
MeterRegistry
registry
=
context
.
getBean
(
MeterRegistry
.
class
);
MeterRegistry
registry
=
context
.
getBean
(
MeterRegistry
.
class
);
RestTemplate
restTemplate
=
context
.
getBean
(
RestTemplateBuilder
.
class
)
RestTemplateBuilder
builder
=
context
.
getBean
(
RestTemplateBuilder
.
class
);
.
build
();
validateRestTemplate
(
builder
,
registry
);
validateRestTemplate
(
restTemplate
,
registry
);
});
});
}
}
...
@@ -65,11 +64,10 @@ public class RestTemplateMetricsConfigurationTests {
...
@@ -65,11 +64,10 @@ public class RestTemplateMetricsConfigurationTests {
public
void
restTemplateCanBeCustomizedManually
()
{
public
void
restTemplateCanBeCustomizedManually
()
{
this
.
contextRunner
.
run
((
context
)
->
{
this
.
contextRunner
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
MetricsRestTemplateCustomizer
.
class
);
assertThat
(
context
).
hasSingleBean
(
MetricsRestTemplateCustomizer
.
class
);
RestTemplate
restTemplate
=
new
RestTemplateBuilder
()
RestTemplateBuilder
customBuilder
=
new
RestTemplateBuilder
()
.
customizers
(
context
.
getBean
(
MetricsRestTemplateCustomizer
.
class
))
.
customizers
(
context
.
getBean
(
MetricsRestTemplateCustomizer
.
class
));
.
build
();
MeterRegistry
registry
=
context
.
getBean
(
MeterRegistry
.
class
);
MeterRegistry
registry
=
context
.
getBean
(
MeterRegistry
.
class
);
validateRestTemplate
(
restTemplate
,
registry
);
validateRestTemplate
(
customBuilder
,
registry
);
});
});
}
}
...
@@ -96,8 +94,8 @@ public class RestTemplateMetricsConfigurationTests {
...
@@ -96,8 +94,8 @@ public class RestTemplateMetricsConfigurationTests {
assertThat
(
registry
.
get
(
"http.client.requests"
).
meters
()).
hasSize
(
3
);
assertThat
(
registry
.
get
(
"http.client.requests"
).
meters
()).
hasSize
(
3
);
assertThat
(
this
.
out
.
toString
()).
doesNotContain
(
assertThat
(
this
.
out
.
toString
()).
doesNotContain
(
"Reached the maximum number of URI tags for 'http.client.requests'."
);
"Reached the maximum number of URI tags for 'http.client.requests'."
);
assertThat
(
this
.
out
.
toString
())
.
doesNotContain
(
assertThat
(
this
.
out
.
toString
())
"Are you using 'uriVariables' on RestTemplate calls
?"
);
.
doesNotContain
(
"Are you using 'uriVariables'
?"
);
});
});
}
}
...
@@ -115,13 +113,23 @@ public class RestTemplateMetricsConfigurationTests {
...
@@ -115,13 +113,23 @@ public class RestTemplateMetricsConfigurationTests {
return
registry
;
return
registry
;
}
}
private
void
validateRestTemplate
(
RestTemplate
restTemplate
,
MeterRegistry
registry
)
{
private
void
validateRestTemplate
(
RestTemplate
Builder
builder
,
MockRestServiceServer
server
=
MockRestServiceServer
.
createServer
(
restTemplate
);
MeterRegistry
registry
)
{
server
.
expect
(
requestTo
(
"/test"
)).
andRespond
(
withStatus
(
HttpStatus
.
OK
)
);
RestTemplate
restTemplate
=
mockRestTemplate
(
builder
);
assertThat
(
registry
.
find
(
"http.client.requests"
).
meter
()).
isNull
();
assertThat
(
registry
.
find
(
"http.client.requests"
).
meter
()).
isNull
();
assertThat
(
restTemplate
.
getForEntity
(
"/test"
,
Void
.
class
).
getStatusCode
())
assertThat
(
restTemplate
.
isEqualTo
(
HttpStatus
.
OK
);
.
getForEntity
(
"/projects/{project}"
,
Void
.
class
,
"spring-boot"
)
registry
.
get
(
"http.client.requests"
).
meter
();
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
registry
.
get
(
"http.client.requests"
).
tags
(
"uri"
,
"/projects/{project}"
)
.
meter
()).
isNotNull
();
}
private
RestTemplate
mockRestTemplate
(
RestTemplateBuilder
builder
)
{
RestTemplate
restTemplate
=
builder
.
build
();
MockRestServiceServer
server
=
MockRestServiceServer
.
createServer
(
restTemplate
);
server
.
expect
(
requestTo
(
"/projects/spring-boot"
))
.
andRespond
(
withStatus
(
HttpStatus
.
OK
));
return
restTemplate
;
}
}
}
}
spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/WebClientMetricsConfigurationTests.java
View file @
4634811c
...
@@ -17,16 +17,15 @@
...
@@ -17,16 +17,15 @@
package
org
.
springframework
.
boot
.
actuate
.
autoconfigure
.
metrics
.
web
.
client
;
package
org
.
springframework
.
boot
.
actuate
.
autoconfigure
.
metrics
.
web
.
client
;
import
io.micrometer.core.instrument.MeterRegistry
;
import
io.micrometer.core.instrument.MeterRegistry
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties
;
import
org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun
;
import
org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun
;
import
org.springframework.boot.actuate.metrics.web.reactive.client.WebClientExchangeTagsProvider
;
import
org.springframework.boot.actuate.metrics.web.reactive.client.WebClientExchangeTagsProvider
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration
;
import
org.springframework.boot.test.context.assertj.AssertableApplicationContext
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.boot.test.rule.OutputCapture
;
import
org.springframework.boot.test.rule.OutputCapture
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
...
@@ -45,36 +44,23 @@ import static org.mockito.Mockito.mock;
...
@@ -45,36 +44,23 @@ import static org.mockito.Mockito.mock;
* Tests for {@link WebClientMetricsConfiguration}
* Tests for {@link WebClientMetricsConfiguration}
*
*
* @author Brian Clozel
* @author Brian Clozel
* @author Stephane Nicoll
*/
*/
public
class
WebClientMetricsConfigurationTests
{
public
class
WebClientMetricsConfigurationTests
{
private
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
with
(
MetricsRun
.
simple
())
.
with
(
MetricsRun
.
simple
())
.
withConfiguration
(
AutoConfigurations
.
of
(
WebClientAutoConfiguration
.
class
));
.
withConfiguration
(
AutoConfigurations
.
of
(
WebClientAutoConfiguration
.
class
));
private
ClientHttpConnector
connector
;
@Rule
@Rule
public
OutputCapture
out
=
new
OutputCapture
();
public
OutputCapture
out
=
new
OutputCapture
();
@Before
public
void
setup
()
{
this
.
connector
=
mock
(
ClientHttpConnector
.
class
);
given
(
this
.
connector
.
connect
(
any
(),
any
(),
any
()))
.
willReturn
(
Mono
.
just
(
new
MockClientHttpResponse
(
HttpStatus
.
OK
)));
}
@Test
@Test
public
void
webClientCreatedWithBuilderIsInstrumented
()
{
public
void
webClientCreatedWithBuilderIsInstrumented
()
{
this
.
contextRunner
.
run
((
context
)
->
{
this
.
contextRunner
.
run
((
context
)
->
{
WebClient
.
Builder
builder
=
context
.
getBean
(
WebClient
.
Builder
.
class
);
WebClient
webClient
=
builder
.
clientConnector
(
this
.
connector
).
build
();
MeterRegistry
registry
=
context
.
getBean
(
MeterRegistry
.
class
);
MeterRegistry
registry
=
context
.
getBean
(
MeterRegistry
.
class
);
assertThat
(
registry
.
find
(
"http.client.requests"
).
meter
()).
isNull
();
WebClient
.
Builder
builder
=
context
.
getBean
(
WebClient
.
Builder
.
class
);
webClient
.
get
().
uri
(
"http://example.org/projects/{project}"
,
"spring-boot"
)
validateWebClient
(
builder
,
registry
);
.
exchange
().
block
();
assertThat
(
registry
.
find
(
"http.client.requests"
)
.
tags
(
"uri"
,
"/projects/{project}"
).
meter
()).
isNotNull
();
});
});
}
}
...
@@ -89,20 +75,10 @@ public class WebClientMetricsConfigurationTests {
...
@@ -89,20 +75,10 @@ public class WebClientMetricsConfigurationTests {
@Test
@Test
public
void
afterMaxUrisReachedFurtherUrisAreDenied
()
{
public
void
afterMaxUrisReachedFurtherUrisAreDenied
()
{
this
.
contextRunner
this
.
contextRunner
.
withPropertyValues
(
"management.metrics.web.client.max-uri-tags=
10
"
)
.
withPropertyValues
(
"management.metrics.web.client.max-uri-tags=
2
"
)
.
run
((
context
)
->
{
.
run
((
context
)
->
{
WebClient
.
Builder
builder
=
context
.
getBean
(
WebClient
.
Builder
.
class
);
MeterRegistry
registry
=
getInitializedMeterRegistry
(
context
);
WebClient
webClient
=
builder
.
clientConnector
(
this
.
connector
).
build
();
assertThat
(
registry
.
get
(
"http.client.requests"
).
meters
()).
hasSize
(
2
);
MetricsProperties
properties
=
context
.
getBean
(
MetricsProperties
.
class
);
int
maxUriTags
=
properties
.
getWeb
().
getClient
().
getMaxUriTags
();
MeterRegistry
registry
=
context
.
getBean
(
MeterRegistry
.
class
);
for
(
int
i
=
0
;
i
<
maxUriTags
+
10
;
i
++)
{
webClient
.
get
().
uri
(
"http://example.org/projects/"
+
i
).
exchange
()
.
block
();
}
assertThat
(
registry
.
get
(
"http.client.requests"
).
meters
())
.
hasSize
(
maxUriTags
);
assertThat
(
this
.
out
.
toString
()).
contains
(
assertThat
(
this
.
out
.
toString
()).
contains
(
"Reached the maximum number of URI tags for 'http.client.requests'."
);
"Reached the maximum number of URI tags for 'http.client.requests'."
);
assertThat
(
this
.
out
.
toString
())
assertThat
(
this
.
out
.
toString
())
...
@@ -110,8 +86,48 @@ public class WebClientMetricsConfigurationTests {
...
@@ -110,8 +86,48 @@ public class WebClientMetricsConfigurationTests {
});
});
}
}
@Test
public
void
shouldNotDenyNorLogIfMaxUrisIsNotReached
()
{
this
.
contextRunner
.
withPropertyValues
(
"management.metrics.web.client.max-uri-tags=5"
)
.
run
((
context
)
->
{
MeterRegistry
registry
=
getInitializedMeterRegistry
(
context
);
assertThat
(
registry
.
get
(
"http.client.requests"
).
meters
()).
hasSize
(
3
);
assertThat
(
this
.
out
.
toString
()).
doesNotContain
(
"Reached the maximum number of URI tags for 'http.client.requests'."
);
assertThat
(
this
.
out
.
toString
())
.
doesNotContain
(
"Are you using 'uriVariables'?"
);
});
}
private
MeterRegistry
getInitializedMeterRegistry
(
AssertableApplicationContext
context
)
{
WebClient
webClient
=
mockWebClient
(
context
.
getBean
(
WebClient
.
Builder
.
class
));
MeterRegistry
registry
=
context
.
getBean
(
MeterRegistry
.
class
);
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
webClient
.
get
().
uri
(
"http://example.org/projects/"
+
i
).
exchange
().
block
();
}
return
registry
;
}
private
void
validateWebClient
(
WebClient
.
Builder
builder
,
MeterRegistry
registry
)
{
WebClient
webClient
=
mockWebClient
(
builder
);
assertThat
(
registry
.
find
(
"http.client.requests"
).
meter
()).
isNull
();
webClient
.
get
().
uri
(
"http://example.org/projects/{project}"
,
"spring-boot"
)
.
exchange
().
block
();
assertThat
(
registry
.
find
(
"http.client.requests"
)
.
tags
(
"uri"
,
"/projects/{project}"
).
meter
()).
isNotNull
();
}
private
WebClient
mockWebClient
(
WebClient
.
Builder
builder
)
{
ClientHttpConnector
connector
=
mock
(
ClientHttpConnector
.
class
);
given
(
connector
.
connect
(
any
(),
any
(),
any
()))
.
willReturn
(
Mono
.
just
(
new
MockClientHttpResponse
(
HttpStatus
.
OK
)));
return
builder
.
clientConnector
(
connector
).
build
();
}
@Configuration
@Configuration
protected
static
class
CustomTagsProviderConfig
{
static
class
CustomTagsProviderConfig
{
@Bean
@Bean
public
WebClientExchangeTagsProvider
customTagsProvider
()
{
public
WebClientExchangeTagsProvider
customTagsProvider
()
{
...
...
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