cabin 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cabin/channel.rb CHANGED
@@ -47,6 +47,18 @@ class Cabin::Channel
47
47
  include Cabin::Mixins::Logger
48
48
  attr_accessor :metrics
49
49
 
50
+ # Get a channel for a given identifier. If this identifier has never been
51
+ # used, a new channel is created for it.
52
+ # The default identifier is the application executable name.
53
+ #
54
+ # This is useful for using the same Cabin::Channel across your
55
+ # entire application.
56
+ public
57
+ def self.get(identifier=$0)
58
+ @channels ||= Hash.new { |h,k| h[k] = Cabin::Channel.new }
59
+ return @channels[identifier]
60
+ end # def Cabin::Channel.get
61
+
50
62
  # Create a new logging channel.
51
63
  # The default log level is 'info'
52
64
  public
@@ -0,0 +1,56 @@
1
+ require "cabin/namespace"
2
+
3
+ module Cabin
4
+ module Inspectable
5
+ # Provide a saner inspect method that's easier to configure.
6
+ #
7
+ # By default, will inspect all instance variables. You can tune
8
+ # this by setting @inspectables to an array of ivar symbols, like:
9
+ # [ :@hello, :@world ]
10
+ #
11
+ # class Foo
12
+ # include Cabin::Inspectable
13
+ #
14
+ # def initialize
15
+ # @inspectables = [:@foo, :@bar]
16
+ # @foo = 123
17
+ # @bar = "hello"
18
+ # @baz = "ok"
19
+ # end
20
+ # end
21
+ #
22
+ # foo = Foo.new
23
+ # foo.inspect == '<Foo(1) @foo=123 @bar="hello" >'
24
+ def inspect
25
+ if instance_variable_defined?(:inspectables)
26
+ ivars = @inspectables
27
+ else
28
+ ivars = instance_variables
29
+ end
30
+ str = "<#{self.class.name}(@#{self.object_id}) "
31
+ ivars.each do |ivar|
32
+ str << "#{ivar}=#{instance_variable_get(ivar).inspect} "
33
+ end
34
+ str << ">"
35
+ return str
36
+ end
37
+ end
38
+
39
+ def self.__Inspectable(*ivars)
40
+ mod = Module.new
41
+ mod.instance_eval do
42
+ define_method(:inspect) do
43
+ ivars = instance_variables if ivars.empty?
44
+ str = "<#{self.class.name}(@#{self.object_id}) "
45
+ ivars.each do |ivar|
46
+ str << "#{ivar}=#{instance_variable_get(ivar).inspect} "
47
+ end
48
+ str << ">"
49
+ return str
50
+ end
51
+ end
52
+ return mod
53
+ end # def Cabin.Inspectable
54
+ end # module Cabin
55
+
56
+
data/lib/cabin/metrics.rb CHANGED
@@ -45,6 +45,7 @@ require "cabin/metrics/histogram"
45
45
  class Cabin::Metrics
46
46
  include Enumerable
47
47
 
48
+ # Get us a new metrics container.
48
49
  public
49
50
  def initialize
50
51
  @metrics = {}
@@ -52,26 +53,45 @@ class Cabin::Metrics
52
53
 
53
54
  private
54
55
  def create(instance, name, metric_object)
55
- #p :newmetric => [name, instance]
56
- #p [instance, instance.class, instance.class.class]
57
- return @metrics[[instance, name]] = metric_object
56
+ if !instance.is_a?(String)
57
+ instance = "#{instance.class.name}<#{instance.object_id}>"
58
+ end
59
+
60
+ if name.nil?
61
+ metric = instance.to_s
62
+ else
63
+ metric = "#{instance}/#{name}"
64
+ end
65
+ return @metrics[metric] = metric_object
58
66
  end # def create
59
67
 
68
+ # Create a new Counter metric
69
+ # 'instance' is usually an object owning this metric, but it can be a string.
70
+ # 'name' is the name of the metric.
60
71
  public
61
72
  def counter(instance, name=nil)
62
73
  return create(instance, name, Cabin::Metrics::Counter.new)
63
74
  end # def counter
64
75
 
76
+ # Create a new Meter metric
77
+ # 'instance' is usually an object owning this metric, but it can be a string.
78
+ # 'name' is the name of the metric.
65
79
  public
66
80
  def meter(instance, name=nil)
67
81
  return create(instance, name, Cabin::Metrics::Meter.new)
68
82
  end # def meter
69
83
 
84
+ # Create a new Histogram metric
85
+ # 'instance' is usually an object owning this metric, but it can be a string.
86
+ # 'name' is the name of the metric.
70
87
  public
