grape-datadog 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ba4b9a27af459dcef450e036d45bcfb8f41b08f
4
+ data.tar.gz: 599981b5f22f182a9834055ed00338f37b103461
5
+ SHA512:
6
+ metadata.gz: c0a3c9077a3fb3bcd784a6969980a559e68bfbe5ebe96e747a9c41e87059149cf2d0e590671a328bf103658f4a0f5e635cb6c50b06f4d674029ac5c62aa479bb
7
+ data.tar.gz: 77e186ee2d920fc3505e8b4c7b74a5d2481df12eabf0741685c255881eca5b27bac010989c1c8cf722b339424fde634f1b159008cefe67006e39af3d830e3ed4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Artem Chernyshev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ grape-datadog
2
+ =============
3
+
4
+ Datadog stats reporter for [Grape][0], based on code from [this Librato
5
+ gem][1], which is based on [this NewRelic gem][2], using [dogstatsd-ruby][3]
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'grape-datadog'
12
+
13
+ Or install:
14
+
15
+ $ gem install grape-datadog
16
+
17
+ Include it in your Grape API like this:
18
+
19
+ class TestAPI < Grape::API
20
+ use Grape::Datadog::Middleware
21
+
22
+ get 'hello' do
23
+ "Hello World"
24
+ end
25
+ end
26
+
27
+ For full configuration options, please see the [Documentation][4].
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Make a pull request
36
+
37
+ [0]: https://github.com/intridea/grape
38
+ [1]: https://github.com/seanmoon/grape-librato
39
+ [2]: https://github.com/flyerhzm/newrelic-grape
40
+ [3]: https://github.com/DataDog/dogstatsd-ruby
41
+ [4]: http://www.rubydoc.info/gems/grape-datadog
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'rake'
12
+
13
+ require 'rspec/core'
14
+ require 'rspec/core/rake_task'
15
+
16
+ RSpec::Core::RakeTask.new(:spec) do |spec|
17
+ spec.pattern = FileList['spec/**/*_spec.rb']
18
+ end
19
+
20
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grape/datadog/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "grape-datadog"
8
+ s.version = Grape::Datadog::VERSION.dup
9
+ s.authors = ["Artem Chernyshev", "Dimitrij Denissenko"]
10
+ s.email = ["artem.0xD2@gmail.com"]
11
+ s.description = %q{Datadog metrics for grape}
12
+ s.summary = %q{Datadog metrics for grape}
13
+ s.homepage = "https://github.com/bsm/grape-datadog"
14
+
15
+ s.files = `git ls-files`.split($/)
16
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^(spec)/})
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_runtime_dependency(%q<grape>)
21
+ s.add_runtime_dependency(%q<dogstatsd-ruby>)
22
+
23
+ s.add_development_dependency(%q<rake>)
24
+ s.add_development_dependency(%q<bundler>)
25
+ s.add_development_dependency(%q<rack-test>)
26
+ s.add_development_dependency(%q<rspec>, "~> 3.0")
27
+ end
@@ -0,0 +1 @@
1
+ require "grape/datadog"
@@ -0,0 +1,3 @@
1
+ %w|version middleware|.each do |name|
2
+ require "grape/datadog/#{name}"
3
+ end
@@ -0,0 +1,59 @@
1
+ require 'grape'
2
+ require 'statsd'
3
+ require 'socket'
4
+
5
+ module Grape
6
+ module Datadog
7
+
8
+ class Middleware < ::Grape::Middleware::Base
9
+
10
+ # Create a new +Datadog+ middleware instance.
11
+ #
12
+ # ==== Options
13
+ # * <tt>:hostname</tt> - the hostname used for instrumentation, defaults to system hostname, respects +INSTRUMENTATION_HOSTNAME+ env variable
14
+ # * <tt>:metric_name</tt> - the metric name (prefix) to use, defaults to "grape.request"
15
+ # * <tt>:tags</tt> - array of custom tags, these can be plain strings or lambda blocks accepting a rack env instance
16
+ # * <tt>:statsd_host</tt> - the statsD host, defaults to "localhost", respects +STATSD_HOST+ env variable
17
+ # * <tt>:statsd_port</tt> - the statsD port, defaults to 8125, respects +STATSD_PORT+ env variable
18
+ # * <tt>:prefer_global</tt> - if set, tries to find global `$statsd` instance, otherwise connects to +statsd_host+:+statsd_port. Default: true
19
+ def initialize(app, opts = {})
20
+ hostname = opts[:hostname] || ENV['INSTRUMENTATION_HOSTNAME'] || Socket.gethostname
21
+ statsd_host = opts[:statsd_host] || ENV['STATSD_HOST'] || "localhost"
22
+ statsd_port = (opts[:statsd_port] || ENV['STATSD_PORT'] || 8125).to_i
23
+
24
+ @app = app
25
+ @metric = opts[:metric_name] || "grape.request"
26
+ @statsd = opts[:prefer_global] == false || !defined?($statsd) ? ::Statsd.new(statsd_host, statsd_port) : $statsd
27
+ @tags = opts[:tags] || []
28
+ @tags.push "host:#{hostname}"
29
+ end
30
+
31
+ def call(env)
32
+ tags = prepare_tags(env)
33
+ @statsd.time "#{@metric}.time", :tags => tags do
34
+ resp = @app.call(env)
35
+ tags.push "status:#{resp.status}"
36
+ @statsd.increment @metric, :tags => tags
37
+ resp
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def prepare_tags(env)
44
+ path = env['api.endpoint'].routes.first.route_path[1..-1].gsub("/", ".").sub(/\(\.:format\)\z/, "").gsub(/\.:(\w+)/, '.{\1}')
45
+ @tags.map do |tag|
46
+ case tag
47
+ when String
48
+ tag
49
+ when Proc
50
+ tag.call(env)
51
+ end
52
+ end.compact + ["method:#{env[Rack::REQUEST_METHOD]}", "path:#{path}"]
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
59
+
@@ -0,0 +1,5 @@
1
+ module Grape
2
+ module Datadog
3
+ VERSION = "1.1.0"
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Datadog::Middleware do
4
+
5
+ class TestAPI < Grape::API
6
+ use Grape::Datadog::Middleware,
7
+ hostname: "test.host",
8
+ tags: ["custom:tag", lambda {|env| "scheme:#{Rack::Request.new(env).scheme}" }]
9
+
10
+ get 'echo/:key1/:key2' do
11
+ "#{params['key1']} #{params['key2']}"
12
+ end
13
+ end
14
+
15
+ def app; TestAPI; end
16
+
17
+ before { $statsd.buffer.clear }
18
+
19
+ it 'should be configurable' do
20
+ subject = described_class.new(nil, hostname: "test.host")
21
+ expect(subject.instance_variable_get(:@tags)).to eq(["host:test.host"])
22
+ expect(subject.instance_variable_get(:@statsd)).to be_instance_of(MockStatsd)
23
+
24
+ subject = described_class.new(nil, prefer_global: false)
25
+ expect(subject.instance_variable_get(:@statsd)).to be_instance_of(Statsd)
26
+ end
27
+
28
+ it 'should send an increment and timing event for each request' do
29
+ get '/echo/1/1234'
30
+ expect(last_response.status).to eq(200)
31
+ expect(last_response.body).to eq('1 1234')
32
+
33
+ expect($statsd.buffer).to eq([
34
+ "grape.request:1|c|#custom:tag,scheme:http,host:test.host,method:GET,path:echo.{key1}.{key2},status:200",
35
+ "grape.request.time:333|ms|#custom:tag,scheme:http,host:test.host,method:GET,path:echo.{key1}.{key2},status:200"
36
+ ])
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Datadog do
4
+ it "has a version" do
5
+ expect(Grape::Datadog::VERSION).to be_instance_of(String)
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rack/test'
5
+
6
+ RSpec.configure do |config|
7
+ config.include Rack::Test::Methods
8
+ end
9
+
10
+ require 'grape'
11
+ require 'grape-datadog'
12
+
13
+ class MockStatsd < Statsd
14
+ def timing(stat, ms, opts={}); super(stat, 333, opts); end
15
+ def flush_buffer; end
16
+ alias :send_stat :send_to_buffer
17
+ end
18
+
19
+ $statsd = MockStatsd.new(nil, nil, {}, 10000)
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-datadog
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artem Chernyshev
8
+ - Dimitrij Denissenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-09-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: grape
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: dogstatsd-ruby
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rack-test
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '3.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '3.0'
98
+ description: Datadog metrics for grape
99
+ email:
100
+ - artem.0xD2@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - grape-datadog.gemspec
111
+ - lib/grape-datadog.rb
112
+ - lib/grape/datadog.rb
113
+ - lib/grape/datadog/middleware.rb
114
+ - lib/grape/datadog/version.rb
115
+ - spec/grape/datadog/middleware_spec.rb
116
+ - spec/grape/datadog/version_spec.rb
117
+ - spec/spec_helper.rb
118
+ homepage: https://github.com/bsm/grape-datadog
119
+ licenses: []
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.7
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Datadog metrics for grape
141
+ test_files:
142
+ - spec/grape/datadog/middleware_spec.rb
143
+ - spec/grape/datadog/version_spec.rb
144
+ - spec/spec_helper.rb