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
ce3dfc57
Commit
ce3dfc57
authored
Mar 23, 2021
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.4.x'
Closes gh-25767
parents
db782f50
33cf8b3b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
21 deletions
+60
-21
RestTemplateMetricsConfigurationTests.java
...ics/web/client/RestTemplateMetricsConfigurationTests.java
+20
-5
MetricsClientHttpRequestInterceptor.java
...trics/web/client/MetricsClientHttpRequestInterceptor.java
+27
-15
RootUriTemplateHandler.java
...ringframework/boot/web/client/RootUriTemplateHandler.java
+13
-1
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 @
ce3dfc57
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
21
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -63,6 +63,16 @@ class RestTemplateMetricsConfigurationTests {
});
}
@Test
void
restTemplateWithRootUriIsInstrumented
()
{
this
.
contextRunner
.
run
((
context
)
->
{
MeterRegistry
registry
=
context
.
getBean
(
MeterRegistry
.
class
);
RestTemplateBuilder
builder
=
context
.
getBean
(
RestTemplateBuilder
.
class
);
builder
=
builder
.
rootUri
(
"/root"
);
validateRestTemplate
(
builder
,
registry
,
"/root"
);
});
}
@Test
void
restTemplateCanBeCustomizedManually
()
{
this
.
contextRunner
.
run
((
context
)
->
{
...
...
@@ -130,17 +140,22 @@ class RestTemplateMetricsConfigurationTests {
}
private
void
validateRestTemplate
(
RestTemplateBuilder
builder
,
MeterRegistry
registry
)
{
RestTemplate
restTemplate
=
mockRestTemplate
(
builder
);
this
.
validateRestTemplate
(
builder
,
registry
,
""
);
}
private
void
validateRestTemplate
(
RestTemplateBuilder
builder
,
MeterRegistry
registry
,
String
rootUri
)
{
RestTemplate
restTemplate
=
mockRestTemplate
(
builder
,
rootUri
);
assertThat
(
registry
.
find
(
"http.client.requests"
).
meter
()).
isNull
();
assertThat
(
restTemplate
.
getForEntity
(
"/projects/{project}"
,
Void
.
class
,
"spring-boot"
).
getStatusCode
())
.
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
registry
.
get
(
"http.client.requests"
).
tags
(
"uri"
,
"/projects/{project}"
).
meter
()).
isNotNull
();
assertThat
(
registry
.
get
(
"http.client.requests"
).
tags
(
"uri"
,
rootUri
+
"/projects/{project}"
).
meter
())
.
isNotNull
();
}
private
RestTemplate
mockRestTemplate
(
RestTemplateBuilder
builder
)
{
private
RestTemplate
mockRestTemplate
(
RestTemplateBuilder
builder
,
String
rootUri
)
{
RestTemplate
restTemplate
=
builder
.
build
();
MockRestServiceServer
server
=
MockRestServiceServer
.
createServer
(
restTemplate
);
server
.
expect
(
requestTo
(
"/projects/spring-boot"
)).
andRespond
(
withStatus
(
HttpStatus
.
OK
));
server
.
expect
(
requestTo
(
rootUri
+
"/projects/spring-boot"
)).
andRespond
(
withStatus
(
HttpStatus
.
OK
));
return
restTemplate
;
}
...
...
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/MetricsClientHttpRequestInterceptor.java
View file @
ce3dfc57
...
...
@@ -29,6 +29,7 @@ import org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.boot.actuate.metrics.AutoTimer
;
import
org.springframework.boot.web.client.RootUriTemplateHandler
;
import
org.springframework.core.NamedThreadLocal
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.http.client.ClientHttpRequestExecution
;
...
...
@@ -100,21 +101,10 @@ class MetricsClientHttpRequestInterceptor implements ClientHttpRequestIntercepto
}
UriTemplateHandler
createUriTemplateHandler
(
UriTemplateHandler
delegate
)
{
return
new
UriTemplateHandler
()
{
@Override
public
URI
expand
(
String
url
,
Map
<
String
,
?>
arguments
)
{
urlTemplate
.
get
().
push
(
url
);
return
delegate
.
expand
(
url
,
arguments
);
}
@Override
public
URI
expand
(
String
url
,
Object
...
arguments
)
{
urlTemplate
.
get
().
push
(
url
);
return
delegate
.
expand
(
url
,
arguments
);
}
};
if
(
delegate
instanceof
RootUriTemplateHandler
)
{
return
((
RootUriTemplateHandler
)
delegate
).
withHandlerWrapper
(
CapturingUriTemplateHandler:
:
new
);
}
return
new
CapturingUriTemplateHandler
(
delegate
);
}
private
Timer
.
Builder
getTimeBuilder
(
HttpRequest
request
,
ClientHttpResponse
response
)
{
...
...
@@ -123,6 +113,28 @@ class MetricsClientHttpRequestInterceptor implements ClientHttpRequestIntercepto
.
description
(
"Timer of RestTemplate operation"
);
}
private
static
final
class
CapturingUriTemplateHandler
implements
UriTemplateHandler
{
private
final
UriTemplateHandler
delegate
;
private
CapturingUriTemplateHandler
(
UriTemplateHandler
delegate
)
{
this
.
delegate
=
delegate
;
}
@Override
public
URI
expand
(
String
url
,
Map
<
String
,
?>
arguments
)
{
urlTemplate
.
get
().
push
(
url
);
return
this
.
delegate
.
expand
(
url
,
arguments
);
}
@Override
public
URI
expand
(
String
url
,
Object
...
arguments
)
{
urlTemplate
.
get
().
push
(
url
);
return
this
.
delegate
.
expand
(
url
,
arguments
);
}
}
private
static
final
class
UrlTemplateThreadLocal
extends
NamedThreadLocal
<
Deque
<
String
>>
{
private
UrlTemplateThreadLocal
()
{
...
...
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java
View file @
ce3dfc57
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
21
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -18,6 +18,7 @@ package org.springframework.boot.web.client;
import
java.net.URI
;
import
java.util.Map
;
import
java.util.function.Function
;
import
org.springframework.util.Assert
;
import
org.springframework.util.StringUtils
;
...
...
@@ -84,6 +85,17 @@ public class RootUriTemplateHandler implements UriTemplateHandler {
return
this
.
rootUri
;
}
/**
* Derives a new {@code RootUriTemplateHandler} from this one, wrapping its delegate
* {link UriTemplateHandler} by applying the given {@code wrapper}.
* @param wrapper the wrapper to apply to the delegate URI template handler
* @return the new handler
* @since 2.3.10
*/
public
RootUriTemplateHandler
withHandlerWrapper
(
Function
<
UriTemplateHandler
,
UriTemplateHandler
>
wrapper
)
{
return
new
RootUriTemplateHandler
(
this
.
rootUri
,
wrapper
.
apply
(
this
.
handler
));
}
/**
* Add a {@link RootUriTemplateHandler} instance to the given {@link RestTemplate}.
* @param restTemplate the {@link RestTemplate} to add the handler to
...
...
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