datadog_proxy 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 71f1f0b5b195dceb4d7654d6e89464a30511b001
4
- data.tar.gz: 9fa3cb4d71bc6c6e0ebef24a574cb7bb251cb2f0
3
+ metadata.gz: d3eb27154e8cc8f437356761bcd3b4ead2f4662c
4
+ data.tar.gz: fe6c1e54b4decfadd4eddb5c6b612486d996d419
5
5
  SHA512:
6
- metadata.gz: 57b93293e11f86a679a7e9fc59e4dc4ee4e80851bec2fa76cf475fcf75fd5d829c3b0b06539439cfe134479cdb577f49ee39586b94e9b8183cf11f1bad4e596c
7
- data.tar.gz: 362e0b215762e8208494fd477ccd562b8f51b5d8e52701f408c84678240684d2414ff451c1145f972eca7b29ca5830b60279c9759dae28a8aee1aec7a2097165
6
+ metadata.gz: 9727e86e1191f043f8bd59ab69427442b013ad973521f06aea83c9b68b92d1812a97d380a8c8e515f3b8e30563918322bd9ac53b19ec6358ed6568ea24ec5795
7
+ data.tar.gz: fc8064c5c1dd47c182619fe5229d30f8ef136764aae823f30c536ce3c32032f52f7ed1cd0c669baadc4cb2af22b4fdbf54adf62eba92294c41ea9e9f2923ad64
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_dependency 'sinatra', '~> 1.4.5'
21
21
  spec.add_dependency 'dogapi', '~> 1.10.0'
22
22
  spec.add_dependency 'dotenv', '~> 0.11.1'
23
+ spec.add_dependency 'chronic', '~> 0.10.2'
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1.6"
25
26
  spec.add_development_dependency "rake"
@@ -1,14 +1,16 @@
1
1
  require 'datadog_proxy'
2
2
  require 'sinatra'
3
+ require 'chronic'
3
4
 
4
5
  module DatadogProxy
5
6
  class App < Sinatra::Application
7
+ class Error < StandardError; end
8
+
6
9
  def initialize(*args)
7
10
  super(*args)
8
11
  end
9
12
 
10
13
  def client
11
- p :hello
12
14
  @_client ||= DatadogClient.new(ENV['DATADOG_API_KEY'], ENV['DATADOG_APP_KEY'])
13
15
  end
14
16
 
@@ -28,14 +30,25 @@ module DatadogProxy
28
30
  get '/graphs/snapshot' do
29
31
  options = {}
30
32
  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[:start] = _parse_time(params[:start]) if params[:start]
34
+ options[:end] = _parse_time(params[:end]) if params[:end]
33
35
  options[:duration] = params[:duration].to_i if params[:duration]
34
-
36
+ p options
35
37
  url = client.graph_snapshot_url(options)
36
38
 
37
39
  redirect url, 302
38
40
  end
41
+
42
+ private
43
+ def _parse_time(str)
44
+ if /^\d+$/ =~ str
45
+ Time.at(str.to_i)
46
+ else
47
+ time = Chronic.parse(str)
48
+ return time if time
49
+ raise Error, "Cannot parse time (#{str})"
50
+ end
51
+ end
39
52
  end
40
53
  end
41
54
 
@@ -1,5 +1,7 @@
1
1
  require 'datadog_proxy'
2
2
  require 'dogapi'
3
+ require 'timeout'
4
+ require 'net/https'
3
5
 
4
6
  module DatadogProxy
5
7
  class DatadogClient
@@ -7,15 +9,10 @@ module DatadogProxy
7
9
 
8
10
  def initialize(api_key, app_key)
9
11
  @client = Dogapi::Client.new(api_key, app_key)
10
- @graph_snapshot_url_cache = {}
11
12
  end
12
13
 
13
14
  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}"
15
+ _graph_snapshot_url(options)
19
16
  end
20
17
 
21
18
  private
@@ -47,7 +44,28 @@ module DatadogProxy
47
44
  raise Error, "Failed to get a snapshot."
48
45
  end
49
46
 
50
- response[1]['snapshot_url']
47
+ snapshot_url = response[1]['snapshot_url']
48
+ timeout(10) do
49
+ _wait_for_generating(snapshot_url)
50
+ end
51
+
52
+ snapshot_url
53
+ end
54
+
55
+ def _wait_for_generating(url)
56
+ while true
57
+ uri = URI.parse(url)
58
+ req = Net::HTTP::Head.new(uri.path)
59
+ http = Net::HTTP.new(uri.host, uri.port)
60
+ http.use_ssl = true
61
+ res = http.start do
62
+ http.request(req)
63
+ end
64
+ if res.code == '200' && res["Content-Length"].to_i > 1024
65
+ return
66
+ end
67
+ sleep 1
68
+ end
51
69
  end
52
70
  end
53
71
  end
@@ -1,3 +1,3 @@
1
1
  module DatadogProxy
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datadog_proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryota Arai
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.11.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: chronic
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.2
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement