cloudpi-appender 0.1.0
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/lib/cloudpi-appender.rb +116 -0
- metadata +45 -0
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'socket'
|
3
|
+
require 'json'
|
4
|
+
require 'logger'
|
5
|
+
require 'timeout'
|
6
|
+
|
7
|
+
module CloudPI
|
8
|
+
|
9
|
+
class Appender
|
10
|
+
|
11
|
+
CONNECT_TIMEOUT = 0.1 # sec
|
12
|
+
|
13
|
+
def initialize(bridge_ip = 'localhost', bridge_port = 5999, logger = nil, policy = {})
|
14
|
+
@bridge_ip = bridge_ip
|
15
|
+
@bridge_port = bridge_port
|
16
|
+
|
17
|
+
@logger = logger
|
18
|
+
@logger = Logger.new(STDOUT) unless @logger
|
19
|
+
|
20
|
+
begin
|
21
|
+
json = JSON.parse(ENV['VCAP_APPLICATION'], {:symbolize_names => true})
|
22
|
+
@app_ip = json[:host]
|
23
|
+
@app_port = json[:port]
|
24
|
+
@app_id = json[:app_id]
|
25
|
+
@instance_id = json[:instance_id]
|
26
|
+
rescue JSON::ParseError, TypeError
|
27
|
+
@logger.error "parsing error: ENV['VCAP_APPLICATION']"
|
28
|
+
end
|
29
|
+
|
30
|
+
@policy = policy
|
31
|
+
@policy = {} unless @policy.is_a? Hash
|
32
|
+
|
33
|
+
connect_to_bridge
|
34
|
+
end
|
35
|
+
|
36
|
+
def connect_to_bridge
|
37
|
+
Timeout::timeout(CONNECT_TIMEOUT) do
|
38
|
+
@bridge = TCPSocket.new(@bridge_ip, @bridge_port)
|
39
|
+
end
|
40
|
+
send_policy
|
41
|
+
@logger.info("connected to bridge: #{@bridge_ip}:#{@bridge_port}")
|
42
|
+
rescue => e
|
43
|
+
@logger.error("bridge connection failed: #{e.message}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def disconnect_bridge
|
47
|
+
@bridge.close if @bridge
|
48
|
+
@logger.info("bridge connection closed.")
|
49
|
+
end
|
50
|
+
|
51
|
+
def connected_to_bridge?
|
52
|
+
return false unless @bridge && !@bridge.closed?
|
53
|
+
true
|
54
|
+
end
|
55
|
+
|
56
|
+
def check_and_reconnect_to_bridge
|
57
|
+
connect_to_bridge unless connected_to_bridge?
|
58
|
+
end
|
59
|
+
|
60
|
+
def parse_json(json)
|
61
|
+
body = JSON.parse(json, {:symbolize_names => true})
|
62
|
+
raise JSON::ParseError unless body.is_a?(Hash) # not allowed array json
|
63
|
+
body
|
64
|
+
rescue JSON::ParseError
|
65
|
+
@logger.error("wrong metric json message recieved.")
|
66
|
+
{}
|
67
|
+
end
|
68
|
+
|
69
|
+
def send_to_bridge(msg)
|
70
|
+
return unless msg
|
71
|
+
check_and_reconnect_to_bridge
|
72
|
+
@bridge.write("#{msg}\0")
|
73
|
+
@logger.debug("sent message to bridge: #{msg}")
|
74
|
+
rescue => e
|
75
|
+
@logger.error("bridge write error: #{e.message}")
|
76
|
+
@bridge.close
|
77
|
+
end
|
78
|
+
|
79
|
+
def metric_key
|
80
|
+
# instance_ip:instance_port:app_id:instance_id
|
81
|
+
:"#{@app_ip}:#{@app_port}:#{@app_id}:#{@instance_id}"
|
82
|
+
end
|
83
|
+
|
84
|
+
def set_policy(policy)
|
85
|
+
@policy = policy
|
86
|
+
@policy = {} unless @policy.is_a? Hash
|
87
|
+
@logger.info("set metric policy: #{@policy.to_json}")
|
88
|
+
send_policy
|
89
|
+
end
|
90
|
+
|
91
|
+
def send_heartbeat
|
92
|
+
msg = { metric_key => {:alive => Time.now.to_i} }.to_json
|
93
|
+
send_to_bridge(msg)
|
94
|
+
@logger.info("heartbeat sent: #{msg}")
|
95
|
+
end
|
96
|
+
|
97
|
+
def msg_with_when(msg)
|
98
|
+
msg.merge({ :when => Time.now.to_i })
|
99
|
+
end
|
100
|
+
|
101
|
+
def send_policy
|
102
|
+
msg = { metric_key => msg_with_when(@policy) }.to_json
|
103
|
+
send_to_bridge(msg)
|
104
|
+
@logger.info("policy sent: #{msg}")
|
105
|
+
end
|
106
|
+
|
107
|
+
def send(msg)
|
108
|
+
return unless msg.is_a? Hash
|
109
|
+
msg = { metric_key => msg_with_when(msg) }.to_json
|
110
|
+
send_to_bridge(msg)
|
111
|
+
@logger.info("value sent: #{msg}")
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloudpi-appender
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Junsang Yang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-01 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: CloudPI PaaS - Application Custom Metric Client for Ruby
|
15
|
+
email: zuns00@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/cloudpi-appender.rb
|
21
|
+
homepage: http://www.not.yet/cloudpi-appender
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.17
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: CloudPI Bridge Appender for Ruby
|
45
|
+
test_files: []
|