datadog_proxy 0.0.1

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: ee6baa026168cbdd7dd4cd683e469bdc23e08bbf
4
+ data.tar.gz: eb7bfb149b18c23817b651d975cafdbbb4c63498
5
+ SHA512:
6
+ metadata.gz: be44bc1e51f7657ab4cf20636e9393aabeaa0b345a65a2f95ae64db0517d0560a208e90220c3679a825acb36e31713d7617e5f7d1344c458df5141d7e8ea4157
7
+ data.tar.gz: b5da5c589bdb031f2f0b8761058e6180d8fd3d9af4786ab59c0649891fc158b832fb7db47bfd6efdd9377a870d13150081a6765adbfe761110794053a51c6f24
data/.env.example ADDED
@@ -0,0 +1,3 @@
1
+ # DATADOG_API_KEY=apikey
2
+ # DATADOG_APP_KEY=appkey
3
+
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in datadog_proxy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ryota Arai
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,29 @@
1
+ # DatadogProxy
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'datadog_proxy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install datadog_proxy
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/datadog_proxy/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/config.ru ADDED
@@ -0,0 +1,4 @@
1
+ require 'datadog_proxy'
2
+
3
+ run DatadogProxy::App.new
4
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'datadog_proxy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "datadog_proxy"
8
+ spec.version = DatadogProxy::VERSION
9
+ spec.authors = ["Ryota Arai"]
10
+ spec.email = ["ryota-arai@cookpad.com"]
11
+ spec.summary = %q{A proxy to datadog API server.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'sinatra', '~> 1.4.5'
21
+ spec.add_dependency 'dogapi', '~> 1.10.0'
22
+ spec.add_dependency 'dotenv', '~> 0.11.1'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rack-test"
27
+ spec.add_development_dependency "rspec", "~> 3.0.0"
28
+ end
@@ -0,0 +1,10 @@
1
+ require "datadog_proxy/version"
2
+ require "datadog_proxy/app"
3
+ require "datadog_proxy/datadog_client"
4
+
5
+ require 'dotenv'
6
+ Dotenv.load
7
+
8
+ module DatadogProxy
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,41 @@
1
+ require 'datadog_proxy'
2
+ require 'sinatra'
3
+
4
+ module DatadogProxy
5
+ class App < Sinatra::Application
6
+ def initialize(*args)
7
+ super(*args)
8
+ end
9
+
10
+ def client
11
+ p :hello
12
+ @_client ||= DatadogClient.new(ENV['DATADOG_API_KEY'], ENV['DATADOG_APP_KEY'])
13
+ end
14
+
15
+ get '/' do
16
+ <<-EOMD
17
+ # Datadog Proxy
18
+ ## Endpoints
19
+ ### GET /graphs/snapshot
20
+ #### Parameters
21
+ * query
22
+ * start: seconds since unix epoch
23
+ * end: seconds since unix epoch
24
+ * duration: seconds
25
+ EOMD
26
+ end
27
+
28
+ get '/graphs/snapshot' do
29
+ options = {}
30
+ options[:query] = params[:query]
31
+ options[:start] = Time.at(params[:start].to_i) if params[:start]
32
+ options[:end] = Time.at(params[:end].to_i) if params[:end]
33
+ options[:duration] = params[:duration].to_i if params[:duration]
34
+
35
+ url = client.graph_snapshot_url(options)
36
+
37
+ redirect url, 302
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,55 @@
1
+ require 'datadog_proxy'
2
+ require 'dogapi'
3
+
4
+ module DatadogProxy
5
+ class DatadogClient
6
+ class Error < StandardError; end
7
+
8
+ def initialize(api_key, app_key)
9
+ @client = Dogapi::Client.new(api_key, app_key)
10
+ @graph_snapshot_url_cache = {}
11
+ end
12
+
13
+ def graph_snapshot_url(options)
14
+ # TODO: cap cache size or use external storage like memcached
15
+ @graph_snapshot_url_cache[options.hash] ||= _graph_snapshot_url(options)
16
+
17
+ # Append time to avoid the graph is cached.
18
+ "#{@graph_snapshot_url_cache[options.hash]}?#{Time.now.to_i}"
19
+ end
20
+
21
+ private
22
+
23
+ def _graph_snapshot_url(options)
24
+ query = options[:query]
25
+ raise Error, "query is not supplied" unless query
26
+
27
+ case
28
+ when options[:start] && options[:end]
29
+ start_time = options[:start]
30
+ end_time = options[:end]
31
+ when options[:start] && options[:duration]
32
+ start_time = options[:start]
33
+ end_time = start_time + options[:duration]
34
+ when options[:end] && options[:duration]
35
+ end_time = options[:end]
36
+ start_time = end_time - options[:duration]
37
+ else
38
+ raise Error, "Two of start, end and duration are necessary at least."
39
+ end
40
+
41
+ response = @client.graph_snapshot(
42
+ query, start_time.to_i, end_time.to_i
43
+ )
44
+
45
+ if response[0] != '200'
46
+ # error
47
+ raise Error, "Failed to get a snapshot."
48
+ end
49
+
50
+ response[1]['snapshot_url']
51
+ end
52
+ end
53
+ end
54
+
55
+
@@ -0,0 +1,3 @@
1
+ module DatadogProxy
2
+ VERSION = "0.0.1"
3
+ end
data/spec/app_spec.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Datadog Proxy' do
4
+ include Rack::Test::Methods
5
+
6
+ def app
7
+ DatadogProxy::App
8
+ end
9
+
10
+ let(:client) do
11
+ double(:client)
12
+ end
13
+
14
+ before do
15
+ allow_any_instance_of(DatadogProxy::App).to receive(:client).and_return(client)
16
+ end
17
+
18
+ describe "GET /" do
19
+ it "returns docuement in markdown" do
20
+ get '/'
21
+ expect(last_response).to be_ok
22
+ expect(last_response.body).to match(/^# Datadog Proxy/)
23
+ end
24
+ end
25
+
26
+ describe "GET /graphs/snapshot" do
27
+ context "with query, start and end" do
28
+ it "returns the url of a graph" do
29
+ expect(client).to receive(:graph_snapshot_url).with(
30
+ {
31
+ query: 'query',
32
+ start: Time.at(0),
33
+ end: Time.at(1024),
34
+ }
35
+ ).and_return('http://example.com/graph')
36
+
37
+ get '/graphs/snapshot', query: "query", start: "0", end: "1024"
38
+ expect(last_response.status).to eq(302)
39
+ expect(last_response.header['Location']).to eq('http://example.com/graph')
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,83 @@
1
+ require 'rack/test'
2
+ require 'datadog_proxy'
3
+
4
+ ENV['RACK_ENV'] = 'test'
5
+
6
+ # This file was generated by the `rspec --init` command. Conventionally, all
7
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
8
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
9
+ # file to always be loaded, without a need to explicitly require it in any files.
10
+ #
11
+ # Given that it is always loaded, you are encouraged to keep this file as
12
+ # light-weight as possible. Requiring heavyweight dependencies from this file
13
+ # will add to the boot time of your test suite on EVERY test run, even for an
14
+ # individual file that may not need all of that loaded. Instead, make a
15
+ # separate helper file that requires this one and then use it only in the specs
16
+ # that actually need it.
17
+ #
18
+ # The `.rspec` file also contains a few flags that are not defaults but that
19
+ # users commonly want.
20
+ #
21
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
22
+ RSpec.configure do |config|
23
+ # The settings below are suggested to provide a good initial experience
24
+ # with RSpec, but feel free to customize to your heart's content.
25
+ =begin
26
+ # These two settings work together to allow you to limit a spec run
27
+ # to individual examples or groups you care about by tagging them with
28
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
29
+ # get run.
30
+ config.filter_run :focus
31
+ config.run_all_when_everything_filtered = true
32
+
33
+ # Many RSpec users commonly either run the entire suite or an individual
34
+ # file, and it's useful to allow more verbose output when running an
35
+ # individual spec file.
36
+ if config.files_to_run.one?
37
+ # Use the documentation formatter for detailed output,
38
+ # unless a formatter has already been configured
39
+ # (e.g. via a command-line flag).
40
+ config.default_formatter = 'doc'
41
+ end
42
+
43
+ # Print the 10 slowest examples and example groups at the
44
+ # end of the spec run, to help surface which specs are running
45
+ # particularly slow.
46
+ config.profile_examples = 10
47
+
48
+ # Run specs in random order to surface order dependencies. If you find an
49
+ # order dependency and want to debug it, you can fix the order by providing
50
+ # the seed, which is printed after each run.
51
+ # --seed 1234
52
+ config.order = :random
53
+
54
+ # Seed global randomization in this process using the `--seed` CLI option.
55
+ # Setting this allows you to use `--seed` to deterministically reproduce
56
+ # test failures related to randomization by passing the same `--seed` value
57
+ # as the one that triggered the failure.
58
+ Kernel.srand config.seed
59
+
60
+ # rspec-expectations config goes here. You can use an alternate
61
+ # assertion/expectation library such as wrong or the stdlib/minitest
62
+ # assertions if you prefer.
63
+ config.expect_with :rspec do |expectations|
64
+ # Enable only the newer, non-monkey-patching expect syntax.
65
+ # For more details, see:
66
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
67
+ expectations.syntax = :expect
68
+ end
69
+
70
+ # rspec-mocks config goes here. You can use an alternate test double
71
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
72
+ config.mock_with :rspec do |mocks|
73
+ # Enable only the newer, non-monkey-patching expect syntax.
74
+ # For more details, see:
75
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
76
+ mocks.syntax = :expect
77
+
78
+ # Prevents you from mocking or stubbing a method that does not exist on
79
+ # a real object. This is generally recommended.
80
+ mocks.verify_partial_doubles = true
81
+ end
82
+ =end
83
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datadog_proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryota Arai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: dogapi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.10.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.10.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: dotenv
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.11.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.11.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.0.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.0.0
111
+ description:
112
+ email:
113
+ - ryota-arai@cookpad.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".env.example"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - config.ru
126
+ - datadog_proxy.gemspec
127
+ - lib/datadog_proxy.rb
128
+ - lib/datadog_proxy/app.rb
129
+ - lib/datadog_proxy/datadog_client.rb
130
+ - lib/datadog_proxy/version.rb
131
+ - spec/app_spec.rb
132
+ - spec/spec_helper.rb
133
+ homepage: ''
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.2.2
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: A proxy to datadog API server.
157
+ test_files:
158
+ - spec/app_spec.rb
159
+ - spec/spec_helper.rb