Remove thread lock by loading RuntimeContext explicitly. by WqyJh · Pull Request #3763 · open-telemetry/opentelemetry-python

Hi @WqyJh, thanks for the change -- I'm very much in favor of this idea. Would you be able to add test coverage?

Sure, I'd like to. However, I didn't see the coverage report, therefore not sure where the uncovered parts are. AFAIK, all functions related to this modification including context.get_current(), context.attach(), context.detach() already have tests.

That's good. What do you think about adding at least one test for _load_runtime_context as well?

Added an unitest test_load_runtime_context.

However, I found that OTEL_PYTHON_CONTEXT not working properly. If OTEL_PYTHON_CONTEXT=contextvars_context, an ContextVarsRuntimeContext instance would be loaded, however, if OTEL_PYTHON_CONTEXT=context, an Context instance won't be loaded. This is caused by entry_points function, the return value contains only one EntryPoint named contextvars_context.

image

It's clear that this problem is not introduced by this PR because I didn't modify these code lines. If you also think it's an abnormal behavior, I think we should merge this PR first and open a new issue for this problem.

    default_context = "contextvars_context"

    configured_context = environ.get(
        OTEL_PYTHON_CONTEXT, default_context
    )  # type: str

    try:
        return next(  # type: ignore
            iter(  # type: ignore
                entry_points(  # type: ignore
                    group="opentelemetry_context",
                    name=configured_context,
                )
            )
        ).load()()
    except Exception:  # pylint: disable=broad-except
        logger.exception(
            "Failed to load context: %s", configured_context
        )