cloudpi-appmetric 0.0.6

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.8.3"
13
+ gem "rcov", ">= 0"
14
+ gem "json"
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Junsang Yang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = cloudpi-appmetric
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to cloudpi-appmetric
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Junsang Yang. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "cloudpi-appmetric"
18
+ gem.homepage = "http://github.com/zuns00@gmail.com/cloudpi-appmetric"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{CloudPI PaaS - Application Metric Client for ruby}
21
+ gem.description = %Q{CloudPI PaaS - Application Metric Client for ruby}
22
+ gem.email = "junsang.yang@samsung.com"
23
+ gem.authors = ["Junsang Yang"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "cloudpi-appmetric #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.6
@@ -0,0 +1,169 @@
1
+ require 'socket'
2
+ require 'json'
3
+
4
+ module CloudPI
5
+
6
+ class AppMetric
7
+
8
+ class CircularQueue
9
+
10
+ def initialize(size)
11
+ @memory = []
12
+ @front = 0
13
+ @rear = 0
14
+ @lock = Mutex.new
15
+
16
+ @queue_size = size
17
+ end
18
+
19
+ def is_empty
20
+ @lock.synchronize { return @front == @rear }
21
+ end
22
+
23
+ def is_full
24
+ @lock.synchronize { return ((@rear + 1) % @queue_size) == @front }
25
+ end
26
+
27
+ def first
28
+ return nil if is_empty
29
+ @lock.synchronize { return @memory[(@front + 1) % @queue_size] }
30
+ end
31
+
32
+ def pop
33
+ return if is_empty
34
+ @lock.synchronize { @front = (@front + 1) % @queue_size }
35
+ end
36
+
37
+ def enqueue(data)
38
+ if is_full
39
+ @lock.synchronize {
40
+ @front = (@front + 1) % @queue_size
41
+ @rear = (@rear + 1) % @queue_size
42
+ @memory[@rear] = data
43
+ }
44
+ else
45
+ @lock.synchronize {
46
+ @rear = (@rear + 1) % @queue_size
47
+ @memory[@rear] = data
48
+ }
49
+ end
50
+ end
51
+
52
+ def dequeue
53
+ return nil if is_empty
54
+
55
+ @lock.synchronize {
56
+ @front = (@front + 1) % @queue_size
57
+ return @memory[@front]
58
+ }
59
+ end
60
+
61
+ def cursor
62
+ return "#{@front}:#{@rear}"
63
+ end
64
+ end
65
+
66
+ def initialize(bridge_ip = 'localhost', bridge_port = 5999, metric_policy = '{}')
67
+ @app_ip = ENV["VCAP_APP_HOST"] || '127.0.0.1'
68
+ @app_port = ENV["VCAP_APP_PORT"] || 9999
69
+ env_vcap_application = JSON.parse(ENV["VCAP_APPLICATION"])
70
+ instance_id = env_vcap_application["instance_id"] if env_vcap_application
71
+ @app_id = instance_id || 'NO_INSTANCE_ID'
72
+ @bridge_ip = bridge_ip
73
+ @bridge_port = bridge_port
74
+ @metric_policy = JSON.parse(metric_policy)
75
+ @queue = CircularQueue.new(4096)
76
+
77
+ connect
78
+ thread_loop
79
+ end
80
+
81
+ def connect
82
+ begin
83
+ @conn = TCPSocket.new(@bridge_ip, @bridge_port)
84
+ make_policy_unpublished
85
+ send_metric_policy
86
+ rescue
87
+ # ignore connection error
88
+ nil
89
+ end
90
+ end
91
+
92
+ def close
93
+ @conn.close if @conn
94
+ end
95
+
96
+ def metric_key
97
+ "#{@app_ip}:#{@app_port}:#{@app_id}"
98
+ end
99
+
100
+ def set_metric_policy(metric_policy)
101
+ # TODO: parse error handling
102
+ @metric_policy = JSON.parse(metric_policy)
103
+ end
104
+
105
+ def make_policy_unpublished
106
+ @is_policy_published = false;
107
+ end
108
+
109
+ def send_metric_policy
110
+ send_metric_value(@metric_policy.to_json)
111
+ @is_policy_published = true;
112
+ end
113
+
114
+ def send_heartbeat
115
+ send_metric_value({:alive => Time.now.to_i}.to_json)
116
+ end
117
+
118
+ def send_metric_value(message)
119
+ # TODO: parse error handling
120
+ # add 'when' value into message -> {:when => Time.now, ... }
121
+ msg = JSON.parse(message)
122
+ msg[:when] = Time.now.to_i
123
+ # add metric_key -> {"metric_key":"message"}
124
+ message = {metric_key => msg}.to_json
125
+
126
+ @queue.enqueue(message)
127
+ end
128
+
129
+ def thread_loop
130
+ thread = Thread.new do
131
+ loop do
132
+ while !@queue.is_empty do
133
+ puts "not empty queue: #{@queue.cursor}"
134
+ @message = @queue.first
135
+ begin
136
+ @conn.write("#{@message}\0")
137
+ puts "send msg: #{@message}"
138
+ @queue.pop
139
+ rescue
140
+ # retry to connect
141
+ sleep(1.0)
142
+ connect
143
+ end
144
+ end # while
145
+ sleep(3.0)
146
+ end # loop
147
+ end # thread
148
+
149
+ heartbeat_thread = Thread.new do
150
+ loop do
151
+ send_heartbeat
152
+ sleep(10.0)
153
+ end # loop
154
+ end # thread
155
+
156
+ set_trap(thread)
157
+ set_trap(heartbeat_thread)
158
+ end
159
+
160
+ def set_trap(thread)
161
+ # Thread exit when signal triggered
162
+ Signal.trap("TERM") do
163
+ Thread.kill(thread) if thread
164
+ end
165
+ end
166
+
167
+ end
168
+
169
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
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
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'cloudpi-appmetric'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestCloudpiAppmetric < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudpi-appmetric
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 6
10
+ version: 0.0.6
11
+ platform: ruby
12
+ authors:
13
+ - Junsang Yang
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-10 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :development
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ version_requirements: *id001
32
+ name: shoulda
33
+ prerelease: false
34
+ - !ruby/object:Gem::Dependency
35
+ type: :development
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 31
42
+ segments:
43
+ - 3
44
+ - 12
45
+ version: "3.12"
46
+ version_requirements: *id002
47
+ name: rdoc
48
+ prerelease: false
49
+ - !ruby/object:Gem::Dependency
50
+ type: :development
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 23
57
+ segments:
58
+ - 1
59
+ - 0
60
+ - 0
61
+ version: 1.0.0
62
+ version_requirements: *id003
63
+ name: bundler
64
+ prerelease: false
65
+ - !ruby/object:Gem::Dependency
66
+ type: :development
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ hash: 49
73
+ segments:
74
+ - 1
75
+ - 8
76
+ - 3
77
+ version: 1.8.3
78
+ version_requirements: *id004
79
+ name: jeweler
80
+ prerelease: false
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ version_requirements: *id005
93
+ name: rcov
94
+ prerelease: false
95
+ - !ruby/object:Gem::Dependency
96
+ type: :development
97
+ requirement: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ version_requirements: *id006
107
+ name: json
108
+ prerelease: false
109
+ description: CloudPI PaaS - Application Metric Client for ruby
110
+ email: junsang.yang@samsung.com
111
+ executables: []
112
+
113
+ extensions: []
114
+
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.rdoc
118
+ files:
119
+ - .document
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.rdoc
123
+ - Rakefile
124
+ - VERSION
125
+ - lib/cloudpi-appmetric.rb
126
+ - test/helper.rb
127
+ - test/test_cloudpi-appmetric.rb
128
+ homepage: http://github.com/zuns00@gmail.com/cloudpi-appmetric
129
+ licenses:
130
+ - MIT
131
+ post_install_message:
132
+ rdoc_options: []
133
+
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ hash: 3
151
+ segments:
152
+ - 0
153
+ version: "0"
154
+ requirements: []
155
+
156
+ rubyforge_project:
157
+ rubygems_version: 1.8.6
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: CloudPI PaaS - Application Metric Client for ruby
161
+ test_files: []
162
+