simple-metrics 0.0.10-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/.gitignore +17 -0
  2. data/.rbenv-version +1 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +13 -0
  6. data/README.md +126 -0
  7. data/Rakefile +7 -0
  8. data/doc/NurseRatched.html +246 -0
  9. data/doc/Rdio.html +245 -0
  10. data/doc/Samovar.html +241 -0
  11. data/doc/Simple/Metrics/Check.html +312 -0
  12. data/doc/Simple/Metrics/Graphite.html +268 -0
  13. data/doc/Simple/Metrics/HEALTHY.html +144 -0
  14. data/doc/Simple/Metrics/Health.html +292 -0
  15. data/doc/Simple/Metrics/Healthchecks.html +425 -0
  16. data/doc/Simple/Metrics/Meter.html +284 -0
  17. data/doc/Simple/Metrics/RackMetrics.html +371 -0
  18. data/doc/Simple/Metrics/Timer.html +235 -0
  19. data/doc/Simple/Metrics/UNHEALTHY.html +144 -0
  20. data/doc/Simple/Metrics/WARNING.html +144 -0
  21. data/doc/Simple/Metrics.html +523 -0
  22. data/doc/Simple.html +117 -0
  23. data/doc/_index.html +277 -0
  24. data/doc/class_list.html +53 -0
  25. data/doc/css/common.css +1 -0
  26. data/doc/css/full_list.css +57 -0
  27. data/doc/css/style.css +328 -0
  28. data/doc/file.README.html +210 -0
  29. data/doc/file_list.html +55 -0
  30. data/doc/frames.html +28 -0
  31. data/doc/index.html +210 -0
  32. data/doc/js/app.js +214 -0
  33. data/doc/js/full_list.js +173 -0
  34. data/doc/js/jquery.js +4 -0
  35. data/doc/method_list.html +196 -0
  36. data/doc/top-level-namespace.html +114 -0
  37. data/lib/java/jackson-core-asl-1.9.5.jar +0 -0
  38. data/lib/java/jackson-mapper-asl-1.9.5.jar +0 -0
  39. data/lib/java/metrics-core-2.1.1.jar +0 -0
  40. data/lib/java/metrics-graphite-2.1.1.jar +0 -0
  41. data/lib/java/metrics-servlet-2.1.1.jar +0 -0
  42. data/lib/java/slf4j-api-1.6.4.jar +0 -0
  43. data/lib/simple/metrics/graphite.rb +23 -0
  44. data/lib/simple/metrics/health.rb +15 -0
  45. data/lib/simple/metrics/healthcheck.rb +65 -0
  46. data/lib/simple/metrics/meter.rb +22 -0
  47. data/lib/simple/metrics/rack_metrics.rb +32 -0
  48. data/lib/simple/metrics/timer.rb +22 -0
  49. data/lib/simple/metrics.rb +51 -0
  50. data/lib/simple/version.rb +5 -0
  51. data/lib/simple.rb +6 -0
  52. data/simple-metrics.gemspec +21 -0
  53. data/spec/metrics/graphite_spec.rb +9 -0
  54. data/spec/metrics/healthcheck_spec.rb +55 -0
  55. data/spec/metrics/meter_spec.rb +20 -0
  56. data/spec/metrics/timer_spec.rb +29 -0
  57. data/spec/spec_helper.rb +13 -0
  58. metadata +174 -0
