-
Notifications
You must be signed in to change notification settings - Fork 21
Adding Sanitizer interface and implementation #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
0f77624
afa6c6d
a772d57
a188eee
fc8f1aa
edc0a4e
d8fdac9
45243d8
a27e6d3
5aa1dc5
1b941ff
6e4f04a
5328ba1
5cb9f0f
3e34116
dde5ff4
fbbf111
f1d8f25
bb561ec
315c559
d0c33f8
08ed2d5
8b09947
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
|
|
||
| package com.uber.m3.tally; | ||
|
|
||
| import com.uber.m3.tally.sanitizers.ScopeSanitizer; | ||
| import com.uber.m3.util.ImmutableMap; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
@@ -41,6 +42,7 @@ class ScopeImpl implements Scope { | |
| private String separator; | ||
| private ImmutableMap<String, String> tags; | ||
| private Buckets defaultBuckets; | ||
| private ScopeSanitizer sanitizer; | ||
|
|
||
| private ScheduledExecutorService scheduler; | ||
| private Registry registry; | ||
|
|
@@ -57,6 +59,7 @@ class ScopeImpl implements Scope { | |
| ScopeImpl(ScheduledExecutorService scheduler, Registry registry, ScopeBuilder builder) { | ||
| this.scheduler = scheduler; | ||
| this.registry = registry; | ||
| this.sanitizer = builder.sanitizer; | ||
|
|
||
| this.reporter = builder.reporter; | ||
| this.prefix = builder.prefix; | ||
|
|
@@ -67,33 +70,37 @@ class ScopeImpl implements Scope { | |
|
|
||
| @Override | ||
| public Counter counter(String name) { | ||
| return counters.computeIfAbsent(name, ignored -> | ||
| final String finalName = sanitizer.sanitizeName(name); | ||
| return counters.computeIfAbsent(finalName, ignored -> | ||
| // NOTE: This will called at most once | ||
| new CounterImpl(this, fullyQualifiedName(name)) | ||
| new CounterImpl(this, fullyQualifiedName(finalName)) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public Gauge gauge(String name) { | ||
| return gauges.computeIfAbsent(name, ignored -> | ||
| final String finalName = sanitizer.sanitizeName(name); | ||
| return gauges.computeIfAbsent(finalName, ignored -> | ||
| // NOTE: This will called at most once | ||
| new GaugeImpl(this, fullyQualifiedName(name))); | ||
| new GaugeImpl(this, fullyQualifiedName(finalName))); | ||
| } | ||
|
|
||
| @Override | ||
| public Timer timer(String name) { | ||
| final String finalName = sanitizer.sanitizeName(name); | ||
| // Timers report directly to the {@code StatsReporter}, and therefore not added to reporting queue | ||
| // i.e. they are not buffered | ||
| return timers.computeIfAbsent(name, ignored -> new TimerImpl(fullyQualifiedName(name), tags, reporter)); | ||
| return timers.computeIfAbsent(finalName, ignored -> new TimerImpl(fullyQualifiedName(finalName), tags, reporter)); | ||
| } | ||
|
|
||
| @Override | ||
| public Histogram histogram(String name, @Nullable Buckets buckets) { | ||
| return histograms.computeIfAbsent(name, ignored -> | ||
| final String finalName = sanitizer.sanitizeName(name); | ||
| return histograms.computeIfAbsent(finalName, ignored -> | ||
| // NOTE: This will called at most once | ||
| new HistogramImpl( | ||
| this, | ||
| fullyQualifiedName(name), | ||
| fullyQualifiedName(finalName), | ||
| tags, | ||
| Optional.ofNullable(buckets) | ||
| .orElse(defaultBuckets) | ||
|
|
@@ -103,12 +110,13 @@ public Histogram histogram(String name, @Nullable Buckets buckets) { | |
|
|
||
| @Override | ||
| public Scope tagged(Map<String, String> tags) { | ||
| return subScopeHelper(prefix, tags); | ||
| return subScopeHelper(prefix, sanitizeTags(tags)); | ||
| } | ||
|
|
||
| @Override | ||
| public Scope subScope(String name) { | ||
| return subScopeHelper(fullyQualifiedName(name), null); | ||
| final String finalName = sanitizer.sanitizeName(name); | ||
| return subScopeHelper(fullyQualifiedName(finalName), null); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -136,12 +144,32 @@ public void close() { | |
| } | ||
| } | ||
|
|
||
| private Map<String, String> sanitizeTags(Map<String, String> tags) { | ||
| boolean hasChange = false; | ||
| for (Map.Entry<String, String> kv : tags.entrySet()) { | ||
| if (!sanitizer.sanitizeKey(kv.getKey()).equals(kv.getKey()) || !sanitizer.sanitizeValue(kv.getValue()).equals(kv.getValue())) { | ||
| hasChange = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!hasChange) { | ||
| return tags; | ||
| } | ||
|
|
||
| ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>(); | ||
| if (tags != null) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this check is redundant. If
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather re-phrase this: @longquanzheng please move this check at the beginning to make sure there's no NPE
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah good point. Thanks |
||
| tags.forEach((key, value) -> builder.put(sanitizer.sanitizeKey(key), sanitizer.sanitizeValue(value))); | ||
| } | ||
| return builder.build(); | ||
| } | ||
|
|
||
| <T extends Reportable> void addToReportingQueue(T metric) { | ||
| reportingQueue.add(metric); | ||
| } | ||
|
|
||
| /** | ||
| * Reports using the specified reporter. | ||
| * | ||
| * @param reporter the reporter to report | ||
| */ | ||
| void report(StatsReporter reporter) { | ||
|
|
@@ -193,6 +221,7 @@ String fullyQualifiedName(String name) { | |
|
|
||
| /** | ||
| * Returns a {@link Snapshot} of this {@link Scope}. | ||
| * | ||
| * @return a {@link Snapshot} of this {@link Scope} | ||
| */ | ||
| public Snapshot snapshot() { | ||
|
|
@@ -205,12 +234,12 @@ public Snapshot snapshot() { | |
| String id = keyForPrefixedStringMap(name, tags); | ||
|
|
||
| snap.counters().put( | ||
| id, | ||
| new CounterSnapshotImpl( | ||
| name, | ||
| tags, | ||
| counter.getValue().snapshot() | ||
| ) | ||
| id, | ||
| new CounterSnapshotImpl( | ||
| name, | ||
| tags, | ||
| counter.getValue().snapshot() | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -220,12 +249,12 @@ public Snapshot snapshot() { | |
| String id = keyForPrefixedStringMap(name, tags); | ||
|
|
||
| snap.gauges().put( | ||
| id, | ||
| new GaugeSnapshotImpl( | ||
| name, | ||
| tags, | ||
| gauge.getValue().snapshot() | ||
| ) | ||
| id, | ||
| new GaugeSnapshotImpl( | ||
| name, | ||
| tags, | ||
| gauge.getValue().snapshot() | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -235,12 +264,12 @@ public Snapshot snapshot() { | |
| String id = keyForPrefixedStringMap(name, tags); | ||
|
|
||
| snap.timers().put( | ||
| id, | ||
| new TimerSnapshotImpl( | ||
| name, | ||
| tags, | ||
| timer.getValue().snapshot() | ||
| ) | ||
| id, | ||
| new TimerSnapshotImpl( | ||
| name, | ||
| tags, | ||
| timer.getValue().snapshot() | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -250,13 +279,13 @@ public Snapshot snapshot() { | |
| String id = keyForPrefixedStringMap(name, tags); | ||
|
|
||
| snap.histograms().put( | ||
| id, | ||
| new HistogramSnapshotImpl( | ||
| name, | ||
| tags, | ||
| histogram.getValue().snapshotValues(), | ||
| histogram.getValue().snapshotDurations() | ||
| ) | ||
| id, | ||
| new HistogramSnapshotImpl( | ||
| name, | ||
| tags, | ||
| histogram.getValue().snapshotValues(), | ||
| histogram.getValue().snapshotDurations() | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
@@ -283,12 +312,13 @@ private Scope subScopeHelper(String prefix, Map<String, String> tags) { | |
| return registry.subscopes.computeIfAbsent( | ||
| key, | ||
| (k) -> new ScopeBuilder(scheduler, registry) | ||
| .reporter(reporter) | ||
| .prefix(prefix) | ||
| .separator(separator) | ||
| .tags(mergedTags) | ||
| .defaultBuckets(defaultBuckets) | ||
| .build() | ||
| .reporter(reporter) | ||
| .prefix(prefix) | ||
| .separator(separator) | ||
| .tags(mergedTags) | ||
| .sanitizer(sanitizer) | ||
| .defaultBuckets(defaultBuckets) | ||
| .build() | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright (c) 2020 Uber Technologies, Inc. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
|
|
||
| package com.uber.m3.tally.sanitizers; | ||
|
|
||
| /** | ||
| * SanitizeRange is a range of characters (inclusive on both ends). | ||
| */ | ||
| public class SanitizeRange { | ||
|
longquanzheng marked this conversation as resolved.
Outdated
|
||
|
|
||
| private final char low; | ||
| private final char high; | ||
|
|
||
| private SanitizeRange(char low, char high) { | ||
| this.low = low; | ||
| this.high = high; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add precondition asserting that
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see an example of "Precondition"(maybe I didn't find in a correct way) so I throw a Runtime exception here. LMK if you want to assert in a different way.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, precondition reference was mostly notional (please do not import Guava for that) |
||
| } | ||
|
|
||
| public static SanitizeRange of(char low, char high) { | ||
| return new SanitizeRange(low, high); | ||
| } | ||
|
|
||
| char low() { | ||
| return low; | ||
| } | ||
|
|
||
| char high() { | ||
| return high; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright (c) 2020 Uber Technologies, Inc. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
|
|
||
| package com.uber.m3.tally.sanitizers; | ||
|
|
||
| /** | ||
| * SanitizerImpl sanitizes the provided input based on the function executed. | ||
| */ | ||
| class SanitizerImpl implements ScopeSanitizer { | ||
|
|
||
| private final StringSanitizer nameSanitizer; | ||
| private final StringSanitizer keySanitizer; | ||
| private final StringSanitizer valueSanitizer; | ||
|
|
||
| SanitizerImpl(StringSanitizer nameSanitizer, StringSanitizer keySanitizer, StringSanitizer valueSanitizer) { | ||
| this.nameSanitizer = nameSanitizer; | ||
| this.keySanitizer = keySanitizer; | ||
| this.valueSanitizer = valueSanitizer; | ||
| } | ||
|
|
||
| /** | ||
| * Name sanitizes the provided 'name' string. | ||
| * @param name the name string | ||
| * @return the sanitized name | ||
| */ | ||
| @Override | ||
| public String sanitizeName(String name) { | ||
| return this.nameSanitizer.sanitize(name); | ||
| } | ||
|
|
||
| /** | ||
| * Key sanitizes the provided 'key' string. | ||
| * @param key the key string | ||
| * @return the sanitized key | ||
| */ | ||
| @Override | ||
| public String sanitizeKey(String key) { | ||
|
longquanzheng marked this conversation as resolved.
Outdated
|
||
| return this.keySanitizer.sanitize(key); | ||
| } | ||
|
|
||
| /** | ||
| * Value sanitizes the provided 'value' string. | ||
| * @param value the value string | ||
| * @return the sanitized value | ||
| */ | ||
| @Override | ||
| public String sanitizeValue(String value) { | ||
|
longquanzheng marked this conversation as resolved.
Outdated
|
||
| return this.valueSanitizer.sanitize(value); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this opt-in -- by default sanitizer should be no-op
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean having it as
nullby default in the builder?This would be a little bit tricky. Then the ScopeImpl will have to do this null check everywhere, is that okay? (It would be easier if this is Kotlin and we can use
?operator, sigh)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NVM, looks like I will apply another comment from you and Andrey in #97 (comment)