opentelemetry-java
opentelemetry-java copied to clipboard
Add OtlpHttpSpanExporterBuilder#exportAsJson()
Is your feature request related to a problem? Please describe.
To unit test and assert on the exported traces, it would be easier if we could configure OtlpHttpSpanExporterBuilder to exportAsJson().
Describe the solution you'd like
Add a public method OtlpHttpSpanExporterBuilder#exportAsJson() part of the API.
Describe alternatives you've considered
We can assert on the application/x-protobuf binary protocol, but we are limited to using "String.contains()" since it is a binary blob as far we are concerned.
We can use reflection to enable exportAsJson(), it works, but it is not future proof: it may break at any time if code changes.
Additional context Add any other context or screenshots about the feature request here.
What about using the opentelemetry-sdk-testing artifact? It is specifically designed to enable writing unit tests for your telemetry and instrumentation.
Thanks, that's super useful indeed.
However it's a bit more complicated in my case: our "unit tests" look more like "integration tests", and I cannot hook the InMemorySpanExporter.
Another option is to spin up an in-memory implementation of the server-side of the export, using some generated proto bindings. That implementation can then be queried programmatically to make assertions. This is how integration tests of some things work in the agent codebase, IIRC.
here's an example of what @jkwatson is describing: https://github.com/open-telemetry/opentelemetry-java-examples/tree/main/telemetry-testing
@JnRouvignac did that end up working for you or are you still chasing after json?
Hello,
Apologies for being unresponsive, this totally got out of my radar.
I already had a mock server that receives the exported trace as protobuf, but I was doing stupid String.contains() on
the protobuf which is pretty poor, and does not work for doing equality checks on integers.
Thanks to your comment, I think I now have all I need. More specifically, the following code allows me to parse the protobuf requests and then start making interesting asserts against the content of the spans: https://github.com/open-telemetry/opentelemetry-java-examples/blob/main/telemetry-testing/src/test/java/io/opentelemetry/example/telemetry/ApplicationTest.java#L97-L105 .
That's is all I need, so thank you very much! I am closing this issue now.