71
88
  def histogram(instance, name=nil)
72
89
  return create(instance, name, Cabin::Metrics::Histogram.new)
73
90
  end # def histogram
74
91
 
92
+ # Create a new Timer metric
93
+ # 'instance' is usually an object owning this metric, but it can be a string.
94
+ # 'name' is the name of the metric.
75
95
  public
76
96
  def timer(instance, name=nil)
77
97
  return create(instance, name, Cabin::Metrics::Timer.new)
@@ -1,12 +1,16 @@
1
1
  require "cabin/namespace"
2
+ require "cabin/inspectable"
2
3
  require "thread"
3
4
 
4
5
  class Cabin::Metrics::Counter
6
+ include Cabin::Inspectable
7
+
5
8
  # A new Counter.
6
9
  #
7
10
  # Counters can be incremented and decremented only by 1 at a time..
8
11
  public
9
12
  def initialize
13
+ @inspectables = [ :@value ]
10
14
  @value = 0
11
15
  @lock = Mutex.new
12
16
  end # def initialize
@@ -1,9 +1,13 @@
1
1
  require "cabin/namespace"
2
+ require "cabin/inspectable"
2
3
 
3
4
  class Cabin::Metrics::Gauge
5
+ include Cabin::Inspectable
6
+
4
7
  # A new Gauge. The block given will be called every time the metric is read.
5
8
  public
6
9
  def initialize(&block)
10
+ @inspectables = [ ]
7
11
  @block = block
8
12
  end # def initialize
9
13
 
@@ -1,11 +1,15 @@
1
1
  require "cabin/namespace"
2
+ require "cabin/inspectable"
2
3
  require "thread"
3
4
 
4
5
  class Cabin::Metrics::Histogram
6
+ include Cabin::Inspectable
7
+
5
8
  # A new Histogram.
6
9
  public
7
10
  def initialize
8
11
  @lock = Mutex.new
12
+ @inspectables = [ :@total, :@min, :@max, :@count, :@mean ]
9
13
 
10
14
  # Histogram should track many things, including:
11
15
  # - percentiles (50, 75, 90, 95, 99?)
@@ -1,12 +1,16 @@
1
1
  require "cabin/namespace"
2
+ require "cabin/inspectable"
2
3
  require "thread"
3
4
 
4
5
  class Cabin::Metrics::Meter
6
+ include Cabin::Inspectable
7
+
5
8
  # A new Meter
6
9
  #
7
10
  # Counters can be incremented and decremented only by 1 at a time..
8
11
  public
9
12
  def initialize
13
+ @inspectables = [ :@value ]
10
14
  @value = 0
11
15
  @lock = Mutex.new
12
16
  end # def initialize
@@ -1,4 +1,5 @@
1
1
  require "cabin/namespace"
2
+ require "cabin/inspectable"
2
3
  require "cabin/metrics/histogram"
3
4
  require "thread"
4
5
 
@@ -13,10 +13,18 @@ class Cabin::Outputs::StdlibLogger
13
13
 
14
14
  # Receive an event
15
15
  public
16
- def <<(data)
17
- method = data[:level].downcase.to_sym || :info
16
+ def <<(event)
17
+ if !event.include?(:level)
18
+ event[:level] = :info
19
+ end
20
+ method = event[:level].downcase.to_sym || :info
21
+ event.delete(:level)
18
22
 
19
- message = "#{data[:message]} #{data.to_json}"
23
+ data = event.clone
24
+ # delete things from the 'data' portion that's not really data.
25
+ data.delete(:message)
26
+ data.delete(:timestamp)
27
+ message = "#{event[:message]} #{data.to_json}"
20
28
 
21
29
  #p [@logger.level, logger.class::DEBUG]
22
30
  # This will call @logger.info(data) or something similar.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cabin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-07 00:00:00.000000000 Z
12
+ date: 2012-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &15419600 !ruby/object:Gem::Requirement
16
+ requirement: &15693460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *15419600
24
+ version_requirements: *15693460
25
25
  description: This is an experiment to try and make logging more flexible and more
26
26
  consumable. Plain text logs are bullshit, let's emit structured and contextual logs.
27
27
  Metrics, too!
@@ -42,6 +42,7 @@ files:
42
42
  - lib/cabin/metrics/gauge.rb
43
43
  - lib/cabin/metrics/histogram.rb
44
44
  - lib/cabin/metrics/meter.rb
45
+ - lib/cabin/inspectable.rb
45
46
  - lib/cabin/timer.rb
46
47
  - lib/cabin/metrics.rb
47
48
  - lib/cabin/context.rb