Skip to content

Commit b68fc82

Browse files
author
Anuraag Agrawal
authored
Make classes that can be shutdown Closeable (#2285)
* Make classes that can be shutdown Closeable * Glad not my password * Drift
1 parent efd559f commit b68fc82

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

exporters/logging/src/test/java/io/opentelemetry/exporter/logging/LoggingSpanExporterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void setUp() {
8282

8383
@AfterEach
8484
void tearDown() {
85-
exporter.shutdown();
85+
exporter.close();
8686
}
8787

8888
@Test

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerManagement.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
import io.opentelemetry.sdk.trace.config.TraceConfig;
1111
import io.opentelemetry.sdk.trace.config.TraceConfigBuilder;
1212
import io.opentelemetry.sdk.trace.export.SpanExporter;
13+
import java.io.Closeable;
14+
import java.util.concurrent.TimeUnit;
1315
import java.util.function.Supplier;
1416

1517
/**
1618
* "Management" interface for the Tracing SDK. This interface exposes methods for configuring the
1719
* Tracing SDK, as well as several lifecycle methods.
1820
*/
19-
public interface SdkTracerManagement {
21+
public interface SdkTracerManagement extends Closeable {
2022

2123
/**
2224
* Returns the active {@code TraceConfig}.
@@ -80,4 +82,22 @@ public interface SdkTracerManagement {
8082
* @see SpanProcessor#forceFlush()
8183
*/
8284
CompletableResultCode forceFlush();
85+
86+
/**
87+
* Attempts to stop all the activity for this {@link Tracer}. Calls {@link
88+
* SpanProcessor#shutdown()} for all registered {@link SpanProcessor}s.
89+
*
90+
* <p>This operation may block until all the Spans are processed. Must be called before turning
91+
* off the main application to ensure all data are processed and exported.
92+
*
93+
* <p>After this is called, newly created {@code Span}s will be no-ops.
94+
*
95+
* <p>After this is called, further attempts at re-using or reconfiguring this instance will
96+
* result in undefined behavior. It should be considered a terminal operation for the SDK
97+
* implementation.
98+
*/
99+
@Override
100+
default void close() {
101+
shutdown().join(10, TimeUnit.SECONDS);
102+
}
83103
}

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SpanProcessor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
import io.opentelemetry.api.trace.Span;
99
import io.opentelemetry.context.Context;
1010
import io.opentelemetry.sdk.common.CompletableResultCode;
11+
import java.io.Closeable;
1112
import java.util.ArrayList;
1213
import java.util.Arrays;
1314
import java.util.List;
15+
import java.util.concurrent.TimeUnit;
1416

1517
/**
1618
* SpanProcessor is the interface {@code TracerSdk} uses to allow synchronous hooks for when a
1719
* {@code Span} is started or when a {@code Span} is ended.
1820
*/
19-
public interface SpanProcessor {
21+
public interface SpanProcessor extends Closeable {
2022

2123
/**
2224
* Returns a {@link SpanProcessor} which simply delegates all processing to the {@code processors}
@@ -99,4 +101,13 @@ default CompletableResultCode shutdown() {
99101
default CompletableResultCode forceFlush() {
100102
return CompletableResultCode.ofSuccess();
101103
}
104+
105+
/**
106+
* Closes this {@link SpanProcessor} after processing any remaining spans, releasing any
107+
* resources.
108+
*/
109+
@Override
110+
default void close() {
111+
shutdown().join(10, TimeUnit.SECONDS);
112+
}
102113
}

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/SpanExporter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import io.opentelemetry.sdk.common.CompletableResultCode;
99
import io.opentelemetry.sdk.trace.SdkTracerManagement;
1010
import io.opentelemetry.sdk.trace.data.SpanData;
11+
import java.io.Closeable;
1112
import java.util.ArrayList;
1213
import java.util.Arrays;
1314
import java.util.Collection;
1415
import java.util.List;
16+
import java.util.concurrent.TimeUnit;
1517

1618
/**
1719
* An interface that allows different tracing services to export recorded data for sampled spans in
@@ -20,7 +22,7 @@
2022
* <p>To export data this MUST be register to the {@code TracerSdk} using a {@link
2123
* SimpleSpanProcessor} or a {@code BatchSampledSpansProcessor}.
2224
*/
23-
public interface SpanExporter {
25+
public interface SpanExporter extends Closeable {
2426

2527
/**
2628
* Returns a {@link SpanExporter} which simply delegates all exports to the {@code exporters} in
@@ -81,4 +83,10 @@ static SpanExporter composite(Iterable<SpanExporter> exporters) {
8183
* @return a {@link CompletableResultCode} which is completed when shutdown completes.
8284
*/
8385
CompletableResultCode shutdown();
86+
87+
/** Closes this {@link SpanExporter}, releasing any resources. */
88+
@Override
89+
default void close() {
90+
shutdown().join(10, TimeUnit.SECONDS);
91+
}
8492
}

sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ void shutdown() {
138138
Mockito.verify(spanProcessor, Mockito.times(1)).shutdown();
139139
}
140140

141+
@Test
142+
void close() {
143+
tracerFactory.close();
144+
Mockito.verify(spanProcessor, Mockito.times(1)).shutdown();
145+
}
146+
141147
@Test
142148
void forceFlush() {
143149
tracerFactory.forceFlush();

sdk/trace/src/test/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessorTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class SimpleSpanProcessorTest {
6666
@BeforeEach
6767
void setUp() {
6868
simpleSampledSpansProcessor = SimpleSpanProcessor.create(spanExporter);
69+
when(spanExporter.shutdown()).thenReturn(CompletableResultCode.ofSuccess());
6970
}
7071

7172
@Test
@@ -212,6 +213,12 @@ void shutdown() {
212213
verify(spanExporter).shutdown();
213214
}
214215

216+
@Test
217+
void close() {
218+
simpleSampledSpansProcessor.close();
219+
verify(spanExporter).shutdown();
220+
}
221+
215222
@Test
216223
void buildFromProperties_defaultSampledFlag() {
217224
Properties properties = new Properties();

0 commit comments

Comments
 (0)