counters 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -71,7 +71,15 @@ $ cat counters.log
71
71
  2011-02-21T09:46:27.989183000 - latency: crawler.processing 0.3123122982101s
72
72
  </code></pre>
73
73
 
74
- You may also output your counters to "StatsD":https://github.com/etsy/statsd. The only change that might be surprising is magnitudes are output as timer events. Magnitudes are used to record
74
+ You may also output your counters to "StatsD":https://github.com/etsy/statsd. The only change that might be surprising is magnitudes are output as timer events. Magnitudes are used to record values (such as process RSS, packet sizes, etc).
75
+
76
+ Instantiate a StatsD instance:
77
+
78
+ <pre><code>Counter = Counters::StatsD.new("127.0.0.1", 8125, :namespace => "analyzer")
79
+
80
+ # Alternatively, use a URI:
81
+ Counter = Counters::StatsD.new(:url => "udp://127.0.0.1:8125", :namespace => "crawler")
82
+ </code></pre>
75
83
 
76
84
  See the file "samples/crawler.rb":blob/master/samples/crawler.rb for a more detailed example.
77
85
 
@@ -5,12 +5,23 @@ module Counters
5
5
  def initialize(path_or_io_or_logger, options={})
6
6
  super(options)
7
7
 
8
- @logger = if path_or_io_or_logger.kind_of?(Logger) then
8
+ @logger = if path_or_io_or_logger.kind_of?(Logger) || path_or_io_or_logger.respond_to?(:add) then
9
9
  path_or_io_or_logger
10
+
11
+ elsif path_or_io_or_logger.respond_to?(:to_str) then
12
+ # path to something
13
+ raise ArgumentError, "Counters::File expects a path with a non-empty name (or a Logger, IO instance); received: #{path_or_io_or_logger.inspect}" if path_or_io_or_logger.to_str.empty?
14
+
15
+ logger = Logger.new(path_or_io_or_logger)
16
+ logger.formatter = lambda {|severity, datetime, progname, msg| "#{datetime.strftime("%Y-%m-%dT%H:%M:%S.%N")} - #{msg}\n"}
17
+ logger
18
+
10
19
  elsif path_or_io_or_logger.respond_to?(:<<) then
20
+ # IO instance
11
21
  logger = Logger.new(path_or_io_or_logger)
12
22
  logger.formatter = lambda {|severity, datetime, progname, msg| "#{datetime.strftime("%Y-%m-%dT%H:%M:%S.%N")} - #{msg}\n"}
13
23
  logger
24
+
14
25
  else
15
26
  raise ArgumentError, "Counters::File expects an object which is either a Logger or respond to #<<, received a #{path_or_io_or_logger.class}"
16
27
  end
@@ -1,3 +1,3 @@
1
1
  module Counters
2
- VERSION = "1.1.2"
2
+ VERSION = "1.1.3"
3
3
  end
@@ -2,6 +2,86 @@ require "spec_helper"
2
2
  require "logger"
3
3
  require "tempfile"
4
4
 
5
+ describe Counters::File, "#initialize" do
6
+ context "given an existing Logger instance" do
7
+ let! :logger do
8
+ Logger.new(STDOUT)
9
+ end
10
+
11
+ it "should use the existing Logger instance" do
12
+ Logger.should_not_receive(:new)
13
+ Counters::File.new(logger)
14
+ end
15
+
16
+ it "should not change the formatting" do
17
+ logger.should_not_receive(:formatter=)
18
+ Counters::File.new(logger)
19
+ end
20
+ end
21
+
22
+ context "given an object that responds_to?(:add) (such as ActiveSupport::BufferedLogger)" do
23
+ class FakeLogger
24
+ def add(*args)
25
+ raise "called for nothing"
26
+ end
27
+ end
28
+
29
+ let! :logger do
30
+ FakeLogger.new
31
+ end
32
+
33
+ it "should use the existing instance" do
34
+ Logger.should_not_receive(:new)
35
+ Counters::File.new(logger)
36
+ end
37
+ end
38
+
39
+ context "given a String (representing a path)" do
40
+ let :path do
41
+ "log/mycounters.log"
42
+ end
43
+
44
+ let :logger do
45
+ double("logger").as_null_object
46
+ end
47
+
48
+ it "should instantiate a Logger with the passed path" do
49
+ Logger.should_receive(:new).with(path).and_return(logger)
50
+ Counters::File.new(path)
51
+ end
52
+
53
+ it "should set the formatting" do
54
+ Logger.stub(:new).and_return(logger)
55
+ logger.should_receive(:formatter=)
56
+ Counters::File.new(path)
57
+ end
58
+ end
59
+
60
+ context "given an IO object" do
61
+ let :io do
62
+ STDOUT
63
+ end
64
+
65
+ let :logger do
66
+ double("logger").as_null_object
67
+ end
68
+
69
+ it "should instantiate a Logger with the passed IO" do
70
+ Logger.should_receive(:new).with(io).and_return(logger)
71
+ Counters::File.new(io)
72
+ end
73
+
74
+ it "should set the formatting" do
75
+ Logger.stub(:new).and_return(logger)
76
+ logger.should_receive(:formatter=)
77
+ Counters::File.new(io)
78
+ end
79
+ end
80
+
81
+ it { lambda { Counters::File.new(nil) }.should raise_error(ArgumentError) }
82
+ it { lambda { Counters::File.new("") }.should raise_error(ArgumentError) }
83
+ end
84
+
5
85
  describe Counters::File do
6
86
  TIMESTAMP_RE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3,}/
7
87
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: counters
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.2
5
+ version: 1.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Fran\xC3\xA7ois Beausoleil"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-07 00:00:00 -04:00
13
+ date: 2011-04-11 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency