public MaestroTracingContext initTracingContext(
      WorkflowSummary workflowSummary, StepRuntimeSummary runtimeSummary) {
    try {
      final Span initSpan = tracer.nextSpan();
      try {
        initSpan.name(INIT_SPAN_NAME);
        tagInitSpan(initSpan, workflowSummary, runtimeSummary);
        initSpan.start();
      } finally {
        // always send the init Span asap with tags.
        initSpan.finish();
      }

      final TraceContext initContext = initSpan.context();
      LOG.trace(
          "Created initial span with traceID: {}, spanID: {}, maestro object {}, {}",
          initContext.traceIdString(),
          initContext.spanIdString(),
          workflowSummary.getIdentity(),
          runtimeSummary.getIdentity());

      // runSpan corresponds to the lifecycle of a step attempt.
      Span runSpan =
          tracer.nextSpanWithParent(samplerFunctionAlways, samplerFunctionAlwaysArg, initContext);
      runSpan.name(RUN_SPAN_NAME);
      TraceContext runContext = runSpan.context();
      LOG.trace(
          "Created run span with traceID: {}, spanID: {}, parentSpanID: {}",
          runContext.traceIdString(),
          runContext.spanIdString(),
          runContext.parentIdString());

      return MaestroTracingContext.fromTraceContext(runContext);
    } catch (Exception ex) {
      LOG.warn(
          "Exception caught when initializing trace for {}, {}",
          workflowSummary.getIdentity(),
          runtimeSummary.getIdentity(),
          ex);
      return null;
    }
  }
@Test
  public void testInitTracingContext() {
    Span initSpan = mock(Span.class);
    TraceContext initContext = TraceContext.newBuilder().traceId(49L).spanId(50L).build();
    when(initSpan.context()).thenReturn(initContext);

    when(mockTracer.nextSpan()).thenReturn(initSpan);
    MaestroTracingManager tm = new TestTracingManager(mockTracer);

    Span runSpan = mock(Span.class);
    TraceContext runContext =
        TraceContext.newBuilder()
            .traceId(1L)
            .traceIdHigh(2L)
            .spanId(3L)
            .parentId(4L)
            .sampled(null)
            .build();
    when(runSpan.context()).thenReturn(runContext);
    when(mockTracer.nextSpanWithParent(any(), any(), any())).thenReturn(runSpan);

    WorkflowSummary workflowSummary = new WorkflowSummary();
    workflowSummary.setWorkflowId("wf");
    workflowSummary.setWorkflowInstanceId(123L);
    workflowSummary.setWorkflowRunId(1L);
    workflowSummary.setWorkflowUuid("wf-uuid");

    StepRuntimeSummary runtimeSummary =
        StepRuntimeSummary.builder()
            .stepId("step")
            .stepInstanceId(456L)
            .stepAttemptId(3L)
            .stepInstanceUuid("step-uuid")
            .build();

    MaestroTracingContext context = tm.initTracingContext(workflowSummary, runtimeSummary);
    verify(initSpan, times(1)).name(any());
    verify(initSpan, atLeast(8)).tag(any(), any());
    verify(initSpan, times(1)).start();
    verify(initSpan, times(1)).finish();

    verify(runSpan, times(1)).name(any());
    assertEquals(context.toTraceContext(), runContext);
  }