@@ -0,0 +1,65 @@
1
+ java_import com.yammer.metrics.core.HealthCheck
2
+
3
+ module Simple
4
+ module Metrics
5
+ module Healthchecks
6
+ include Simple::Metrics
7
+
8
+ # Create a new healthcheck
9
+ #
10
+ # @param [String] name The name of the healthcheck, eg `check_db`.
11
+ # @return [MetricName] the newly created metric name
12
+ def new_healthcheck(name, &blk)
13
+ healthcheck = Check.new(name, &blk)
14
+ Java::ComYammerMetrics::HealthChecks.register(healthcheck)
15
+ run_healthcheck_loop
16
+ end
17
+
18
+ # Run the healthcheck loop
19
+ # This starts a new thread that runs all of the defined healthcheck loops.
20
+ def run_healthcheck_loop
21
+ return if @running_healthcheck_loop
22
+ Thread.new do
23
+ loop do
24
+ run_all_healthchecks
25
+ sleep 5
26
+ end
27
+ end
28
+ @running_healthcheck_loop = true
29
+ end
30
+
31
+ # Run all of the healthchecks that have been defined
32
+ def run_all_healthchecks
33
+ Java::ComYammerMetrics::HealthChecks.runHealthChecks
34
+ end
35
+ end
36
+
37
+ class Check < Java::ComYammerMetricsCore::HealthCheck
38
+ def initialize(name, &blk)
39
+ super(name)
40
+ @blk = blk
41
+ end
42
+
43
+ # Call the block and capture the return value.
44
+ # If the result is `Simple::Metrics::HEALTHY`, mark the healthcheck as healthy.
45
+ # If the result is a `Simple::Metrics::WARNING`, mark the healthcheck in the warning state.
46
+ # If the result is a `Simple::Metrics::UNHEALTHY`, or throws an error,
47
+ # mark the healthcheck as unhealthy.
48
+ def check
49
+ begin
50
+ r = @blk.call
51
+ case (r.is_a?(Class) ? r.new : r)
52
+ when Simple::Metrics::HEALTHY
53
+ Java::ComYammerMetricsCore::HealthCheck::Result.healthy
54
+ when Simple::Metrics::WARNING
55
+ Java::ComYammerMetricsCore::HealthCheck::Result.warning
56
+ when Simple::Metrics::UNHEALTHY
57
+ Java::ComYammerMetricsCore::HealthCheck::Result.unhealthy(r.message)
58
+ end
59
+ rescue StandardError => e
60
+ Java::ComYammerMetricsCore::HealthCheck::Result.unhealthy(e.message)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,22 @@
1
+ module Simple
2
+ module Metrics
3
+ module Meter
4
+ include Simple::Metrics
5
+
6
+ # Create a new meter
7
+ #
8
+ # @param [String] name The name of the meter
9
+ # @param [String] klass_name The name of the class, usually the application. Defaults to
10
+ # `self.class.name`
11
+ def define_meter(name, klass_name = self.class.name)
12
+ type = "meter"
13
+ metric_name = new_metric_name(klass_name, name, type)
14
+ meter = Java::ComYammerMetrics::Metrics.new_meter(metric_name, name.to_s,
15
+ Simple::Metrics::DEFAULT_RATE_UNIT)
16
+ define_method("#{name}") do
17
+ meter
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ module Simple
2
+ module Metrics
3
+ class RackMetrics
4
+ extend Simple::Metrics::Meter
5
+
6
+ # Rack middleware for capturing response codes
7
+ #
8
+ # Creates a series of response meters that will be marked based on the response
9
+ # code that is returned by rack.
10
+ # @param [Object] app Rack application
11
+ # @param [String] application_name The name of the application
12
+ def initialize(app, application_name)
13
+ @app = app
14
+
15
+ self.class.define_meter '2xx-responses', application_name
16
+ self.class.define_meter '3xx-responses', application_name
17
+ self.class.define_meter '4xx-responses', application_name
18
+ self.class.define_meter '5xx-responses', application_name
19
+ end
20
+
21
+ def call(env)
22
+ begin
23
+ status, headers, body = @app.call(env)
24
+ ensure
25
+ status_code = (status ||= 500).to_s[0]
26
+ status_method = "#{status_code}xx-responses".to_sym
27
+ self.send(status_method).mark
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,22 @@
1
+ module Simple
2
+ module Metrics
3
+ module Timer
4
+ # Create creates a new timer, registers it with the metrics registry,
5
+ # yields the timer to the block and stops it when the block returns.
6
+ #
7
+ # @param [String] name The name of the timer.
8
+ def timer(name)
9
+ metric_name = new_metric_name(sanitize_classname(self.class.name), name, "timer")
10
+ new_timer = metrics_registry.newTimer(metric_name,
11
+ DEFAULT_DURATION_UNIT,
12
+ DEFAULT_RATE_UNIT)
13
+ captured_timer = new_timer.time
14
+ begin
15
+ yield new_timer
16
+ ensure
17
+ captured_timer.stop
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ require 'java'
2
+ require 'java/metrics-core-2.1.1'
3
+ require 'java/slf4j-api-1.6.4'
4
+ require 'java/jackson-core-asl-1.9.5'
5
+ require 'java/jackson-mapper-asl-1.9.5'
6
+ require 'java/metrics-servlet-2.1.1'
7
+
8
+ require_relative 'metrics/timer'
9
+ require_relative 'metrics/meter'
10
+ require_relative 'metrics/healthcheck'
11
+ require_relative 'metrics/health'
12
+ require_relative 'metrics/graphite'
13
+
14
+ java_import java.util.concurrent.TimeUnit
15
+ java_import com.yammer.metrics.Metrics
16
+ java_import com.yammer.metrics.core.MetricName
17
+
18
+ module Simple
19
+ module Metrics
20
+ DEFAULT_DURATION_UNIT = TimeUnit::MILLISECONDS
21
+ DEFAULT_RATE_UNIT = TimeUnit::SECONDS
22
+ DEFAULT_TIMING_UNIT = TimeUnit::NANOSECONDS
23
+
24
+ include Timer
25
+
26
+ # Sanitize classnames for graphite
27
+ #
28
+ # @param [Object] klass_name The request object
29
+ # @return [String] The class name without colons
30
+ def sanitize_classname(klass_name)
31
+ klass_name.gsub(":","")
32
+ end
33
+
34
+ # The default metrics registry
35
+ #
36
+ # @return [Java::ComYammerMetrics::Metrics] the default metrics registry
37
+ def metrics_registry
38
+ Java::ComYammerMetrics::Metrics.defaultRegistry
39
+ end
40
+
41
+ # Create a new metric name
42
+ #
43
+ # @param [String] klass_name The name of the class, usually the application name
44
+ # @param [String] name The name of the metric
45
+ # @param [String] type The type of metric
46
+ # @return [MetricName] the newly created metric name
47
+ def new_metric_name(klass_name, name, type)
48
+ MetricName.new(klass_name.downcase, type, name.to_s)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ module Simple
2
+ module Metrics
3
+ VERSION = "0.0.10"
4
+ end
5
+ end
data/lib/simple.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "simple/version"
2
+
3
+ module Simple
4
+ end
5
+
6
+ require "simple/metrics"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/simple/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["dan herrera", "james pozdena"]
6
+ gem.email = ["dan@simple.com"]
7
+ gem.platform = 'java'
8
+ gem.description = %q{A simple JRuby wrapper around codahale's metrics package}
9
+ gem.summary = %q{MEASURE ALL THE THINGS!}
10
+ gem.homepage = "https://github.com/SimpleFinance/simple-metrics"
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "simple-metrics"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = Simple::Metrics::VERSION
16
+
17
+ gem.add_development_dependency 'rspec'
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'yard'
20
+ gem.add_development_dependency 'kramdown'
21
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Simple::Metrics do
4
+ class GraphiteSpec
5
+ extend Simple::Metrics::Graphite
6
+
7
+ enable_graphite_reporter('graphite.example.com', 2003, 'example.class')
8
+ end
9
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Simple::Metrics::Healthchecks do
4
+ it "should register a new healthcheck" do
5
+ class NurseRatched
6
+ extend Simple::Metrics::Healthchecks
7
+
8
+ class << self
9
+ def status=(v)
10
+ @status = v
11
+ end
12
+ def status
13
+ @status
14
+ end
15
+ end
16
+
17
+ self.status = "healthy"
18
+
19
+ new_healthcheck("temperature") do
20
+ case self.status
21
+ when "healthy"
22
+ Simple::Metrics::HEALTHY
23
+ when "unhealthy"
24
+ Simple::Metrics::UNHEALTHY.new("Too Many Tacos!")
25
+ when "dead"
26
+ raise "Pants"
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ it "should run all healthchecks" do
33
+ NurseRatched.status = "healthy"
34
+ NurseRatched.run_all_healthchecks.entrySet.each do |result|
35
+ result.getValue.isHealthy.should be_true
36
+ end
37
+ end
38
+
39
+ it "should return the last error message when unhealthy" do
40
+ NurseRatched.status = "unhealthy"
41
+ NurseRatched.run_all_healthchecks.entrySet.each do |result|
42
+ result.getValue.isHealthy.should be_false
43
+ result.getValue.getMessage.should == "Too Many Tacos!"
44
+ end
45
+ end
46
+
47
+ it "should return an unhealthy if the check raises an error" do
48
+ NurseRatched.status = "dead"
49
+ NurseRatched.run_all_healthchecks.entrySet.each do |result|
50
+ result.getValue.isHealthy.should be_false
51
+ result.getValue.getMessage.should == "Pants"
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Simple::Metrics do
4
+ class Rdio
5
+ extend Simple::Metrics::Meter
6
+
7
+ define_meter :bump_meter
8
+
9
+ def bump_dat
10
+ bump_meter.mark
11
+ end
12
+ end
13
+
14
+ let(:rdio) { Rdio.new }
15
+
16
+ it "should increment a meter" do
17
+ rdio.bump_dat
18
+ rdio.bump_meter.count.should == 1
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Simple::Metrics::Timer do
4
+ context "setting a metrics timer" do
5
+ class Samovar
6
+ include Simple::Metrics
7
+ attr_accessor :tea_timer
8
+
9
+ def make_tea
10
+ timer("tea time") do |tea_timer|
11
+ @tea_timer = tea_timer
12
+ "Brewing..."
13
+ end
14
+ end
15
+ end
16
+
17
+ let(:samovar) { Samovar.new }
18
+
19
+ it "should yield a new timer to the block" do
20
+ samovar.make_tea
21
+ samovar.tea_timer.count.should_not be_zero
22
+ end
23
+
24
+ it "should create a new timer" do
25
+ samovar.make_tea
26
+ samovar.tea_timer.should be_a_kind_of(Java::ComYammerMetricsCore::Timer)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ end
12
+
13
+ require 'simple/metrics'
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-metrics
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.10
6
+ platform: java
7
+ authors:
8
+ - dan herrera
9
+ - james pozdena
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-06 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ version_requirements: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ none: false
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ none: false
29
+ prerelease: false
30
+ type: :development
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ version_requirements: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ none: false
39
+ requirement: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ none: false
45
+ prerelease: false
46
+ type: :development
47
+ - !ruby/object:Gem::Dependency
48
+ name: yard
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ none: false
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ none: false
61
+ prerelease: false
62
+ type: :development
63
+ - !ruby/object:Gem::Dependency
64
+ name: kramdown
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ none: false
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ none: false
77
+ prerelease: false
78
+ type: :development
79
+ description: A simple JRuby wrapper around codahale's metrics package
80
+ email:
81
+ - dan@simple.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rbenv-version
88
+ - .rspec
89
+ - Gemfile
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - doc/NurseRatched.html
94
+ - doc/Rdio.html
95
+ - doc/Samovar.html
96
+ - doc/Simple.html
97
+ - doc/Simple/Metrics.html
98
+ - doc/Simple/Metrics/Check.html
99
+ - doc/Simple/Metrics/Graphite.html
100
+ - doc/Simple/Metrics/HEALTHY.html
101
+ - doc/Simple/Metrics/Health.html
102
+ - doc/Simple/Metrics/Healthchecks.html
103
+ - doc/Simple/Metrics/Meter.html
104
+ - doc/Simple/Metrics/RackMetrics.html
105
+ - doc/Simple/Metrics/Timer.html
106
+ - doc/Simple/Metrics/UNHEALTHY.html
107
+ - doc/Simple/Metrics/WARNING.html
108
+ - doc/_index.html
109
+ - doc/class_list.html
110
+ - doc/css/common.css
111
+ - doc/css/full_list.css
112
+ - doc/css/style.css
113
+ - doc/file.README.html
114
+ - doc/file_list.html
115
+ - doc/frames.html
116
+ - doc/index.html
117
+ - doc/js/app.js
118
+ - doc/js/full_list.js
119
+ - doc/js/jquery.js
120
+ - doc/method_list.html
121
+ - doc/top-level-namespace.html
122
+ - lib/java/jackson-core-asl-1.9.5.jar
123
+ - lib/java/jackson-mapper-asl-1.9.5.jar
124
+ - lib/java/metrics-core-2.1.1.jar
125
+ - lib/java/metrics-graphite-2.1.1.jar
126
+ - lib/java/metrics-servlet-2.1.1.jar
127
+ - lib/java/slf4j-api-1.6.4.jar
128
+ - lib/simple.rb
129
+ - lib/simple/metrics.rb
130
+ - lib/simple/metrics/graphite.rb
131
+ - lib/simple/metrics/health.rb
132
+ - lib/simple/metrics/healthcheck.rb
133
+ - lib/simple/metrics/meter.rb
134
+ - lib/simple/metrics/rack_metrics.rb
135
+ - lib/simple/metrics/timer.rb
136
+ - lib/simple/version.rb
137
+ - simple-metrics.gemspec
138
+ - spec/metrics/graphite_spec.rb
139
+ - spec/metrics/healthcheck_spec.rb
140
+ - spec/metrics/meter_spec.rb
141
+ - spec/metrics/timer_spec.rb
142
+ - spec/spec_helper.rb
143
+ homepage: https://github.com/SimpleFinance/simple-metrics
144
+ licenses: []
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ none: false
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ! '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ none: false
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 1.8.24
164
+ signing_key:
165
+ specification_version: 3
166
+ summary: MEASURE ALL THE THINGS!
167
+ test_files:
168
+ - spec/metrics/graphite_spec.rb
169
+ - spec/metrics/healthcheck_spec.rb
170
+ - spec/metrics/meter_spec.rb
171
+ - spec/metrics/timer_spec.rb
172
+ - spec/spec_helper.rb
173
+ has_rdoc:
174
+ ...