Skip to content

Commit

Permalink
Merge pull request #1 from Talkdesk/http-rb-instrumentation
Browse files Browse the repository at this point in the history
Add http.rb instrumentation to NewRelic
  • Loading branch information
tjsousa authored Aug 29, 2016
2 parents 59688c4 + 7a27bc0 commit 7390aaa
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# newrelic_httprb
New Relic instrumentation for the http, the gem! (http.rb)

New Relic instrumentation for http, the gem! (http.rb)

## Requirements

* [newrelic_rpm](newrelic/rpm)
* [http.rb](httprb/http)

## Install

Just add the gem to your Gemfile

For optional requires, use:

```ruby
require 'newrelic/httprb'
```

## License

(The MIT License)

Copyright (c) 2016 Tiago Sousa

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs = ['lib', 'test']
t.test_files = FileList['test/*_test.rb']
end

desc "Run tests"
task :default => :test
2 changes: 2 additions & 0 deletions lib/newrelic/httprb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'newrelic_httprb/instrumentation'
require 'newrelic_httprb/version'
32 changes: 32 additions & 0 deletions lib/newrelic_httprb/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
DependencyDetection.defer do
named :http_rb

depends_on do
defined?(HTTP) && defined?(HTTP::Client)
end

executes do
::NewRelic::Agent.logger.info 'Installing http.rb instrumentation'
require 'new_relic/agent/cross_app_tracing'
require 'newrelic_httprb/wrappers'
end

executes do
class HTTP::Client
def perform_with_newrelic_trace(request, options)
wrapped_request = ::NewRelicHTTP::HTTPRequest.new(request)

response = nil
::NewRelic::Agent::CrossAppTracing.tl_trace_http_request(wrapped_request) do
response = perform_without_newrelic_trace(request, options)
::NewRelicHTTP::HTTPResponse.new(response)
end

response
end

alias perform_without_newrelic_trace perform
alias perform perform_with_newrelic_trace
end
end
end
3 changes: 3 additions & 0 deletions lib/newrelic_httprb/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module NewRelicHTTP
VERSION = '0.0.1'
end
51 changes: 51 additions & 0 deletions lib/newrelic_httprb/wrappers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module NewRelicHTTP
class HTTPResponse
attr_reader :response

def initialize(response)
@response = response
end

def [](key)
_, value = response.headers.find { |k,_| key.downcase == k.downcase }
value unless value.nil?
end

def to_hash
response.headers
end
end

class HTTPRequest
attr_reader :request, :uri

def initialize(request)
@request = request
@uri = request.uri
end

def type
"http.rb"
end

def host
if hostname = self['host']
hostname.split(':').first
else
request.host
end
end

def method
request.verb.upcase
end

def [](key)
request.headers[key]
end

def []=(key, value)
request.headers[key] = value
end
end
end
23 changes: 23 additions & 0 deletions newrelic_httprb.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.expand_path('../lib/newrelic_httprb/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Tiago Sousa"]
gem.email = ["[email protected]"]
gem.description = %q{New Relic instrumentation for http.rb}
gem.summary = %q{New Relic instrumentation for http.rb}
gem.homepage = "https://github.com/Talkdesk/newrelic_httprb"
gem.license = "MIT"

gem.name = "newrelic_httprb"
gem.require_paths = ["lib"]
gem.version = NewRelicHTTP::VERSION

gem.files = `git ls-files`.split($/)
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})

gem.add_dependency 'newrelic_rpm', '~> 3.11'
gem.add_dependency 'http'

gem.add_development_dependency 'rake'
gem.add_development_dependency 'test-unit'
end
49 changes: 49 additions & 0 deletions test/instrumentation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "http"
require "test/unit"
require "newrelic_rpm"
require "newrelic/httprb"

class HTTPTest < Test::Unit::TestCase
include NewRelic::Agent::Instrumentation::ControllerInstrumentation

URL = "http://www.google.com/index.html"

def setup
NewRelic::Agent.manual_start
@engine = NewRelic::Agent.instance.stats_engine
@engine.clear_stats
end

def assert_metrics(*m)
m.each do |x|
assert @engine.get_stats_no_scope(x), "#{x} not in metrics"
end
end

def test_get
response = HTTP.get URL

assert_match /<head>/i, response.body
assert_metrics "External/all",
"External/www.google.com/http.rb/GET",
"External/allOther",
"External/www.google.com/all"
end

def test_post
HTTP.post URL

assert_metrics "External/all",
"External/www.google.com/http.rb/POST",
"External/allOther",
"External/www.google.com/all"
end

def test_ignore
NewRelic::Agent.disable_all_tracing do
HTTP.get URL
end

assert_empty @engine.to_h
end
end

0 comments on commit 7390aaa

Please sign in to comment.