app_monit 0.0.8 → 0.0.9

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: e46f1d8fcbf444975b5d9c8ba3f743454e8ace06
4
- data.tar.gz: fb7161001cbc233a5a0042697a14dec6e143fbb2
3
+ metadata.gz: 3cc611bcf7f462c54bb7a498370acb4eb8ed495c
4
+ data.tar.gz: ba61adecdc84d5b64fe4675e3beef81bb195c6e3
5
5
  SHA512:
6
- metadata.gz: bed68ca508a66d0ad28801b106eb88d7c04d9b471952ecb85e8b3e44f8f4f4a9f541cf5b72975818c233112b70531920af6a9d72cb8783afaeb0cba446df06a2
7
- data.tar.gz: 612d8e2a6545282a5122d901b6a76e0e4bfab1f3e78efbf7a8b27d97c9813d2d9ca743c0b00097d2acb2c0f7287df4074a0aaaaa9bd0232b1b25b8e318c5971e
6
+ metadata.gz: aa91ce5ad4f39c717b8605867ecd0dc183af99e63755367b85d0c613be299dc4e2d0414bb70d239226573f86e4a1294c07f66b8e4871b8376dd8e7a559947351
7
+ data.tar.gz: b3101a5aa3e7e942811a4ac9d82eb8fd26e3ca1f68544296e8f81c0d472f9308921208cfba47db076dfecc0881eba96fbf1fd1680e280699139424c0fe2a4530
data/appmonit_gem.gemspec CHANGED
@@ -23,5 +23,4 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "minitest", ">= 5.2.2"
24
24
  spec.add_development_dependency "minitest-reporters"
25
25
  spec.add_development_dependency "webmock", ">= 1.17.1"
26
- spec.add_development_dependency "guard-minitest" if RUBY_PLATFORM =~ /darwin/
27
26
  end
data/lib/app_monit.rb CHANGED
@@ -4,7 +4,20 @@ require "app_monit/config"
4
4
  require "app_monit/http"
5
5
  require "app_monit/event"
6
6
  require "app_monit/query"
7
+ require "app_monit/worker"
7
8
  require "net/https"
9
+ require "logger"
8
10
 
9
11
  module AppMonit
12
+ MUTEX = Mutex.new
13
+
14
+ class << self
15
+ def logger
16
+ return @logger if @logger
17
+ AppMonit::MUTEX.synchronize do
18
+ return @logger if @logger
19
+ @logger = ::Logger.new(STDOUT)
20
+ end
21
+ end
22
+ end
10
23
  end
@@ -1,7 +1,7 @@
1
1
  module AppMonit
2
2
  class Config
3
3
  class << self
4
- attr_writer :api_key, :env, :end_point, :fail_silent, :enabled, :timeout, :version
4
+ attr_writer :api_key, :env, :end_point, :fail_silent, :enabled, :timeout, :version, :async, :log_level, :flush_rate
5
5
 
6
6
  def api_key
7
7
  @api_key || raise(ApiKeyNotSetError.new("Please set your API key"))
@@ -27,6 +27,18 @@ module AppMonit
27
27
  @enabled.nil? ? env != "test" : @enabled
28
28
  end
29
29
 
30
+ def log_level
31
+ @log_level || Logger::DEBUG
32
+ end
33
+
34
+ def async?
35
+ @async
36
+ end
37
+
38
+ def flush_rate
39
+ @flush_rate || 60
40
+ end
41
+
30
42
  def timeout
31
43
  @timeout || 1
32
44
  end
@@ -7,7 +7,7 @@ module AppMonit
7
7
  rescue Http::Error
8
8
  false
9
9
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse,
10
- Net::HTTPHeaderSyntaxError, Net::ProtocolError => error
10
+ Net::HTTPHeaderSyntaxError, Net::ProtocolError, Errno::ETIMEDOUT => error
11
11
  raise error unless AppMonit::Config.fail_silent
12
12
  false
13
13
  end
@@ -23,7 +23,16 @@ module AppMonit
23
23
 
24
24
  message[:payload] = data_hash
25
25
 
26
- client.post('/v1/events', message)
26
+ post(message)
27
+ end
28
+
29
+ def self.post(message)
30
+ if AppMonit::Config.async?
31
+ AppMonit.logger.debug("[Event] push: #{message}")
32
+ AppMonit::Worker.instance.push(message)
33
+ else
34
+ client.post('/v1/events', message)
35
+ end
27
36
  end
28
37
 
29
38
  def self.client
