metrics_logger 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 50a0dd2beab583c7d2ec7de9473bd9ac44baade0
4
+ data.tar.gz: 3275069ade2f2504916022e68d76259ac306a34c
5
+ SHA512:
6
+ metadata.gz: 3fa48ca3d2cbc862dca177aad53ee6bd8ea58f9362b09be50da3cda9e18daf680fbb137dc416b7dbe927ba833ee36608f8a01690ee45920e6400316bd7263907
7
+ data.tar.gz: 3c796f567c3a7a5fdfcc60d80df39b508f434169c8936177d7b0f6f022e41325ff8eadb51ee09b31d2cafe48f3efe36573686aadb5859e6b231fa2ab51022804
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in metrics_logger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ben Zhang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # MetricsLogger
2
+
3
+ For OpenTSDB data logging. For duration data and arbitrary logged values, total, count, average values are calculated per minute interval and sent to the server every minute. The logger singleton runs in a separate thread so this gem works well with jruby which supports native threads. If endpoint returns error or times out, backup_endpoint is tried.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'metrics_logger'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install metrics_logger
18
+
19
+ ## Usage
20
+
21
+ ### Configure Logger
22
+ ```ruby
23
+ MetricsLogger.configure do |config|
24
+ config.app_name = "my_app" # used to make value key to opentsdb. eg: my_app.metric_name.total
25
+ config.endpoint = "http://my_opentsdb_server:4242/api/put"
26
+ config.backup_endpoint = "http://my_backup_opentsdb_server:4242/api/put"
27
+ config.sync_interval = 60 #in seconds
28
+ ```
29
+
30
+ ### Logging Things
31
+ #### 1. log arbitrary values
32
+ ```ruby
33
+ MetricsLogger.log("metric_name", key_1: "value_1", key_2: "value_2")
34
+ ```
35
+
36
+ #### 2. log duration with block
37
+ ```ruby
38
+ MetricsLogger.log_duration "metric_name" do
39
+ #do something
40
+ end
41
+ ```
42
+ **Note: Remember that variables declared inside the block will not be accessible outside the block. If you want to initialize a varaible and use it after block closes, declare the variable before the block.**
43
+
44
+ #### 3. log duration without block
45
+ ```ruby
46
+ logger.MetricsLogger.new
47
+ #do something
48
+ logger.log_duration "metric_name"
49
+ ```
50
+
51
+ #### 4. Log values once per minute
52
+ ```ruby
53
+ MetricsLogger.sample("metric_name", object: some_ruby_object, method: "count")
54
+ ```
55
+
56
+ ###Start Logger Process
57
+ ```ruby
58
+ Thread.new { MetricsLogger.start }
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( https://github.com/[my-github-username]/metrics_logger/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,13 @@
1
+ require "metrics_logger/version"
2
+ require "metrics_logger/configuration"
3
+ require "metrics_logger/metrics_logger"
4
+ require 'thread_safe'
5
+ require 'timecop'
6
+ require 'faraday'
7
+ require 'json'
8
+
9
+ module MetricsLogger
10
+ def self.method_missing(method, *args, &block)
11
+ MetricsLogger.send(method, *args, &block)
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module MetricsLogger
2
+ class Configuration
3
+ attr_accessor :endpoint, :backup_endpoint, :sync_interval, :app_name, :tags
4
+
5
+ def initialize
6
+ @endpoint = nil
7
+ @backup_endpoint = nil
8
+ @sync_interval = 60
9
+ @app_name = nil
10
+ @tags = {}
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,133 @@
1
+ require 'timeout'
2
+
3
+ module MetricsLogger
4
+ class MetricsLogger
5
+ TIMEOUT = 10
6
+
7
+ def initialize
8
+ @start_time_in_milliseconds = Time.now.to_f * 1000
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.configure
16
+ yield(configuration)
17
+ end
18
+
19
+ def log_duration(type)
20
+ end_time = Time.now
21
+ end_time_in_milliseconds = end_time.to_f * 1000
22
+ duration = end_time_in_milliseconds - @start_time_in_milliseconds
23
+ metric = {duration: duration}
24
+ self.class.enqueue(type, metric)
25
+ end
26
+
27
+ #Class Methods
28
+ def self.log(type, metrics)
29
+ metrics.each do |key, value|
30
+ self.enqueue(type, key => value)
31
+ end
32
+ end
33
+
34
+ def self.log_duration(type, &blk)
35
+ logger = self.new
36
+ yield
37
+ logger.log_duration(type)
38
+ end
39
+
40
+ def self.enqueue(type, metric)
41
+ key = metric.to_a[0][0]
42
+ value = metric.to_a[0][1]
43
+ puts caller.inspect if value.nil?
44
+
45
+ sub_type = "#{type}.#{key}"
46
+
47
+ if queue[sub_type].nil?
48
+ Mutex.new.synchronize do
49
+ queue[sub_type] ||= ThreadSafe::Array.new
50
+ end
51
+ end
52
+
53
+ queue[sub_type].push value
54
+ nil
55
+ end
56
+
57
+ def self.queue
58
+ if @queue.nil?
59
+ Mutex.new.synchronize do
60
+ @queue ||= ThreadSafe::Hash.new
61
+ end
62
+ else
63
+ @queue
64
+ end
65
+ end
66
+
67
+ def self.sampling_definitions
68
+ if @sampling_definitions.nil?
69
+ Mutex.new.synchronize do
70
+ @sampling_definitions ||= ThreadSafe::Array.new
71
+ end
72
+ else
73
+ @sampling_definitions
74
+ end
75
+ end
76
+
77
+ def self.reset
78
+ @queue = ThreadSafe::Hash.new
79
+ @sampling_definitions = ThreadSafe::Array.new
80
+ end
81
+
82
+ def self.sync
83
+ @queue, batched_data = ThreadSafe::Hash.new, queue
84
+
85
+ data = []
86
+ timestamp = (Time.now.to_f * 1000).to_i
87
+ app_name = configuration.app_name
88
+
89
+ batched_data.each do |sub_type, values|
90
+ total = values.compact.inject(:+)
91
+ count = values.count
92
+ average = total.to_f / count
93
+
94
+ data << {metric: "#{app_name}.#{sub_type}.total", timestamp: timestamp, value: total, tags: configuration.tags}
95
+ data << {metric: "#{app_name}.#{sub_type}.count", timestamp: timestamp, value: count, tags: configuration.tags}
96
+ data << {metric: "#{app_name}.#{sub_type}.average", timestamp: timestamp, value: average, tags: configuration.tags}
97
+ end
98
+
99
+ sampling_definitions.each do |defintion|
100
+ value = defintion[:object].send(defintion[:method])
101
+ data << {metric: "#{app_name}.#{defintion[:name]}", timestamp: timestamp, value: value, tags: configuration.tags}
102
+ end
103
+
104
+ send_data(data) if data[0]
105
+ end
106
+
107
+ def self.send_data(data)
108
+ begin
109
+ Timeout::timeout(TIMEOUT) { Faraday.post(configuration.endpoint, data.to_json) }
110
+ rescue
111
+ begin
112
+ Timeout::timeout(TIMEOUT) { Faraday.post(configuration.backup_endpoint, data.to_json) }
113
+ rescue
114
+ puts "#{Time.now} Cannot reach metrics server."
115
+ end
116
+ end
117
+ end
118
+
119
+ def self.sample(name, options = {})
120
+ sampling_definitions << {name: name, object: options[:object], method: options[:method]}
121
+ end
122
+
123
+ def self.start
124
+ raise "Must specify app name in MetricsLogger configuration" unless configuration.app_name
125
+ raise "Must specify endpoint in MetricsLogger configuration" unless configuration.endpoint
126
+
127
+ while true
128
+ sleep configuration.sync_interval
129
+ sync
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,3 @@
1
+ module MetricsLogger
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'metrics_logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "metrics_logger"
8
+ spec.version = MetricsLogger::VERSION
9
+ spec.authors = ["Ben Zhang"]
10
+ spec.email = ["benzhangpro@gmail.com"]
11
+ spec.summary = %q{sends application metrics to opentsdb database.}
12
+ spec.description = %q{sync with server asynchronusly in separate thread.}
13
+ spec.homepage = "http://github.com/zben/metrics_logger"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.3.2"
23
+ spec.add_development_dependency "rspec", "~> 3.0.0"
24
+ spec.add_development_dependency "timecop", "~> 0.7.1"
25
+
26
+ spec.add_runtime_dependency "faraday", "~> 0.9.0"
27
+ spec.add_runtime_dependency "thread_safe", "~> 0.3.4"
28
+ spec.add_runtime_dependency "json", "~> 1.8.1"
29
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe MetricsLogger do
4
+ after do
5
+ MetricsLogger.reset
6
+ Timecop.return
7
+ end
8
+
9
+ describe ".configure" do
10
+ before do
11
+ MetricsLogger.configure do |config|
12
+ config.endpoint = "http://movefast1.snc1:4242/test"
13
+ config.backup_endpoint = "http://movefast5.snc1:4242/test"
14
+ config.sync_interval = 60
15
+ end
16
+ end
17
+
18
+ it "sets correct endpoint and backup endpoint" do
19
+ expect(MetricsLogger.configuration.endpoint).to eq("http://movefast1.snc1:4242/test")
20
+ expect(MetricsLogger.configuration.backup_endpoint).to eq("http://movefast5.snc1:4242/test")
21
+ expect(MetricsLogger.configuration.sync_interval).to eq(60)
22
+ end
23
+ end
24
+
25
+ describe "#log_duration" do
26
+ it "logs correct duration" do
27
+ Timecop.freeze(Time.parse("2014-01-01 1:0:0"))
28
+ logger = MetricsLogger.new
29
+
30
+ Timecop.freeze(Time.parse("2014-01-01 1:0:1"))
31
+ logger.log_duration("stream")
32
+
33
+ Timecop.freeze(Time.parse("2014-01-01 2:0:0"))
34
+ logger = MetricsLogger.new
35
+
36
+ Timecop.freeze(Time.parse("2014-01-01 2:0:2"))
37
+ logger.log_duration("stream")
38
+
39
+ expect(MetricsLogger.queue).to eq({"stream.duration" => [1000, 2000]})
40
+ end
41
+ end
42
+
43
+ describe ".log" do
44
+ it "logs correct values" do
45
+ MetricsLogger.log('stream', lag: 100, other: 200)
46
+ MetricsLogger.log('stream', lag: 200, other: 400)
47
+
48
+ expect(MetricsLogger.queue).to eq({
49
+ "stream.lag" => [100, 200],
50
+ "stream.other" => [200, 400]
51
+ })
52
+
53
+ end
54
+ end
55
+
56
+ describe ".log_duration" do
57
+ it "logs correct values" do
58
+ MetricsLogger.log_duration "stream" do
59
+ sleep 0.1
60
+ end
61
+
62
+ expect(MetricsLogger.queue["stream.duration"][0]).to be_within(5).of(100)
63
+ end
64
+ end
65
+
66
+ describe ".sync" do
67
+ before do
68
+ Timecop.freeze(Time.parse("2014-01-01 1:0:0"))
69
+ MetricsLogger.configure do |config|
70
+ config.app_name = "test"
71
+ config.tags = {host: "hostname", env: "production"}
72
+ end
73
+ end
74
+
75
+ it "sends correct api call" do
76
+ MetricsLogger.log('stream', lag: 100, other: 200)
77
+ MetricsLogger.log('stream', lag: 200, other: 400)
78
+ MetricsLogger.log('stream', lag: 300, other: 600)
79
+
80
+ test_object = {a: 1, b: 2}
81
+ MetricsLogger.sample("queue_size", object: test_object, method: "size")
82
+
83
+ expected_data = [
84
+ {metric: "test.stream.lag.total", timestamp: 1388566800000, value: 600, tags: {host: "hostname", env: "production"}},
85
+ {metric: "test.stream.lag.count", timestamp: 1388566800000, value: 3, tags: {host: "hostname", env: "production"}},
86
+ {metric: "test.stream.lag.average", timestamp: 1388566800000, value: 200.0, tags: {host: "hostname", env: "production"}},
87
+ {metric: "test.stream.other.total", timestamp: 1388566800000, value: 1200, tags: {host: "hostname", env: "production"}},
88
+ {metric: "test.stream.other.count", timestamp: 1388566800000, value: 3, tags: {host: "hostname", env: "production"}},
89
+ {metric: "test.stream.other.average", timestamp: 1388566800000, value: 400.0, tags: {host: "hostname", env: "production"}},
90
+ {metric: "test.queue_size", timestamp: 1388566800000, value: 2, tags: {host: "hostname", env: "production"}}
91
+ ]
92
+
93
+ expect(Faraday).to receive(:post).with(MetricsLogger.configuration.endpoint, expected_data.to_json)
94
+
95
+ MetricsLogger.sync
96
+ end
97
+ end
98
+
99
+ describe ".sample" do
100
+ it "adds to sampling_definitions correctly" do
101
+ object = {test: "test", test2: "test"}
102
+ MetricsLogger.sample("test", object: object, method: "size")
103
+ expect(MetricsLogger.sampling_definitions).to eq([{name: "test", object: object, method: "size"}])
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,10 @@
1
+ require 'metrics_logger'
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ Bundler.require :all
6
+
7
+ require 'thread_safe'
8
+ require 'timecop'
9
+ require 'faraday'
10
+ require 'json'
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metrics_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Ben Zhang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: '1.6'
25
+ prerelease: false
26
+ type: :development
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 10.3.2
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 10.3.2
39
+ prerelease: false
40
+ type: :development
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: 3.0.0
53
+ prerelease: false
54
+ type: :development
55
+ - !ruby/object:Gem::Dependency
56
+ name: timecop
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.1
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 0.7.1
67
+ prerelease: false
68
+ type: :development
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.0
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: 0.9.0
81
+ prerelease: false
82
+ type: :runtime
83
+ - !ruby/object:Gem::Dependency
84
+ name: thread_safe
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 0.3.4
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: 0.3.4
95
+ prerelease: false
96
+ type: :runtime
97
+ - !ruby/object:Gem::Dependency
98
+ name: json
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: 1.8.1
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ~>
107
+ - !ruby/object:Gem::Version
108
+ version: 1.8.1
109
+ prerelease: false
110
+ type: :runtime
111
+ description: sync with server asynchronusly in separate thread.
112
+ email:
113
+ - benzhangpro@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - lib/metrics_logger.rb
124
+ - lib/metrics_logger/configuration.rb
125
+ - lib/metrics_logger/metrics_logger.rb
126
+ - lib/metrics_logger/version.rb
127
+ - metrics_logger.gemspec
128
+ - spec/metrics_logger_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: http://github.com/zben/metrics_logger
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.2.2
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: sends application metrics to opentsdb database.
154
+ test_files:
155
+ - spec/metrics_logger_spec.rb
156
+ - spec/spec_helper.rb