Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/src/main/java/com/uber/m3/tally/ScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package com.uber.m3.tally;

import com.uber.m3.tally.sanitizers.ScopeSanitizer;
import com.uber.m3.tally.sanitizers.ScopeSanitizerBuilder;
import com.uber.m3.util.Duration;
import com.uber.m3.util.ImmutableMap;

Expand Down Expand Up @@ -55,6 +57,7 @@ public class ScopeBuilder {
protected String separator = DEFAULT_SEPARATOR;
protected ImmutableMap<String, String> tags;
protected Buckets defaultBuckets = DEFAULT_SCOPE_BUCKETS;
protected ScopeSanitizer sanitizer = new ScopeSanitizerBuilder().build();
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Author

@longquanzheng longquanzheng Apr 22, 2021

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 null by 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)

Copy link
Copy Markdown
Author

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)


private ScheduledExecutorService scheduler;
private ScopeImpl.Registry registry;
Expand Down Expand Up @@ -128,6 +131,17 @@ public ScopeBuilder defaultBuckets(Buckets defaultBuckets) {
return this;
}

/**
* Update the sanitizer.
*
* @param sanitizer value to update to
* @return Builder with new param updated
*/
public ScopeBuilder sanitizer(ScopeSanitizer sanitizer) {
this.sanitizer = sanitizer;
return this;
}

// Private build method - clients should rely on `reportEvery` to create root scopes, and
// a root scope's `tagged` and `subScope` functions to create subscopes.
ScopeImpl build() {
Expand Down
110 changes: 70 additions & 40 deletions core/src/main/java/com/uber/m3/tally/ScopeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this check is redundant. If tags is null it will fail on tags.entrySet()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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()
)
);
}

Expand All @@ -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()
)
);
}

Expand All @@ -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()
)
);
}

Expand All @@ -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()
)
);
}
}
Expand All @@ -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()
);
}

Expand Down
47 changes: 47 additions & 0 deletions core/src/main/java/com/uber/m3/tally/sanitizers/SanitizeRange.java
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 {
Comment thread
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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add precondition asserting that high >= low

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}
}
67 changes: 67 additions & 0 deletions core/src/main/java/com/uber/m3/tally/sanitizers/SanitizerImpl.java
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) {
Comment thread
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) {
Comment thread
longquanzheng marked this conversation as resolved.
Outdated
return this.valueSanitizer.sanitize(value);
}
}
Loading