From e9ba5109bd282e2148e326aef9ab4d923f4e75ea Mon Sep 17 00:00:00 2001 From: raymond zhao Date: Fri, 25 Oct 2024 15:58:28 -0400 Subject: [PATCH 1/3] Add conditional --- .../statsd/serialization/stat_serializer.rb | 6 +++++- spec/statsd/serialization/stat_serializer_spec.rb | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/datadog/statsd/serialization/stat_serializer.rb b/lib/datadog/statsd/serialization/stat_serializer.rb index 9b75a24..6401483 100644 --- a/lib/datadog/statsd/serialization/stat_serializer.rb +++ b/lib/datadog/statsd/serialization/stat_serializer.rb @@ -38,7 +38,11 @@ def global_tags attr_reader :tag_serializer def formatted_metric_name(metric_name) - formatted = Symbol === metric_name ? metric_name.name : metric_name.to_s + if Symbol === metric_name && metric_name.respond_to?(:name) + formatted = metric_name.name + else + formatted = metric_name.to_s + end if formatted.include?('::') formatted = formatted.gsub('::', '.') diff --git a/spec/statsd/serialization/stat_serializer_spec.rb b/spec/statsd/serialization/stat_serializer_spec.rb index 939a0e5..ecf5c27 100644 --- a/spec/statsd/serialization/stat_serializer_spec.rb +++ b/spec/statsd/serialization/stat_serializer_spec.rb @@ -108,6 +108,20 @@ 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' + expect(input).to eq :'somecount::test' + + input = :'somecount:|@test' + output = subject.format(input, 1, 'c') + expect(output).to eq 'somecount___test:1|c' + expect(input).to eq :'somecount:|@test' + end + end end context 'benchmark' do From 0b2a983e5ec71b1af2e87676e3d5490ed0db6b7d Mon Sep 17 00:00:00 2001 From: raymond zhao Date: Mon, 4 Nov 2024 10:15:46 -0500 Subject: [PATCH 2/3] Use RUBY_VERSION --- .../statsd/serialization/stat_serializer.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/datadog/statsd/serialization/stat_serializer.rb b/lib/datadog/statsd/serialization/stat_serializer.rb index 6401483..9a242b2 100644 --- a/lib/datadog/statsd/serialization/stat_serializer.rb +++ b/lib/datadog/statsd/serialization/stat_serializer.rb @@ -37,13 +37,18 @@ def global_tags attr_reader :prefix attr_reader :tag_serializer - def formatted_metric_name(metric_name) - if Symbol === metric_name && metric_name.respond_to?(:name) - formatted = metric_name.name - else - formatted = 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) + metric_name.is_a?(Symbol) ? 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!(':|@', '_') From d89642614e83b4f0017caa70167b468a6e4d5f63 Mon Sep 17 00:00:00 2001 From: raymond zhao Date: Mon, 4 Nov 2024 12:39:07 -0500 Subject: [PATCH 3/3] feedback --- lib/datadog/statsd/serialization/stat_serializer.rb | 2 +- spec/statsd/serialization/stat_serializer_spec.rb | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/datadog/statsd/serialization/stat_serializer.rb b/lib/datadog/statsd/serialization/stat_serializer.rb index 9a242b2..c8e9bcb 100644 --- a/lib/datadog/statsd/serialization/stat_serializer.rb +++ b/lib/datadog/statsd/serialization/stat_serializer.rb @@ -43,7 +43,7 @@ def metric_name_to_string(metric_name) end else def metric_name_to_string(metric_name) - metric_name.is_a?(Symbol) ? metric_name.name : metric_name.to_s + Symbol === metric_name ? metric_name.name : metric_name.to_s end end diff --git a/spec/statsd/serialization/stat_serializer_spec.rb b/spec/statsd/serialization/stat_serializer_spec.rb index ecf5c27..894e79d 100644 --- a/spec/statsd/serialization/stat_serializer_spec.rb +++ b/spec/statsd/serialization/stat_serializer_spec.rb @@ -114,12 +114,10 @@ input = :'somecount::test' output = subject.format(input, 1, 'c') expect(output).to eq 'somecount.test:1|c' - expect(input).to eq :'somecount::test' input = :'somecount:|@test' output = subject.format(input, 1, 'c') expect(output).to eq 'somecount___test:1|c' - expect(input).to eq :'somecount:|@test' end end end