amnesia 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.markdown +31 -3
  2. data/lib/amnesia.rb +27 -4
  3. data/lib/amnesia/host.rb +27 -22
  4. metadata +8 -14
@@ -8,7 +8,7 @@ Hopefully with Amnesia you'll know exactly whats happening with memory when it c
8
8
 
9
9
  ## Why?
10
10
 
11
- Its always nice to have some statistics to see how everything is performing within your stack. Memcached seems to be a mystery box that people don't really pay alot of attention to.
11
+ Its always nice to have some statistics to see how everything is performing within your stack. Memcached seems to be a mystery box that people don't really pay a lot of attention to.
12
12
 
13
13
  Amnesia tells you how your application is performing, when it misses, when it is running sweet, when you're about to run out of memcached and (perhaps) fall down in a screaming heap.
14
14
 
@@ -16,7 +16,7 @@ Amnesia tells you how your application is performing, when it misses, when it is
16
16
 
17
17
  All stats are since each memcached instance was restarted
18
18
 
19
- Available as a cumluative result of all your memcached instances, or single instances alone:
19
+ Available as a cumulative result of all your memcached instances, or single instances alone:
20
20
 
21
21
  * Cache hits and misses
22
22
  * Reads and writes
@@ -40,11 +40,39 @@ Available for single instances only:
40
40
  "config.ru":
41
41
 
42
42
  require 'amnesia'
43
- use Amnesia::Application, :hosts => ["localhost:11211"]
43
+ use Amnesia::Application
44
44
  run Sinatra::Application
45
45
 
46
46
  ### Then, cruise on over to `your-host.tld/amnesia`
47
47
 
48
+
49
+ ## Configuration options
50
+
51
+ ### Hosts
52
+ Amnesia will work automagically if you drop it on a Heroku powered app, likewise—for a "standard" memcache host (running on localhost:11211, the default.).
53
+
54
+ When you need to specify where your memcache hosts can be found, you can either set an environment variable:
55
+
56
+ ENV["MEMCACHE_SERVERS"] = ['localhost:11211']
57
+
58
+ or alternately, you can set it within your `config.ru`:
59
+
60
+ use Amnesia::Application, :hosts => ["mc1.yourapp.com:11211", "mc2.yourapp.com:11211"]
61
+
62
+ ### Authentication
63
+
64
+ When you want to keep your Amnesia data private, you can set an environment variable that will enable http basic authentication:
65
+
66
+ ENV["AMNESIA_CREDS"] = ben:schwarz
67
+
68
+ in your shell, you might do it like this:
69
+
70
+ export AMNESIA_CREDS=ben:schwarz
71
+
72
+ on heroku, like this:
73
+
74
+ heroku config:add AMNESIA_CREDS=ben:schwarz
75
+
48
76
  ## Potential issues
49
77
 
50
78
  * Hosts are listed as "Inactive" or "Not Responding"
@@ -1,5 +1,5 @@
1
1
  require 'sinatra'
2
- require 'memcache'
2
+ require 'memcached'
3
3
  require 'gchart'
4
4
  require 'haml'
5
5
 
@@ -19,6 +19,10 @@ module Amnesia
19
19
 
20
20
  def initialize(app, configuration = {})
21
21
  Amnesia.config = configuration
22
+ # Heroku
23
+ Amnesia.config[:hosts] ||= [nil] if ENV['MEMCACHE_SERVERS']
24
+ # Default if nothing set
25
+ Amnesia.config[:hosts] ||= ['127.0.0.1:11211']
22
26
  super(app)
23
27
  end
24
28
 
@@ -40,16 +44,35 @@ module Amnesia
40
44
  rescue
41
45
  nil
42
46
  end
47
+
48
+ def protected!
49
+ unless authorized?
50
+ response['WWW-Authenticate'] = %(Basic realm="Amnesia")
51
+ throw(:halt, [401, "Not authorized\n"])
52
+ end
53
+ end
54
+
55
+ def authorized?
56
+ if ENV['AMNESIA_CREDS']
57
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
58
+ @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ENV['AMNESIA_CREDS'].split(':')
59
+ else
60
+ # No auth needed.
61
+ true
62
+ end
63
+ end
43
64
  end
