# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

import json
import os

from opentelemetry.sdk import trace
from opentelemetry.sdk.resources import PROCESS_PID


def main() -> None:
    tracer_provider = trace.TracerProvider(shutdown_on_exit=False)
    tracer = tracer_provider.get_tracer("cached")
    parent_pid = os.getpid()
    parent_resource_pid = tracer_provider.resource.attributes[PROCESS_PID]
    parent_tracer_pid = tracer.resource.attributes[PROCESS_PID]

    pid = os.fork()
    if not pid:
        child_pid = os.getpid()
        new_tracer = tracer_provider.get_tracer("new")
        span = tracer.start_span("child")
        print(
            json.dumps(
                {
                    "child_pid": child_pid,
                    "provider_pid": tracer_provider.resource.attributes[PROCESS_PID],
                    "cached_tracer_pid": tracer.resource.attributes[PROCESS_PID],
                    "new_tracer_pid": new_tracer.resource.attributes[PROCESS_PID],
                    "span_pid": span.resource.attributes[PROCESS_PID],
                }
            ),
            flush=True,
        )
        # pylint: disable-next=protected-access
        os._exit(0)

    os.waitpid(pid, 0)
    print(
        json.dumps(
            {
                "parent_pid": parent_pid,
                "parent_resource_pid": parent_resource_pid,
                "parent_tracer_pid": parent_tracer_pid,
                "parent_resource_pid_after_fork": tracer_provider.resource.attributes[PROCESS_PID],
                "parent_tracer_pid_after_fork": tracer.resource.attributes[PROCESS_PID],
            }
        ),
        flush=True,
    )


if __name__ == "__main__":
    main()
