statue 0.2.0 → 0.2.1

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: 7423b7af642844b2743653ad8e98d4e07e49de3f
4
- data.tar.gz: 8d6c9aa58322ab48e39322079def0658a508aeb5
3
+ metadata.gz: aaaa801fa23349d6323a19dae5bd071644310838
4
+ data.tar.gz: 8ad955fe46e3bdb66f6fd5e38ef0019637601e6e
5
5
  SHA512:
6
- metadata.gz: f53624b253cc74dd7f10c44feb52ddb00dd81b37b21fd80fdc98dd990c5f52edcf2c3afe55dd82da904d8370682b7ed82ad66f98359d2b1729e6e7996bc62ce5
7
- data.tar.gz: 3011d204d3fb77793a06597c73286518660e36ef60a2f000165755f54e40c9744fe7aac7f114ab7a8a0fd526dbf03bcfb6cddebc8c387a856efa16863761514c
6
+ metadata.gz: 15786cf65ccb148c747a4e7852f5fbde2a3a93900e27d3a9ee534964b4ae52a48b7c1ffb438d72c14f4be47d329476154be6ebf50d2447d38d94ec135315637d
7
+ data.tar.gz: aeb292041f9827f72c880a61c88bac5cd53a322f52ccefe7e1f91c5bc857a416845c575617f1db6f6753c5cacb758749d8062e010bbcad11952e5df2e247ce1f
data/README.md CHANGED
@@ -37,11 +37,31 @@ Statue.backend = Statue::LoggerBackend.new(Rails.logger)
37
37
 
38
38
  ## Usage
39
39
 
40
+ ### Common meassurments
41
+
40
42
  `Statue.report_increment('metric.name')` -> send to Statsd an increment for the counter `metric.name`
41
43
 
42
- `Statue.report_duration('metric.name') { some_operation } # => some_operation_result` -> send to Statsd the measure for the block duration in `metric.name`
44
+ `Statue.report_duration('metric.name') { some_operation } # => some_operation_result` -> send to Statsd the
45
+ measure for the block duration in `metric.name`
46
+
47
+ `Statue.report_success_or_failure('metric.name') { some_operation } # => some_operation_result` -> checks the
48
+ result of the block, if its a `truthy` value, then increments `metric.name.success`, else it increments
49
+ `metric.name.failure`.
50
+
51
+ ### Stopwatch
52
+
53
+ The stopwatch provides an easy way to track the duration of a long process with multiple phases.
43
54
 
44
- `Statue.report_success_or_failure('metric.name') { some_operation } # => some_operation_result` -> checks the result of the block, if its a `truthy` value, then increments `metric.name.success`, else it increments `metric.name.failure`.
55
+ ```ruby
56
+ stopwatch = Statue.stopwatch("metric") # => Starts tracking time
57
+
58
+ while something do
59
+ do_something
60
+ stopwatch.partial # => reports duration from last partial until now as: "metric.runtime.partial"
61
+ end
62
+
63
+ stopwatch.stop # => reports duration from start until now as: "metric.runtime.total"
64
+ ```
45
65
 
46
66
  ### Rack Integration
47
67
 
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
@@ -0,0 +1,39 @@
1
+ module Statue
2
+ class Stopwatch
3
+
4
+ def initialize(name:, now: clock_now, reporter: Statue)
5
+ @reporter = reporter
6
+ @name = name
7
+ @start = @partial = now
8
+ end
9
+
10
+ def partial(suffix = nil, now: clock_now, **options)
11
+ previous, @partial = @partial, now
12
+
13
+ @reporter.report_duration(metric_name(suffix || "runtime.partial"), @partial - previous, **options)
14
+ end
15
+
16
+ def stop(suffix = nil, now: clock_now, report_partial: false, **options)
17
+ partial(report_partial.is_a?(String) ? report_partial : nil, now: now, **options) if report_partial
18
+
19
+ previous, @start = @start, now
20
+
21
+ @reporter.report_duration(metric_name(suffix || "runtime.total"), @start - previous, **options)
22
+ end
23
+
24
+ def reset(now: clock_now)
25
+ @start = @partial = now
26
+ end
27
+
28
+ private
29
+
30
+ def metric_name(suffix)
31
+ "#{@name}.#{suffix}"
32
+ end
33
+
34
+ def clock_now
35
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
36
+ end
37
+
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module Statue
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
data/lib/statue.rb CHANGED
@@ -2,6 +2,7 @@ require 'statue/version'
2
2
 
3
3
  require 'statue/backends'
4
4
  require 'statue/metric'
5
+ require 'statue/stopwatch'
5
6
 
6
7
  module Statue
7
8
  extend self
@@ -38,6 +39,10 @@ module Statue
38
39
  raise
39
40
  end
40
41
 
42
+ def stopwatch(metric_name)
43
+ Stopwatch.new(name: metric_name, reporter: self)
44
+ end
45
+
41
46
  def backend
42
47
  @backend ||= UDPBackend.new
43
48
  end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ describe Statue::Stopwatch do
