Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 8f68802

Browse files
authored
Merge pull request #826 from HailongWen/noop-span-store
Add NoopRunningSpanStore and NoopSampledSpanStore.
2 parents e5664ab + 690a1fe commit 8f68802

File tree

10 files changed

+279
-28
lines changed

10 files changed

+279
-28
lines changed

api/src/main/java/io/opencensus/trace/TraceComponent.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
* <p>Unless otherwise noted all methods (on component) results are cacheable.
3030
*/
3131
public abstract class TraceComponent {
32-
private static final NoopTraceComponent noopTraceComponent = new NoopTraceComponent();
3332

3433
/**
3534
* Returns the {@link Tracer} with the provided implementations. If no implementation is provided
@@ -75,11 +74,13 @@ public abstract class TraceComponent {
7574
*
7675
* @return an instance that contains no-op implementations for all the instances.
7776
*/
78-
static TraceComponent getNoopTraceComponent() {
79-
return noopTraceComponent;
77+
static TraceComponent newNoopTraceComponent() {
78+
return new NoopTraceComponent();
8079
}
8180

8281
private static final class NoopTraceComponent extends TraceComponent {
82+
private final ExportComponent noopExportComponent = ExportComponent.newNoopExportComponent();
83+
8384
@Override
8485
public Tracer getTracer() {
8586
return Tracer.getNoopTracer();
@@ -97,7 +98,7 @@ public Clock getClock() {
9798

9899
@Override
99100
public ExportComponent getExportComponent() {
100-
return ExportComponent.getNoopExportComponent();
101+
return noopExportComponent;
101102
}
102103

103104
@Override

api/src/main/java/io/opencensus/trace/Tracing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static TraceComponent loadTraceComponent(ClassLoader classLoader) {
103103
+ "default implementation for TraceComponent.",
104104
e);
105105
}
106-
return TraceComponent.getNoopTraceComponent();
106+
return TraceComponent.newNoopTraceComponent();
107107
}
108108

109109
// No instance of this class.

api/src/main/java/io/opencensus/trace/export/ExportComponent.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package io.opencensus.trace.export;
1818

1919
import io.opencensus.trace.TraceOptions;
20-
import javax.annotation.Nullable;
2120

2221
/**
2322
* Class that holds the implementation instances for {@link SpanExporter}, {@link RunningSpanStore}
@@ -27,15 +26,13 @@
2726
*/
2827
public abstract class ExportComponent {
2928

30-
private static final NoopExportComponent NOOP_EXPORT_COMPONENT = new NoopExportComponent();
31-
3229
/**
3330
* Returns the no-op implementation of the {@code ExportComponent}.
3431
*
3532
* @return the no-op implementation of the {@code ExportComponent}.
3633
*/
37-
public static ExportComponent getNoopExportComponent() {
38-
return NOOP_EXPORT_COMPONENT;
34+
public static ExportComponent newNoopExportComponent() {
35+
return new NoopExportComponent();
3936
}
4037

4138
/**
@@ -51,36 +48,35 @@ public static ExportComponent getNoopExportComponent() {
5148
* Returns the {@link RunningSpanStore} that can be used to get useful debugging information about
5249
* all the current active spans.
5350
*
54-
* @return the {@code RunningSpanStore} or {@code null} if not supported.
51+
* @return the {@code RunningSpanStore}.
5552
*/
56-
@Nullable
5753
public abstract RunningSpanStore getRunningSpanStore();
5854

5955
/**
6056
* Returns the {@link SampledSpanStore} that can be used to get useful debugging information, such
6157
* as latency based sampled spans, error based sampled spans.
6258
*
63-
* @return the {@code SampledSpanStore} or {@code null} if not supported.
59+
* @return the {@code SampledSpanStore}.
6460
*/
65-
@Nullable
6661
public abstract SampledSpanStore getSampledSpanStore();
6762

6863
private static final class NoopExportComponent extends ExportComponent {
64+
private final SampledSpanStore noopSampledSpanStore =
65+
SampledSpanStore.newNoopSampledSpanStore();
66+
6967
@Override
7068
public SpanExporter getSpanExporter() {
7169
return SpanExporter.getNoopSpanExporter();
7270
}
7371

74-
@Nullable
7572
@Override
7673
public RunningSpanStore getRunningSpanStore() {
77-
return null;
74+
return RunningSpanStore.getNoopRunningSpanStore();
7875
}
7976

80-
@Nullable
8177
@Override
8278
public SampledSpanStore getSampledSpanStore() {
83-
return null;
79+
return noopSampledSpanStore;
8480
}
8581
}
8682
}

api/src/main/java/io/opencensus/trace/export/RunningSpanStore.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,19 @@
3737
@ThreadSafe
3838
public abstract class RunningSpanStore {
3939

40+
private static final RunningSpanStore NOOP_RUNNING_SPAN_STORE = new NoopRunningSpanStore();
41+
4042
protected RunningSpanStore() {}
4143

44+
/**
45+
* Returns the no-op implementation of the {@code RunningSpanStore}.
46+
*
47+
* @return the no-op implementation of the {@code RunningSpanStore}.
48+
*/
49+
static RunningSpanStore getNoopRunningSpanStore() {
50+
return NOOP_RUNNING_SPAN_STORE;
51+
}
52+
4253
/**
4354
* Returns the summary of all available data such, as number of running spans.
4455
*
@@ -151,4 +162,21 @@ public static Filter create(String spanName, int maxSpansToReturn) {
151162
*/
152163
public abstract int getMaxSpansToReturn();
153164
}
165+
166+
private static final class NoopRunningSpanStore extends RunningSpanStore {
167+
168+
private static final Summary EMPTY_SUMMARY =
169+
Summary.create(Collections.<String, PerSpanNameSummary>emptyMap());
170+
171+
@Override
172+
public Summary getSummary() {
173+
return EMPTY_SUMMARY;
174+
}
175+
176+
@Override
177+
public Collection<SpanData> getRunningSpans(Filter filter) {
178+
checkNotNull(filter, "filter");
179+
return Collections.<SpanData>emptyList();
180+
}
181+
}
154182
}

api/src/main/java/io/opencensus/trace/export/SampledSpanStore.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import com.google.auto.value.AutoValue;
2323
import com.google.common.annotations.VisibleForTesting;
24+
import com.google.common.collect.Maps;
25+
import com.google.common.collect.Sets;
2426
import io.opencensus.trace.Span;
2527
import io.opencensus.trace.Status;
2628
import io.opencensus.trace.Status.CanonicalCode;
@@ -31,6 +33,7 @@
3133
import java.util.Set;
3234
import java.util.concurrent.TimeUnit;
3335
import javax.annotation.Nullable;
36+
import javax.annotation.concurrent.GuardedBy;
3437
import javax.annotation.concurrent.Immutable;
3538
import javax.annotation.concurrent.ThreadSafe;
3639

@@ -48,6 +51,17 @@ public abstract class SampledSpanStore {
4851

4952
protected SampledSpanStore() {}
5053

54+
/**
55+
* Returns a {@code SampledSpanStore} that maintains a set of span names, but always returns an
56+
* empty list of {@link SpanData}.
57+
*
58+
* @return a {@code SampledSpanStore} that maintains a set of span names, but always returns an
59+
* empty list of {@code SpanData}.
60+
*/
61+
static SampledSpanStore newNoopSampledSpanStore() {
62+
return new NoopSampledSpanStore();
63+
}
64+
5165
/**
5266
* Returns the summary of all available data, such as number of sampled spans in the latency based
5367
* samples or error based samples.
@@ -370,4 +384,61 @@ public static ErrorFilter create(
370384
*/
371385
public abstract int getMaxSpansToReturn();
372386
}
387+
388+
@ThreadSafe
389+
private static final class NoopSampledSpanStore extends SampledSpanStore {
390+
private static final PerSpanNameSummary EMPTY_PER_SPAN_NAME_SUMMARY =
391+
PerSpanNameSummary.create(
392+
Collections.<SampledSpanStore.LatencyBucketBoundaries, Integer>emptyMap(),
393+
Collections.<CanonicalCode, Integer>emptyMap());
394+
395+
@GuardedBy("registeredSpanNames")
396+
private final Set<String> registeredSpanNames = Sets.newHashSet();
397+
398+
@Override
399+
public Summary getSummary() {
400+
Map<String, PerSpanNameSummary> result = Maps.newHashMap();
401+
synchronized (registeredSpanNames) {
402+
for (String registeredSpanName : registeredSpanNames) {
403+
result.put(registeredSpanName, EMPTY_PER_SPAN_NAME_SUMMARY);
404+
}
405+
}
406+
return Summary.create(result);
407+
}
408+
409+
@Override
410+
public Collection<SpanData> getLatencySampledSpans(LatencyFilter filter) {
411+
checkNotNull(filter, "latencyFilter");
412+
return Collections.<SpanData>emptyList();
413+
}
414+
415+
@Override
416+
public Collection<SpanData> getErrorSampledSpans(ErrorFilter filter) {
417+
checkNotNull(filter, "errorFilter");
418+
return Collections.<SpanData>emptyList();
419+
}
420+
421+
@Override
422+
public void registerSpanNamesForCollection(Collection<String> spanNames) {
423+
checkNotNull(spanNames, "spanNames");
424+
synchronized (registeredSpanNames) {
425+
registeredSpanNames.addAll(spanNames);
426+
}
427+
}
428+
429+
@Override
430+
public void unregisterSpanNamesForCollection(Collection<String> spanNames) {
431+
checkNotNull(spanNames);
432+
synchronized (registeredSpanNames) {
433+
registeredSpanNames.removeAll(spanNames);
434+
}
435+
}
436+
437+
@Override
438+
public Set<String> getRegisteredSpanNamesForCollection() {
439+
synchronized (registeredSpanNames) {
440+
return Collections.<String>unmodifiableSet(Sets.newHashSet(registeredSpanNames));
441+
}
442+
}
443+
}
373444
}

api/src/test/java/io/opencensus/trace/TraceComponentTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,29 @@
3131
public class TraceComponentTest {
3232
@Test
3333
public void defaultTracer() {
34-
assertThat(TraceComponent.getNoopTraceComponent().getTracer()).isSameAs(Tracer.getNoopTracer());
34+
assertThat(TraceComponent.newNoopTraceComponent().getTracer()).isSameAs(Tracer.getNoopTracer());
3535
}
3636

3737
@Test
3838
public void defaultBinaryPropagationHandler() {
39-
assertThat(TraceComponent.getNoopTraceComponent().getPropagationComponent())
39+
assertThat(TraceComponent.newNoopTraceComponent().getPropagationComponent())
4040
.isSameAs(PropagationComponent.getNoopPropagationComponent());
4141
}
4242

4343
@Test
4444
public void defaultClock() {
45-
assertThat(TraceComponent.getNoopTraceComponent().getClock()).isInstanceOf(ZeroTimeClock.class);
45+
assertThat(TraceComponent.newNoopTraceComponent().getClock()).isInstanceOf(ZeroTimeClock.class);
4646
}
4747

4848
@Test
4949
public void defaultTraceExporter() {
50-
assertThat(TraceComponent.getNoopTraceComponent().getExportComponent())
51-
.isSameAs(ExportComponent.getNoopExportComponent());
50+
assertThat(TraceComponent.newNoopTraceComponent().getExportComponent())
51+
.isInstanceOf(ExportComponent.newNoopExportComponent().getClass());
5252
}
5353

5454
@Test
5555
public void defaultTraceConfig() {
56-
assertThat(TraceComponent.getNoopTraceComponent().getTraceConfig())
56+
assertThat(TraceComponent.newNoopTraceComponent().getTraceConfig())
5757
.isSameAs(TraceConfig.getNoopTraceConfig());
5858
}
5959
}

api/src/test/java/io/opencensus/trace/TracingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public void defaultBinaryPropagationHandler() {
7272

7373
@Test
7474
public void defaultTraceExporter() {
75-
assertThat(Tracing.getExportComponent()).isSameAs(ExportComponent.getNoopExportComponent());
75+
assertThat(Tracing.getExportComponent())
76+
.isInstanceOf(ExportComponent.newNoopExportComponent().getClass());
7677
}
7778

7879
@Test

api/src/test/java/io/opencensus/trace/export/ExportComponentTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/** Unit tests for {@link ExportComponent}. */
2626
@RunWith(JUnit4.class)
2727
public class ExportComponentTest {
28-
private final ExportComponent exportComponent = ExportComponent.getNoopExportComponent();
28+
private final ExportComponent exportComponent = ExportComponent.newNoopExportComponent();
2929

3030
@Test
3131
public void implementationOfSpanExporter() {
@@ -34,11 +34,13 @@ public void implementationOfSpanExporter() {
3434

3535
@Test
3636
public void implementationOfActiveSpans() {
37-
assertThat(exportComponent.getRunningSpanStore()).isNull();
37+
assertThat(exportComponent.getRunningSpanStore())
38+
.isEqualTo(RunningSpanStore.getNoopRunningSpanStore());
3839
}
3940

4041
@Test
4142
public void implementationOfSampledSpanStore() {
42-
assertThat(exportComponent.getSampledSpanStore()).isNull();
43+
assertThat(exportComponent.getSampledSpanStore())
44+
.isInstanceOf(SampledSpanStore.newNoopSampledSpanStore().getClass());
4345
}
4446
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2017, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.trace.export;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.util.Collection;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
import org.junit.rules.ExpectedException;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.JUnit4;
27+
28+
/** Unit tests for {@link NoopRunningSpanStore}. */
29+
@RunWith(JUnit4.class)
30+
public final class NoopRunningSpanStoreTest {
31+
32+
private final RunningSpanStore runningSpanStore =
33+
ExportComponent.newNoopExportComponent().getRunningSpanStore();
34+
35+
@Rule public final ExpectedException thrown = ExpectedException.none();
36+
37+
@Test
38+
public void noopRunningSpanStore_GetSummary() {
39+
RunningSpanStore.Summary summary = runningSpanStore.getSummary();
40+
assertThat(summary.getPerSpanNameSummary()).isEmpty();
41+
}
42+
43+
@Test
44+
public void noopRunningSpanStore_GetRunningSpans_DisallowsNull() {
45+
thrown.expect(NullPointerException.class);
46+
runningSpanStore.getRunningSpans(null);
47+
}
48+
49+
@Test
50+
public void noopRunningSpanStore_GetRunningSpans() {
51+
Collection<SpanData> runningSpans =
52+
runningSpanStore.getRunningSpans(RunningSpanStore.Filter.create("TestSpan", 0));
53+
assertThat(runningSpans).isEmpty();
54+
}
55+
}

0 commit comments

Comments
 (0)