mongodb-instrumentation 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 268adbea20ad896adceac9cda531c99d3fb616f2
4
+ data.tar.gz: da508f03465be829d2fab469182ac21b2e1d65d6
5
+ SHA512:
6
+ metadata.gz: ab6ed51e8e89dd95637acdb3fb445e3da9667a6d5fffaaf314d8e4d0734b3d3388b80083b81e9030cf4a39c90b4b6da7bf43f5068f3d141a7b21c425bfc0afcd
7
+ data.tar.gz: 332124ec50735a62b42efa54451a174f04d1771e3f307f2b1351a4a580def54c466c8bd6378850be17cb518d413df6430d6286cedf50b01070c13b3948abee81
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise 'mongo-latest' do
2
+ gem 'mongo'
3
+ end
4
+
5
+ appraise 'mongo-2.5.0' do
6
+ gem 'mongo', '2.5.0'
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mongodb-instrumentation.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ashwin Chandrasekar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Mongodb::Instrumentation
2
+
3
+ This gem provides auto-instrumentation for the MongoDB Ruby Driver by subscribing to Monitoring notifications.
4
+
5
+ ## Supported Versions
6
+
7
+ - MRI 2.0 and newer
8
+ - mongo 2.1 and newer
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'mongodb-instrumentation'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install mongodb-instrumentation
25
+
26
+ ## Usage
27
+
28
+ To enable instrumentation, simply add this line of code:
29
+
30
+ ```ruby
31
+ require 'mongodb/instrumentation'
32
+
33
+ MongoDB::Instrumentation.instrument
34
+ ```
35
+
36
+ The `instrument` method optionally takes a `tracer` argument to explicitly
37
+ set the tracer to use. If not provided, the instrumentation will default to the
38
+ OpenTracing global tracer.
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ 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).
45
+
46
+ ### Unit tests
47
+
48
+ Run the RSpec tests with
49
+
50
+ ```bash
51
+ bundle exec rspec
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/signalfx/ruby-mongodb-instrumentation.
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mongodb/instrumentation"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ require "mongodb/instrumentation/version"
2
+ require "mongodb/instrumentation/command_subscriber"
3
+ require "opentracing"
4
+
5
+ module MongoDB
6
+ module Instrumentation
7
+ class << self
8
+ def instrument(tracer: OpenTracing.global_tracer)
9
+ Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND, MongoDB::Instrumentation::CommandSubscriber.new(tracer: tracer))
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ module MongoDB
2
+ module Instrumentation
3
+ class CommandSubscriber
4
+
5
+ attr_reader :requests
6
+
7
+ def initialize(tracer: OpenTracing.global_tracer)
8
+ @tracer = tracer
9
+
10
+ @requests = {}
11
+ end
12
+
13
+ def started(event)
14
+ # start command span
15
+ tags = {
16
+ # opentracing tags
17
+ 'component' => 'ruby-mongodb',
18
+ 'db.instance' => event.database_name,
19
+ 'db.statement' => event.command,
20
+ 'db.type' => 'mongo',
21
+ 'span.kind' => 'client',
22
+
23
+ # extra info
24
+ 'mongo.command.name' => event.command_name,
25
+ 'mongo.operation.id' => event.operation_id,
26
+ 'mongo.request.id' => event.request_id,
27
+ }
28
+ span =@tracer.start_span(event.command_name, tags: tags)
29
+
30
+ @requests[event.request_id] = span
31
+ end
32
+
33
+ def succeeded(event)
34
+ return if @requests[event.request_id].nil?
35
+
36
+ # tag the reported duration, in case it differs from what we saw
37
+ # through the notifications times
38
+ span = @requests[event.request_id]
39
+ span.set_tag("took.ms", event.duration * 1000)
40
+
41
+ span.finish()
42
+ @requests.delete(event.request_id)
43
+ end
44
+
45
+ def failed(event)
46
+ return if @requests[event.request_id].nil?
47
+
48
+ # tag the reported duration and any error message that came through
49
+ span = @requests[event.request_id]
50
+ span.set_tag("took.ms", event.duration * 1000)
51
+ span.set_tag("error", true)
52
+ span.log_kv(key: "message", value: event.message)
53
+
54
+ span.finish()
55
+ @requests.delete(event.request_id)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ module Mongodb
2
+ module Instrumentation
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mongodb/instrumentation/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongodb-instrumentation"
8
+ spec.version = Mongodb::Instrumentation::VERSION
9
+ spec.authors = ["Ashwin Chandrasekar"]
10
+ spec.email = ["achandrasekar@signalfx.com"]
11
+
12
+ spec.summary = %q{Instrumentation for MongoDB}
13
+ spec.description = %q{OpenTracing auto-instrumentation for applications using the MongoDB Ruby driver.}
14
+ spec.homepage = "http://github.com/signalfx/ruby-mongodb-instrumentation"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "opentracing_test_tracer", "~> 0.1"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ spec.add_development_dependency "appraisal", "~> 2.2"
40
+ spec.add_development_dependency "mongo", "~> 2.6.2"
41
+ end
data/test_app/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'jaeger-client'
4
+ gem 'mongo'
5
+ gem 'mongodb-instrumentation'
data/test_app/test.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'jaeger/client'
2
+ require 'jaeger/client/http_sender'
3
+ require 'mongo'
4
+ require 'mongodb/instrumentation'
5
+
6
+ def list_all(collection)
7
+ collection.find.each do |document|
8
+ puts document
9
+ end
10
+ end
11
+
12
+ # set up the exporter
13
+ ingest_url = "http://localhost:14268/api/traces"
14
+ service_name = "mongodb-test"
15
+ headers = { }
16
+ encoder = Jaeger::Client::Encoders::ThriftEncoder.new(service_name: service_name)
17
+ http_sender = Jaeger::Client::HttpSender.new(url: ingest_url, headers: headers, encoder: encoder)
18
+ OpenTracing.global_tracer = Jaeger::Client.build(service_name: service_name, sender: http_sender)
19
+
20
+ # set up the instrumentation
21
+ MongoDB::Instrumentation.instrument
22
+
23
+ # suppress the logs, since the default makes reading the output difficult
24
+ Mongo::Logger.logger.level = ::Logger::FATAL
25
+
26
+ # create a connection to the local mongo instance
27
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'testdb')
28
+
29
+ # print collections directly
30
+ client.collections.each { |coll| puts coll.name }
31
+
32
+ # print collections by accessing a database
33
+ db = client.database
34
+ puts db.collection_names
35
+
36
+ # insert document
37
+ collection = client[:test_collection]
38
+ doc = { name: 'Test', value: 'TestValue'}
39
+ result = collection.insert_one(doc)
40
+ puts "Inserted #{result.n} document"
41
+
42
+ # insert many documents
43
+ docs = []
44
+ for i in 1..10 do
45
+ docs << { name: "Test#{i}", value: "Value#{i}"}
46
+ end
47
+ result = collection.insert_many(docs)
48
+ puts "Inserted #{result.inserted_count} documents"
49
+
50
+ # query all documents
51
+ list_all(collection)
52
+
53
+ # update
54
+ result = collection.update_many( {}, { '$set' => { 'value' => 'test_updated' } } )
55
+ puts "Modified #{result.modified_count} documents"
56
+
57
+ # query
58
+ list_all(collection)
59
+
60
+ # delete document
61
+ result = collection.delete_one( { name: 'Test' } )
62
+ puts "Deleted #{result.deleted_count}"
63
+ list_all(collection)
64
+ result = collection.delete_many( { name: /Test/ } )
65
+ puts "Deleted #{result.deleted_count}"
66
+
67
+ list_all(collection)
68
+
69
+ documents = collection.find(:name => 'Test').skip(10).limit(10)
70
+
71
+ # delete the collection and close the connection
72
+ collection.drop
73
+ client.close
74
+
75
+ # sleep to let the Jaeger spans finish sending
76
+ sleep(10)
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongodb-instrumentation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ashwin Chandrasekar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: opentracing_test_tracer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: appraisal
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mongo
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.6.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.6.2
97
+ description: OpenTracing auto-instrumentation for applications using the MongoDB Ruby
98
+ driver.
99
+ email:
100
+ - achandrasekar@signalfx.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Appraisals
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/mongodb/instrumentation.rb
114
+ - lib/mongodb/instrumentation/command_subscriber.rb
115
+ - lib/mongodb/instrumentation/version.rb
116
+ - mongodb-instrumentation.gemspec
117
+ - test_app/Gemfile
118
+ - test_app/test.rb
119
+ homepage: http://github.com/signalfx/ruby-mongodb-instrumentation
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.6.13
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Instrumentation for MongoDB
143
+ test_files: []