librato-rack 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### Version 0.4.1
2
+ * Support a pre-configured tracker object
3
+ * Make log-prefix configurable
4
+ * Break pid-locking out of startup checks
5
+
1
6
  ### Version 0.4.0
2
7
  * Add HTTP method (GET, POST) metrics
3
8
  * Add log buffering support
data/lib/librato/rack.rb CHANGED
@@ -41,14 +41,14 @@ module Librato
41
41
 
42
42
  def initialize(app, options={})
43
43
  old_style = false
44
- if options.respond_to?(:tracker) # old-style single argument
44
+ if options.respond_to?(:log_level) # old-style single argument
45
45
  config = options
46
46
  old_style = true
47
47
  else
48
48
  config = options.fetch(:config, Configuration.new)
49
49
  end
50
50
  @app, @config = app, config
51
- @tracker = Tracker.new(@config)
51
+ @tracker = @config.tracker || Tracker.new(@config)
52
52
  Librato.register_tracker(@tracker) # create global reference
53
53
 
54
54
  if old_style
@@ -13,8 +13,8 @@ module Librato
13
13
  EVENT_MODES = [:eventmachine, :synchrony]
14
14
 
15
15
  attr_accessor :user, :token, :autorun, :api_endpoint, :tracker,
16
- :source_pids, :log_level, :flush_interval, :log_target,
17
- :disable_rack_metrics
16
+ :source_pids, :log_level, :log_prefix, :log_target,
17
+ :disable_rack_metrics, :flush_interval
18
18
  attr_reader :prefix, :source, :deprecations
19
19
 
20
20
  def initialize
@@ -23,18 +23,11 @@ module Librato
23
23
  self.api_endpoint = Librato::Metrics.api_endpoint
24
24
  self.flush_interval = 60
25
25
  self.source_pids = false
26
+ self.log_prefix = '[librato-rack] '
26
27
  @listeners = []
27
28
  @deprecations = []
28
29
 
29
- # check environment
30
- self.user = ENV['LIBRATO_USER'] || ENV['LIBRATO_METRICS_USER']
31
- self.token = ENV['LIBRATO_TOKEN'] || ENV['LIBRATO_METRICS_TOKEN']
32
- self.autorun = detect_autorun
33
- self.prefix = ENV['LIBRATO_PREFIX'] || ENV['LIBRATO_METRICS_PREFIX']
34
- self.source = ENV['LIBRATO_SOURCE'] || ENV['LIBRATO_METRICS_SOURCE']
35
- self.log_level = ENV['LIBRATO_LOG_LEVEL'] || :info
36
- self.event_mode = ENV['LIBRATO_EVENT_MODE']
37
- check_deprecations
30
+ load_configuration
38
31
  end
39
32
 
40
33
  def event_mode
@@ -57,6 +50,19 @@ module Librato
57
50
  !!@explicit_source
58
51
  end
59
52
 
53
+ # check environment variables and capture current state
54
+ # for configuration
55
+ def load_configuration
56
+ self.user = ENV['LIBRATO_USER'] || ENV['LIBRATO_METRICS_USER']
57
+ self.token = ENV['LIBRATO_TOKEN'] || ENV['LIBRATO_METRICS_TOKEN']
58
+ self.autorun = detect_autorun
59
+ self.prefix = ENV['LIBRATO_PREFIX'] || ENV['LIBRATO_METRICS_PREFIX']
60
+ self.source = ENV['LIBRATO_SOURCE'] || ENV['LIBRATO_METRICS_SOURCE']
61
+ self.log_level = ENV['LIBRATO_LOG_LEVEL'] || :info
62
+ self.event_mode = ENV['LIBRATO_EVENT_MODE']
63
+ check_deprecations
64
+ end
65
+
60
66
  def prefix=(prefix)
61
67
  @prefix = prefix
62
68
  @listeners.each { |l| l.prefix = prefix }
@@ -14,7 +14,7 @@ module Librato
14
14
  def initialize(outlet=nil)
15
15
  @buffer = []
16
16
  self.outlet = outlet
17
- self.prefix = '[librato-rack] '
17
+ self.prefix = ''
18
18
  end
19
19
 
20
20
  # @example Simple logging
@@ -25,7 +25,7 @@ module Librato
25
25
  # thread will not pass with the fork
26
26
  def check_worker
27
27
  return if @worker # already running
28
- return if !should_start?
28
+ return unless start_worker?
29
29
  log(:debug) { "config: #{config.dump}" }
30
30
  @pid = $$
31
31
  log(:debug) { ">> starting up worker for pid #{@pid}..." }
@@ -65,6 +65,23 @@ module Librato
65
65
  config.source_pids ? "#{source}.#{$$}" : source
