Skip to content

Commit 27da7db

Browse files
pgoronmeiliang86
authored andcommitted
Make NoopScope metric scope really a no-op operation (#312)
When workflow client code don't provide a metric scope, a default "NoopScope" is instantiated by cadence client framework. With the current implementation of NoopScope, a single threaded executor is indirectly created by RootScopeBuilder for reporting empty metrics at regular interval. This executor is never shutdown. The side effect is that a client process that just launches a workflow and returns from main method will stay alive due to the non daemon thread of this executor. NoopScope rewritten to not launch any reporting threads.
1 parent b44435d commit 27da7db

File tree

1 file changed

+75
-46
lines changed

1 file changed

+75
-46
lines changed

src/main/java/com/uber/cadence/internal/metrics/NoopScope.java

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,92 @@
2020
import com.uber.m3.tally.Buckets;
2121
import com.uber.m3.tally.Capabilities;
2222
import com.uber.m3.tally.CapableOf;
23-
import com.uber.m3.tally.RootScopeBuilder;
23+
import com.uber.m3.tally.Counter;
24+
import com.uber.m3.tally.Gauge;
25+
import com.uber.m3.tally.Histogram;
2426
import com.uber.m3.tally.Scope;
25-
import com.uber.m3.tally.StatsReporter;
27+
import com.uber.m3.tally.Stopwatch;
28+
import com.uber.m3.tally.Timer;
2629
import com.uber.m3.util.Duration;
2730
import java.util.Map;
2831

29-
public final class NoopScope {
30-
private static class NoopReporter implements StatsReporter {
31-
32-
@Override
33-
public void reportCounter(String name, Map<String, String> tags, long value) {}
34-
35-
@Override
36-
public void reportGauge(String name, Map<String, String> tags, double value) {}
37-
38-
@Override
39-
public void reportTimer(String name, Map<String, String> tags, Duration interval) {}
40-
41-
@Override
42-
public void reportHistogramValueSamples(
43-
String name,
44-
Map<String, String> tags,
45-
Buckets buckets,
46-
double bucketLowerBound,
47-
double bucketUpperBound,
48-
long samples) {}
49-
50-
@Override
51-
public void reportHistogramDurationSamples(
52-
String name,
53-
Map<String, String> tags,
54-
Buckets buckets,
55-
Duration bucketLowerBound,
56-
Duration bucketUpperBound,
57-
long samples) {}
58-
59-
@Override
60-
public Capabilities capabilities() {
61-
return CapableOf.REPORTING_TAGGING;
62-
}
32+
public final class NoopScope implements Scope {
33+
private static Scope noopScope;
34+
private static Counter noopCounter;
35+
private static Gauge noopGauge;
36+
private static Timer noopTimer;
37+
private static Histogram noopHistogram;
6338

64-
@Override
65-
public void flush() {}
39+
@Override
40+
public Counter counter(String name) {
41+
return noopCounter;
42+
}
6643

67-
@Override
68-
public void close() {}
44+
@Override
45+
public Gauge gauge(String name) {
46+
return noopGauge;
6947
}
7048

71-
private NoopScope() {}
49+
@Override
50+
public Timer timer(String name) {
51+
return noopTimer;
52+
}
53+
54+
@Override
55+
public Histogram histogram(String name, Buckets buckets) {
56+
return noopHistogram;
57+
}
58+
59+
@Override
60+
public Scope tagged(Map<String, String> tags) {
61+
return this;
62+
}
63+
64+
@Override
65+
public Scope subScope(String name) {
66+
return this;
67+
}
68+
69+
@Override
70+
public Capabilities capabilities() {
71+
return CapableOf.NONE;
72+
}
7273

73-
private static Scope instance;
74+
@Override
75+
public void close() {}
76+
77+
private NoopScope() {}
7478

7579
public static synchronized Scope getInstance() {
76-
if (instance == null) {
77-
instance =
78-
new RootScopeBuilder().reporter(new NoopReporter()).reportEvery(Duration.ofMinutes(1));
80+
if (noopScope == null) {
81+
noopCounter = delta -> {};
82+
noopGauge = value -> {};
83+
noopTimer =
84+
new Timer() {
85+
@Override
86+
public void record(Duration interval) {}
87+
88+
@Override
89+
public Stopwatch start() {
90+
return new Stopwatch(0, stopwatchStart -> {});
91+
}
92+
};
93+
noopHistogram =
94+
new Histogram() {
95+
@Override
96+
public void recordValue(double value) {}
97+
98+
@Override
99+
public void recordDuration(Duration value) {}
100+
101+
@Override
102+
public Stopwatch start() {
103+
return new Stopwatch(0, stopwatchStart -> {});
104+
}
105+
};
106+
107+
noopScope = new NoopScope();
79108
}
80-
return instance;
109+
return noopScope;
81110
}
82111
}

0 commit comments

Comments
 (0)