opencensus-go
opencensus-go copied to clipboard
trace: exporter for unit testing
Supply a trace.Exporter
useful for testing that just collects all spans and events in memory for later validation.
We will probably need similar things for stats
and tag
so maybe we should collect all these under a single mocks
package.
Adding it to R2 for now, we will move it to R3 if we can't deliver.
The Go style is having the testing packages near the actual package, see net/http and net/http/httptest. We should have a trace/tracetest and stats/statstest.
Not sure if we need to provide anything for the tag package for testing.
Hi there!
I've been working on implementing OpenCensus, and would like to unit test to make sure metrics are being sent over to exporters correctly. Has there been any traction on this?
Not that I know of. I think partially the reason is that the interface is so simple to implement that most people just create an implementation themselves in test code.
the interface is so simple to implement
This is true, but there's another part required to trigger the exporter, which I didn't realise until just now after looking through the test code here: it's best to unregister the view to trigger the exporting. https://github.com/census-instrumentation/opencensus-go/blob/7c764632b5a454dfa7c14e9ad2bf05218fceeedf/plugin/ochttp/route_test.go#L53
Before, I was trying to trigger exporting with view.SetReportingPeriod()
, but then you must also time.Sleep()
for a duration longer than the reporting period, and the exporter will be called every time the reporting time passes, rather than just once with the above method.
I'd love it if such a test exporter would automatically do this, or some variant, so the test doesn't have to trigger exporting =)