Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conditional for when formatting metric name symbol #297

Merged
merged 3 commits into from
Nov 4, 2024
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
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Suggested change
Symbol === metric_name ? metric_name.name : metric_name.to_s
metric_name.is_a?(Symbol) ? metric_name.name : metric_name.to_s
Do not use === (...read more)

The case equality operator === in Ruby is used to test equality within a when clause of a case statement. However, it's often considered a bad practice to use this operator explicitly outside of a case statement. This is because its behavior can be quite unpredictable and confusing, as it behaves differently for different classes.

The use of the === operator can lead to code that is harder to read and understand. It's also potentially prone to bugs, as it might not behave as expected with certain objects. Therefore, it's recommended to avoid the explicit use of the === operator.

Instead of using the === operator, it's better to use more explicit methods that clearly indicate what you're trying to achieve. For example, if you're trying to check if a string matches a regular expression, you can use the match? method. If you want to check if an object is an instance of a certain class, you can use the is_a? method. These methods are much more clear and straightforward, leading to better, more maintainable code.

View in Datadog  Leave us feedback  Documentation

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
Loading