@@ -1,3 +1,3 @@
1
1
  module AppMonit
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -0,0 +1,83 @@
1
+ module AppMonit
2
+ class Worker
3
+ MUTEX = Mutex.new
4
+ MAX_MULTIPLIER = 5
5
+
6
+ attr_accessor :events
7
+
8
+ class << self
9
+ def instance
10
+ return @instance if @instance
11
+ MUTEX.synchronize do
12
+ return @instance if @instance
13
+ @instance = new.start
14
+ end
15
+ end
16
+ end
17
+
18
+ def initialize
19
+ @queue = Queue.new
20
+ @multiplier = 1
21
+ @flush_rate = AppMonit::Config.flush_rate
22
+ reset
23
+ end
24
+
25
+ def reset
26
+ @events = []
27
+ @allow_flush = true
28
+ end
29
+
30
+ def start
31
+ logger.debug('Start collecting')
32
+ Thread.new do
33
+ AppMonit.logger.debug('Waiting for queue')
34
+ while (event = @queue.pop)
35
+ begin
36
+ case event
37
+ when :flush
38
+ @allow_flush = true
39
+ send_to_collector
40
+ else
41
+ logger.debug 'Received event'
42
+ events << event
43
+ if @allow_flush && events.count > 10
44
+ send_to_collector
45
+ end
46
+ end
47
+ rescue Exception => e
48
+ logger.debug ['Event error:', event.inspect, e.message]
49
+ @allow_flush = false
50
+ end
51
+ end
52
+ end
53
+ start_flusher
54
+
55
+ self
56
+ end
57
+
58
+ def start_flusher
59
+ Thread.new do
60
+ loop do
61
+ sleep @multiplier * @flush_rate
62
+ push(:flush)
63
+ end
64
+ end
65
+ end
66
+
67
+ def push(event)
68
+ @queue << event
69
+ end
70
+
71
+ def logger
72
+ AppMonit.logger
73
+ end
74
+
75
+ def send_to_collector
76
+ if @events.any?
77
+ logger.debug 'Sending to collector'
78
+ AppMonit::Http.post('/v1/events', event: events)
79
+ end
80
+ reset
81
+ end
82
+ end
83
+ end
@@ -12,7 +12,7 @@ describe AppMonit::Query do
12
12
  %w(count count_unique minimum maximum average sum funnel).each do |method_name|
13
13
  describe method_name do
14
14
  it 'gets the results with the given params' do
15
- stub_request(:get, /api.appmon.it\/v1\/queries\/#{method_name}/).to_return(code: '200', body: {result: '0'}.to_json)
15
+ stub_request(:get, /api.appmon.it\/v1\/queries\/#{method_name}/).to_return(body: {result: '0'}.to_json)
16
16
 
17
17
  params = { valid: 'params' }
18
18
  subject.send(method_name, 'collection_name', params)
data/test.rb ADDED
@@ -0,0 +1,13 @@
1
+ $:<<'lib'
2
+ require 'app_monit'
3
+
4
+ AppMonit::Config.end_point = 'http://localhost:3000'
5
+ AppMonit::Config.api_key = 'SOMEKEY'
6
+ AppMonit::Config.enabled = true
7
+ AppMonit::Config.async = true
8
+ AppMonit::Config.flush_rate = 1
9
+
10
+ AppMonit::Event.create('test', with: 'payload')
11
+
12
+ AppMonit.logger.debug 'Press enter to exit'
13
+ gets
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_monit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Redmar Kerkhoff
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-28 00:00:00.000000000 Z
12
+ date: 2015-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -81,20 +81,6 @@ dependencies:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
83
  version: 1.17.1
84
- - !ruby/object:Gem::Dependency
85
- name: guard-minitest
86
- requirement: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: '0'
91
- type: :development
92
- prerelease: false
93
- version_requirements: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- version: '0'
98
84
  description: Client gem for pushing events from ruby to the appmon.it service
99
85
  email:
100
86
  - redmar@appmon.it
@@ -120,10 +106,12 @@ files:
120
106
  - lib/app_monit/http.rb
121
107
  - lib/app_monit/query.rb
122
108
  - lib/app_monit/version.rb
109
+ - lib/app_monit/worker.rb
123
110
  - spec/app_monit/event_spec.rb
124
111
  - spec/app_monit/http_spec.rb
125
112
  - spec/app_monit/query_spec.rb
126
113
  - spec/spec_helper.rb
114
+ - test.rb
127
115
  homepage: https://appmon.it
128
116
  licenses:
129
117
  - MIT
@@ -144,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
132
  version: '0'
145
133
  requirements: []
146
134
  rubyforge_project:
147
- rubygems_version: 2.2.1
135
+ rubygems_version: 2.4.5
148
136
  signing_key:
149
137
  specification_version: 4
150
138
  summary: Client gem for pushing events from ruby to the appmon.it service