-
-
Notifications
You must be signed in to change notification settings - Fork 522
Automatic collection of Sequel queries #2814
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -62,3 +62,5 @@ gem "benchmark-ips" | |
| gem "benchmark_driver" | ||
| gem "benchmark-ipsa" | ||
| gem "benchmark-memory" | ||
|
|
||
| gem "sequel" | ||
41 changes: 41 additions & 0 deletions
41
sentry-rails/spec/dummy/test_rails_app/app/controllers/sequel_users_controller.rb
This file contains hidden or 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,41 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "sequel" | ||
| require "sentry/sequel" | ||
|
|
||
| class SequelUsersController < ActionController::Base | ||
| def index | ||
| users = SEQUEL_DB[:users].all | ||
| render json: users | ||
| end | ||
|
|
||
| def create | ||
| id = SEQUEL_DB[:users].insert(name: params[:name], email: params[:email]) | ||
| render json: { id: id, name: params[:name], email: params[:email] }, status: :created | ||
| end | ||
|
|
||
| def show | ||
| user = SEQUEL_DB[:users].where(id: params[:id]).first | ||
| if user | ||
| render json: user | ||
| else | ||
| render json: { error: "Not found" }, status: :not_found | ||
| end | ||
| end | ||
|
|
||
| def update | ||
| SEQUEL_DB[:users].where(id: params[:id]).update(name: params[:name]) | ||
| user = SEQUEL_DB[:users].where(id: params[:id]).first | ||
| render json: user | ||
| end | ||
|
|
||
| def destroy | ||
| SEQUEL_DB[:users].where(id: params[:id]).delete | ||
| head :no_content | ||
| end | ||
|
|
||
| def exception | ||
| SEQUEL_DB[:users].all | ||
| raise "Something went wrong!" | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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,200 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| begin | ||
| require "simplecov" | ||
| SimpleCov.command_name "SequelTracing" | ||
| rescue LoadError | ||
| end | ||
|
|
||
| require "sequel" | ||
| require "sentry/sequel" | ||
|
|
||
| require_relative "../dummy/test_rails_app/app/controllers/sequel_users_controller" | ||
|
|
||
| RSpec.describe "Sequel Tracing with Rails", type: :request do | ||
| before(:all) do | ||
| if RUBY_ENGINE == "jruby" | ||
| SEQUEL_DB = Sequel.connect("jdbc:sqlite::memory:") | ||
| else | ||
| SEQUEL_DB = Sequel.sqlite | ||
| end | ||
|
|
||
| SEQUEL_DB.create_table :users do | ||
| primary_key :id | ||
| String :name | ||
| String :email | ||
| end | ||
|
|
||
| SEQUEL_DB[:users].count | ||
| SEQUEL_DB.extension(:sentry) | ||
| end | ||
|
|
||
| after(:all) do | ||
| SEQUEL_DB.drop_table?(:users) | ||
| Object.send(:remove_const, :SEQUEL_DB) | ||
| end | ||
|
|
||
| before do | ||
| make_basic_app do |config, app| | ||
| config.traces_sample_rate = 1.0 | ||
| config.enabled_patches << :sequel | ||
| end | ||
| end | ||
|
|
||
| let(:transport) { Sentry.get_current_client.transport } | ||
|
|
||
| describe "SELECT queries" do | ||
| it "creates a transaction with Sequel span for index action" do | ||
| get "/sequel/users" | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| expect(transport.events.count).to eq(1) | ||
|
|
||
| transaction = transport.events.last.to_h | ||
|
|
||
| expect(transaction[:type]).to eq("transaction") | ||
| expect(transaction.dig(:contexts, :trace, :op)).to eq("http.server") | ||
|
|
||
| sequel_span = transaction[:spans].find { |span| span[:op] == "db.sql.sequel" } | ||
|
|
||
| expect(sequel_span).not_to be_nil | ||
| expect(sequel_span[:description]).to include("SELECT") | ||
| expect(sequel_span[:description]).to include("users") | ||
| expect(sequel_span[:origin]).to eq("auto.db.sequel") | ||
| expect(sequel_span[:data]["db.system"]).to eq("sqlite") | ||
| end | ||
| end | ||
|
|
||
| describe "INSERT queries" do | ||
| it "creates a transaction with Sequel span for create action" do | ||
| post "/sequel/users", params: { name: "John Doe", email: "[email protected]" } | ||
|
|
||
| expect(response).to have_http_status(:created) | ||
|
|
||
| transaction = transport.events.last.to_h | ||
| expect(transaction[:type]).to eq("transaction") | ||
|
|
||
| insert_span = transaction[:spans].find do |span| | ||
| span[:op] == "db.sql.sequel" && span[:description]&.include?("INSERT") | ||
| end | ||
|
|
||
| expect(insert_span).not_to be_nil | ||
| expect(insert_span[:description]).to include("INSERT") | ||
| expect(insert_span[:description]).to include("users") | ||
| expect(insert_span[:origin]).to eq("auto.db.sequel") | ||
| end | ||
| end | ||
|
|
||
| describe "UPDATE queries" do | ||
| it "creates a transaction with Sequel span for update action" do | ||
| SEQUEL_DB[:users].insert(name: "Jane Doe", email: "[email protected]") | ||
|
|
||
| put "/sequel/users/1", params: { name: "Jane Smith" } | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
|
|
||
| transaction = transport.events.last.to_h | ||
|
|
||
| update_span = transaction[:spans].find do |span| | ||
| span[:op] == "db.sql.sequel" && span[:description]&.include?("UPDATE") | ||
| end | ||
|
|
||
| expect(update_span).not_to be_nil | ||
| expect(update_span[:description]).to include("UPDATE") | ||
| expect(update_span[:description]).to include("users") | ||
| end | ||
| end | ||
|
|
||
| describe "DELETE queries" do | ||
| it "creates a transaction with Sequel span for delete action" do | ||
| SEQUEL_DB[:users].insert(name: "Delete Me", email: "[email protected]") | ||
|
|
||
| delete "/sequel/users/1" | ||
|
|
||
| expect(response).to have_http_status(:no_content) | ||
|
|
||
| transaction = transport.events.last.to_h | ||
|
|
||
| delete_span = transaction[:spans].find do |span| | ||
| span[:op] == "db.sql.sequel" && span[:description]&.include?("DELETE") | ||
| end | ||
|
|
||
| expect(delete_span).not_to be_nil | ||
| expect(delete_span[:description]).to include("DELETE") | ||
| expect(delete_span[:description]).to include("users") | ||
| end | ||
| end | ||
|
|
||
| describe "exception handling" do | ||
| it "creates both error event and transaction with Sequel span" do | ||
| get "/sequel/exception" | ||
|
|
||
| expect(response).to have_http_status(:internal_server_error) | ||
|
|
||
| expect(transport.events.count).to eq(2) | ||
|
|
||
| error_event = transport.events.first.to_h | ||
| transaction = transport.events.last.to_h | ||
|
|
||
| expect(error_event[:exception][:values].first[:type]).to eq("RuntimeError") | ||
| expect(error_event[:exception][:values].first[:value]).to include("Something went wrong!") | ||
|
|
||
| sequel_span = transaction[:spans].find { |span| span[:op] == "db.sql.sequel" } | ||
| expect(sequel_span).not_to be_nil | ||
| expect(sequel_span[:description]).to include("SELECT") | ||
|
|
||
| expect(error_event.dig(:contexts, :trace, :trace_id)).to eq( | ||
| transaction.dig(:contexts, :trace, :trace_id) | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| describe "span timing" do | ||
| it "records proper start and end timestamps" do | ||
| get "/sequel/users" | ||
|
|
||
| transaction = transport.events.last.to_h | ||
| sequel_span = transaction[:spans].find { |span| span[:op] == "db.sql.sequel" } | ||
|
|
||
| expect(sequel_span[:start_timestamp]).not_to be_nil | ||
| expect(sequel_span[:timestamp]).not_to be_nil | ||
| expect(sequel_span[:start_timestamp]).to be < sequel_span[:timestamp] | ||
| end | ||
| end | ||
|
|
||
| describe "Sequel and ActiveRecord coexistence" do | ||
| it "records spans for both database systems in the same application" do | ||
| Post.create!(title: "Test Post") | ||
|
|
||
| SEQUEL_DB[:users].insert(name: "Sequel User", email: "[email protected]") | ||
|
|
||
| transport.events.clear | ||
|
|
||
| get "/sequel/users" | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
|
|
||
| transaction = transport.events.last.to_h | ||
| sequel_spans = transaction[:spans].select { |span| span[:op] == "db.sql.sequel" } | ||
|
|
||
| expect(sequel_spans.length).to be >= 1 | ||
| expect(sequel_spans.first[:data]["db.system"]).to eq("sqlite") | ||
| end | ||
|
|
||
| it "records ActiveRecord spans separately from Sequel spans" do | ||
| transport.events.clear | ||
|
|
||
| get "/posts" | ||
|
|
||
| expect(response).to have_http_status(:internal_server_error) # raises "foo" in PostsController#index | ||
|
|
||
| transaction = transport.events.last.to_h | ||
|
|
||
| ar_spans = transaction[:spans].select { |span| span[:op] == "db.sql.active_record" } | ||
| sequel_spans = transaction[:spans].select { |span| span[:op] == "db.sql.sequel" } | ||
|
|
||
| expect(ar_spans.length).to be >= 1 | ||
| expect(sequel_spans.length).to eq(0) | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sentry | ||
| module Sequel | ||
| OP_NAME = "db.sql.sequel" | ||
| SPAN_ORIGIN = "auto.db.sequel" | ||
|
|
||
| # Sequel Database extension module that instruments queries | ||
| module DatabaseExtension | ||
| def log_connection_yield(sql, conn, args = nil) | ||
| return super unless Sentry.initialized? | ||
|
|
||
| Sentry.with_child_span(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f, origin: SPAN_ORIGIN) do |span| | ||
| result = super | ||
|
|
||
| if span | ||
| span.set_description(sql) | ||
| span.set_data(Span::DataConventions::DB_SYSTEM, database_type.to_s) | ||
| span.set_data(Span::DataConventions::DB_NAME, opts[:database]) if opts[:database] | ||
| span.set_data(Span::DataConventions::SERVER_ADDRESS, opts[:host]) if opts[:host] | ||
| span.set_data(Span::DataConventions::SERVER_PORT, opts[:port]) if opts[:port] | ||
| end | ||
|
|
||
| result | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| ::Sequel::Database.register_extension(:sentry, Sentry::Sequel::DatabaseExtension) | ||
| end | ||
|
|
||
| Sentry.register_patch(:sequel) do | ||
| ::Sequel::Database.extension(:sentry) if defined?(::Sequel::Database) | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.