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
9587003d
Commit
9587003d
authored
Sep 01, 2016
by
Jon Schneider
Committed by
Spencer Gibb
Sep 01, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sanitize Servo metric tag key/value pairs before publishing to Atlas (#1315)
parent
8f7ea41f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
4 deletions
+40
-4
AtlasMetricObserver.java
...work/cloud/netflix/metrics/atlas/AtlasMetricObserver.java
+22
-4
AtlasMetricObserverTests.java
...cloud/netflix/metrics/atlas/AtlasMetricObserverTests.java
+18
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/metrics/atlas/AtlasMetricObserver.java
View file @
9587003d
...
...
@@ -38,6 +38,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
import
com.fasterxml.jackson.dataformat.smile.SmileFactory
;
import
com.netflix.servo.Metric
;
import
com.netflix.servo.annotations.DataSourceType
;
import
com.netflix.servo.monitor.MonitorConfig
;
import
com.netflix.servo.publish.MetricObserver
;
import
com.netflix.servo.tag.BasicTag
;
import
com.netflix.servo.tag.Tag
;
...
...
@@ -96,7 +97,7 @@ public class AtlasMetricObserver implements MetricObserver {
return
true
;
}
protected
static
String
normalizeAtlasUri
(
String
uri
)
{
static
String
normalizeAtlasUri
(
String
uri
)
{
if
(
uri
!=
null
)
{
Matcher
matcher
=
Pattern
.
compile
(
"(.+?)(/api/v1/publish)?/?"
).
matcher
(
uri
);
if
(
matcher
.
matches
())
...
...
@@ -119,7 +120,7 @@ public class AtlasMetricObserver implements MetricObserver {
return
;
}
List
<
Metric
>
metrics
=
addTypeTagsAsNecessary
(
rawMetrics
);
List
<
Metric
>
metrics
=
sanitizeTags
(
addTypeTagsAsNecessary
(
rawMetrics
)
);
for
(
int
i
=
0
;
i
<
metrics
.
size
();
i
+=
config
.
getBatchSize
())
{
List
<
Metric
>
batch
=
metrics
.
subList
(
i
,
...
...
@@ -217,8 +218,25 @@ public class AtlasMetricObserver implements MetricObserver {
return
totalMetricsInBatch
;
}
protected
static
List
<
Metric
>
addTypeTagsAsNecessary
(
List
<
Metric
>
metrics
)
{
List
<
Metric
>
typedMetrics
=
new
ArrayList
<>();
static
List
<
Metric
>
sanitizeTags
(
List
<
Metric
>
metrics
)
{
List
<
Metric
>
sanitized
=
new
ArrayList
<>(
metrics
.
size
());
for
(
Metric
m
:
metrics
)
{
MonitorConfig
.
Builder
config
=
MonitorConfig
.
builder
(
toValidCharset
(
m
.
getConfig
().
getName
()));
for
(
Tag
tag
:
m
.
getConfig
().
getTags
())
{
config
.
withTag
(
toValidCharset
(
tag
.
getKey
()),
toValidCharset
(
tag
.
getValue
()));
}
config
.
withPublishingPolicy
(
m
.
getConfig
().
getPublishingPolicy
());
sanitized
.
add
(
new
Metric
(
config
.
build
(),
m
.
getTimestamp
(),
m
.
getValue
()));
}
return
sanitized
;
}
private
static
String
toValidCharset
(
String
name
)
{
return
name
.
replaceAll
(
"[^\\.\\-\\w]"
,
"_"
);
}
static
List
<
Metric
>
addTypeTagsAsNecessary
(
List
<
Metric
>
metrics
)
{
List
<
Metric
>
typedMetrics
=
new
ArrayList
<>(
metrics
.
size
());
for
(
Metric
m
:
metrics
)
{
String
value
=
m
.
getConfig
().
getTags
().
getValue
(
DataSourceType
.
KEY
);
Metric
transformed
;
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/metrics/atlas/AtlasMetricObserverTests.java
View file @
9587003d
...
...
@@ -17,6 +17,7 @@ import java.util.ArrayList;
import
java.util.Collections
;
import
java.util.List
;
import
com.netflix.servo.tag.Tag
;
import
org.junit.Test
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -192,6 +193,23 @@ public class AtlasMetricObserverTests {
is
(
equalTo
(
AtlasMetricObserver
.
PublishMetricsBatchStatus
.
PartialSuccess
)));
}
@Test
public
void
sanitizeMetrics
()
{
String
mixtureOfValidAndInvalidChars
=
"a_1.2-Z/ A"
;
Metric
m
=
new
Metric
(
new
MonitorConfig
.
Builder
(
mixtureOfValidAndInvalidChars
)
.
withTag
(
mixtureOfValidAndInvalidChars
,
mixtureOfValidAndInvalidChars
).
build
(),
0
,
1
);
Metric
sanitizedMetric
=
AtlasMetricObserver
.
sanitizeTags
(
Collections
.
singletonList
(
m
)).
get
(
0
);
String
valid
=
"a_1.2-Z__A"
;
assertThat
(
sanitizedMetric
.
getConfig
().
getName
(),
is
(
equalTo
(
valid
)));
Tag
tag
=
sanitizedMetric
.
getConfig
().
getTags
().
iterator
().
next
();
assertThat
(
tag
.
getKey
(),
is
(
equalTo
(
valid
)));
assertThat
(
tag
.
getValue
(),
is
(
equalTo
(
valid
)));
}
private
List
<
Metric
>
generateMetrics
(
int
numberOfMetrics
)
{
List
<
Metric
>
metrics
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
numberOfMetrics
;
i
++)
...
...
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