Skip to content

Commit

Permalink
Merge pull request #4 from doximity/jh_add_query_types
Browse files Browse the repository at this point in the history
Added db.query_type and db.query_category tags to traces
  • Loading branch information
jeremiahishere authored Jan 18, 2022
2 parents e4ab41f + 5c1919a commit c06f09d
Show file tree
Hide file tree
Showing 29 changed files with 79 additions and 62 deletions.
26 changes: 2 additions & 24 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ executors:
ruby-latest:
resource_class: small
docker:
- image: circleci/ruby:latest
- image: circleci/ruby:2.7
environment:
BUNDLE_VERSION: "~> 1.17"

Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
- v1-bundle-{{ checksum "Gemfile.lock" }}-
- run:
name: Install Ruby Dependencies
command: bundle check --path=vendor/bundle || bundle install --local --frozen --path=vendor/bundle --jobs=4 --retry=3 && bundle clean
command: bundle install --path=vendor/bundle # install every time so we don't have to package vendor/bundle with the gem
- save_cache:
key: v1-bundle-{{ checksum "Gemfile.lock" }}-
paths:
Expand Down Expand Up @@ -91,19 +91,6 @@ workflows:
name: gem-build
requires:
- build
- pre-release-approval:
<<: *pr_only
type: approval
requires:
- gem-build
- gem/publish:
<<: *pr_only
name: gem-publish
to_rubygems: true
pre_release: true
requires:
- pre-release-approval
context: artifact_publishing

final-release:
jobs:
Expand All @@ -115,12 +102,3 @@ workflows:
name: gem-build
requires:
- build
- gem/publish:
<<: *version_tags_only
name: gem-publish
to_rubygems: true
pre_release: false
requires:
- gem-build
context: artifact_publishing

6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
Changelog
=========

## 0.5.0 01/18/2022
* Added db.query_type and db.query_category tags
* Added forwards compatibility code for the connection_config for rails 6.2

## 0.4.0 04/28/2020
* Add SQL sanitizers

## 0.3.0 04/22/2020
* Set up build pipeline with circleci and gem-publisher
* Fixed linting issues
* Renamed gem to `activerecord-instrumentation`
* Renamed gem to `activerecord-instrumentation`
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

# BEFORE changing this file, read https://wiki.doximity.com/articles/gemfile-maintenance
source "https://artifacts.dox.support/repository/gems"
source "https://rubygems.org"

gemspec
49 changes: 18 additions & 31 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
PATH
remote: .
specs:
activerecord-instrumentation (0.4.0)
activerecord-instrumentation (0.5.0)
activerecord
opentracing (~> 0.5)

GEM
remote: https://artifacts.dox.support/repository/gems/
remote: https://rubygems.org/
specs:
activemodel (6.0.2.2)
activesupport (= 6.0.2.2)
activerecord (6.0.2.2)
activemodel (= 6.0.2.2)
activesupport (= 6.0.2.2)
activesupport (6.0.2.2)
activemodel (6.1.4.4)
activesupport (= 6.1.4.4)
activerecord (6.1.4.4)
activemodel (= 6.1.4.4)
activesupport (= 6.1.4.4)
activesupport (6.1.4.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
ast (2.4.0)
concurrent-ruby (1.1.6)
concurrent-ruby (1.1.9)
diff-lcs (1.3)
dox-style (1.3.0)
rubocop (~> 0.77)
rubocop-performance (~> 1.5.1)
rubocop-rails (~> 2.2.0)
rubocop-rspec (~> 1.37.0)
i18n (1.8.2)
i18n (1.8.11)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.4)
minitest (5.14.0)
minitest (5.15.0)
opentracing (0.5.0)
opentracing_test_tracer (0.1.1)
opentracing
parallel (1.19.1)
parser (2.7.1.1)
ast (~> 2.4.0)
rack (2.2.2)
rainbow (3.0.0)
rake (13.0.1)
rdoc (6.2.1)
Expand All @@ -63,30 +57,23 @@ GEM
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.7)
rubocop-performance (1.5.2)
rubocop (>= 0.71.0)
rubocop-rails (2.2.1)
rack (>= 1.1)
rubocop (>= 0.72.0)
rubocop-rspec (1.37.1)
rubocop (>= 0.68.1)
ruby-progressbar (1.10.1)
sdoc (1.1.0)
rdoc (>= 5.0)
sqlite3 (1.4.2)
thread_safe (0.3.6)
tzinfo (1.2.7)
thread_safe (~> 0.1)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (1.6.1)
zeitwerk (2.3.0)
zeitwerk (2.5.3)

PLATFORMS
ruby

DEPENDENCIES
activerecord-instrumentation!
bundler (~> 1.17)
dox-style
opentracing_test_tracer (~> 0.1)
rake (~> 13.0)
rspec (~> 3.9.0)
Expand Down
1 change: 0 additions & 1 deletion activerecord-instrumentation.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.17"
spec.add_development_dependency "dox-style"
spec.add_development_dependency "opentracing_test_tracer", "~> 0.1"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.9.0"
Expand Down
48 changes: 46 additions & 2 deletions lib/active_record/open_tracing/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ class Processor
SPAN_KIND = "client"
DB_TYPE = "sql"

