def _tracer_setup() -> Iterator[Tracer]:
    """\
    Context manager with common setup for tracing endpoints

    Yields a tracer (from a fresh SDK with new exporter) then finally flushes
    spans created during the test after.
    """

    tracer_provider = TracerProvider(
        sampler=ALWAYS_ON,
        active_span_processor=BatchSpanProcessor(
            CloudTraceSpanExporter(project_id=os.environ.get("PROJECT_ID"))
        ),
    )
    tracer = tracer_provider.get_tracer(INSTRUMENTING_MODULE_NAME)

    try:
        yield tracer
    finally:
        tracer_provider.shutdown()
def common_setup() -> Iterator[tuple[str, Tracer]]:
    """\
    Context manager with common setup for test endpoints

    It extracts the test-id header, creates a tracer, and finally flushes
    spans created during the test
    """

    if TEST_ID not in request.headers:
        raise Exception(f"{TEST_ID} header is required")
    test_id = request.headers[TEST_ID]

    tracer_provider = TracerProvider(
        sampler=ALWAYS_ON,
        active_span_processor=BatchSpanProcessor(
            CloudTraceSpanExporter(project_id=os.environ.get("PROJECT_ID"))),
    )
    tracer = tracer_provider.get_tracer(INSTRUMENTING_MODULE_NAME)

    try:
        yield test_id, tracer
    finally:
        tracer_provider.shutdown()