From b4b5ae1dddfb3c4333faa67b8cd14ee5ded4bd12 Mon Sep 17 00:00:00 2001 From: Eduardo Navarro Date: Tue, 28 May 2024 09:59:31 +0200 Subject: [PATCH] Fix tags middleware parsing Don't include key-value hash elements which: - are only a sequence of empty strings - are nil Reintroduce tags spec. --- lib/influxdb/rails/configuration.rb | 6 +++++- spec/unit/{tags.rb => influx_db/rails/tags_spec.rb} | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) rename spec/unit/{tags.rb => influx_db/rails/tags_spec.rb} (91%) diff --git a/lib/influxdb/rails/configuration.rb b/lib/influxdb/rails/configuration.rb index 3aad799..0424646 100644 --- a/lib/influxdb/rails/configuration.rb +++ b/lib/influxdb/rails/configuration.rb @@ -67,7 +67,11 @@ class Configuration set_defaults( measurement_name: "rails".freeze, ignored_hooks: [].freeze, - tags_middleware: ->(tags) { tags }, + tags_middleware: lambda { |tags| + tags.reject do |_key, value| + value.nil? || (value.is_a?(String) && value.squish.empty?) + end + }, rails_app_name: nil, ignored_environments: %w[test cucumber selenium].freeze, environment: ::Rails.env, diff --git a/spec/unit/tags.rb b/spec/unit/influx_db/rails/tags_spec.rb similarity index 91% rename from spec/unit/tags.rb rename to spec/unit/influx_db/rails/tags_spec.rb index b98319c..0213e3c 100644 --- a/spec/unit/tags.rb +++ b/spec/unit/influx_db/rails/tags_spec.rb @@ -26,7 +26,7 @@ it "removes empty strings" do subject = described_class.new(config: config, tags: { hans: "", franz: " " }) - expect(subject.to_h).not_to a_hash_including(hans: "", franz: " ") + expect(subject.to_h).not_to a_hash_including(:hans, :franz) end it "returns symbols" do @@ -36,7 +36,7 @@ it "removes nil" do subject = described_class.new(config: config, tags: { hans: nil }) - expect(subject.to_h).not_to a_hash_including(hans: nil) + expect(subject.to_h).not_to a_hash_including(:hans) end it "leaves arrays alone" do