open-telemetry / opentelemetry-java-instrumentation

OpenTelemetry auto-instrumentation and instrumentation libraries for Java
https://opentelemetry.io
Apache License 2.0
1.88k stars 823 forks source link

How to test auto-instrumented code #4560

Closed trask closed 1 year ago

trask commented 2 years ago

Creating an issue from this discussion because this sounds like a great use-case for the project to support in the future.

Discussed in https://github.com/open-telemetry/opentelemetry-java-instrumentation/discussions/4550

Originally posted by **rkennedy-mode** October 29, 2021 We've created an OpenTelemetry extension for JUnit 5, which allows us to capture logged spans. We utilize this to add some additional checking to verify that we're generating the spans we expect with proper parenting. We also run the auto-instrumentation agent in production, which causes some issues for the tests, which we don't run with the agent. I'm curious if there's any recommendations here regarding running test with/without the agent or some other way to ensure that tests have something approaching what we'll see in production. In particular, we have a fair bit of asynchronous code that jumps across CompletableFuture/ExecutorService type boundaries that we want to make sure are generating spans with the proper parents.
trask commented 2 years ago

hi @rkennedy-mode! I think this is a great use-case for the project to support. We have some nice infra for running the javaagent during tests for this repository and checking the resulting spans. It seems that this could be extended/useful for this use case. We are currently working toward stabilizing the Instrumenter API for external usage, but have started thinking about tackling the same for the testing infra after that.

aivinog1 commented 1 year ago

Hello @trask! I have a similar question in Stackoverflow, maybe I can help to implement this? I have thoughts about moving the custom memory exporter from the agent-for-testing module inside the main agent and just adding the in-memory exporting mode (like oltp, jaeger, etc.). And add some testing module that allows to register/deregister this agent in the test's JVM with this mode and APIs to verify the exporter's logic.

aivinog1 commented 1 year ago

Or I think if there are difficulties with sharing the data in memory between tests and the agent we could implement some sort of file exporter and export the data in the temp folder in the tests and read it from this directory afterward 🤔

trask commented 1 year ago

@aivinog1 check out https://github.com/open-telemetry/opentelemetry-java-docs/issues/42 for a nice alternative

aivinog1 commented 1 year ago

@aivinog1 check out open-telemetry/opentelemetry-java-docs#42 for a nice alternative

Thanks, actually this is a good idea 👍

trask commented 1 year ago

Closing, I think https://github.com/open-telemetry/opentelemetry-java-examples/tree/main/telemetry-testing is a good minimal testing example