Skip to content

Commit d4a566a

Browse files
authored
Merge pull request #2 from Vuta/feature/improve_appsignal_to_support_tagging
Feature/improve appsignal to support tagging
2 parents 2a17bc7 + cfe2367 commit d4a566a

File tree

5 files changed

+76
-13
lines changed

5 files changed

+76
-13
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ EventTracer.info action: 'Action', message: 'Message', appsignal: { increment_co
111111
# counter_2, 2
112112
```
113113

114+
We can also add [tags](https://docs.appsignal.com/metrics/custom.html#metric-tags) for metric
115+
116+
```ruby
117+
# Sample usage
118+
EventTracer.info(
119+
action: 'Action',
120+
message: 'Message',
121+
appsignal: {
122+
increment_counter: {
123+
counter_1: { value: 1, tags: { region: 'eu' } }
124+
}
125+
}
126+
)
127+
# This calls .increment_counter on Appsignal once with additional tag
128+
# counter_1, 1, region: 'eu'
129+
114130
**3. Datadog**
115131

116132
Datadog via dogstatsd-ruby (4.8.1) is currently supported for the following metric functions available for the EventTracer's log methods

event_tracer.gemspec

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
1818
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
1919
spec.require_paths = %w[lib/event_tracer lib]
2020

21-
spec.add_development_dependency 'bundler', '~> 2.0'
22-
spec.add_development_dependency 'rake', '~> 10.0'
23-
spec.add_development_dependency 'rspec', '~> 3.0'
21+
spec.add_development_dependency "bundler", "~> 2.1.4"
22+
spec.add_development_dependency "rake", "~> 10.0"
23+
spec.add_development_dependency "rspec", "~> 3.0"
2424
end

lib/event_tracer/appsignal_logger.rb

+20-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88
# Usage: EventTracer.register :appsignal, EventTracer::AppsignalLogger.new(Appsignal)
99
# appsignal_logger.info appsignal: { increment_counter: { counter_1: 1, counter_2: 2 }, set_gauge: { gauge_1: 1 } }
10+
# appsignal_logger.info appsignal: { set_gauge: { gauge_1: { value: 1, tags: { region: 'eu' } } } }
1011
module EventTracer
1112
class AppsignalLogger < BasicDecorator
1213

@@ -21,6 +22,8 @@ class AppsignalLogger < BasicDecorator
2122
return LogResult.new(false, "Appsignal metric #{metric} invalid") unless metric_args && metric_args.is_a?(Hash)
2223

2324
send_metric metric, metric_args
25+
rescue InvalidTagError => e
26+
return LogResult.new(false, e.message)
2427
end
2528

2629
LogResult.new(true)
@@ -37,10 +40,24 @@ def applied_metrics(appsignal_args)
3740
end
3841

3942
def send_metric(metric, payload)
40-
payload.each do |increment, value|
41-
appsignal.send(metric, increment, value)
43+
payload.each do |increment, attribute|
44+
if attribute.is_a?(Hash)
45+
begin
46+
appsignal.send(
47+
metric,
48+
increment,
49+
attribute.fetch(:value),
50+
attribute.fetch(:tags)
51+
)
52+
rescue KeyError
53+
raise InvalidTagError, "Appsignal payload { #{increment}: #{attribute} } invalid"
54+
end
55+
else
56+
appsignal.send(metric, increment, attribute)
57+
end
4258
end
4359
end
44-
4560
end
61+
62+
class InvalidTagError < StandardError; end
4663
end

lib/event_tracer/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module EventTracer
2-
VERSION = '0.2.0'.freeze
2+
VERSION = '0.2.1'.freeze
33
end

spec/event_tracer/appsignal_logger_spec.rb

+36-6
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,26 @@
5151
end
5252

5353
shared_examples_for 'processes_hashed_inputs' do
54-
let(:appsignal_payload) { {
55-
increment_counter: { 'Counter_1' => 1, 'Counter_2' => 2 },
56-
add_distribution_value: { 'Distribution_1' => 10 },
57-
set_gauge: { 'Gauge_1' => 100 }
58-
} }
54+
let(:appsignal_payload) do
55+
{
56+
increment_counter: {
57+
'Counter_1' => { value: 1, tags: { tag_a: 'a', tag_b: 'b' } },
58+
'Counter_2' => 2
59+
},
60+
add_distribution_value: { 'Distribution_1' => 10 },
61+
set_gauge: {
62+
'Gauge_1' => 100,
63+
'Gauge_2' => { value: 200, tags: { region: 'eu' } }
64+
}
65+
}
66+
end
5967

6068
it 'processes each hash keyset as a metric iteration' do
61-
expect(mock_appsignal).to receive(:increment_counter).with('Counter_1', 1)
69+
expect(mock_appsignal).to receive(:increment_counter).with('Counter_1', 1, tag_a: 'a', tag_b: 'b')
6270
expect(mock_appsignal).to receive(:increment_counter).with('Counter_2', 2)
6371
expect(mock_appsignal).to receive(:add_distribution_value).with('Distribution_1', 10)
6472
expect(mock_appsignal).to receive(:set_gauge).with('Gauge_1', 100)
73+
expect(mock_appsignal).to receive(:set_gauge).with('Gauge_2', 200, region: 'eu')
6574

6675
result = subject.send(expected_call, appsignal: appsignal_payload)
6776

@@ -88,6 +97,27 @@
8897
end
8998
end
9099
end
100+
101+
context 'with invalid tagging payload' do
102+
let(:appsignal_payload) do
103+
{
104+
metric => {
105+
'Counter_1' => { value: 1, tag: { tag_a: 'a' } }
106+
}
107+
}
108+
end
109+
110+
it 'rejects the payload and return failure result' do
111+
expect(mock_appsignal).not_to receive(:increment_counter)
112+
expect(mock_appsignal).not_to receive(:add_distribution_value)
113+
expect(mock_appsignal).not_to receive(:set_gauge)
114+
115+
result = subject.send(expected_call, appsignal: appsignal_payload)
116+
117+
expect(result.success?).to eq false
118+
expect(result.error).to eq "Appsignal payload { Counter_1: {:value=>1, :tag=>{:tag_a=>\"a\"}} } invalid"
119+
end
120+
end
91121
end
92122
end
93123

0 commit comments

Comments
 (0)