4
+ after do
5
+ Statue.backend.captures.clear
6
+ end
7
+
8
+ describe "#partial" do
9
+ it "reports the duration between start and the partial call" do
10
+ stopwatch = Statue::Stopwatch.new(name: "my_watch", now: 0)
11
+ stopwatch.partial(now: 42)
12
+
13
+ assert_equal 1, Statue.backend.captures.size
14
+ assert_equal "my_watch.runtime.partial:42|ms", Statue.backend.captures.first.to_s
15
+ end
16
+
17
+ it "can report multiple partials" do
18
+ stopwatch = Statue::Stopwatch.new(name: "my_watch", now: 0)
19
+ (1..20).each { |now| stopwatch.partial(now: now) }
20
+
21
+ assert_equal 20, Statue.backend.captures.size
22
+ Statue.backend.captures.each do |metric|
23
+ assert_equal "my_watch.runtime.partial:1|ms", metric.to_s
24
+ end
25
+ end
26
+
27
+ it "tracks time correctly" do
28
+ stopwatch = Statue::Stopwatch.new(name: "my_watch")
29
+ stopwatch.partial
30
+
31
+ assert Statue.backend.captures.first.value > 0, "partial metric time should be greater than zero"
32
+ end
33
+ end
34
+
35
+ describe "#stop" do
36
+ it "reports the duration between start and the stop call" do
37
+ stopwatch = Statue::Stopwatch.new(name: "my_watch", now: 0)
38
+ stopwatch.stop(now: 42)
39
+
40
+ assert_equal 1, Statue.backend.captures.size
41
+ assert_equal "my_watch.runtime.total:42|ms", Statue.backend.captures.first.to_s
42
+ end
43
+
44
+ it "is not affected by partials" do
45
+ stopwatch = Statue::Stopwatch.new(name: "my_watch", now: 0)
46
+ (1..20).each { |now| stopwatch.partial(now: now) }
47
+ stopwatch.stop(now: 21)
48
+
49
+ assert_equal 21, Statue.backend.captures.size
50
+ *partials, total = Statue.backend.captures
51
+ assert_equal "my_watch.runtime.total:21|ms", total.to_s
52
+ end
53
+
54
+ it "can send the last partial duration" do
55
+ stopwatch = Statue::Stopwatch.new(name: "my_watch", now: 0)
56
+ (1..20).each { |now| stopwatch.partial(now: now) }
57
+ stopwatch.stop(now: 21, report_partial: true)
58
+
59
+ assert_equal 22, Statue.backend.captures.size
60
+ *partials, total = Statue.backend.captures
61
+ assert_equal "my_watch.runtime.total:21|ms", total.to_s
62
+ partials.each do |metric|
63
+ assert_equal "my_watch.runtime.partial:1|ms", metric.to_s
64
+ end
65
+ end
66
+
67
+ it "can send the last partial duration with a special name" do
68
+ stopwatch = Statue::Stopwatch.new(name: "my_watch", now: 0)
69
+ (1..20).each { |now| stopwatch.partial(now: now) }
70
+ stopwatch.stop(now: 21, report_partial: "runtime.final_lap")
71
+
72
+ assert_equal 22, Statue.backend.captures.size
73
+ *partials, special_partial, total = Statue.backend.captures
74
+ assert_equal "my_watch.runtime.final_lap:1|ms", special_partial.to_s
75
+ end
76
+
77
+ it "tracks time correctly" do
78
+ stopwatch = Statue::Stopwatch.new(name: "my_watch")
79
+ stopwatch.stop
80
+
81
+ assert Statue.backend.captures.first.value > 0, "partial metric time should be greater than zero"
82
+ end
83
+ end
84
+
85
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Barreneche
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-18 00:00:00.000000000 Z
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,7 +55,8 @@ dependencies:
55
55
  description: Track application metrics to Statsie
56
56
  email:
57
57
  - devs@restorando.com
58
- executables: []
58
+ executables:
59
+ - rake
59
60
  extensions: []
60
61
  extra_rdoc_files: []
61
62
  files:
@@ -65,6 +66,7 @@ files:
65
66
  - LICENSE.txt
66
67
  - README.md
67
68
  - Rakefile
69
+ - bin/rake
68
70
  - lib/statue.rb
69
71
  - lib/statue/backends.rb
70
72
  - lib/statue/backends/capture.rb
@@ -73,9 +75,11 @@ files:
73
75
  - lib/statue/backends/udp.rb
74
76
  - lib/statue/metric.rb
75
77
  - lib/statue/rack_statistics.rb
78
+ - lib/statue/stopwatch.rb
76
79
  - lib/statue/version.rb
77
80
  - statue.gemspec
78
81
  - test/statue_test.rb
82
+ - test/stopwatch_test.rb
79
83
  - test/test_helper.rb
80
84
  homepage: ''
81
85
  licenses:
@@ -97,10 +101,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
101
  version: '0'
98
102
  requirements: []
99
103
  rubyforge_project:
100
- rubygems_version: 2.4.7
104
+ rubygems_version: 2.4.8
101
105
  signing_key:
102
106
  specification_version: 4
103
107
  summary: Easily track application metrics into Statsie
104
108
  test_files:
105
109
  - test/statue_test.rb
110
+ - test/stopwatch_test.rb
106
111
  - test/test_helper.rb