metricsense 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ /vendor/
2
+ /pkg/
3
+ /coverage/
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+
2
+ = MetricSense for Ruby
3
+
4
+ Log everything in JSON - MetricSense is an application metrics collection API.
5
+
6
+ See also https://github.com/treasure-data/fluent-plugin-metricsense
7
+
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ #require 'rake/testtask'
5
+ #
6
+ #Rake::TestTask.new(:test) do |test|
7
+ # test.libs << 'lib' << 'test'
8
+ # test.test_files = FileList['test/plugin/*.rb']
9
+ # test.verbose = true
10
+ #end
11
+
12
+ #task :coverage do |t|
13
+ # ENV['SIMPLE_COV'] = '1'
14
+ # Rake::Task["test"].invoke
15
+ #end
16
+
17
+ task :default => [:build]
@@ -0,0 +1,3 @@
1
+ module MetricSense
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,142 @@
1
+ #
2
+ # MetricSense for Ruby
3
+ #
4
+ # Copyright (C) 2012 Sadayuki Furuhashi
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'forwardable'
19
+ require 'fluent-logger'
20
+
21
+ module MetricSense
22
+ class Event
23
+ def initialize(tag)
24
+ @tag = tag
25
+ @time = Time.now
26
+ @values = {}
27
+ @segments = {}
28
+ @facts = {}
29
+ end
30
+
31
+ attr_reader :values, :segments, :facts
32
+
33
+ def tag(v=nil)
34
+ @tag = v if v
35
+ @tag
36
+ end
37
+
38
+ def time(v=nil)
39
+ @time = v if v
40
+ @time
41
+ end
42
+
43
+ def value(map)
44
+ @values.merge!(map)
45
+ self
46
+ end
47
+
48
+ def segment(map)
49
+ @segments.merge!(map)
50
+ self
51
+ end
52
+
53
+ def fact(map)
54
+ @facts.merge!(map)
55
+ self
56
+ end
57
+ end
58
+
59
+ class Collector
60
+ def initialize(config)
61
+ @config = config
62
+ tag_prefix = @config['tag_prefix']
63
+ @event_prefix = @config['event_prefix'] || 'metricsense.event'
64
+ @metric_prefix = @config['metric_prefix'] || 'metricsense.metric'
65
+ opts = {}
66
+ opts[:host] = @config['host'] if @config['host']
67
+ opts[:port] = @config['port'] if @config['port']
68
+ @logger = Fluent::Logger::FluentLogger.new(tag_prefix, opts)
69
+ #@logger = Fluent::Logger::ConsoleLogger.new(STDOUT)
70
+ end
71
+
72
+ attr_reader :config
73
+
74
+ def measure(e)
75
+ record = e.facts.merge(e.segments).merge!(e.values)
76
+ @logger.post_with_time("#{@event_prefix}.#{e.tag}", record, e.time)
77
+
78
+ values = {:count=>1}.merge!(e.values)
79
+ values.each_pair {|k,v|
80
+ tag = "#{@metric_prefix}.#{e.tag}.#{k}"
81
+ record = e.segments.merge(:value=>v)
82
+ @logger.post_with_time(tag, record, e.time)
83
+ }
84
+ end
85
+ end
86
+
87
+ class Context
88
+ def initialize(collector)
89
+ @collector = collector
90
+ @events = {}
91
+ end
92
+
93
+ attr_reader :events
94
+
95
+ def event(tag)
96
+ @events[tag] ||= Event.new(tag)
97
+ end
98
+
99
+ alias [] event
100
+
101
+ def clear!
102
+ @events.clear
103
+ nil
104
+ end
105
+
106
+ def measure!
107
+ return nil unless @collector
108
+ @events.keys.each {|k|
109
+ event = @events[k]
110
+ @collector.measure(event) if event.tag
111
+ @events.delete(k)
112
+ }
113
+ nil
114
+ end
115
+ end
116
+
117
+ module ClassMethods
118
+ extend Forwardable
119
+
120
+ def config(conf)
121
+ @collector = Collector.new(conf)
122
+ self
123
+ end
124
+
125
+ attr_reader :collector
126
+
127
+ def measure(tag, values={})
128
+ @collector.measure Event.new(tag).value(values) if @collector
129
+ end
130
+
131
+ def context
132
+ Thread.current[MetricSense.to_s] ||= Context.new(@collector)
133
+ end
134
+
135
+ def_delegators 'context', :event, :[], :clear!, :measure!
136
+ def_delegators 'event(nil)', :tag, :value, :segment, :fact
137
+ def_delegators 'event(nil)', :values, :segments, :facts
138
+ end
139
+
140
+ extend ClassMethods
141
+ end
142
+
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'metricsense/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "metricsense"
6
+ s.description = "MetricSense event collection API for Ruby"
7
+ s.summary = s.description
8
+ s.homepage = "https://github.com/treasure-data/metricsense"
9
+ s.version = MetricSense::VERSION
10
+ s.authors = ["Sadayuki Furuhashi"]
11
+ s.email = "sf@treasure-data.com"
12
+ s.has_rdoc = false
13
+ s.require_paths = ['lib']
14
+ #s.platform = Gem::Platform::RUBY
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+
19
+ s.add_development_dependency "rake", ">= 0.8.7"
20
+ s.add_development_dependency 'bundler', ['>= 1.0.0']
21
+ s.add_development_dependency "simplecov", ">= 0.5.4"
22
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metricsense
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sadayuki Furuhashi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.7
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.0.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.5.4
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.4
62
+ description: MetricSense event collection API for Ruby
63
+ email: sf@treasure-data.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - Gemfile
70
+ - README.rdoc
71
+ - Rakefile
72
+ - lib/metricsense.rb
73
+ - lib/metricsense/version.rb
74
+ - metricsense.gemspec
75
+ homepage: https://github.com/treasure-data/metricsense
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.23
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: MetricSense event collection API for Ruby
99
+ test_files: []
100
+ has_rdoc: false