Skip to content

Commit f54f0b0

Browse files
committed
support tagging for appsignal metric
1 parent 0be1f3e commit f54f0b0

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

Diff for: README.md

+17
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ EventTracer.info action: 'Action', message: 'Message', appsignal: { increment_co
104104
# counter_2, 2
105105
```
106106

107+
We can also add [tags](https://docs.appsignal.com/metrics/custom.html#metric-tags) for metric
108+
109+
```ruby
110+
# Sample usage
111+
EventTracer.info(
112+
action: 'Action',
113+
message: 'Message',
114+
appsignal: {
115+
increment_counter: {
116+
counter_1: { value: 1, tags: { region: 'eu' } }
117+
}
118+
}
119+
)
120+
# This calls .increment_counter on Appsignal once with additional tag
121+
# counter_1, 1, region: 'eu'
122+
```
123+
107124
**Summary**
108125

109126
In all the generated interface for `EventTracer` logging could look something like this

Diff for: lib/event_tracer/appsignal_logger.rb

+18-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, 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 Exception => e
26+
return LogResult.new(false, e.message)
2427
end
2528

2629
LogResult.new(true)
@@ -37,10 +40,22 @@ 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 Exception, "Appsignal payload { #{increment}: #{attribute} } invalid"
54+
end
55+
else
56+
appsignal.send(metric, increment, attribute)
57+
end
4258
end
4359
end
44-
4560
end
4661
end

Diff for: 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)