thirtysix 0.0.2 → 0.0.3

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: 80a87d7c92d6d215816ac100818dac9e0ac9ee1e
4
- data.tar.gz: c416ffc956da0afd1eb3105b2509975ce2729335
3
+ metadata.gz: 0ebddafbe1045a12501e5df96fa6d2b15e6c5c72
4
+ data.tar.gz: 711d3d9cea916f2c5ae650d3378d88767b176755
5
5
  SHA512:
6
- metadata.gz: 354aaaa56a7eab627e3ffd95235b0b5611f6f54cfa5404abf95f4cfbea6415dd501dfbae24a5c590c74d65101347b46baa4ec61173f155c33b932977ea18700e
7
- data.tar.gz: 20fe48621762137449d0af99e720da8d8d1b023247ee08246daf2a24bec5ae889bac8d9d4332ab5d8b310673bba381ded39a815b5c0de960793755622390aa35
6
+ metadata.gz: d5573dab0362dbed7e830a8738ca0853cc3c41690a719e03df7890d883ab323aeb959ca3c2308a8b65d08c0ce08a83aeebdde12e8fd372a9784995a5c5bffed3
7
+ data.tar.gz: 1fbdedda992ad6855922ea3cccec2bead0745d41175eaf98fbb6b4982c8ba04ba68bbbb6ebf92fbc8a38c5b3f6b55a717a6d3fd1b5ed69cb9e159e60261d6fb9
@@ -0,0 +1,80 @@
1
+ require 'objspace'
2
+ require 'thirtysix/app_request'
3
+ require 'thirtysix/rack/railtie' if defined?(Rails)
4
+
5
+ module Thirtysix
6
+ module Rack
7
+ class Middleware
8
+ def initialize(app)
9
+ puts "Thirtysix: Initializing Middleware"
10
+ @app = app
11
+ end
12
+
13
+ def call(env)
14
+ start_time = Time.now
15
+ start_memory = get_memory
16
+ start_objects = ObjectSpace.count_objects_size
17
+
18
+ status, headers, body = @app.call(env)
19
+
20
+ if env["ORIGINAL_FULLPATH"].include? "assets"
21
+ puts "Thirtysix: Skipping instrumentation for assets requests"
22
+ else
23
+ end_time = Time.now
24
+ end_memory = get_memory
25
+ end_objects = ObjectSpace.count_objects_size
26
+
27
+ app_request = Thirtysix::AppRequest.new
28
+ app_request.request_time_in_milliseconds = ((end_time - start_time) * 1000).to_i
29
+ app_request.memory_in_mb = end_memory - start_memory
30
+ app_request.build(start_objects, end_objects, env)
31
+
32
+ push app_request
33
+ end
34
+
35
+ [status, headers, body]
36
+ end
37
+
38
+ #
39
+ # Returns current process' memory usage in MB
40
+ # Returns nil if any error occurred
41
+ # Only works on Darwin10
42
+ # Shamelessly stolen from:
43
+ # https://github.com/newrelic/rpm/blob/master/lib/new_relic/agent/samplers/memory_sampler.rb
44
+ #
45
+ def get_memory
46
+ (`ps -o rss #{$$}`.split("\n")[1].to_f / 1024.0) rescue nil
47
+ end
48
+
49
+ def thirtysix_api_key
50
+ ENV['THIRTYSIX_API_KEY']
51
+ end
52
+
53
+ def thirtysix_url
54
+ ENV['THIRTYSIX_URL']
55
+ end
56
+
57
+ def thirtysix_path
58
+ "/api/app_requests"
59
+ end
60
+
61
+ def endpoint_uri
62
+ @endpoint_uri ||= URI(thirtysix_url + thirtysix_path)
63
+ end
64
+
65
+ def push(app_request)
66
+ begin
67
+ http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
68
+ http.open_timeout = 0.2
69
+ http.read_timeout = 0.2
70
+ headers = {'Content-Type' => 'text/json', 'X-THIRTYSIX-API-KEY' => thirtysix_api_key}
71
+ request = Net::HTTP::Post.new(endpoint_uri.request_uri, headers)
72
+ request.body = app_request.to_json
73
+ response = http.request(request)
74
+ rescue Exception => e
75
+ puts "Thirtysix: We could not contact the Thirtysix client on #{thirtysix_url} [#{e.message}]"
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thirtysix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary Moore
@@ -18,6 +18,7 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - Gemfile
20
20
  - README.md
21
+ - lib/thirtysix/rack/middleware.rb
21
22
  homepage: http://github.com/gmoore/thirtysix
22
23
  licenses:
23
24
  - MIT