Skip to content

Commit

Permalink
Add ActiveRecord tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
indrekj committed Jul 8, 2018
0 parents commit 4ddbd28
Show file tree
Hide file tree
Showing 17 changed files with 478 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
/tracer-test

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
43 changes: 43 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require: rubocop-rspec

Style/Documentation:
Enabled: no

Style/IfUnlessModifier:
Enabled: no

RSpec/NestedGroups:
Max: 4

RSpec/ExampleLength:
Enabled: no

RSpec/MultipleExpectations:
Enabled: no

Metrics/BlockLength:
Enabled: no

Metrics/MethodLength:
Enabled: no

Metrics/AbcSize:
Enabled: no

Metrics/ClassLength:
Enabled: no

Metrics/ParameterLists:
Enabled: no

Lint/UnusedMethodArgument:
Enabled: no

Style/FrozenStringLiteralComment:
Enabled: yes
EnforcedStyle: always
Include:
- 'lib/**/*'

Metrics/LineLength:
Max: 120
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.4.1
before_install: gem install bundler -v 1.16.2
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in activerecord-tracer.gemspec
gemspec
81 changes: 81 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
PATH
remote: .
specs:
active_record-tracer (0.1.0)
activerecord (~> 5.0)
opentracing (~> 0.3)

GEM
remote: https://rubygems.org/
specs:
activemodel (5.2.0)
activesupport (= 5.2.0)
activerecord (5.2.0)
activemodel (= 5.2.0)
activesupport (= 5.2.0)
arel (>= 9.0)
activesupport (5.2.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
ast (2.4.0)
concurrent-ruby (1.0.5)
diff-lcs (1.3)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
minitest (5.11.3)
opentracing (0.4.1)
opentracing_test_tracer (0.1.0)
opentracing
parallel (1.12.1)
parser (2.5.1.0)
ast (~> 2.4.0)
powerpack (0.1.2)
rainbow (3.0.0)
rake (10.5.0)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
rubocop (0.54.0)
parallel (~> 1.10)
parser (>= 2.5)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
rubocop-rspec (1.24.0)
rubocop (>= 0.53.0)
ruby-progressbar (1.9.0)
sqlite3 (1.3.13)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.4.0)

PLATFORMS
ruby

DEPENDENCIES
active_record-tracer!
bundler (~> 1.16)
opentracing_test_tracer (~> 0.1)
rake (~> 10.0)
rspec (~> 3.0)
rubocop (~> 0.54.0)
rubocop-rspec (~> 1.24.0)
sqlite3 (~> 1.3.13)

BUNDLED WITH
1.16.2
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Indrek Juhkam

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.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ActiveRecord::Tracer

Adds OpenTracing instrumentation to ActiveRecord

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'active_record-tracer'
```

## Usage

```ruby
require 'opentracing'
OpenTracing.global_tracer = TracerImplementation.new

require 'active_record/tracer'
ActiveRecord::Tracer.instrument
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activerecord-tracer.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'

RSpec::Core::RakeTask.new(:spec)

RuboCop::RakeTask.new(:rubocop)

task default: %i[rubocop spec]
33 changes: 33 additions & 0 deletions active_record-tracer.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'active_record/tracer/version'

Gem::Specification.new do |spec|
spec.name = 'active_record-tracer'
spec.version = ActiveRecord::Tracer::VERSION
spec.authors = ['SaleMove TechMovers']
spec.email = ['[email protected]']

spec.summary = 'ActiveRecord OpenTracing tracer'
spec.description = ''
spec.homepage = ''
spec.license = 'MIT'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'opentracing_test_tracer', '~> 0.1'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 0.54.0'
spec.add_development_dependency 'rubocop-rspec', '~> 1.24.0'
spec.add_development_dependency 'sqlite3', '~> 1.3.13'

spec.add_dependency 'activerecord', '~> 5.0'
spec.add_dependency 'opentracing', '~> 0.3'
end
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'active_record/tracer'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require 'irb'
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
21 changes: 21 additions & 0 deletions lib/active_record/tracer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'active_record'
require 'opentracing'

require 'active_record/tracer/version'
require 'active_record/tracer/processor'

module ActiveRecord
module Tracer
def self.instrument(tracer: OpenTracing.global_tracer)
processor = Processor.new(tracer)

ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
processor.call(*args)
end

self
end
end
end
63 changes: 63 additions & 0 deletions lib/active_record/tracer/processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

module ActiveRecord
module Tracer
class Processor
DEFAULT_OPERATION_NAME = 'sql.query'.freeze
COMPONENT_NAME = 'ActiveRecord'.freeze
SPAN_KIND = 'client'.freeze
DB_TYPE = 'sql'.freeze

def initialize(tracer)
@tracer = tracer
end

def call(_event_name, start, finish, _id, payload)
span = @tracer.start_span(
payload[:name] || DEFAULT_OPERATION_NAME,
start_time: start,
tags: {
component: COMPONENT_NAME,
:'span.kind' => SPAN_KIND,
:'db.instance' => db_instance,
:'db.cached' => payload.fetch(:cached, false),
:'db.statement' => payload.fetch(:sql).squish,
:'db.type' => DB_TYPE
}
)

if (exception = payload[:exception_object])
span.set_tag(:error, true)
span.log_kv(
event: 'error',
:'error.kind' => exception.class.to_s,
:'error.object' => exception,
message: exception.message,
stack: exception.backtrace.join("\n")
)
end

span.finish(end_time: finish)
end

private

def db_instance
@db_instance ||= begin
connection_config = ActiveRecord::Base.connection_config
username = connection_config[:username]
host = connection_config[:host]
database = connection_config.fetch(:database)
vendor = connection_config.fetch(:adapter)

str = String.new('')
str << "#{vendor}://"
str << username if username
str << "@#{host}" if host
str << "/#{database}"
str
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/active_record/tracer/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module ActiveRecord
module Tracer
VERSION = '0.1.0'.freeze
end
end
Loading

0 comments on commit 4ddbd28

Please sign in to comment.