From e89cbfe6a653286da4bd66fbc5ce37b3eb23c1f7 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 18 Sep 2024 12:36:32 -0700 Subject: [PATCH] Avoid allocations when using delay_serialization (#295) Co-authored-by: ywenc --- lib/datadog/statsd.rb | 2 +- lib/datadog/statsd/message_buffer.rb | 3 ++- spec/integrations/delay_serialization_spec.rb | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/datadog/statsd.rb b/lib/datadog/statsd.rb index 043f497..32f8d1a 100644 --- a/lib/datadog/statsd.rb +++ b/lib/datadog/statsd.rb @@ -431,7 +431,7 @@ def send_stats(stat, delta, type, opts = EMPTY_OPTIONS) if sample_rate == 1 || opts[:pre_sampled] || rand <= sample_rate full_stat = if @delay_serialization - [[stat, delta, type], {tags: opts[:tags], sample_rate: sample_rate}] + [stat, delta, type, opts[:tags], sample_rate] else serializer.to_stat(stat, delta, type, tags: opts[:tags], sample_rate: sample_rate) end diff --git a/lib/datadog/statsd/message_buffer.rb b/lib/datadog/statsd/message_buffer.rb index 48debfe..2ea01d2 100644 --- a/lib/datadog/statsd/message_buffer.rb +++ b/lib/datadog/statsd/message_buffer.rb @@ -28,7 +28,8 @@ def add(message) # Serializes the message if it hasn't been already. Part of the # delay_serialization feature. if message.is_a?(Array) - message = @serializer.to_stat(*message[0], **message[1]) + stat, delta, type, tags, sample_rate = message + message = @serializer.to_stat(stat, delta, type, tags: tags, sample_rate: sample_rate) end message_size = message.bytesize diff --git a/spec/integrations/delay_serialization_spec.rb b/spec/integrations/delay_serialization_spec.rb index 63884dc..18b2cfc 100644 --- a/spec/integrations/delay_serialization_spec.rb +++ b/spec/integrations/delay_serialization_spec.rb @@ -6,7 +6,7 @@ # expects an Array is passed and not a String expect(buffer) .to receive(:add) - .with([["boo", 1, "c"], {tags: nil, sample_rate: 1}]) + .with(["boo", 1, "c", nil, 1]) # and then expect no more adds! expect(buffer).to receive(:add).exactly(0).times expect(buffer)