# Used to guess what type of query is running based on the first word of the query
#
# Categories are
# table: Run an action against a table changes the table metadata or configuration
# read: Read from the database
# write: Write or delete records to the database
# unknown: Can't tell the query action from the first word of the query
# not_found: First word of the query is not in this list
QUERY_CATEGORIES = {
alter: "table",
call: "unknown", # run a subquery
create: "table",
delete: "write",
drop: "table",
do: "read",
handler: "table", # table metadata
import: "write",
insert: "write",
load: "write", # covers LOAD XML and LOAD DATA queries
rename: "table",
replace: "write", # insert, on duplicate overwrite
select: "read",
table: "read", # similar to select
truncate: "table",
update: "write",
values: "unknown", # generates rows to use as a table but doesn't hit the database
with: "unknown" # sets up subqueries in preparation for other queries
}

attr_reader :tracer, :sanitizer, :sql_logging_enabled

def initialize(tracer, sanitizer: nil, sql_logging_enabled: true)
Expand Down Expand Up @@ -54,9 +83,22 @@ def tags_for_payload(payload)
}.merge(db_statement(payload))
end

# rubocop:disable Metrics/MethodLength
def db_statement(payload)
sql_logging_enabled ? { "db.statement" => sanitize_sql(payload.fetch(:sql).squish) } : {}
if sql_logging_enabled
query_sql = sanitize_sql(payload.fetch(:sql).squish)
first_word = query_sql.split.first.downcase

{
"db.statement" => query_sql,
"db.query_type" => first_word,
"db.query_category" => QUERY_CATEGORIES[first_word.to_sym] || "not_found"
}
else
{}
end
end
# rubocop:enable Metrics/MethodLength

def sanitize_sql(sql)
sanitizer ? sanitizer.sanitize(sql) : sql
Expand All @@ -76,7 +118,9 @@ def db_instance
end

def connection_config
@connection_config ||= ActiveRecord::Base.connection_config
# Rails 6.2 will deprecate ActiveRecord::Base.connection_config
@connection_config ||=
ActiveRecord::Base.try(:connection_db_config)&.configuration_hash || ActiveRecord::Base.connection_config
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/open_tracing/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module ActiveRecord
module OpenTracing
VERSION = "0.4.0"
VERSION = "0.5.0"
end
end
6 changes: 6 additions & 0 deletions spec/active_record/open_tracing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class User < ActiveRecord::Base
"component" => "ActiveRecord",
"span.kind" => "client",
"db.instance" => "tracer-test",
"db.query_category" => "read",
"db.query_type" => "select",
"db.statement" => 'SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?',
"db.cached" => false,
"db.type" => "sql",
Expand Down Expand Up @@ -65,6 +67,8 @@ class User < ActiveRecord::Base
"component" => "ActiveRecord",
"span.kind" => "client",
"db.instance" => "tracer-test",
"db.query_category" => "read",
"db.query_type" => "select",
"db.statement" => "SELECT COUNT(1) FROM users",
"db.cached" => false,
"db.type" => "sql",
Expand All @@ -90,6 +94,8 @@ class User < ActiveRecord::Base
"component" => "ActiveRecord",
"span.kind" => "client",
"db.instance" => "tracer-test",
"db.query_category" => "read",
"db.query_type" => "select",
"db.statement" => "SELECT * FROM users WHERE email IS NULL",
"db.cached" => false,
"db.type" => "sql",
Expand Down
Binary file removed vendor/cache/activemodel-6.0.2.2.gem
Binary file not shown.
Binary file added vendor/cache/activemodel-6.1.4.4.gem
Binary file not shown.
Binary file removed vendor/cache/activerecord-6.0.2.2.gem
Binary file not shown.
Binary file added vendor/cache/activerecord-6.1.4.4.gem
Binary file not shown.
Binary file removed vendor/cache/activesupport-6.0.2.2.gem
Binary file not shown.
Binary file added vendor/cache/activesupport-6.1.4.4.gem
Binary file not shown.
Binary file removed vendor/cache/concurrent-ruby-1.1.6.gem
Binary file not shown.
Binary file added vendor/cache/concurrent-ruby-1.1.9.gem
Binary file not shown.
Binary file removed vendor/cache/dox-style-1.3.0.gem
Binary file not shown.
Binary file added vendor/cache/i18n-1.8.11.gem
Binary file not shown.
Binary file removed vendor/cache/i18n-1.8.2.gem
Binary file not shown.
Binary file removed vendor/cache/minitest-5.14.0.gem
Binary file not shown.
Binary file added vendor/cache/minitest-5.15.0.gem
Binary file not shown.
Binary file removed vendor/cache/rack-2.2.2.gem
Binary file not shown.
Binary file removed vendor/cache/rubocop-performance-1.5.2.gem
Binary file not shown.
Binary file removed vendor/cache/rubocop-rails-2.2.1.gem
Binary file not shown.
Binary file removed vendor/cache/thread_safe-0.3.6.gem
Binary file not shown.
Binary file removed vendor/cache/tzinfo-1.2.7.gem
Binary file not shown.
Binary file added vendor/cache/tzinfo-2.0.4.gem
Binary file not shown.
Binary file removed vendor/cache/zeitwerk-2.3.0.gem
Binary file not shown.
Binary file added vendor/cache/zeitwerk-2.5.3.gem
Binary file not shown.

0 comments on commit c06f09d

Please sign in to comment.