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?
Asked
Active
Viewed 51 times
-2

Matthew MacFarland
- 2,413
- 3
- 26
- 34
-
1You'll have to mock them (and/or proxy them) - I'm not an Open-Telemetry user, but if `Histogram
` and `Counter – Dai Jul 12 '23 at 15:43` don't have `interface` types you can mock them with then you may need to resort to low-level CLR hacking to intercept those method calls. -
1They're meters, so read them with a MetricsListener. It's the same with Activities. Both are explicit testing ports for your application – Panagiotis Kanavos Jul 12 '23 at 16:38
1 Answers
2
You can't test them directly, however, I don't think that's the way that they should be tested.
You can create a MetricsListener
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
.

Martin
- 2,180
- 4
- 21
- 41