Search code examples
c#open-telemetry

How can calls to Histogram<T> and Counter<T> be verified in unit tests?


I'd like to make a assertions in unit tests to confirm that the telemetry meters Histogram<T> and Counter<T> are being called. If I have a reference to the meter instance that the unit test code is calling, is there a way to inspect it and confirm that the Record or Add methods are being called or not?


Solution

  • You can't test them directly, however, I don't think that's the way that they should be tested.

    You can create a MeterListener that can listen to a specific metric and verify that you get the right outcome rather than the right value is passed. With this, you can pass in a Metric with a specific auto-generated name that you can assert against. Similar to the way that you'd test for Activity.