Skip to content

Commit

Permalink
Avoid allocations when using delay_serialization (#295)
Browse files Browse the repository at this point in the history
Co-authored-by: ywenc <[email protected]>
  • Loading branch information
jhawthorn and ywenc committed Sep 18, 2024
1 parent a3291f2 commit e89cbfe
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/datadog/statsd/message_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/integrations/delay_serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e89cbfe

Please sign in to comment.