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

FIX: stop raising NoMethodError when processing unregistered types #209

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 2 additions & 4 deletions lib/prometheus_exporter/server/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ def process_hash(obj)
if collector = @collectors[obj["type"]]
collector.collect(obj)
else
metric = @metrics[obj["name"]]
if !metric
metric = register_metric_unsafe(obj)
end
metric = @metrics[obj["name"]] || register_metric_unsafe(obj) or
Copy link
Member

Choose a reason for hiding this comment

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

|| and or in the same line is terrifying. perhaps

metric = @metrics[obj["name"]] || register_metric_unsafe(obj)
if !metric
   return
end

I know it is way more verbose but it is less scary

Copy link
Author

Choose a reason for hiding this comment

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

Ha, fair enough. How about I meet you half way?

# register_metric_unsafe will log an error if it returns nil, so that code path can end here.
return unless metric

I can leave out the comment if you prefer, but I almost always include them with early returns/guard clauses.

return

keys = obj["keys"] || {}
if obj["custom_labels"]
Expand Down
8 changes: 8 additions & 0 deletions test/server/collector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ def test_it_can_pass_options_to_histogram
assert_equal(text, collector.prometheus_metrics_text)
end

def test_it_does_not_raise_on_fail_to_register
collector = PrometheusExporter::Server::Collector.new
json = {
type: :something_with_no_registered_collector
}.to_json
collector.process(json) # Should not raise an exception; previously raised NoMethodError
end

def test_it_can_collect_sidekiq_metrics
collector = PrometheusExporter::Server::Collector.new
client = PipedClient.new(collector)
Expand Down