-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CAMEL-21202 - Add a ThreadPoolFactory to propagate OpenTelemetry contexts #15496
Conversation
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🤖 CI automation will test this PR automatically. 🐫 Apache Camel Committers, please review the following items:
|
Do you mind create a JIRA for these kind of PRs and link to the JIRA in the commit message. |
Do you have any links/docs about otel and wrapping these threads is a best practice ? |
https://opentelemetry.io/docs/languages/java/instrumentation/#context-propagation-between-threads |
Yes but would that not be wrong as the thread is reused by Camel later to process other exchanges and then the thread has stale data from the previous exchanges. |
aeb286b
to
d54614e
Compare
I replaced the implementation with wrapping |
I fail to understand that, because that only works for a task/runnable that are isolated. You do this generally for all of Camel's thread pool, and threads such as a JMS listener will then use the same task/runnable but to process all the incoming JMS messages, and each of those JMS messages is a different Exchange and span id in that otel world. |
Yes, @davsclaus, you are correct. I understand the issue, but this PR is handling only thread pools that are created by Camel itself. I don't have enough time/resources to test all components, but I'll be happy to help as much as possible. Regarding the exchanges and it's span ids, this is already / should be handled by |
@davsclaus IMO we would need a new SPI for
That way, other libraries in the Thread know what context to grab. Most libraries do a Context.current() which relies on ThreadLocal storage... |
Hmm I think this is somewhat wrong with these thread pool. If the purpose is to ensure the otel context is correct for the given exchange and to store/get that via thread locals, then look at MDC and the It has callbacks for before/after and coupled with the Exchange processing by Camel which allows better to setup/cleanup otel context accordingly. |
Ah yeah the camel-opentelemetry is doing that through CamelEvents but perhaps the UnitOfWork API is more suited, will check it out |
@davsclaus are we guaranteed to be called on the same Thread though in the UnitOfWork hooks ? Unfortunately OpenTelemetry requires it [1] |
b0f1608
to
7f36ce1
Compare
Yes but only when you use before/afterProcess callbacks and turn this on (like MDC does) Line 161 in b4b66d5
I think this was implemented for the same reason that MDC also needs callback for before/after on the same thread. PS: It may be that Camel in transacted mode does not execute before/after processor (we can add this if needed) but transacted is forced executing synchronously anyway by Camel. |
Be mindful that before/after process is executed for every step (EIP i.e. Processor) in the route. |
Hi @davsclaus thanks for your insights ! After some testing I don't think this is enough... Ideally OpenTelemetry wants a hook when a thread starts some work and another hook when the work is done ( everything in the same thread):
Naively using the before/after process hooks in the UnitOfWorkAPI I get some errors like :
For MDC to achieve this when in Multicast with parallel processing, it seems some more code was added in camel-core [1] |
Having to use this ThreadLocal stuff seems like a tough requirement IMO so I'm investigating if we can remove it [1] [2] [1] https://github.com/johnpoth/camel/commits/opentelemetry-rewrite/ |
yes thread locals and MDC is poor in a modern multi threaded world. However its fine if there is a few situations like multicast + parallel its not work (just document this). We just need it to work good for 99% use-cases. |
We can expose beforeExecute/afterExecute from Then we can find a nicer way to do this without having to wrap every thread pool executor implementation in Camel. |
One caveat with that is the before and after is executed when the task is being processed, and the original thread that added the task may be doing something else, and the need for this context propagation may be too late. However this would only be in cases where the thread pool is busy and tasks and filled up in the backlog queue. |
...src/main/java/org/apache/camel/opentelemetry/OpenTelemetryInstrumentedThreadPoolFactory.java
Show resolved
Hide resolved
This PR Makes it possible to build a hook into |
I will rebase this PR against #15679. |
…exts Signed-off-by: Adriano Machado <[email protected]>
…ce context propagation
2c07866
to
285ad28
Compare
@ammachado is there more work, or can this PR be marked ready |
👍🏻 Ready to go. |
/component-test camel-opentelemetry Result ✅ The tests passed successfully |
🤖 The Apache Camel test robot will run the tests for you 👍 |
…exts (#15496) * CAMEL-21202 - Add a ThreadPoolFactory to propagate OpenTelemetry contexts Signed-off-by: Adriano Machado <[email protected]> * CAMEL-21202 - Adding missing implementation for SheduledExecutorService context propagation * CAMEL-21202 - Adding the instrumented thread pool factory to the tests * Add strict checking for OpenTelemetry contexts * Add ThreadFactoryListener implementation for OTEL context propagation --------- Signed-off-by: Adriano Machado <[email protected]>
…exts (#15496) * CAMEL-21202 - Add a ThreadPoolFactory to propagate OpenTelemetry contexts Signed-off-by: Adriano Machado <[email protected]> * CAMEL-21202 - Adding missing implementation for SheduledExecutorService context propagation * CAMEL-21202 - Adding the instrumented thread pool factory to the tests * Add strict checking for OpenTelemetry contexts * Add ThreadFactoryListener implementation for OTEL context propagation --------- Signed-off-by: Adriano Machado <[email protected]>
Description
Add a ThreadPoolFactory to propagate OpenTelemetry contexts
Target
camel-3.x
, whereas Camel 4 uses themain
branch)Tracking
Apache Camel coding standards and style
mvn clean install -DskipTests
locally from root folder and I have committed all auto-generated changes.