hashicorp / go-metrics

A Golang library for exporting performance and runtime metrics to external metrics systems (i.e. statsite, statsd)
MIT License
1.46k stars 177 forks source link

Prevent dogstatsd sink from clobbering the metric key for other sinks #156

Closed mkeeler closed 1 year ago

mkeeler commented 1 year ago

Fixes: #154

If using the Fanout sink and including the dogstatsd sink, any sinks sent the metric AFTER dogstatsd could see invalid metric names. This was due to some inadvertent modifications to the original metric key. The new implementation ensure that when modifying the orignial metric key, a new backing array is used and will have key parts copied into it instead of changing elements in the original metric key.

chapmanc commented 1 year ago

This looks good but should we also make a copy of the key at the point the fan out passes the key to the other sinks? To avoid the sinks from affecting each other?

mkeeler commented 1 year ago

I looked at a few other sinks and this one is the only one that modified the key before converting it to a string.

As metrics get emitted a lot, I figured it would put unnecessary pressure on the GC to copy every time when it should be the responsibility of the sink to not modify.

chapmanc commented 1 year ago

That's a good call out. We should probably add a unit test to ensure that keys don't get changed. A new sink or changes to existing sinks could easily resurface this.