Skip to content

Commit

Permalink
Add conditional for when formatting metric name symbol (#297)
Browse files Browse the repository at this point in the history
* Add conditional

* Use RUBY_VERSION

* feedback
  • Loading branch information
rayz authored Nov 4, 2024
1 parent 333f852 commit 5f962c9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/datadog/statsd/serialization/stat_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,18 @@ def global_tags
attr_reader :prefix
attr_reader :tag_serializer

def formatted_metric_name(metric_name)
formatted = Symbol === metric_name ? metric_name.name : metric_name.to_s
if RUBY_VERSION < '3'
def metric_name_to_string(metric_name)
metric_name.to_s
end
else
def metric_name_to_string(metric_name)
Symbol === metric_name ? metric_name.name : metric_name.to_s
end
end

def formatted_metric_name(metric_name)
formatted = metric_name_to_string(metric_name)
if formatted.include?('::')
formatted = formatted.gsub('::', '.')
formatted.tr!(':|@', '_')
Expand Down
12 changes: 12 additions & 0 deletions spec/statsd/serialization/stat_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@
expect(input).to eq 'somecount:|@test'
end
end

context "when metric name is a symbol" do
it 'formats correctly without error' do
input = :'somecount::test'
output = subject.format(input, 1, 'c')
expect(output).to eq 'somecount.test:1|c'

input = :'somecount:|@test'
output = subject.format(input, 1, 'c')
expect(output).to eq 'somecount___test:1|c'
end
end
end

context 'benchmark' do
Expand Down

0 comments on commit 5f962c9

Please sign in to comment.