56quarters / cadence

An extensible Statsd client for Rust
https://docs.rs/cadence/
Apache License 2.0
84 stars 27 forks source link

macro variants that do not panic if global default client is not set #210

Open danielnorberg opened 4 months ago

danielnorberg commented 4 months ago

Thank you a lot for this library!

Would it make sense to add variants of the statsd macros that do not panic if the global default client is not set?

When testing code makes use of the statsd macros it is sometimes awkward to have to initialize the global default client in unit tests to avoid the macros panicking.

Would be happy to contribute a PR to add such macros if you think it would make sense.

Wdyt?

56quarters commented 4 months ago

Thanks for the issue! I can understand that using the macros makes tests hard to write. I'm not sure about adding non-panicking versions since that seems like it would make it even harder to detect incorrect configurations (and it's already UDP in a lot of cases).

I'm curious about your use case and how the macros and statsd are involved in the tests. Does the code under test make metrics calls that you'd like to verify as part of the tests? Or does it just happen to make metrics calls that you don't care about while testing? If it's the latter, there's no harm in calling cadence_macros::set_global_client() multiple times in tests - any calls after the first time are ignored.