17
17
package io .opencensus .implcore .trace .export ;
18
18
19
19
import com .google .common .annotations .VisibleForTesting ;
20
+ import io .opencensus .implcore .internal .DaemonThreadFactory ;
20
21
import io .opencensus .implcore .trace .SpanImpl ;
21
22
import io .opencensus .trace .export .ExportComponent ;
22
23
import io .opencensus .trace .export .SpanData ;
35
36
public final class SpanExporterImpl extends SpanExporter {
36
37
private static final Logger logger = Logger .getLogger (ExportComponent .class .getName ());
37
38
38
- private final WorkerThread workerThread ;
39
+ private final Worker worker ;
40
+ private final Thread workerThread ;
39
41
40
42
/**
41
43
* Constructs a {@code SpanExporterImpl} that exports the {@link SpanData} asynchronously.
@@ -49,9 +51,8 @@ public final class SpanExporterImpl extends SpanExporter {
49
51
*/
50
52
static SpanExporterImpl create (int bufferSize , long scheduleDelayMillis ) {
51
53
// TODO(bdrutu): Consider to add a shutdown hook to not avoid dropping data.
52
- WorkerThread workerThread = new WorkerThread (bufferSize , scheduleDelayMillis );
53
- workerThread .start ();
54
- return new SpanExporterImpl (workerThread );
54
+ Worker worker = new Worker (bufferSize , scheduleDelayMillis );
55
+ return new SpanExporterImpl (worker );
55
56
}
56
57
57
58
/**
@@ -60,29 +61,32 @@ static SpanExporterImpl create(int bufferSize, long scheduleDelayMillis) {
60
61
* @param span the {@code Span} to be added.
61
62
*/
62
63
public void addSpan (SpanImpl span ) {
63
- workerThread .addSpan (span );
64
+ worker .addSpan (span );
64
65
}
65
66
66
67
@ Override
67
68
public void registerHandler (String name , Handler handler ) {
68
- workerThread .registerHandler (name , handler );
69
+ worker .registerHandler (name , handler );
69
70
}
70
71
71
72
@ Override
72
73
public void unregisterHandler (String name ) {
73
- workerThread .unregisterHandler (name );
74
+ worker .unregisterHandler (name );
74
75
}
75
76
76
- private SpanExporterImpl (WorkerThread workerThread ) {
77
- this .workerThread = workerThread ;
77
+ private SpanExporterImpl (Worker worker ) {
78
+ this .workerThread =
79
+ new DaemonThreadFactory ("ExportComponent.ServiceExporterThread" ).newThread (worker );
80
+ this .workerThread .start ();
81
+ this .worker = worker ;
78
82
}
79
83
80
84
@ VisibleForTesting
81
85
Thread getServiceExporterThread () {
82
86
return workerThread ;
83
87
}
84
88
85
- // Worker thread that batches multiple span data and calls the registered services to export
89
+ // Worker in a thread that batches multiple span data and calls the registered services to export
86
90
// that data.
87
91
//
88
92
// The map of registered handlers is implemented using ConcurrentHashMap ensuring full
@@ -91,7 +95,7 @@ Thread getServiceExporterThread() {
91
95
//
92
96
// The list of batched data is protected by an explicit monitor object which ensures full
93
97
// concurrency.
94
- private static final class WorkerThread extends Thread {
98
+ private static final class Worker implements Runnable {
95
99
private final Object monitor = new Object ();
96
100
97
101
@ GuardedBy ("monitor" )
@@ -140,12 +144,10 @@ private void onBatchExport(List<SpanData> spanDataList) {
140
144
141
145
// TODO: Decide whether to use a different class instead of LinkedList.
142
146
@ SuppressWarnings ("JdkObsolete" )
143
- private WorkerThread (int bufferSize , long scheduleDelayMillis ) {
147
+ private Worker (int bufferSize , long scheduleDelayMillis ) {
144
148
spans = new LinkedList <SpanImpl >();
145
149
this .bufferSize = bufferSize ;
146
150
this .scheduleDelayMillis = scheduleDelayMillis ;
147
- setDaemon (true );
148
- setName ("ExportComponent.ServiceExporterThread" );
149
151
}
150
152
151
153
// Returns an unmodifiable list of all buffered spans data to ensure that any registered
0 commit comments