opentelemetry-java icon indicating copy to clipboard operation
opentelemetry-java copied to clipboard

Add OtlpHttpSpanExporterBuilder#exportAsJson()

Open JnRouvignac opened this issue 1 year ago • 4 comments

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.

JnRouvignac avatar Jun 22 '24 15:06 JnRouvignac

What about using the opentelemetry-sdk-testing artifact? It is specifically designed to enable writing unit tests for your telemetry and instrumentation.

jkwatson avatar Jun 22 '24 21:06 jkwatson

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.

JnRouvignac avatar Jul 18 '24 12:07 JnRouvignac

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.

jkwatson avatar Jul 19 '24 01:07 jkwatson

here's an example of what @jkwatson is describing: https://github.com/open-telemetry/opentelemetry-java-examples/tree/main/telemetry-testing

trask avatar Jul 19 '24 15:07 trask

@JnRouvignac did that end up working for you or are you still chasing after json?

breedx-splk avatar Oct 03 '24 20:10 breedx-splk

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.

JnRouvignac avatar Oct 09 '24 21:10 JnRouvignac