-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #889 from stefanvermaas/main
Adds support for tracking `http.rb` HTTP requests
- Loading branch information
Showing
13 changed files
with
290 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'http', '~> 5.0' | ||
|
||
gemspec :path => '../' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
class Hooks | ||
# @api private | ||
class HttpHook < Appsignal::Hooks::Hook | ||
register :http_rb | ||
|
||
def dependencies_present? | ||
defined?(HTTP::Client) && Appsignal.config && Appsignal.config[:instrument_http_rb] | ||
end | ||
|
||
def install | ||
require "appsignal/integrations/http" | ||
HTTP::Client.send(:prepend, Appsignal::Integrations::HttpIntegration) | ||
|
||
Appsignal::Environment.report_enabled("http_rb") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
module Integrations | ||
module HttpIntegration | ||
def request(verb, uri, opts = {}) | ||
parsed_request_uri = URI.parse(uri) | ||
request_uri = "#{parsed_request_uri.scheme}://#{parsed_request_uri.host}" | ||
|
||
begin | ||
Appsignal.instrument("request.http_rb", "#{verb.upcase} #{request_uri}") do | ||
super | ||
end | ||
rescue Exception => error # rubocop:disable Lint/RescueException | ||
Appsignal.set_error(error) | ||
raise error | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Appsignal::Hooks::HttpHook do | ||
before :context do | ||
start_agent | ||
end | ||
|
||
if DependencyHelper.http_present? | ||
context "with instrument_http_rb set to true" do | ||
describe "#dependencies_present?" do | ||
subject { described_class.new.dependencies_present? } | ||
|
||
it { is_expected.to be_truthy } | ||
end | ||
end | ||
|
||
context "with instrument_http_rb set to false" do | ||
describe "#dependencies_present?" do | ||
before { Appsignal.config.config_hash[:instrument_http_rb] = false } | ||
after { Appsignal.config.config_hash[:instrument_http_rb] = true } | ||
subject { described_class.new.dependencies_present? } | ||
|
||
it { is_expected.to be_falsy } | ||
end | ||
end | ||
|
||
it "installs the HTTP plugin" do | ||
expect(HTTP::Client.included_modules) | ||
.to include(Appsignal::Integrations::HttpIntegration) | ||
end | ||
else | ||
describe "#dependencies_present?" do | ||
subject { described_class.new.dependencies_present? } | ||
|
||
it { is_expected.to be_falsy } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# frozen_string_literal: true | ||
|
||
if DependencyHelper.http_present? | ||
require "appsignal/integrations/http" | ||
|
||
describe Appsignal::Integrations::HttpIntegration do | ||
let(:transaction) { http_request_transaction } | ||
|
||
around do |example| | ||
keep_transactions { example.run } | ||
end | ||
|
||
before do | ||
set_current_transaction(transaction) | ||
end | ||
|
||
before :context do | ||
start_agent | ||
end | ||
|
||
it "should instrument a HTTP request" do | ||
stub_request(:get, "http://www.google.com") | ||
|
||
HTTP.get("http://www.google.com") | ||
|
||
transaction_hash = transaction.to_h | ||
expect(transaction_hash).to include("namespace" => Appsignal::Transaction::HTTP_REQUEST) | ||
expect(transaction_hash["events"].first).to include( | ||
"body" => "", | ||
"body_format" => Appsignal::EventFormatter::DEFAULT, | ||
"name" => "request.http_rb", | ||
"title" => "GET http://www.google.com" | ||
) | ||
end | ||
|
||
it "should instrument a HTTPS request" do | ||
stub_request(:get, "https://www.google.com") | ||
|
||
HTTP.get("https://www.google.com") | ||
|
||
transaction_hash = transaction.to_h | ||
expect(transaction_hash).to include("namespace" => Appsignal::Transaction::HTTP_REQUEST) | ||
expect(transaction_hash["events"].first).to include( | ||
"body" => "", | ||
"body_format" => Appsignal::EventFormatter::DEFAULT, | ||
"name" => "request.http_rb", | ||
"title" => "GET https://www.google.com" | ||
) | ||
end | ||
|
||
context "with request parameters" do | ||
it "does not include the query parameters in the title" do | ||
stub_request(:get, "https://www.google.com?q=Appsignal") | ||
|
||
HTTP.get("https://www.google.com", :params => { :q => "Appsignal" }) | ||
|
||
expect(transaction.to_h["events"].first).to include( | ||
"body" => "", | ||
"title" => "GET https://www.google.com" | ||
) | ||
end | ||
|
||
it "does not include the request body in the title" do | ||
stub_request(:post, "https://www.google.com") | ||
.with(:body => { :q => "Appsignal" }.to_json) | ||
|
||
HTTP.post("https://www.google.com", :json => { :q => "Appsignal" }) | ||
|
||
expect(transaction.to_h["events"].first).to include( | ||
"body" => "", | ||
"title" => "POST https://www.google.com" | ||
) | ||
end | ||
end | ||
|
||
context "with an HTTP exception" do | ||
let(:error) { ExampleException.new("oh no!") } | ||
|
||
it "reports the exception and re-raises it" do | ||
stub_request(:get, "https://www.google.com").and_raise(error) | ||
|
||
expect do | ||
HTTP.get("https://www.google.com") | ||
end.to raise_error(ExampleException) | ||
|
||
transaction_hash = transaction.to_h | ||
expect(transaction_hash).to include("namespace" => Appsignal::Transaction::HTTP_REQUEST) | ||
expect(transaction_hash["events"].first).to include( | ||
"body" => "", | ||
"body_format" => Appsignal::EventFormatter::DEFAULT, | ||
"name" => "request.http_rb", | ||
"title" => "GET https://www.google.com" | ||
) | ||
|
||
expect(transaction_hash["error"]).to include( | ||
"backtrace" => kind_of(String), | ||
"name" => error.class.name, | ||
"message" => error.message | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters