diff --git a/lib/influxdb/rails/tags.rb b/lib/influxdb/rails/tags.rb index 281a6bb..4f20f12 100644 --- a/lib/influxdb/rails/tags.rb +++ b/lib/influxdb/rails/tags.rb @@ -9,7 +9,7 @@ def initialize(config:, tags: {}, additional_tags: InfluxDB::Rails.current.tags) def to_h expanded_tags.reject do |_, value| - value.blank? + value.to_s.blank? end end diff --git a/lib/influxdb/rails/values.rb b/lib/influxdb/rails/values.rb index c2e4ac6..fe91c02 100644 --- a/lib/influxdb/rails/values.rb +++ b/lib/influxdb/rails/values.rb @@ -8,7 +8,7 @@ def initialize(values: {}, additional_values: InfluxDB::Rails.current.values) def to_h expanded_values.reject do |_, value| - value.blank? + value.to_s.blank? end end diff --git a/spec/requests/action_controller_metrics_spec.rb b/spec/requests/action_controller_metrics_spec.rb index 288c9ea..15ac743 100644 --- a/spec/requests/action_controller_metrics_spec.rb +++ b/spec/requests/action_controller_metrics_spec.rb @@ -18,11 +18,27 @@ expect_metric( name: "rails", tags: a_hash_including( - method: "MetricsController#index", + hook: "process_action", + status: 200, + format: :html, + http_method: "GET" + ), + values: a_hash_including( + view: be_between(1, 500), + db: be_between(1, 500), + controller: be_between(1, 500) + ) + ) + end + + it "writes default and custom tags" do + get "/metrics" + + expect_metric( + name: "rails", + tags: a_hash_including( hook: "process_action", - status: 200, - format: :html, - http_method: "GET", + location: "MetricsController#index", additional_tag: :value, server: Socket.gethostname, app_name: :app_name, @@ -30,10 +46,7 @@ ), values: a_hash_including( additional_value: :value, - request_id: :request_id, - view: be_between(1, 500), - db: be_between(1, 500), - controller: be_between(1, 500) + request_id: :request_id ) ) end diff --git a/spec/requests/action_mailer_deliver_metrics_spec.rb b/spec/requests/action_mailer_deliver_metrics_spec.rb index 9ff84f3..94e734d 100644 --- a/spec/requests/action_mailer_deliver_metrics_spec.rb +++ b/spec/requests/action_mailer_deliver_metrics_spec.rb @@ -18,18 +18,11 @@ expect_metric( tags: a_hash_including( - hook: "deliver", - mailer: "MetricMailer", - location: "MetricsController#index", - additional_tag: :value, - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware + hook: "deliver", + mailer: "MetricMailer" ), values: a_hash_including( - additional_value: :value, - request_id: :request_id, - value: 1 + value: 1 ) ) end diff --git a/spec/requests/action_view_collection_metrics_spec.rb b/spec/requests/action_view_collection_metrics_spec.rb index c176bc1..b898a21 100644 --- a/spec/requests/action_view_collection_metrics_spec.rb +++ b/spec/requests/action_view_collection_metrics_spec.rb @@ -18,19 +18,12 @@ expect_metric( name: "rails", tags: a_hash_including( - location: "MetricsController#index", - hook: "render_collection", - additional_tag: :value, - filename: include("spec/support/views/metrics/_item.html.erb"), - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware + hook: "render_collection", + filename: include("spec/support/views/metrics/_item.html.erb") ), values: a_hash_including( - additional_value: :value, - count: 3, - request_id: :request_id, - value: be_between(1, 500) + count: 3, + value: be_between(1, 500) ) ) end @@ -43,8 +36,7 @@ expect_metric( name: "rails", tags: a_hash_including( - location: "MetricsController#index", - hook: "render_collection" + hook: "render_collection" ), timestamp: 1_514_797_200 ) @@ -58,8 +50,7 @@ expect_no_metric( name: "rails", tags: a_hash_including( - location: "MetricsController#index", - hook: "render_collection" + hook: "render_collection" ) ) end diff --git a/spec/requests/action_view_partial_metrics_spec.rb b/spec/requests/action_view_partial_metrics_spec.rb index 67a3d19..f74e1be 100644 --- a/spec/requests/action_view_partial_metrics_spec.rb +++ b/spec/requests/action_view_partial_metrics_spec.rb @@ -17,18 +17,11 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "render_partial", - additional_tag: :value, - filename: include("spec/support/views/metrics/_item.html.erb"), - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware + hook: "render_partial", + filename: include("spec/support/views/metrics/_item.html.erb") ), values: a_hash_including( - additional_value: :value, - request_id: :request_id, - value: be_between(1, 500) + value: be_between(1, 500) ) ) end @@ -40,8 +33,7 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "render_partial" + hook: "render_partial" ), timestamp: 1_514_797_200 ) @@ -54,8 +46,7 @@ expect_no_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "render_partial" + hook: "render_partial" ) ) end diff --git a/spec/requests/action_view_template_metrics_spec.rb b/spec/requests/action_view_template_metrics_spec.rb index 31a48d8..23c6b91 100644 --- a/spec/requests/action_view_template_metrics_spec.rb +++ b/spec/requests/action_view_template_metrics_spec.rb @@ -17,18 +17,11 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "render_template", - additional_tag: :value, - filename: include("spec/support/views/metrics/index.html.erb"), - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware + hook: "render_template", + filename: include("spec/support/views/metrics/index.html.erb") ), values: a_hash_including( - additional_value: :value, - request_id: :request_id, - value: be_between(1, 500) + value: be_between(1, 500) ) ) end @@ -40,8 +33,7 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "render_template" + hook: "render_template" ), timestamp: 1_514_797_200 ) @@ -54,8 +46,7 @@ expect_no_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "render_template" + hook: "render_template" ) ) end diff --git a/spec/requests/active_job_enqueue_metrics_spec.rb b/spec/requests/active_job_enqueue_metrics_spec.rb index db959c0..580096c 100644 --- a/spec/requests/active_job_enqueue_metrics_spec.rb +++ b/spec/requests/active_job_enqueue_metrics_spec.rb @@ -18,20 +18,13 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "enqueue", - job: "MetricJob", - queue: "default", - state: "queued", - additional_tag: :value, - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware + hook: "enqueue", + job: "MetricJob", + queue: "default", + state: "queued" ), values: a_hash_including( - additional_value: :value, - request_id: :request_id, - value: 1 + value: 1 ) ) end @@ -43,7 +36,6 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", hook: "enqueue" ), timestamp: 1_514_797_200 @@ -57,7 +49,6 @@ expect_no_metric( tags: a_hash_including( - location: "MetricsController#index", hook: "enqueue" ) ) diff --git a/spec/requests/active_job_perform_metrics_spec.rb b/spec/requests/active_job_perform_metrics_spec.rb index 93940b0..0e6ac8c 100644 --- a/spec/requests/active_job_perform_metrics_spec.rb +++ b/spec/requests/active_job_perform_metrics_spec.rb @@ -20,14 +20,10 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "perform", - state: "succeeded", - job: "MetricJob", - queue: "default", - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware + hook: "perform", + state: "succeeded", + job: "MetricJob", + queue: "default" ), values: a_hash_including( value: be_between(0, 30) @@ -44,7 +40,6 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", hook: "perform" ), timestamp: 1_514_797_200 @@ -60,7 +55,6 @@ expect_no_metric( tags: a_hash_including( - location: "MetricsController#index", hook: "perform" ) ) diff --git a/spec/requests/active_record_instantiation_metrics_spec.rb b/spec/requests/active_record_instantiation_metrics_spec.rb index 1a4b731..ec3e905 100644 --- a/spec/requests/active_record_instantiation_metrics_spec.rb +++ b/spec/requests/active_record_instantiation_metrics_spec.rb @@ -19,19 +19,12 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#show", - hook: "instantiation", - class_name: "Metric", - additional_tag: :value, - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware + hook: "instantiation", + class_name: "Metric" ), values: a_hash_including( - additional_value: :value, - request_id: :request_id, - value: be_between(1, 500), - record_count: 1 + value: be_between(1, 500), + record_count: 1 ) ) end @@ -43,8 +36,7 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#show", - hook: "instantiation" + hook: "instantiation" ), timestamp: 1_514_797_200 ) @@ -57,8 +49,7 @@ expect_no_metric( tags: a_hash_including( - location: "MetricsController#show", - hook: "instantiation" + hook: "instantiation" ) ) end diff --git a/spec/requests/active_record_sql_metrics_spec.rb b/spec/requests/active_record_sql_metrics_spec.rb index f768d9c..2404134 100644 --- a/spec/requests/active_record_sql_metrics_spec.rb +++ b/spec/requests/active_record_sql_metrics_spec.rb @@ -19,21 +19,14 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "sql", - name: "Metric Create", - class_name: "Metric", - operation: "INSERT", - additional_tag: :value, - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware + hook: "sql", + name: "Metric Create", + class_name: "Metric", + operation: "INSERT" ), values: a_hash_including( - additional_value: :value, - request_id: :request_id, - value: be_between(1, 500), - sql: "INSERT INTO \"metrics\" (\"name\", \"created_at\", \"updated_at\") VALUES (xxx)" + value: be_between(1, 500), + sql: "INSERT INTO \"metrics\" (\"name\", \"created_at\", \"updated_at\") VALUES (xxx)" ) ) end @@ -45,21 +38,14 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "sql", - name: "SQL", - class_name: "SQL", - operation: "INSERT", - additional_tag: :value, - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware + hook: "sql", + name: "SQL", + class_name: "SQL", + operation: "INSERT" ), values: a_hash_including( - additional_value: :value, - request_id: :request_id, - value: be_between(1, 500), - sql: "INSERT INTO \"metrics\" (\"name\", \"created_at\", \"updated_at\") VALUES (xxx)" + value: be_between(1, 500), + sql: "INSERT INTO \"metrics\" (\"name\", \"created_at\", \"updated_at\") VALUES (xxx)" ) ) end @@ -71,7 +57,6 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", hook: "sql" ), timestamp: 1_514_797_200 @@ -85,7 +70,6 @@ expect_no_metric( tags: a_hash_including( - location: "MetricsController#index", hook: "sql" ) ) diff --git a/spec/requests/block_inistrumentation_spec.rb b/spec/requests/block_inistrumentation_spec.rb index 558d26b..15fa722 100644 --- a/spec/requests/block_inistrumentation_spec.rb +++ b/spec/requests/block_inistrumentation_spec.rb @@ -17,20 +17,13 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "block_instrumentation", - additional_tag: :value, - server: Socket.gethostname, - app_name: :app_name, - tags_middleware: :tags_middleware, - block_tag: :block_tag, - name: "name" + hook: "block_instrumentation", + block_tag: :block_tag, + name: "name" ), values: a_hash_including( - additional_value: :value, - request_id: :request_id, - block_value: :block_value, - value: be_between(1, 500) + block_value: :block_value, + value: be_between(1, 500) ) ) end @@ -42,8 +35,7 @@ expect_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "block_instrumentation" + hook: "block_instrumentation" ), timestamp: 1_514_797_200 ) @@ -56,8 +48,7 @@ expect_no_metric( tags: a_hash_including( - location: "MetricsController#index", - hook: "block_instrumentation" + hook: "block_instrumentation" ) ) end diff --git a/spec/unit/tags.rb b/spec/unit/tags.rb new file mode 100644 index 0000000..5115b4d --- /dev/null +++ b/spec/unit/tags.rb @@ -0,0 +1,47 @@ +require "spec_helper" + +RSpec.describe InfluxDB::Rails::Tags do + let(:config) { InfluxDB::Rails::Configuration.new } + + describe ".to_h" do + it "returns TrueClass" do + subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: true }) + expect(subject.to_h).to a_hash_including(hans: true) + end + + it "returns FalseClass" do + subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: false }) + expect(subject.to_h).to a_hash_including(hans: false) + end + + it "returns strings" do + subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: "franz" }) + expect(subject.to_h).to a_hash_including(hans: "franz") + end + + it "returns strings containing blank" do + subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: "franz hans" }) + expect(subject.to_h).to a_hash_including(hans: "franz hans") + end + + it "removes empty strings" do + subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: "", franz: " " }) + expect(subject.to_h).not_to a_hash_including(hans: "", franz: " ") + end + + it "returns symbols" do + subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: :franz }) + expect(subject.to_h).to a_hash_including(hans: :franz) + end + + it "removes nil" do + subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: nil }) + expect(subject.to_h).not_to a_hash_including(hans: nil) + end + + it "leaves arrays alone" do + subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: [], franz: %w[a b] }) + expect(subject.to_h).to a_hash_including(hans: [], franz: %w[a b]) + end + end +end