@@ -243,6 +243,15 @@ public static CoreTracerBuilder builder() {
243
243
244
244
private final PropagationTags .Factory propagationTagsFactory ;
245
245
246
+ // Cache used by buildSpan
247
+ private final ThreadLocal <CoreSpanBuilder > tlSpanBuilder =
248
+ new ThreadLocal <CoreSpanBuilder >() {
249
+ @ Override
250
+ protected CoreSpanBuilder initialValue () {
251
+ return new CoreSpanBuilder (CoreTracer .this );
252
+ }
253
+ };
254
+
246
255
@ Override
247
256
public ConfigSnapshot captureTraceConfig () {
248
257
return dynamicConfig .captureTraceConfig ();
@@ -968,7 +977,9 @@ long getTimeWithNanoTicks(long nanoTicks) {
968
977
@ Override
969
978
public CoreSpanBuilder buildSpan (
970
979
final String instrumentationName , final CharSequence operationName ) {
971
- return new CoreSpanBuilder (this , instrumentationName , operationName );
980
+ CoreSpanBuilder tlSpanBuilder = this .tlSpanBuilder .get ();
981
+ tlSpanBuilder .reset (instrumentationName , operationName );
982
+ return tlSpanBuilder ;
972
983
}
973
984
974
985
@ Override
@@ -1401,10 +1412,11 @@ private static <K, V> Map<V, K> invertMap(Map<K, V> map) {
1401
1412
1402
1413
/** Spans are built using this builder */
1403
1414
public static class CoreSpanBuilder implements AgentTracer .SpanBuilder {
1404
- private final String instrumentationName ;
1405
- private final CharSequence operationName ;
1406
1415
private final CoreTracer tracer ;
1407
1416
1417
+ private String instrumentationName ;
1418
+ private CharSequence operationName ;
1419
+
1408
1420
// Builder attributes
1409
1421
private TagMap .Ledger tagLedger ;
1410
1422
private long timestampMicro ;
@@ -1420,13 +1432,27 @@ public static class CoreSpanBuilder implements AgentTracer.SpanBuilder {
1420
1432
private List <AgentSpanLink > links ;
1421
1433
private long spanId ;
1422
1434
1423
- CoreSpanBuilder (
1424
- final CoreTracer tracer ,
1425
- final String instrumentationName ,
1426
- final CharSequence operationName ) {
1435
+ CoreSpanBuilder (CoreTracer tracer ) {
1436
+ this .tracer = tracer ;
1437
+ }
1438
+
1439
+ void reset (String instrumentationName , CharSequence operationName ) {
1427
1440
this .instrumentationName = instrumentationName ;
1428
1441
this .operationName = operationName ;
1429
- this .tracer = tracer ;
1442
+
1443
+ if (this .tagLedger != null ) this .tagLedger .reset ();
1444
+ this .timestampMicro = 0L ;
1445
+ this .parent = null ;
1446
+ this .serviceName = null ;
1447
+ this .resourceName = null ;
1448
+ this .errorFlag = false ;
1449
+ this .spanType = null ;
1450
+ this .ignoreScope = false ;
1451
+ this .builderCiVisibilityContextData = null ;
1452
+ this .builderRequestContextDataIast = null ;
1453
+ this .builderCiVisibilityContextData = null ;
1454
+ this .links = null ;
1455
+ this .spanId = 0L ;
1430
1456
}
1431
1457
1432
1458
@ Override
0 commit comments