diff --git a/impl_core/src/main/java/io/opencensus/implcore/trace/RecordEventsSpanImpl.java b/impl_core/src/main/java/io/opencensus/implcore/trace/RecordEventsSpanImpl.java index 4137b89a38..3d3ec7d882 100644 --- a/impl_core/src/main/java/io/opencensus/implcore/trace/RecordEventsSpanImpl.java +++ b/impl_core/src/main/java/io/opencensus/implcore/trace/RecordEventsSpanImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2017, OpenCensus Authors + * Copyright 2017-20, OpenCensus Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -52,12 +53,15 @@ // TODO(hailongwen): remove the usage of `NetworkEvent` in the future. /** Implementation for the {@link Span} class that records trace events. */ @ThreadSafe -public final class RecordEventsSpanImpl extends Span implements Element { - private static final Logger logger = Logger.getLogger(Tracer.class.getName()); +public class RecordEventsSpanImpl extends Span implements Element { + protected static final Logger logger = Logger.getLogger(Tracer.class.getName()); private static final EnumSet RECORD_EVENTS_SPAN_OPTIONS = EnumSet.of(Span.Options.RECORD_EVENTS); + private static final AtomicInteger CURRENT_OPEN_SPANS = new AtomicInteger(0); + private static final AtomicInteger MAX_SEEN_OPEN_SPANS = new AtomicInteger(0); + // The parent SpanId of this span. Null if this is a root span. @Nullable private final SpanId parentSpanId; // True if the parent is on a different process. @@ -67,7 +71,7 @@ public final class RecordEventsSpanImpl extends Span implements Element toSpanDataTimedEvent(TimestampConverter timestampConverter } } - private RecordEventsSpanImpl( + protected RecordEventsSpanImpl( SpanContext context, String name, @Nullable Kind kind, @@ -580,21 +614,11 @@ private RecordEventsSpanImpl( this.startEndHandler = startEndHandler; this.clock = clock; this.hasBeenEnded = false; + CURRENT_OPEN_SPANS.incrementAndGet(); this.sampleToLocalSpanStore = false; this.numberOfChildren = 0; this.timestampConverter = timestampConverter != null ? timestampConverter : TimestampConverter.now(clock); startNanoTime = clock.nowNanos(); } - - @SuppressWarnings("NoFinalizer") - @Override - protected void finalize() throws Throwable { - synchronized (this) { - if (!hasBeenEnded) { - logger.log(Level.SEVERE, "Span " + name + " is GC'ed without being ended."); - } - } - super.finalize(); - } } diff --git a/impl_core/src/main/java/io/opencensus/implcore/trace/RecordEventsSpanImplWithFinalizer.java b/impl_core/src/main/java/io/opencensus/implcore/trace/RecordEventsSpanImplWithFinalizer.java new file mode 100644 index 0000000000..738042fce8 --- /dev/null +++ b/impl_core/src/main/java/io/opencensus/implcore/trace/RecordEventsSpanImplWithFinalizer.java @@ -0,0 +1,61 @@ +/* + * Copyright 2020, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.implcore.trace; + +import io.opencensus.common.Clock; +import io.opencensus.implcore.internal.TimestampConverter; +import io.opencensus.trace.SpanContext; +import io.opencensus.trace.SpanId; +import io.opencensus.trace.config.TraceParams; +import java.util.logging.Level; +import javax.annotation.Nullable; + +public final class RecordEventsSpanImplWithFinalizer extends RecordEventsSpanImpl { + + protected RecordEventsSpanImplWithFinalizer( + SpanContext context, + String name, + @Nullable Kind kind, + @Nullable SpanId parentSpanId, + @Nullable Boolean hasRemoteParent, + TraceParams traceParams, + StartEndHandler startEndHandler, + @Nullable TimestampConverter timestampConverter, + Clock clock) { + super( + context, + name, + kind, + parentSpanId, + hasRemoteParent, + traceParams, + startEndHandler, + timestampConverter, + clock); + } + + @SuppressWarnings("NoFinalizer") + @Override + protected void finalize() throws Throwable { + synchronized (this) { + if (!hasBeenEnded) { + logger.log(Level.SEVERE, "Span " + name + " is GC'ed without being ended."); + } + } + super.finalize(); + } +}