From f54f0b0b2dadbb8e584a1da320078ed339ab698e Mon Sep 17 00:00:00 2001 From: Tran Anh Vu Date: Mon, 5 Oct 2020 18:53:30 +0700 Subject: [PATCH 1/7] support tagging for appsignal metric --- README.md | 17 +++++++++ lib/event_tracer/appsignal_logger.rb | 21 +++++++++-- spec/event_tracer/appsignal_logger_spec.rb | 42 ++++++++++++++++++---- 3 files changed, 71 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 00e8c9b..a4b4df6 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,23 @@ EventTracer.info action: 'Action', message: 'Message', appsignal: { increment_co # counter_2, 2 ``` +We can also add [tags](https://docs.appsignal.com/metrics/custom.html#metric-tags) for metric + +```ruby +# Sample usage +EventTracer.info( + action: 'Action', + message: 'Message', + appsignal: { + increment_counter: { + counter_1: { value: 1, tags: { region: 'eu' } } + } + } +) +# This calls .increment_counter on Appsignal once with additional tag +# counter_1, 1, region: 'eu' +``` + **Summary** In all the generated interface for `EventTracer` logging could look something like this diff --git a/lib/event_tracer/appsignal_logger.rb b/lib/event_tracer/appsignal_logger.rb index 94209a4..71d61c7 100644 --- a/lib/event_tracer/appsignal_logger.rb +++ b/lib/event_tracer/appsignal_logger.rb @@ -7,6 +7,7 @@ # # Usage: EventTracer.register :appsignal, EventTracer::AppsignalLogger.new(Appsignal) # appsignal_logger.info appsignal: { increment_counter: { counter_1: 1, counter_2: 2 }, set_gauge: { gauge_1: 1 } } +# appsignal_logger.info appsignal: { set_gauge: { gauge_1: { value: 1, region: 'eu' } } } module EventTracer class AppsignalLogger < BasicDecorator @@ -21,6 +22,8 @@ class AppsignalLogger < BasicDecorator return LogResult.new(false, "Appsignal metric #{metric} invalid") unless metric_args && metric_args.is_a?(Hash) send_metric metric, metric_args + rescue Exception => e + return LogResult.new(false, e.message) end LogResult.new(true) @@ -37,10 +40,22 @@ def applied_metrics(appsignal_args) end def send_metric(metric, payload) - payload.each do |increment, value| - appsignal.send(metric, increment, value) + payload.each do |increment, attribute| + if attribute.is_a?(Hash) + begin + appsignal.send( + metric, + increment, + attribute.fetch(:value), + attribute.fetch(:tags) + ) + rescue KeyError + raise Exception, "Appsignal payload { #{increment}: #{attribute} } invalid" + end + else + appsignal.send(metric, increment, attribute) + end end end - end end diff --git a/spec/event_tracer/appsignal_logger_spec.rb b/spec/event_tracer/appsignal_logger_spec.rb index 01c119a..2a6bae1 100644 --- a/spec/event_tracer/appsignal_logger_spec.rb +++ b/spec/event_tracer/appsignal_logger_spec.rb @@ -51,17 +51,26 @@ end shared_examples_for 'processes_hashed_inputs' do - let(:appsignal_payload) { { - increment_counter: { 'Counter_1' => 1, 'Counter_2' => 2 }, - add_distribution_value: { 'Distribution_1' => 10 }, - set_gauge: { 'Gauge_1' => 100 } - } } + let(:appsignal_payload) do + { + increment_counter: { + 'Counter_1' => { value: 1, tags: { tag_a: 'a', tag_b: 'b' } }, + 'Counter_2' => 2 + }, + add_distribution_value: { 'Distribution_1' => 10 }, + set_gauge: { + 'Gauge_1' => 100, + 'Gauge_2' => { value: 200, tags: { region: 'eu' } } + } + } + end it 'processes each hash keyset as a metric iteration' do - expect(mock_appsignal).to receive(:increment_counter).with('Counter_1', 1) + expect(mock_appsignal).to receive(:increment_counter).with('Counter_1', 1, tag_a: 'a', tag_b: 'b') expect(mock_appsignal).to receive(:increment_counter).with('Counter_2', 2) expect(mock_appsignal).to receive(:add_distribution_value).with('Distribution_1', 10) expect(mock_appsignal).to receive(:set_gauge).with('Gauge_1', 100) + expect(mock_appsignal).to receive(:set_gauge).with('Gauge_2', 200, region: 'eu') result = subject.send(expected_call, appsignal: appsignal_payload) @@ -88,6 +97,27 @@ end end end + + context 'with invalid tagging payload' do + let(:appsignal_payload) do + { + metric => { + 'Counter_1' => { value: 1, tag: { tag_a: 'a' } } + } + } + end + + it 'rejects the payload and return failure result' do + expect(mock_appsignal).not_to receive(:increment_counter) + expect(mock_appsignal).not_to receive(:add_distribution_value) + expect(mock_appsignal).not_to receive(:set_gauge) + + result = subject.send(expected_call, appsignal: appsignal_payload) + + expect(result.success?).to eq false + expect(result.error).to eq "Appsignal payload { Counter_1: {:value=>1, :tag=>{:tag_a=>\"a\"}} } invalid" + end + end end end From 3757badddf84ba366da4802641af035415e29cef Mon Sep 17 00:00:00 2001 From: Tran Anh Vu Date: Mon, 5 Oct 2020 22:27:31 +0700 Subject: [PATCH 2/7] Update Bundler to 2.1.4 --- .travis.yml | 2 +- event_tracer.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4810b7c..4bfa3ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,4 +4,4 @@ language: ruby cache: bundler rvm: - 2.3.0 -before_install: gem install bundler -v 1.17.1 +before_install: gem install bundler \ No newline at end of file diff --git a/event_tracer.gemspec b/event_tracer.gemspec index a931c78..4d530c2 100644 --- a/event_tracer.gemspec +++ b/event_tracer.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib/event_tracer", "lib"] - spec.add_development_dependency "bundler", "~> 1.17" + spec.add_development_dependency "bundler", "~> 2.1.4" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" end From 693458dbc3dfba1d6590f122239dd6886468f9bb Mon Sep 17 00:00:00 2001 From: Tran Anh Vu Date: Mon, 5 Oct 2020 22:56:47 +0700 Subject: [PATCH 3/7] Use ruby 2.6.6 on Travis CI --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4bfa3ee..06c9c26 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,5 +3,5 @@ sudo: false language: ruby cache: bundler rvm: - - 2.3.0 + - 2.6.6 before_install: gem install bundler \ No newline at end of file From 05125d275d988ccd2773fd1bd8be045b1b48db8a Mon Sep 17 00:00:00 2001 From: Tran Anh Vu Date: Tue, 6 Oct 2020 11:47:42 +0700 Subject: [PATCH 4/7] add invalid tag error class --- lib/event_tracer/appsignal_logger.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/event_tracer/appsignal_logger.rb b/lib/event_tracer/appsignal_logger.rb index 71d61c7..d50445b 100644 --- a/lib/event_tracer/appsignal_logger.rb +++ b/lib/event_tracer/appsignal_logger.rb @@ -22,7 +22,7 @@ class AppsignalLogger < BasicDecorator return LogResult.new(false, "Appsignal metric #{metric} invalid") unless metric_args && metric_args.is_a?(Hash) send_metric metric, metric_args - rescue Exception => e + rescue InvalidTagError => e return LogResult.new(false, e.message) end @@ -50,7 +50,7 @@ def send_metric(metric, payload) attribute.fetch(:tags) ) rescue KeyError - raise Exception, "Appsignal payload { #{increment}: #{attribute} } invalid" + raise InvalidTagError, "Appsignal payload { #{increment}: #{attribute} } invalid" end else appsignal.send(metric, increment, attribute) @@ -58,4 +58,6 @@ def send_metric(metric, payload) end end end + + class InvalidTagError < StandardError; end end From b1aaf45a2945a42d52bf9a87f6fceaa3400704be Mon Sep 17 00:00:00 2001 From: Tran Anh Vu Date: Fri, 9 Oct 2020 00:27:30 +0700 Subject: [PATCH 5/7] bump to 0.1.3 --- lib/event_tracer/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/event_tracer/version.rb b/lib/event_tracer/version.rb index d5e1c94..873e3a9 100644 --- a/lib/event_tracer/version.rb +++ b/lib/event_tracer/version.rb @@ -1,3 +1,3 @@ module EventTracer - VERSION = "0.1.2" + VERSION = "0.1.3" end From ee4e7a10729ec199b0ba6e7aa1be796940e1c277 Mon Sep 17 00:00:00 2001 From: Tran Anh Vu Date: Sat, 17 Oct 2020 01:44:56 +0700 Subject: [PATCH 6/7] update appsignal logger usage --- lib/event_tracer/appsignal_logger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/event_tracer/appsignal_logger.rb b/lib/event_tracer/appsignal_logger.rb index d50445b..b72df24 100644 --- a/lib/event_tracer/appsignal_logger.rb +++ b/lib/event_tracer/appsignal_logger.rb @@ -7,7 +7,7 @@ # # Usage: EventTracer.register :appsignal, EventTracer::AppsignalLogger.new(Appsignal) # appsignal_logger.info appsignal: { increment_counter: { counter_1: 1, counter_2: 2 }, set_gauge: { gauge_1: 1 } } -# appsignal_logger.info appsignal: { set_gauge: { gauge_1: { value: 1, region: 'eu' } } } +# appsignal_logger.info appsignal: { set_gauge: { gauge_1: { value: 1, tags: { region: 'eu' } } } } module EventTracer class AppsignalLogger < BasicDecorator From cfe23672db701a6b8b14b126cda8886565594b87 Mon Sep 17 00:00:00 2001 From: Tran Anh Vu Date: Thu, 12 Nov 2020 17:17:12 +0700 Subject: [PATCH 7/7] bump to 0.2.1 --- lib/event_tracer/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/event_tracer/version.rb b/lib/event_tracer/version.rb index 14774e2..6e9da17 100644 --- a/lib/event_tracer/version.rb +++ b/lib/event_tracer/version.rb @@ -1,3 +1,3 @@ module EventTracer - VERSION = '0.2.0'.freeze + VERSION = '0.2.1'.freeze end