simple_metrics 0.4.0 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +3 -0
- data/README.markdown +7 -59
- data/Rakefile +17 -0
- data/bin/populate +25 -12
- data/bin/populate_for_demo +65 -0
- data/bin/simple_metrics_server +4 -3
- data/config.ru +13 -0
- data/default_config.yml +34 -0
- data/lib/simple_metrics/bucket.rb +43 -106
- data/lib/simple_metrics/configuration.rb +82 -0
- data/lib/simple_metrics/dashboard.rb +29 -0
- data/lib/simple_metrics/dashboard_repository.rb +59 -0
- data/lib/simple_metrics/data_point/base.rb +59 -0
- data/lib/simple_metrics/data_point/counter.rb +20 -0
- data/lib/simple_metrics/data_point/event.rb +16 -0
- data/lib/simple_metrics/data_point/gauge.rb +19 -0
- data/lib/simple_metrics/data_point/timing.rb +15 -0
- data/lib/simple_metrics/data_point.rb +45 -137
- data/lib/simple_metrics/data_point_repository.rb +115 -0
- data/lib/simple_metrics/functions.rb +5 -5
- data/lib/simple_metrics/graph.rb +69 -32
- data/lib/simple_metrics/importer.rb +64 -0
- data/lib/simple_metrics/instrument.rb +28 -0
- data/lib/simple_metrics/instrument_repository.rb +64 -0
- data/lib/simple_metrics/metric.rb +29 -0
- data/lib/simple_metrics/metric_repository.rb +54 -0
- data/lib/simple_metrics/repository.rb +34 -0
- data/lib/simple_metrics/udp_server.rb +81 -0
- data/lib/simple_metrics/version.rb +1 -1
- data/lib/simple_metrics.rb +21 -76
- data/simple_metrics.gemspec +4 -0
- data/spec/bucket_spec.rb +17 -152
- data/spec/dashboard_repository_spec.rb +52 -0
- data/spec/data_point_repository_spec.rb +77 -0
- data/spec/data_point_spec.rb +14 -64
- data/spec/graph_spec.rb +30 -19
- data/spec/importer_spec.rb +126 -0
- data/spec/instrument_repository_spec.rb +52 -0
- data/spec/metric_repository_spec.rb +53 -0
- data/spec/spec_helper.rb +4 -3
- metadata +93 -23
- data/bin/simple_metrics_client +0 -64
- data/lib/simple_metrics/client.rb +0 -83
- data/lib/simple_metrics/mongo.rb +0 -48
- data/lib/simple_metrics/server.rb +0 -66
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
module SimpleMetrics
|
5
|
+
|
6
|
+
describe InstrumentRepository do
|
7
|
+
|
8
|
+
before do
|
9
|
+
InstrumentRepository.truncate_collections
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:ts) do
|
13
|
+
Time.now.utc.to_i
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#save" do
|
17
|
+
it "saves instrument correctly" do
|
18
|
+
instrument = Instrument.new(:name => "test")
|
19
|
+
InstrumentRepository.save(instrument)
|
20
|
+
|
21
|
+
results = InstrumentRepository.find_all
|
22
|
+
results.should have(1).instrument
|
23
|
+
results.first.name.should == "test"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#find_all" do
|
28
|
+
it "returns all instruments" do
|
29
|
+
instrument1 = Instrument.new(:name => "test")
|
30
|
+
instrument2 = Instrument.new(:name => "test2")
|
31
|
+
InstrumentRepository.save(instrument1)
|
32
|
+
InstrumentRepository.save(instrument2)
|
33
|
+
|
34
|
+
results = InstrumentRepository.find_all
|
35
|
+
results.should have(2).instruments
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#find_one_by_name" do
|
40
|
+
it "returns all instruments" do
|
41
|
+
instrument1 = Instrument.new(:name => "test")
|
42
|
+
instrument2 = Instrument.new(:name => "test2")
|
43
|
+
InstrumentRepository.save(instrument1)
|
44
|
+
InstrumentRepository.save(instrument2)
|
45
|
+
|
46
|
+
result = InstrumentRepository.find_one_by_name("test")
|
47
|
+
result.name.should == "test"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end # describe InstrumentRepository
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
module SimpleMetrics
|
5
|
+
|
6
|
+
describe MetricRepository do
|
7
|
+
|
8
|
+
before do
|
9
|
+
MetricRepository.truncate_collections
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:ts) do
|
13
|
+
Time.now.utc.to_i
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#save" do
|
17
|
+
it "saves metric correctly" do
|
18
|
+
metric = Metric.new(:name => "test", :total => 1)
|
19
|
+
MetricRepository.save(metric)
|
20
|
+
|
21
|
+
results = MetricRepository.find_all
|
22
|
+
results.should have(1).metric
|
23
|
+
results.first.name.should == "test"
|
24
|
+
results.first.total.should == 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#find_all" do
|
29
|
+
it "returns all metrics" do
|
30
|
+
metric1 = Metric.new(:name => "test", :total => 1)
|
31
|
+
metric2 = Metric.new(:name => "test2", :total => 1)
|
32
|
+
MetricRepository.save(metric1)
|
33
|
+
MetricRepository.save(metric2)
|
34
|
+
|
35
|
+
results = MetricRepository.find_all
|
36
|
+
results.should have(2).metrics
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#find_one_by_name" do
|
41
|
+
it "returns all metrics" do
|
42
|
+
metric1 = Metric.new(:name => "test", :total => 1)
|
43
|
+
metric2 = Metric.new(:name => "test2", :total => 1)
|
44
|
+
MetricRepository.save(metric1)
|
45
|
+
MetricRepository.save(metric2)
|
46
|
+
|
47
|
+
result = MetricRepository.find_one_by_name("test")
|
48
|
+
result.name.should == "test"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end # describe MetricRepository
|
52
|
+
|
53
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require "simple_metrics"
|
2
|
+
require "rspec"
|
2
3
|
|
3
4
|
RSpec.configure do |config|
|
4
5
|
config.mock_with :rr
|
5
6
|
end
|
6
7
|
|
7
8
|
SimpleMetrics.logger = Logger.new('/dev/null')
|
8
|
-
SimpleMetrics.
|
9
|
-
|
10
|
-
|
9
|
+
SimpleMetrics.configure do |config|
|
10
|
+
config.db = { :host => 'localhost', :prefix => 'test', :options => {} }
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_metrics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70306351217460 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70306351217460
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70306351212560 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70306351212560
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rr
|
38
|
-
requirement: &
|
38
|
+
requirement: &70306351212060 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,32 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70306351212060
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: shotgun
|
49
|
+
requirement: &70306351211560 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70306351211560
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rack-test
|
60
|
+
requirement: &70306351211140 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70306351211140
|
47
69
|
- !ruby/object:Gem::Dependency
|
48
70
|
name: eventmachine
|
49
|
-
requirement: &
|
71
|
+
requirement: &70306351210700 !ruby/object:Gem::Requirement
|
50
72
|
none: false
|
51
73
|
requirements:
|
52
74
|
- - ! '>='
|
@@ -54,10 +76,10 @@ dependencies:
|
|
54
76
|
version: '0'
|
55
77
|
type: :runtime
|
56
78
|
prerelease: false
|
57
|
-
version_requirements: *
|
79
|
+
version_requirements: *70306351210700
|
58
80
|
- !ruby/object:Gem::Dependency
|
59
81
|
name: daemons
|
60
|
-
requirement: &
|
82
|
+
requirement: &70306351210260 !ruby/object:Gem::Requirement
|
61
83
|
none: false
|
62
84
|
requirements:
|
63
85
|
- - ! '>='
|
@@ -65,10 +87,10 @@ dependencies:
|
|
65
87
|
version: '0'
|
66
88
|
type: :runtime
|
67
89
|
prerelease: false
|
68
|
-
version_requirements: *
|
90
|
+
version_requirements: *70306351210260
|
69
91
|
- !ruby/object:Gem::Dependency
|
70
92
|
name: mongo
|
71
|
-
requirement: &
|
93
|
+
requirement: &70306351209720 !ruby/object:Gem::Requirement
|
72
94
|
none: false
|
73
95
|
requirements:
|
74
96
|
- - ~>
|
@@ -76,10 +98,10 @@ dependencies:
|
|
76
98
|
version: '1.6'
|
77
99
|
type: :runtime
|
78
100
|
prerelease: false
|
79
|
-
version_requirements: *
|
101
|
+
version_requirements: *70306351209720
|
80
102
|
- !ruby/object:Gem::Dependency
|
81
103
|
name: bson
|
82
|
-
requirement: &
|
104
|
+
requirement: &70306351209080 !ruby/object:Gem::Requirement
|
83
105
|
none: false
|
84
106
|
requirements:
|
85
107
|
- - ~>
|
@@ -87,39 +109,82 @@ dependencies:
|
|
87
109
|
version: '1.6'
|
88
110
|
type: :runtime
|
89
111
|
prerelease: false
|
90
|
-
version_requirements: *
|
112
|
+
version_requirements: *70306351209080
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: bson_ext
|
115
|
+
requirement: &70306351207600 !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ~>
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '1.6'
|
121
|
+
type: :runtime
|
122
|
+
prerelease: false
|
123
|
+
version_requirements: *70306351207600
|
124
|
+
- !ruby/object:Gem::Dependency
|
125
|
+
name: activesupport
|
126
|
+
requirement: &70306351206920 !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: *70306351206920
|
91
135
|
description: SimpleMetrics
|
92
136
|
email:
|
93
137
|
- fdietz@gmail.com
|
94
138
|
executables:
|
95
139
|
- populate
|
96
|
-
-
|
140
|
+
- populate_for_demo
|
97
141
|
- simple_metrics_server
|
98
142
|
extensions: []
|
99
143
|
extra_rdoc_files: []
|
100
144
|
files:
|
101
145
|
- .gitignore
|
102
146
|
- .rvmrc
|
147
|
+
- .travis.yml
|
103
148
|
- Gemfile
|
104
149
|
- README.markdown
|
105
150
|
- Rakefile
|
106
151
|
- bin/populate
|
107
|
-
- bin/
|
152
|
+
- bin/populate_for_demo
|
108
153
|
- bin/simple_metrics_server
|
154
|
+
- config.ru
|
155
|
+
- default_config.yml
|
109
156
|
- example/increment.rb
|
110
157
|
- lib/simple_metrics.rb
|
111
158
|
- lib/simple_metrics/bucket.rb
|
112
|
-
- lib/simple_metrics/
|
159
|
+
- lib/simple_metrics/configuration.rb
|
160
|
+
- lib/simple_metrics/dashboard.rb
|
161
|
+
- lib/simple_metrics/dashboard_repository.rb
|
113
162
|
- lib/simple_metrics/data_point.rb
|
163
|
+
- lib/simple_metrics/data_point/base.rb
|
164
|
+
- lib/simple_metrics/data_point/counter.rb
|
165
|
+
- lib/simple_metrics/data_point/event.rb
|
166
|
+
- lib/simple_metrics/data_point/gauge.rb
|
167
|
+
- lib/simple_metrics/data_point/timing.rb
|
168
|
+
- lib/simple_metrics/data_point_repository.rb
|
114
169
|
- lib/simple_metrics/functions.rb
|
115
170
|
- lib/simple_metrics/graph.rb
|
116
|
-
- lib/simple_metrics/
|
117
|
-
- lib/simple_metrics/
|
171
|
+
- lib/simple_metrics/importer.rb
|
172
|
+
- lib/simple_metrics/instrument.rb
|
173
|
+
- lib/simple_metrics/instrument_repository.rb
|
174
|
+
- lib/simple_metrics/metric.rb
|
175
|
+
- lib/simple_metrics/metric_repository.rb
|
176
|
+
- lib/simple_metrics/repository.rb
|
177
|
+
- lib/simple_metrics/udp_server.rb
|
118
178
|
- lib/simple_metrics/version.rb
|
119
179
|
- simple_metrics.gemspec
|
120
180
|
- spec/bucket_spec.rb
|
181
|
+
- spec/dashboard_repository_spec.rb
|
182
|
+
- spec/data_point_repository_spec.rb
|
121
183
|
- spec/data_point_spec.rb
|
122
184
|
- spec/graph_spec.rb
|
185
|
+
- spec/importer_spec.rb
|
186
|
+
- spec/instrument_repository_spec.rb
|
187
|
+
- spec/metric_repository_spec.rb
|
123
188
|
- spec/spec_helper.rb
|
124
189
|
homepage: ''
|
125
190
|
licenses: []
|
@@ -135,7 +200,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
135
200
|
version: '0'
|
136
201
|
segments:
|
137
202
|
- 0
|
138
|
-
hash: -
|
203
|
+
hash: -4426928815847505405
|
139
204
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
205
|
none: false
|
141
206
|
requirements:
|
@@ -144,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
209
|
version: '0'
|
145
210
|
segments:
|
146
211
|
- 0
|
147
|
-
hash: -
|
212
|
+
hash: -4426928815847505405
|
148
213
|
requirements: []
|
149
214
|
rubyforge_project:
|
150
215
|
rubygems_version: 1.8.17
|
@@ -153,6 +218,11 @@ specification_version: 3
|
|
153
218
|
summary: SimpleMetrics
|
154
219
|
test_files:
|
155
220
|
- spec/bucket_spec.rb
|
221
|
+
- spec/dashboard_repository_spec.rb
|
222
|
+
- spec/data_point_repository_spec.rb
|
156
223
|
- spec/data_point_spec.rb
|
157
224
|
- spec/graph_spec.rb
|
225
|
+
- spec/importer_spec.rb
|
226
|
+
- spec/instrument_repository_spec.rb
|
227
|
+
- spec/metric_repository_spec.rb
|
158
228
|
- spec/spec_helper.rb
|
data/bin/simple_metrics_client
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "rubygems"
|
4
|
-
require "bundler/setup"
|
5
|
-
|
6
|
-
require 'optparse'
|
7
|
-
require "simple_metrics"
|
8
|
-
|
9
|
-
options = {
|
10
|
-
:host => 'localhost',
|
11
|
-
:port => 8125,
|
12
|
-
:sample_rate => 1
|
13
|
-
}
|
14
|
-
|
15
|
-
parser ||= OptionParser.new do |opts|
|
16
|
-
opts.banner = "Usage Example: simple_metrics_send com.test.mymetric -c5"
|
17
|
-
|
18
|
-
opts.separator ""
|
19
|
-
opts.separator "Client options:"
|
20
|
-
|
21
|
-
opts.on("-c", "--counter VALUE", "Counter, a relative value") do |value|
|
22
|
-
options[:type] = 'c'
|
23
|
-
options[:stat] = value.to_i
|
24
|
-
end
|
25
|
-
opts.on("-g", "--gauge VALUE", "Gauge, an absolute value ") do |value|
|
26
|
-
options[:type] = 'g'
|
27
|
-
options[:stat] = value.to_i
|
28
|
-
end
|
29
|
-
opts.on("-t", "--timing VALUE", "A timing in ms") do |value|
|
30
|
-
options[:type] = 'ms'
|
31
|
-
options[:stat] = value.to_i
|
32
|
-
end
|
33
|
-
opts.on("-s", "--sample_rate VALUE", "An optional sample rate between 0 and 1 (example: 0.2)") do |value|
|
34
|
-
options[:sample_rate] = value.to_f || 1
|
35
|
-
end
|
36
|
-
|
37
|
-
opts.separator ""
|
38
|
-
|
39
|
-
opts.on("-a", "--address HOST", "bind to HOST address (default: #{options[:host]})") do |host|
|
40
|
-
options[:host] = host
|
41
|
-
end
|
42
|
-
|
43
|
-
opts.on("-p", "--port PORT", "use PORT (default: #{options[:port]})") do |port|
|
44
|
-
options[:port] = port.to_i
|
45
|
-
end
|
46
|
-
|
47
|
-
opts.separator ""
|
48
|
-
opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
|
49
|
-
opts.on_tail('-v', '--version', "Show version") { puts SimpleMetrics::VERSION; exit }
|
50
|
-
|
51
|
-
end.parse!(ARGV)
|
52
|
-
|
53
|
-
command = ARGV.shift
|
54
|
-
arguments = ARGV
|
55
|
-
client = SimpleMetrics::Client.new(options[:host])
|
56
|
-
|
57
|
-
case options[:type]
|
58
|
-
when'c'
|
59
|
-
client.count(command, options[:stat], options[:sample_rate])
|
60
|
-
when 'g'
|
61
|
-
client.gauge(command, options[:stat], options[:sample_rate])
|
62
|
-
when 'ms'
|
63
|
-
client.timing(command, options[:stat], options[:sample_rate])
|
64
|
-
end
|
@@ -1,83 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require "socket"
|
3
|
-
|
4
|
-
module SimpleMetrics
|
5
|
-
|
6
|
-
class Client
|
7
|
-
VERSION = "0.0.1"
|
8
|
-
|
9
|
-
def initialize(host, port = 8125)
|
10
|
-
@host, @port = host, port
|
11
|
-
end
|
12
|
-
|
13
|
-
# send relative value
|
14
|
-
def increment(stat, sample_rate = 1)
|
15
|
-
count(stat, 1, sample_rate)
|
16
|
-
end
|
17
|
-
|
18
|
-
# send relative value
|
19
|
-
def decrement(stat, sample_rate = 1)
|
20
|
-
count(stat, -1, sample_rate)
|
21
|
-
end
|
22
|
-
|
23
|
-
# send relative value
|
24
|
-
def count(stat, count, sample_rate = 1)
|
25
|
-
send_data( stat, count, 'c', sample_rate)
|
26
|
-
end
|
27
|
-
|
28
|
-
# send absolute value
|
29
|
-
# TODO: check if this is actually supported by Statsd server
|
30
|
-
def gauge(stat, value)
|
31
|
-
send_data(stat, value, 'g')
|
32
|
-
end
|
33
|
-
|
34
|
-
# Sends a timing (in ms) (glork)
|
35
|
-
def timing(stat, ms, sample_rate = 1)
|
36
|
-
send_data(stat, ms, 'ms', sample_rate)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Sends a timing (in ms) block based
|
40
|
-
def time(stat, sample_rate = 1, &block)
|
41
|
-
start = Time.now
|
42
|
-
result = block.call
|
43
|
-
timing(stat, ((Time.now - start) * 1000).round, sample_rate)
|
44
|
-
result
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def sampled(sample_rate, &block)
|
50
|
-
if sample_rate < 1
|
51
|
-
block.call if rand <= sample_rate
|
52
|
-
else
|
53
|
-
block.call
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def send_data(stat, delta, type, sample_rate = 1)
|
58
|
-
sampled(sample_rate) do
|
59
|
-
data = "#{stat}:#{delta}|#{type}" # TODO: check stat is valid
|
60
|
-
data << "|@#{sample_rate}" if sample_rate < 1
|
61
|
-
send_to_socket(data)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def send_to_socket(data)
|
66
|
-
logger.debug "SimpleMetrics Client send: #{data}"
|
67
|
-
socket.send(data, 0, @host, @port)
|
68
|
-
rescue Exception => e
|
69
|
-
puts e.backtrace
|
70
|
-
logger.error "SimpleMetrics Client error: #{e}"
|
71
|
-
end
|
72
|
-
|
73
|
-
def socket
|
74
|
-
@socket ||= UDPSocket.new
|
75
|
-
end
|
76
|
-
|
77
|
-
def logger
|
78
|
-
@logger ||= SimpleMetrics.logger
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
data/lib/simple_metrics/mongo.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require "mongo"
|
3
|
-
|
4
|
-
module SimpleMetrics
|
5
|
-
module Mongo
|
6
|
-
extend self
|
7
|
-
|
8
|
-
def ensure_collections_exist
|
9
|
-
SimpleMetrics.logger.debug "SERVER: MongoDB - found following collections: #{db.collection_names.inspect}"
|
10
|
-
Bucket.all.each do |bucket|
|
11
|
-
unless db.collection_names.include?(bucket.name)
|
12
|
-
db.create_collection(bucket.name, :capped => bucket.capped, :size => bucket.size)
|
13
|
-
SimpleMetrics.logger.debug "SERVER: MongoDB - created collection #{bucket.name}, capped: #{bucket.capped}, size: #{bucket.size}"
|
14
|
-
end
|
15
|
-
|
16
|
-
db.collection(bucket.name).ensure_index([['ts', ::Mongo::ASCENDING]])
|
17
|
-
SimpleMetrics.logger.debug "SERVER: MongoDB - ensure index on column ts for collection #{bucket.name}"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def truncate_collections
|
22
|
-
Bucket.all.each do |bucket|
|
23
|
-
if db.collection_names.include?(bucket.name)
|
24
|
-
if bucket.capped?
|
25
|
-
collection(bucket.name).drop # capped collections can't remove elements, drop it instead
|
26
|
-
else
|
27
|
-
collection(bucket.name).remove
|
28
|
-
end
|
29
|
-
SimpleMetrics.logger.debug "SERVER: MongoDB - truncated collection #{bucket.name}"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
@@collection = {}
|
35
|
-
def collection(name)
|
36
|
-
@@collection[name] ||= db.collection(name)
|
37
|
-
end
|
38
|
-
|
39
|
-
def connection
|
40
|
-
@@connection ||= ::Mongo::Connection.new(SimpleMetrics.db_config[:host], SimpleMetrics.db_config[:port])
|
41
|
-
end
|
42
|
-
|
43
|
-
def db
|
44
|
-
@@db ||= connection.db(SimpleMetrics.db_config[:db_name], SimpleMetrics.db_config[:options])
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
end
|
@@ -1,66 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require "eventmachine"
|
3
|
-
|
4
|
-
module SimpleMetrics
|
5
|
-
|
6
|
-
module ClientHandler
|
7
|
-
|
8
|
-
@@data_points = []
|
9
|
-
|
10
|
-
class << self
|
11
|
-
def get_and_clear_data_points
|
12
|
-
data_points = @@data_points.dup
|
13
|
-
@@data_points = []
|
14
|
-
data_points
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def data_points
|
19
|
-
@@data_points
|
20
|
-
end
|
21
|
-
|
22
|
-
def post_init
|
23
|
-
SimpleMetrics.logger.info "ClientHandler entering post_init"
|
24
|
-
end
|
25
|
-
|
26
|
-
def receive_data(data)
|
27
|
-
SimpleMetrics.logger.debug "received_data: #{data.inspect}"
|
28
|
-
|
29
|
-
@@data_points ||= []
|
30
|
-
@@data_points << DataPoint.parse(data)
|
31
|
-
rescue DataPoint::ParserError => e
|
32
|
-
SimpleMetrics.logger.debug "Invalid Data skipped: #{data}"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class Server
|
37
|
-
|
38
|
-
attr_reader :db, :connection
|
39
|
-
|
40
|
-
def start
|
41
|
-
SimpleMetrics.logger.info "SERVER: starting up on #{SimpleMetrics.config[:host]}:#{SimpleMetrics.config[:port]}..."
|
42
|
-
|
43
|
-
Mongo.ensure_collections_exist
|
44
|
-
|
45
|
-
EM.run do
|
46
|
-
EM.open_datagram_socket(SimpleMetrics.config[:host], SimpleMetrics.config[:port], SimpleMetrics::ClientHandler) do |con|
|
47
|
-
EventMachine::add_periodic_timer(SimpleMetrics.config[:flush_interval]) do
|
48
|
-
SimpleMetrics.logger.debug "SERVER: period timer triggered after #{SimpleMetrics.config[:flush_interval]} seconds"
|
49
|
-
|
50
|
-
EM.defer { Bucket.flush_data_points(ClientHandler.get_and_clear_data_points) }
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def stop
|
57
|
-
SimpleMetrics.logger.info "EventMachine stop"
|
58
|
-
EM.stop
|
59
|
-
end
|
60
|
-
|
61
|
-
def to_s
|
62
|
-
"#{SimpleMetrics.config[:host]}:#{SimpleMetrics.config[:port]}"
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|