Repository: open-telemetry/opentelemetry-java
File: sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpanBuilder.java (Java)

Code before:
/** {@link SdkSpanBuilder} is SDK implementation of {@link SpanBuilder}. */
class SdkSpanBuilder implements SpanBuilder {

  private final String spanName;
  private final InstrumentationScopeInfo instrumentationScopeInfo;
  private final TracerSharedState tracerSharedState;
  private final SpanLimits spanLimits;
  private final Context rootContextWithRandomTraceIdBit;

  @Nullable private Context parent; // null means: Use current context.
  private SpanKind spanKind = SpanKind.INTERNAL;
  @Nullable private AttributesMap attributes;
  @Nullable private List<LinkData> links;
  private int totalNumberOfLinksAdded = 0;
  private long startEpochNanos = 0;

  SdkSpanBuilder(
      String spanName,
      InstrumentationScopeInfo instrumentationScopeInfo,
      TracerSharedState tracerSharedState,
      SpanLimits spanLimits) {
    this.spanName = spanName;
    this.instrumentationScopeInfo = instrumentationScopeInfo;
    this.tracerSharedState = tracerSharedState;
    this.spanLimits = spanLimits;
    this.rootContextWithRandomTraceIdBit =
        preparePrimordialContext(
            Context.root(),
            TraceFlags.getDefault().withRandomTraceIdBit(),
            TraceState.getDefault());
  }

  /*
   * A primordial context can be passed as the parent context for a root span
   * if a non-default TraceFlags or TraceState need to be passed to the sampler
   */
  private static Context preparePrimordialContext(
      Context parentContext, TraceFlags traceFlags, TraceState traceState) {
    SpanContext spanContext =
        SpanContext.create(TraceId.getInvalid(), SpanId.getInvalid(), traceFlags, traceState);
    Span span = Span.wrap(spanContext);
    return span.storeInContext(parentContext);
  }

  @Override
  public SpanBuilder setParent(Context context) {
    if (context == null) {
      return this;
    }
    this.parent = context;
    return this;

Review comment (suggestion):
If I'm reading this right, `rootContextWithRandomTraceIdBit` can be `private static final` singleton, since there's no parameters which are dependent on this constructor's params, and the instance itself is immutable and thread safe. 

I've sketched this (and my other suggestions) out in this commit to improve clarity: https://github.com/open-telemetry/opentelemetry-java/commit/c77bad6bb

Code after:
/** {@link SdkSpanBuilder} is SDK implementation of {@link SpanBuilder}. */
class SdkSpanBuilder implements SpanBuilder {

  private static final Context ROOT_CONTEXT_WITH_RANDOM_TRACE_ID_BIT =
      preparePrimordialContext(
          TraceFlags.builder().setRandomTraceId(true).build(), TraceState.getDefault());

  private final String spanName;
  private final InstrumentationScopeInfo instrumentationScopeInfo;
  private final TracerSharedState tracerSharedState;
  private final SpanLimits spanLimits;

  @Nullable private Context parent; // null means: Use current context.
  private SpanKind spanKind = SpanKind.INTERNAL;
  @Nullable private AttributesMap attributes;
  @Nullable private List<LinkData> links;
  private int totalNumberOfLinksAdded = 0;
  private long startEpochNanos = 0;

  SdkSpanBuilder(
      String spanName,
      InstrumentationScopeInfo instrumentationScopeInfo,
      TracerSharedState tracerSharedState,
      SpanLimits spanLimits) {
    this.spanName = spanName;
    this.instrumentationScopeInfo = instrumentationScopeInfo;
    this.tracerSharedState = tracerSharedState;
    this.spanLimits = spanLimits;
  }

  /*
   * A primordial context can be passed as the parent context for a root span
   * if a non-default TraceFlags or TraceState need to be passed to the sampler
   */
  private static Context preparePrimordialContext(TraceFlags traceFlags, TraceState traceState) {
    SpanContext spanContext =
        SpanContext.create(TraceId.getInvalid(), SpanId.getInvalid(), traceFlags, traceState);
    Span span = Span.wrap(spanContext);
    return span.storeInContext(Context.root());
  }

  @Override
  public SpanBuilder setParent(Context context) {
    if (context == null) {
      return this;
    }
    this.parent = context;
    return this;
  }

  @Override