quipo / statsd

Golang StatsD client
MIT License
164 stars 51 forks source link

Provide mock statsd client with convenience recorders. #44

Closed joshrose closed 6 years ago

joshrose commented 6 years ago

It is often useful when unit testing instrumented code to verify that the expected instrumenation is being recorded in addition to verifying the correctness and performance of that code. This commit adds a mock client that enables that form of verification without having to spin up a UDP listener.

The basic approach here is to provide a level of indirection between the statsd.Statsd interface and the methods use to implement that interface. That allows arbitrary behavior to be invoked in response to different methods invoked on the MockStatsdClient. If no such method or methods are provided, calling a method on the MockStatsdClient will just be no-op.

That alone would allow for test cases to provide their own mock functions. But the common use case for testing is to record the events sent to the mock client and verify that the instrumentation methods one expected to be invoked were invoked with the proper arguments. To support this common use case, a series of flient builders are provided for MockStatsdClient that will allow a client to provide a slice to be used to record events. The test file provides examples of using this fluent builder.

joshrose commented 6 years ago

@quipo LMK how you'd like to take this forward, please.

quipo commented 6 years ago

thanks @joshrose, looks great. I never receive notifications about updates to this repo, need to check my github settings...

joshrose commented 6 years ago

Great, thanks.