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
2fd1cacb
Commit
2fd1cacb
authored
Sep 23, 2015
by
Thomas Badie
Committed by
Phillip Webb
Sep 24, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix synchronization issue in OpenTSDBMetricWriter
Closes gh-4010
parent
c6298131
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
17 deletions
+27
-17
OpenTsdbMetricWriter.java
...k/boot/actuate/metrics/opentsdb/OpenTsdbMetricWriter.java
+26
-17
OpenTsdbMetricWriterTests.java
...t/actuate/metrics/opentsdb/OpenTsdbMetricWriterTests.java
+1
-0
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbMetricWriter.java
View file @
2fd1cacb
...
...
@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.metrics.opentsdb;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -43,6 +44,7 @@ import org.springframework.web.client.RestTemplate;
* task to flush periodically.
*
* @author Dave Syer
* @author Thomas Badie
* @since 1.3.0
*/
public
class
OpenTsdbMetricWriter
implements
MetricWriter
{
...
...
@@ -67,7 +69,7 @@ public class OpenTsdbMetricWriter implements MetricWriter {
*/
private
MediaType
mediaType
=
MediaType
.
APPLICATION_JSON
;
private
List
<
OpenTsdbData
>
buffer
=
new
ArrayList
<
OpenTsdbData
>(
this
.
bufferSize
);
private
final
List
<
OpenTsdbData
>
buffer
=
new
ArrayList
<
OpenTsdbData
>(
this
.
bufferSize
);
private
OpenTsdbNamingStrategy
namingStrategy
=
new
DefaultOpenTsdbNamingStrategy
();
...
...
@@ -105,35 +107,42 @@ public class OpenTsdbMetricWriter implements MetricWriter {
OpenTsdbData
data
=
new
OpenTsdbData
(
this
.
namingStrategy
.
getName
(
value
.
getName
()),
value
.
getValue
(),
value
.
getTimestamp
().
getTime
());
this
.
buffer
.
add
(
data
);
if
(
this
.
buffer
.
size
()
>=
this
.
bufferSize
)
{
flush
();
synchronized
(
this
.
buffer
)
{
this
.
buffer
.
add
(
data
);
if
(
this
.
buffer
.
size
()
>=
this
.
bufferSize
)
{
flush
();
}
}
}
/**
* Flush the buffer without waiting for it to fill any further.
*/
@SuppressWarnings
(
"rawtypes"
)
public
void
flush
()
{
if
(
this
.
buffer
.
isEmpty
())
{
List
<
OpenTsdbData
>
snapshot
=
getBufferSnapshot
();
if
(
snapshot
.
isEmpty
())
{
return
;
}
List
<
OpenTsdbData
>
temp
=
new
ArrayList
<
OpenTsdbData
>();
synchronized
(
this
.
buffer
)
{
temp
.
addAll
(
this
.
buffer
);
this
.
buffer
.
clear
();
}
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
this
.
mediaType
));
headers
.
setContentType
(
this
.
mediaType
);
HttpEntity
<
List
<
OpenTsdbData
>>
request
=
new
HttpEntity
<
List
<
OpenTsdbData
>>(
temp
,
headers
);
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
response
=
this
.
restTemplate
.
postForEntity
(
this
.
url
,
request
,
Map
.
class
);
ResponseEntity
<
Map
>
response
=
this
.
restTemplate
.
postForEntity
(
this
.
url
,
new
HttpEntity
<
List
<
OpenTsdbData
>>(
snapshot
,
headers
),
Map
.
class
);
if
(!
response
.
getStatusCode
().
is2xxSuccessful
())
{
logger
.
warn
(
"Cannot write metrics (discarded "
+
temp
.
size
()
+
" values): "
+
response
.
getBody
());
logger
.
warn
(
"Cannot write metrics (discarded "
+
snapshot
.
size
()
+
" values): "
+
response
.
getBody
());
}
}
private
List
<
OpenTsdbData
>
getBufferSnapshot
()
{
synchronized
(
this
.
buffer
)
{
if
(
this
.
buffer
.
isEmpty
())
{
return
Collections
.
emptyList
();
}
List
<
OpenTsdbData
>
snapshot
=
new
ArrayList
<
OpenTsdbData
>(
this
.
buffer
);
this
.
buffer
.
clear
();
return
snapshot
;
}
}
...
...
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbMetricWriterTests.java
View file @
2fd1cacb
...
...
@@ -40,6 +40,7 @@ import static org.mockito.Mockito.verify;
public
class
OpenTsdbMetricWriterTests
{
private
OpenTsdbMetricWriter
writer
;
private
RestOperations
restTemplate
=
Mockito
.
mock
(
RestOperations
.
class
);
@Before
...
...
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