Skip to content
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

Add New Relic support #57

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -94,6 +94,25 @@ The following is a list of some popular variables that don't require the use of
- `stage`: Name of the stage from the capistrano multistage extension.
- `user_name`: Name of the local git author.

## New Relic

This notifier can be useful if you're using [New Relic](https://newrelic.com/) but not [using the Ruby agent](https://newrelic.com/docs/ruby/recording-deployments-with-the-ruby-agent) (so you don't have the rpm_newrelic gem installed or a config/newrelic.yml file.)

```rb
require 'capistrano/notifier/new_relic'

set :notifier_new_relic_options, {
:api_key => 'fffffffffffffffffffffffffffffffffffffffffffffff',
:application_id => 1234567
}
```

Both `:api_key` and `:application_id` must be present. If you set a `NOTES` environment variable:

cap deploy NOTES="fix everything"

The notes will be added as the deployment description.

## StatsD

```rb
Expand Down
64 changes: 64 additions & 0 deletions lib/capistrano/notifier/new_relic.rb
@@ -0,0 +1,64 @@
require 'capistrano/notifier'
require 'net/http'
require 'uri'

class Capistrano::Notifier::NewRelic < Capistrano::Notifier::Base
DEFAULTS = {}

def self.load_into(configuration)
configuration.load do
namespace :deploy do
namespace :notify do
desc 'Notify New Relic of deploy.'
task :new_relic do
run Capistrano::Notifier::NewRelic.new(configuration).command
end
end
end

after 'deploy', 'deploy:notify:new_relic'
end
end

def command
post = Net::HTTP::Post.new(uri.request_uri)
post.add_field 'x-api-key', options[:api_key]
post.set_form_data form_data
response_text = ""
Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
response = http.request(post)
response_text = "#{response.code} #{response.message}"
end
"echo New Relic Deploy notification result: #{response_text}"
end

private

def options
if cap.respond_to? :notifier_new_relic_options
cap.notifier_new_relic_options.reverse_merge DEFAULTS
else
DEFAULTS
end
end

# Form data to be posted
def form_data
@form_data ||= {
'deployment[app_name]' => options[:app_name],
'deployment[application_id]' => options[:application_id],
'deployment[description]' => ENV['NOTES'],
'deployment[revision]' => git_current_revision,
'deployment[changelog]' => git_log,
'deployment[user]' => user_name
}
end

def uri
@uri ||= URI('https://rpm.newrelic.com/deployments.xml')
end
end

if Capistrano::Configuration.instance
Capistrano::Notifier::NewRelic.load_into(Capistrano::Configuration.instance)
end
30 changes: 30 additions & 0 deletions spec/capistrano/notifier/new_relic_spec.rb
@@ -0,0 +1,30 @@
require 'spec_helper'
require 'capistrano/notifier/new_relic'

describe Capistrano::Notifier::NewRelic do
let(:configuration) { Capistrano::Configuration.new }
let(:post) { mock(:post, :add_field => nil,
:set_body_internal => nil,
:response_body_permitted? => nil,
:[] => nil,
:[]= => nil,
:path => nil,
:exec => nil,
:set_form_data => nil) }
subject { described_class.new configuration }

before :each do
configuration.load do
set :application, 'example'
set :notifier_new_relic_options, {
:api_key => 'ffffff',
:application_id => 12345
}
end
Net::HTTP::Post.stub(:new).and_return(post)
end

describe '#command' do
it 'posts to the newrelic api'
end
end