66
66
  end
67
67
 
68
+ # given current state, should the tracker start a reporter thread?
69
+ def should_start?
70
+ if !config.user || !config.token
71
+ # don't show this unless we're debugging, expected behavior
72
+ log :debug, 'halting: credentials not present.'
73
+ elsif config.autorun == false
74
+ log :debug, 'halting: LIBRATO_AUTORUN disabled startup'
75
+ elsif qualified_source !~ SOURCE_REGEX
76
+ log :warn, "halting: '#{qualified_source}' is an invalid source name."
77
+ elsif on_heroku && !config.explicit_source?
78
+ log :warn, 'halting: source must be provided in configuration.'
79
+ else
80
+ return true
81
+ end
82
+ false
83
+ end
84
+
68
85
  # change output stream for logging
69
86
  def update_log_target(target)
70
87
  logger.outlet = target
@@ -110,6 +127,7 @@ module Librato
110
127
  return @logger if @logger
111
128
  @logger = Logger.new(config.log_target)
112
129
  @logger.log_level = config.log_level
130
+ @logger.prefix = config.log_prefix
113
131
  @logger
114
132
  end
115
133
 
@@ -129,34 +147,31 @@ module Librato
129
147
  RUBY_DESCRIPTION.split[0]
130
148
  end
131
149
 
132
- def should_start?
133
- return false if @pid_checked == $$ # only check once per process
134
- @pid_checked = $$
135
- if !config.user || !config.token
136
- # don't show this unless we're debugging, expected behavior
137
- log :debug, 'halting: credentials not present.'
138
- elsif config.autorun == false
139
- log :debug, 'halting: LIBRATO_AUTORUN disabled startup'
140
- elsif qualified_source !~ SOURCE_REGEX
141
- log :warn, "halting: '#{qualified_source}' is an invalid source name."
142
- elsif on_heroku && !config.explicit_source?
143
- log :warn, 'halting: source must be provided in configuration.'
144
- else
145
- return true
146
- end
147
- false
148
- end
149
-
150
150
  def source
151
151
  @source ||= (config.source || Socket.gethostname).downcase
152
152
  end
153
153
 
154
+ # should we spin up a worker? wrap this in a process check
155
+ # so we only actually check once per process. this allows us
156
+ # to check again if the process forks.
157
+ def start_worker?
158
+ if @pid_checked == $$
159
+ false
160
+ else
161
+ @pid_checked = $$
162
+ should_start?
163
+ end
164
+ end
165
+
154
166
  def user_agent
155
- ua_chunks = []
156
- ua_chunks << "librato-rack/#{Librato::Rack::VERSION}"
167
+ ua_chunks = [version_string]
157
168
  ua_chunks << "(#{ruby_engine}; #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}; #{RUBY_PLATFORM})"
158
169
  ua_chunks.join(' ')
159
170
  end
171
+
172
+ def version_string
173
+ "librato-rack/#{Librato::Rack::VERSION}"
174
+ end
160
175
  end
161
176
  end
162
177
  end
@@ -1,5 +1,5 @@
1
1
  module Librato
2
2
  class Rack
3
- VERSION = "0.4.0"
3
+ VERSION = "0.4.1"
4
4
  end
5
5
  end
@@ -72,7 +72,7 @@ module Librato
72
72
  end
73
73
 
74
74
  def test_log_prefix
75
- assert_equal '[librato-rack] ', @logger.prefix
75
+ assert_equal '', @logger.prefix
76
76
 
77
77
  @logger.prefix = '[test prefix] '
78
78
  @logger.log :error, 'an error message'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: librato-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -37,7 +37,7 @@ cert_chain:
37
37
  bktaNmhlblFBRjFDSDk2WmNxY0pIMTc5UzJ0SWlLRE04a2VlUklVT1BDM1dU
38
38
  MGZhb2svMgpnQTJvemRyODUxYy9uQT09Ci0tLS0tRU5EIENFUlRJRklDQVRF
39
39
  LS0tLS0K
40
- date: 2013-08-19 00:00:00.000000000 Z
40
+ date: 2013-08-21 00:00:00.000000000 Z
41
41
  dependencies:
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: librato-metrics
@@ -131,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  segments:
133
133
  - 0
134
- hash: -3276523900394881660
134
+ hash: 3171702989332283637
135
135
  required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  none: false
137
137
  requirements:
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  version: '0'
141
141
  segments:
142
142
  - 0
143
- hash: -3276523900394881660
143
+ hash: 3171702989332283637
144
144
  requirements: []
145
145
  rubyforge_project:
146
146
  rubygems_version: 1.8.23
metadata.gz.sig CHANGED
Binary file