44
65
 
45
66
  get '/amnesia' do
46
- @hosts = Amnesia.config[:hosts].map{|host| Host.new(host)}
67
+ protected!
68
+ @hosts = Amnesia.config[:hosts].map{|host| Amnesia::Host.new(host)}
47
69
  haml :index
48
70
  end
49
71
 
50
72
  get '/amnesia/:host' do
51
- @host = Host.new(params[:host])
73
+ protected!
74
+ @host = Amnesia::Host.new(params[:host])
52
75
  haml :host
53
76
  end
54
77
  end
55
- end
78
+ end
@@ -1,29 +1,34 @@
1
- class Host
2
- attr_reader :address
1
+ module Amnesia
2
+ class Host
3
+ def initialize(address)
4
+ @address = address
5
+ end
3
6
 
4
- def initialize(address)
5
- @address = address
6
- end
7
+ def alive?
8
+ return true if connection.stats
9
+ rescue Memcached::Error
10
+ return false
11
+ end
7
12
 
8
- def alive?
9
- return true if connection.stats
10
- rescue MemCache::MemCacheError
11
- return false
12
- end
13
+ def method_missing(method, *args)
14
+ stats[method.to_s.to_sym].sum if stats.has_key? method.to_s.to_sym
15
+ end
13
16
 
14
- def method_missing(method, *args)
15
- return stats[method.to_s] if stats.has_key? method.to_s
16
- end
17
-
18
- def stats
19
- connection.stats[connection.stats.keys.first]
20
- rescue MemCache::MemCacheError
21
- return {}
22
- end
17
+ def stats
18
+ connection.stats[connection.stats.keys.first]
19
+ connection.stats
20
+ rescue Memcached::Error
21
+ return {}
22
+ end
23
+
24
+ def address
25
+ @address || @connection.servers.join(', ')
26
+ end
23
27
 
24
- private
28
+ private
25
29
 
26
- def connection
27
- @connection ||= MemCache.new(@address)
30
+ def connection
31
+ @connection ||= @address ? Memcached.new(@address) : Memcached.new
32
+ end
28
33
  end
29
34
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amnesia
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease: false
6
5
  segments:
7
6
  - 1
8
7
  - 0
9
- - 0
10
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - Ben Schwarz
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-09-28 00:00:00 +10:00
17
+ date: 2010-10-02 00:00:00 +10:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 15
30
28
  segments:
31
29
  - 1
32
30
  - 0
@@ -34,19 +32,19 @@ dependencies:
34
32
  type: :runtime
35
33
  version_requirements: *id001
36
34
  - !ruby/object:Gem::Dependency
37
- name: memcache-client
35
+ name: memcached-northscale
38
36
  prerelease: false
39
37
  requirement: &id002 !ruby/object:Gem::Requirement
40
38
  none: false
41
39
  requirements:
42
40
  - - ">="
43
41
  - !ruby/object:Gem::Version
44
- hash: 3
45
42
  segments:
46
- - 1
47
- - 5
48
43
  - 0
49
- version: 1.5.0
44
+ - 19
45
+ - 5
46
+ - 4
47
+ version: 0.19.5.4
50
48
  type: :runtime
51
49
  version_requirements: *id002
52
50
  - !ruby/object:Gem::Dependency
@@ -57,7 +55,6 @@ dependencies:
57
55
  requirements:
58
56
  - - "="
59
57
  - !ruby/object:Gem::Version
60
- hash: 23
61
58
  segments:
62
59
  - 1
63
60
  - 0
@@ -73,7 +70,6 @@ dependencies:
73
70
  requirements:
74
71
  - - ">="
75
72
  - !ruby/object:Gem::Version
76
- hash: 7
77
73
  segments:
78
74
  - 3
79
75
  - 0
@@ -114,7 +110,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
110
  requirements:
115
111
  - - ">="
116
112
  - !ruby/object:Gem::Version
117
- hash: 3
118
113
  segments:
119
114
  - 0
120
115
  version: "0"
@@ -123,7 +118,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
118
  requirements:
124
119
  - - ">="
125
120
  - !ruby/object:Gem::Version
126
- hash: 3
127
121
  segments:
128
122
  - 0
129
123
  version: "0"