class AwsXRayPropagator implements TextMapPropagator {

  @Override
  public <C> void inject(Context context, @Nullable C carrier, Setter<C> setter) {
    Objects.requireNonNull(context, "context");
    Objects.requireNonNull(setter, "setter");

    Span span = TracingContextUtils.getSpan(context);
    if (!span.getContext().isValid()) {
      return;
    }

    SpanContext spanContext = span.getContext();

    String otTraceId = spanContext.getTraceIdAsHexString();
    String xrayTraceId =
        TRACE_ID_VERSION
            + TRACE_ID_DELIMITER
            + otTraceId.substring(0, TRACE_ID_FIRST_PART_LENGTH)
            + TRACE_ID_DELIMITER
            + otTraceId.substring(TRACE_ID_FIRST_PART_LENGTH);
    String parentId = spanContext.getSpanIdAsHexString();
    char samplingFlag = spanContext.isSampled() ? IS_SAMPLED : NOT_SAMPLED;
    // TODO: Add OT trace state to the X-Ray trace header

    String traceHeader =
        TRACE_ID_KEY
            + KV_DELIMITER
            + xrayTraceId
            + TRACE_HEADER_DELIMITER
            + PARENT_ID_KEY
            + KV_DELIMITER
            + parentId
            + TRACE_HEADER_DELIMITER
            + SAMPLED_FLAG_KEY
            + KV_DELIMITER
            + samplingFlag;
    setter.set(carrier, TRACE_HEADER_KEY, traceHeader);
  }

  private  AwsXRayPropagator();

}