Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ section below.

## Unreleased changes

- [#416](https://github.com/Shopify/statsd-instrument/pull/416) - Fix missing `metric_prefix` in Aggregator finalizer, causing metrics to lose their prefix when flushed during GC.
- [#415](https://github.com/Shopify/statsd-instrument/pull/415) - Fix `sample_rate` being ignored when aggregation is enabled. Previously, `increment`, `measure`, and `histogram` calls would bypass sampling entirely when aggregation was enabled, causing metrics to be emitted at 100% rate regardless of the configured sample rate.
- [#411](https://github.com/Shopify/statsd-instrument/pull/403) - Add `CompiledMetric::Gauge` as the second metric type to support pre-compiled metric datagrams. It can be used as a replacement over standard `StatsD.gauge`.
- Add support for `nil` tag values in `CompiledMetric` dynamic tags (converted to empty string).
Expand Down
1 change: 1 addition & 0 deletions lib/statsd/instrument/aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def initialize(
@datagram_builders,
@datagram_builder_class,
@default_tags,
@metric_prefix,
)

ObjectSpace.define_finalizer(
Expand Down
25 changes: 25 additions & 0 deletions test/aggregator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,31 @@ def test_with_prefix
assert_equal(1, @sink.datagrams.last.value)
end

def test_finalizer_with_prefix
# Test that the finalizer correctly uses the prefix when flushing metrics
# IMPORTANT: Use empty tags to ensure datagram_builder is not pre-created by tags_sorted
aggregator = StatsD::Instrument::Aggregator.new(@sink, StatsD::Instrument::DatagramBuilder, "MyApp", [])

aggregator.increment("foo", 1, tags: [])
aggregator.increment("bar", 1, tags: [], no_prefix: true)

# Manually trigger the finalizer (simulates GC cleanup)
finalizer = StatsD::Instrument::Aggregator.finalize(
aggregator.instance_variable_get(:@finalizer),
)
finalizer.call

assert_equal(2, @sink.datagrams.size)

prefixed_datagram = @sink.datagrams.find { |d| d.name == "MyApp.foo" }
refute_nil(prefixed_datagram, "Expected to find datagram with prefix 'MyApp.foo'")
assert_equal(1, prefixed_datagram.value)

unprefixed_datagram = @sink.datagrams.find { |d| d.name == "bar" }
refute_nil(unprefixed_datagram, "Expected to find datagram without prefix 'bar'")
assert_equal(1, unprefixed_datagram.value)
end

def test_synchronous_operation_on_thread_failure
# Force thread_healthcheck to return false
@subject.stubs(:thread_healthcheck).returns(false)
Expand Down