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

Commit a3d092a

Browse files
authored
zPages: Enable RunningSpanStore only when /tracez is in use. (#1878)
* zPages: Enable RunningSpanStore only when /tracez is in use. * Add a note in Javadoc. * Only initialize RunningSpanStore once. * Use explicit locking instead of atomic.
1 parent 946c398 commit a3d092a

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

contrib/zpages/src/main/java/io/opencensus/contrib/zpages/ZPageHandlers.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,8 @@ public final class ZPageHandlers {
7878
private static final ZPageHandler statszZPageHandler =
7979
StatszZPageHandler.create(Stats.getViewManager());
8080

81-
static {
82-
// Sets the maximum number of elements as Integer.MAX_VALUE.
83-
Tracing.getExportComponent().getRunningSpanStore().setMaxNumberOfSpans(Integer.MAX_VALUE);
84-
}
85-
8681
private static final Object monitor = new Object();
82+
private static volatile boolean isRunningSpanStoreInitialized = false;
8783

8884
@GuardedBy("monitor")
8985
@Nullable
@@ -99,10 +95,15 @@ public final class ZPageHandlers {
9995
* <p>If no sampled spans based on latency and error codes are available for a given name, make
10096
* sure that the span name is registered to the {@code SampledSpanStore}.
10197
*
98+
* <p>When this method is called for the first time, {@link
99+
* io.opencensus.trace.export.RunningSpanStore} will be enabled automatically. Subsequent calls
100+
* won't update {@link io.opencensus.trace.export.RunningSpanStore} again.
101+
*
102102
* @return a {@code ZPageHandler} for tracing debug.
103103
* @since 0.6
104104
*/
105105
public static ZPageHandler getTracezZPageHandler() {
106+
enableRunningSpanStore();
106107
return tracezZPageHandler;
107108
}
108109

@@ -201,5 +202,19 @@ private static void stop() {
201202
}
202203
}
203204

205+
// Sets the maximum number of elements as Integer.MAX_VALUE to enable RunningSpanStore.
206+
// This method will only execute once even if called multiple times.
207+
private static void enableRunningSpanStore() {
208+
if (!isRunningSpanStoreInitialized) {
209+
synchronized (monitor) {
210+
if (isRunningSpanStoreInitialized) {
211+
return; // Already initialized, small race
212+
}
213+
Tracing.getExportComponent().getRunningSpanStore().setMaxNumberOfSpans(Integer.MAX_VALUE);
214+
isRunningSpanStoreInitialized = true;
215+
}
216+
}
217+
}
218+
204219
private ZPageHandlers() {}
205220
}

0 commit comments

Comments
 (0)