class ContextUtils {

  public static SpanContext entityToSpanContext(final Entity entity) {
    TraceId traceId = TraceId
        .fromLowerBase16(xrayTraceIdToOT(entity.getParentSegment().getTraceId().toString()), 0);
    SpanId spanId = SpanId.fromLowerBase16(entity.getId(), 0);
    TraceFlags flags = TraceFlags.builder()
        .setIsSampled(entity.getParentSegment().isSampled())
        .build();
    Tracestate state = Tracestate.getDefault();

    //Segments with a parent ID are usually remote
    if (entity instanceof Segment && entity.getParentId() != null) {
      return SpanContext.createFromRemoteParent(traceId, spanId, flags, state);
    } else {
      return SpanContext.create(traceId, spanId, flags, state);
    }
  }

}