KEMBAR78
Proposal: Expose a Method to Check if an Executor is Already Wrapped by OpenTelemetry · Issue #6955 · open-telemetry/opentelemetry-java · GitHub
Skip to content

Proposal: Expose a Method to Check if an Executor is Already Wrapped by OpenTelemetry #6955

@ShadabGentle

Description

@ShadabGentle

Description
Currently, the io.opentelemetry.context.CurrentContextExecutorService class, used to wrap ExecutorService with tracing context propagation, is non-public. As a result, there is no public API to determine if an ExecutorService is already wrapped. This limitation makes it challenging to avoid excessive wrapping or recursive wrapping in scenarios where multiple components may apply OpenTelemetry wrapping independently.

Use Case
In our application, we use OpenTelemetry to enable tracing across multiple services. During the setup of our custom Aspect for MQTT consumers, we encountered a scenario where multiple layers could potentially wrap an ExecutorService. Since CurrentContextExecutorService is non-public, there is no way to check if an executor is already wrapped.

The absence of such a mechanism leads to potential issues:

1. Performance Overhead: Re-wrapping executors multiple times unnecessarily adds extra layers and CPU/memory usage.
2. Circular Dependencies: Repeated wrapping can result in unintended behaviors, including stack overflow exceptions in some cases.
A public method to check if an executor is already wrapped would allow developers to handle this elegantly and avoid redundant wrapping.

Proposed Solution
Expose a utility method or make CurrentContextExecutorService public, enabling developers to check if an 'ExecutorService' is already wrapped. For example:

public static boolean isContextWrapped(Executor executor) {
    return executor instanceof CurrentContextExecutorService;
}

Alternatively, add a public method in Context:

public static boolean isTaskWrapping(Executor executor) {
    // Internal check for wrapping
}

This approach would make it straightforward for developers to ensure their wrapping logic is idempotent.

Potential Impact
1. Improved Developer Experience: Simplifies scenarios where multiple layers apply OpenTelemetry wrapping, improving usability.
2. Avoid Performance Overhead: Ensures that executors are wrapped only once, reducing unnecessary overhead.
3. Enhances Debugging: Provides better insight for developers when debugging issues related to executor wrapping.

Steps to Reproduce the Issue

  1. Create an ExecutorService.
  2. Wrap the executor using Context.taskWrapping(executor).
  3. Attempt to check if the executor is already wrapped without relying on non-public classes.

Request for Feedback
I would appreciate feedback on this proposal and happy to contribute a pull request if maintainers are open to this enhancement.

Call to Action
Please consider this feature request for inclusion in a future release of OpenTelemetry. Let us know if there's an alternative approach we can take to solve this issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions