@@ -136,6 +136,8 @@ final String getTraceId() {
136
136
return traceId ;
137
137
}
138
138
139
+ abstract Optional <String > getRequestId ();
140
+
139
141
abstract Optional <String > getOriginatingSpanId ();
140
142
141
143
/** Returns a copy of this Trace which can be independently mutated. */
@@ -195,6 +197,18 @@ boolean isObservable() {
195
197
return true ;
196
198
}
197
199
200
+ @ Override
201
+ Optional <String > getRequestId () {
202
+ if (stack .isEmpty ()) {
203
+ return Optional .empty ();
204
+ }
205
+ OpenSpan top = stack .peekFirst ();
206
+ if (top .type () != SpanType .SERVER_INCOMING ) {
207
+ return Optional .empty ();
208
+ }
209
+ return Optional .of (top .getSpanId ());
210
+ }
211
+
198
212
@ Override
199
213
Optional <String > getOriginatingSpanId () {
200
214
if (stack .isEmpty ()) {
@@ -220,36 +234,52 @@ private static final class Unsampled extends Trace {
220
234
* This allows thread trace state to be cleared when all "started" spans have been "removed".
221
235
*/
222
236
private int numberOfSpans ;
223
- private Optional <String > originatingSpanId = Optional .empty ();
224
-
225
- private Unsampled (int numberOfSpans , String traceId ) {
237
+ private Optional <String > originatingSpanId ;
238
+ private Optional <String > requestId ;
239
+
240
+ private Unsampled (
241
+ int numberOfSpans ,
242
+ String traceId ,
243
+ Optional <String > originatingSpanId ,
244
+ Optional <String > requestId ) {
226
245
super (traceId );
227
246
this .numberOfSpans = numberOfSpans ;
247
+ this .originatingSpanId = originatingSpanId ;
248
+ this .requestId = requestId ;
228
249
validateNumberOfSpans ();
229
250
}
230
251
231
252
private Unsampled (String traceId ) {
232
- this (0 , traceId );
253
+ this (0 , traceId , Optional . empty (), Optional . empty () );
233
254
}
234
255
235
256
@ Override
236
- void fastStartSpan (String _operation , String parentSpanId , SpanType _type ) {
237
- startSpan (Optional .of (parentSpanId ));
257
+ void fastStartSpan (String _operation , String parentSpanId , SpanType type ) {
258
+ if (numberOfSpans == 0 ) {
259
+ originatingSpanId = Optional .of (parentSpanId );
260
+ if (type == SpanType .SERVER_INCOMING ) {
261
+ requestId = Optional .of (Tracers .randomId ());
262
+ }
263
+ }
264
+ numberOfSpans ++;
238
265
}
239
266
240
267
@ Override
241
- void fastStartSpan (String _operation , SpanType _type ) {
268
+ void fastStartSpan (String _operation , SpanType type ) {
269
+ if (numberOfSpans == 0 && type == SpanType .SERVER_INCOMING ) {
270
+ requestId = Optional .of (Tracers .randomId ());
271
+ }
242
272
numberOfSpans ++;
243
273
}
244
274
245
275
@ Override
246
276
protected void push (OpenSpan span ) {
247
- startSpan (span .getParentSpanId ());
248
- }
249
-
250
- private void startSpan (Optional <String > parentSpanId ) {
251
277
if (numberOfSpans == 0 ) {
252
- originatingSpanId = parentSpanId ;
278
+ originatingSpanId = span .getParentSpanId ();
279
+
280
+ if (span .type () == SpanType .SERVER_INCOMING ) {
281
+ requestId = Optional .of (span .getSpanId ());
282
+ }
253
283
}
254
284
numberOfSpans ++;
255
285
}
@@ -267,6 +297,7 @@ Optional<OpenSpan> pop() {
267
297
}
268
298
if (numberOfSpans == 0 ) {
269
299
originatingSpanId = Optional .empty ();
300
+ requestId = Optional .empty ();
270
301
}
271
302
return Optional .empty ();
272
303
}
@@ -282,14 +313,19 @@ boolean isObservable() {
282
313
return false ;
283
314
}
284
315
316
+ @ Override
317
+ Optional <String > getRequestId () {
318
+ return requestId ;
319
+ }
320
+
285
321
@ Override
286
322
Optional <String > getOriginatingSpanId () {
287
323
return originatingSpanId ;
288
324
}
289
325
290
326
@ Override
291
327
Trace deepCopy () {
292
- return new Unsampled (numberOfSpans , getTraceId ());
328
+ return new Unsampled (numberOfSpans , getTraceId (), getOriginatingSpanId (), getRequestId () );
293
329
}
294
330
295
331
/** Internal validation, this should never fail because {@link #pop()} only decrements positive values. */
0 commit comments