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
1e51c5db
Commit
1e51c5db
authored
Aug 05, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.1.x'
parents
26cc6285
1259057a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
1 deletion
+92
-1
MetricFilterAutoConfiguration.java
.../actuate/autoconfigure/MetricFilterAutoConfiguration.java
+13
-1
MetricFilterAutoConfigurationTests.java
...ate/autoconfigure/MetricFilterAutoConfigurationTests.java
+79
-0
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.java
View file @
1e51c5db
...
@@ -37,8 +37,10 @@ import org.springframework.context.annotation.Bean;
...
@@ -37,8 +37,10 @@ import org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.util.StopWatch
;
import
org.springframework.util.StopWatch
;
import
org.springframework.web.filter.OncePerRequestFilter
;
import
org.springframework.web.filter.OncePerRequestFilter
;
import
org.springframework.web.servlet.HandlerMapping
;
import
org.springframework.web.util.UrlPathHelper
;
import
org.springframework.web.util.UrlPathHelper
;
/**
/**
...
@@ -55,6 +57,7 @@ import org.springframework.web.util.UrlPathHelper;
...
@@ -55,6 +57,7 @@ import org.springframework.web.util.UrlPathHelper;
public
class
MetricFilterAutoConfiguration
{
public
class
MetricFilterAutoConfiguration
{
private
static
final
int
UNDEFINED_HTTP_STATUS
=
999
;
private
static
final
int
UNDEFINED_HTTP_STATUS
=
999
;
private
static
final
String
UNKNOWN_PATH_SUFFIX
=
"/unmapped"
;
@Autowired
@Autowired
private
CounterService
counterService
;
private
CounterService
counterService
;
...
@@ -86,10 +89,19 @@ public class MetricFilterAutoConfiguration {
...
@@ -86,10 +89,19 @@ public class MetricFilterAutoConfiguration {
}
}
finally
{
finally
{
stopWatch
.
stop
();
stopWatch
.
stop
();
int
status
=
getStatus
(
response
);
if
(
request
.
getAttribute
(
HandlerMapping
.
BEST_MATCHING_PATTERN_ATTRIBUTE
)
!=
null
)
{
suffix
=
request
.
getAttribute
(
HandlerMapping
.
BEST_MATCHING_PATTERN_ATTRIBUTE
)
.
toString
().
replaceAll
(
"[{}]"
,
"-"
);
}
else
if
(
HttpStatus
.
valueOf
(
status
).
is4xxClientError
())
{
suffix
=
UNKNOWN_PATH_SUFFIX
;
}
String
gaugeKey
=
getKey
(
"response"
+
suffix
);
String
gaugeKey
=
getKey
(
"response"
+
suffix
);
MetricFilterAutoConfiguration
.
this
.
gaugeService
.
submit
(
gaugeKey
,
MetricFilterAutoConfiguration
.
this
.
gaugeService
.
submit
(
gaugeKey
,
stopWatch
.
getTotalTimeMillis
());
stopWatch
.
getTotalTimeMillis
());
String
counterKey
=
getKey
(
"status."
+
getStatus
(
response
)
+
suffix
);
String
counterKey
=
getKey
(
"status."
+
status
+
suffix
);
MetricFilterAutoConfiguration
.
this
.
counterService
.
increment
(
counterKey
);
MetricFilterAutoConfiguration
.
this
.
counterService
.
increment
(
counterKey
);
}
}
}
}
...
...
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfigurationTests.java
View file @
1e51c5db
...
@@ -27,8 +27,16 @@ import org.springframework.boot.actuate.metrics.GaugeService;
...
@@ -27,8 +27,16 @@ import org.springframework.boot.actuate.metrics.GaugeService;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
org.springframework.web.bind.annotation.RestController
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertThat
;
...
@@ -36,7 +44,10 @@ import static org.mockito.BDDMockito.willAnswer;
...
@@ -36,7 +44,10 @@ import static org.mockito.BDDMockito.willAnswer;
import
static
org
.
mockito
.
Matchers
.
anyDouble
;
import
static
org
.
mockito
.
Matchers
.
anyDouble
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
/**
/**
* Tests for {@link MetricFilterAutoConfiguration}.
* Tests for {@link MetricFilterAutoConfiguration}.
...
@@ -68,6 +79,57 @@ public class MetricFilterAutoConfigurationTests {
...
@@ -68,6 +79,57 @@ public class MetricFilterAutoConfigurationTests {
context
.
close
();
context
.
close
();
}
}
@Test
public
void
recordsHttpInteractionsWithTemplateVariable
()
throws
Exception
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
Config
.
class
,
MetricFilterAutoConfiguration
.
class
);
Filter
filter
=
context
.
getBean
(
Filter
.
class
);
MockMvc
mvc
=
MockMvcBuilders
.
standaloneSetup
(
new
MetricFilterTestController
())
.
addFilter
(
filter
).
build
();
mvc
.
perform
(
get
(
"/templateVarTest/foo"
)).
andExpect
(
status
().
isOk
());
verify
(
context
.
getBean
(
CounterService
.
class
)).
increment
(
"status.200.templateVarTest.-someVariable-"
);
verify
(
context
.
getBean
(
GaugeService
.
class
)).
submit
(
eq
(
"response.templateVarTest.-someVariable-"
),
anyDouble
());
context
.
close
();
}
@Test
public
void
recordsKnown404HttpInteractionsAsSingleMetricWithPathAndTemplateVariable
()
throws
Exception
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
Config
.
class
,
MetricFilterAutoConfiguration
.
class
);
Filter
filter
=
context
.
getBean
(
Filter
.
class
);
MockMvc
mvc
=
MockMvcBuilders
.
standaloneSetup
(
new
MetricFilterTestController
())
.
addFilter
(
filter
).
build
();
mvc
.
perform
(
get
(
"/knownPath/foo"
)).
andExpect
(
status
().
isNotFound
());
verify
(
context
.
getBean
(
CounterService
.
class
)).
increment
(
"status.404.knownPath.-someVariable-"
);
verify
(
context
.
getBean
(
GaugeService
.
class
)).
submit
(
eq
(
"response.knownPath.-someVariable-"
),
anyDouble
());
context
.
close
();
}
@Test
public
void
records404HttpInteractionsAsSingleMetric
()
throws
Exception
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
Config
.
class
,
MetricFilterAutoConfiguration
.
class
);
Filter
filter
=
context
.
getBean
(
Filter
.
class
);
MockMvc
mvc
=
MockMvcBuilders
.
standaloneSetup
(
new
MetricFilterTestController
())
.
addFilter
(
filter
).
build
();
mvc
.
perform
(
get
(
"/unknownPath/1"
)).
andExpect
(
status
().
isNotFound
());
mvc
.
perform
(
get
(
"/unknownPath/2"
)).
andExpect
(
status
().
isNotFound
());
verify
(
context
.
getBean
(
CounterService
.
class
),
times
(
2
)).
increment
(
"status.404.unmapped"
);
verify
(
context
.
getBean
(
GaugeService
.
class
),
times
(
2
)).
submit
(
eq
(
"response.unmapped"
),
anyDouble
());
context
.
close
();
}
@Test
@Test
public
void
skipsFilterIfMissingServices
()
throws
Exception
{
public
void
skipsFilterIfMissingServices
()
throws
Exception
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
...
@@ -88,6 +150,23 @@ public class MetricFilterAutoConfigurationTests {
...
@@ -88,6 +150,23 @@ public class MetricFilterAutoConfigurationTests {
public
GaugeService
gaugeService
()
{
public
GaugeService
gaugeService
()
{
return
mock
(
GaugeService
.
class
);
return
mock
(
GaugeService
.
class
);
}
}
}
}
}
}
@RestController
class
MetricFilterTestController
{
@RequestMapping
(
"templateVarTest/{someVariable}"
)
public
String
testTemplateVariableResolution
(
@PathVariable
String
someVariable
)
{
return
someVariable
;
}
@RequestMapping
(
"knownPath/{someVariable}"
)
@ResponseStatus
(
HttpStatus
.
NOT_FOUND
)
@ResponseBody
public
String
testKnownPathWith404Response
(
@PathVariable
String
someVariable
)
{
return
someVariable
;
}
}
\ No newline at end of file
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