Skip to content

Feature/improve appsignal to support tagging #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ 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'

**3. Datadog**

Datadog via dogstatsd-ruby (4.8.1) is currently supported for the following metric functions available for the EventTracer's log methods
Expand Down
6 changes: 3 additions & 3 deletions event_tracer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = %w[lib/event_tracer lib]

spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency "bundler", "~> 2.1.4"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
end
23 changes: 20 additions & 3 deletions lib/event_tracer/appsignal_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, tags: { region: 'eu' } } } }
module EventTracer
class AppsignalLogger < BasicDecorator

Expand All @@ -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 InvalidTagError => e
return LogResult.new(false, e.message)
end

LogResult.new(true)
Expand All @@ -37,10 +40,24 @@ 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 InvalidTagError, "Appsignal payload { #{increment}: #{attribute} } invalid"
end
else
appsignal.send(metric, increment, attribute)
end
end
end

end

class InvalidTagError < StandardError; end
end
2 changes: 1 addition & 1 deletion lib/event_tracer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EventTracer
VERSION = '0.2.0'.freeze
VERSION = '0.2.1'.freeze
end
42 changes: 36 additions & 6 deletions spec/event_tracer/appsignal_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down