union_station_hooks_core 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- OTQ0Y2Q0ZTkxYjEyNWViMDZmMWVkYjAwOGM4MjcyYTU1M2E0MzgxMA==
5
- data.tar.gz: !binary |-
6
- NTU2YzRiYmZlZmM3ZmIwMDNmMjA2YjUwZDY1NmJlOGIyNjM2NmJkOQ==
2
+ SHA1:
3
+ metadata.gz: e14b054c5471e022f9fbf5b7c1d2f12283b4ff1a
4
+ data.tar.gz: 2f20c31503408cd1ba0c96231d7d99cda04e528f
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MWFmMjUwYTg2OGVjYjMxNmZkMGUzYWM4YTMyZjcyYmVmYmM4NzcyZGVhODkz
10
- YzhiNGJhZWI2YjI0MGYxYzc2NWFhYTYzZDVjZDY4MGNmNWU4ZTc4MWY3ZTQ0
11
- MWQ2MDZkMTRkNDJlNzg2YTJkMzMwMjRhNzg1NzZlMjZkMDM5ODM=
12
- data.tar.gz: !binary |-
13
- Y2Y2MDFiNTY0ZWViZGRlMDA3YzQ0MGE1ZWZmZTlmZDc3MGRiMGQ4MTk4Y2My
14
- MWY0NTY0NGI5ZmYwYWFkN2ZlYThhOTAyMmZkOTJjYTgyYWM2MmU3MWM4NjUy
15
- N2M1Y2UxZWFhNzM0ZWI4OTRlMGMwZmNhMGNlYTQ4MGNlNDA1OWE=
6
+ metadata.gz: d22ecc2c83e08afd40b473c2cd1e02eec0d3cebb0f96075a81fbe20f98fe9efaff0b7d88e8e9fd075aa48a3c5573307b1b97af9eb2f000d5b783b004ff9426d4
7
+ data.tar.gz: 5f154eadddc19000ebdae2d3a524bf8a37a9d78c13f1403bc2c515a0b1fc800ab1600355ff49f8440d89b4d65c6fa900d709f311bd0c4db5d371956e733ab1e5
@@ -68,7 +68,14 @@ module UnionStationHooks
68
68
  txn_id = rack_env['PASSENGER_TXN_ID']
69
69
  return nil if !txn_id
70
70
 
71
- reporter = RequestReporter.new(context, txn_id, app_group_name, key)
71
+ # Workaround for Ruby < 2.1 support where there is no function for querying
72
+ # the monotonic time.
73
+ delta_monotonic = rack_env['PASSENGER_DELTA_MONOTONIC']
74
+ if delta_monotonic
75
+ delta_monotonic = delta_monotonic.to_i
76
+ end
77
+
78
+ reporter = RequestReporter.new(context, txn_id, app_group_name, key, delta_monotonic)
72
79
  return if reporter.null?
73
80
 
74
81
  rack_env['union_station_hooks'] = reporter
@@ -186,6 +193,7 @@ module UnionStationHooks
186
193
  if @@config[:debug]
187
194
  UnionStationHooks::Log.debugging = true
188
195
  end
196
+ require_simple_json
189
197
  @@initialized = true
190
198
  end
191
199
  end
@@ -175,7 +175,7 @@ module UnionStationHooks
175
175
  end
176
176
  end
177
177
 
178
- def continue_transaction(txn_id, group_name, category, key)
178
+ def continue_transaction(txn_id, group_name, category, key, delta_monotonic = "0")
179
179
  if !@server_address
180
180
  return Transaction.new(nil, nil)
181
181
  elsif !txn_id || txn_id.empty?
@@ -211,7 +211,7 @@ module UnionStationHooks
211
211
  Utils.encoded_timestamp,
212
212
  key,
213
213
  true)
214
- return Transaction.new(@connection, txn_id)
214
+ return Transaction.new(@connection, txn_id, delta_monotonic)
215
215
  rescue SystemCallError, IOError
216
216
  @connection.disconnect
217
217
  UnionStationHooks::Log.warn(
@@ -102,7 +102,7 @@ module UnionStationHooks
102
102
  # in the {RequestReporter class description}.
103
103
  #
104
104
  # @api private
105
- def initialize(context, txn_id, app_group_name, key)
105
+ def initialize(context, txn_id, app_group_name, key, delta_monotonic)
106
106
  raise ArgumentError, 'Transaction ID must be given' if txn_id.nil?
107
107
  raise ArgumentError, 'App group name must be given' if app_group_name.nil?
108
108
  raise ArgumentError, 'Union Station key must be given' if key.nil?
@@ -110,6 +110,7 @@ module UnionStationHooks
110
110
  @txn_id = txn_id
111
111
  @app_group_name = app_group_name
112
112
  @key = key
113
+ @delta_monotonic = delta_monotonic
113
114
  @transaction = continue_transaction
114
115
  @next_view_rendering_number = 1
115
116
  @next_user_activity_number = 1
@@ -138,7 +139,7 @@ module UnionStationHooks
138
139
 
139
140
  def continue_transaction
140
141
  @context.continue_transaction(@txn_id, @app_group_name,
141
- :requests, @key)
142
+ :requests, @key, @delta_monotonic)
142
143
  end
143
144
 
144
145
  # Called when one of the methods return early upon detecting null
@@ -0,0 +1,395 @@
1
+ # encoding: utf-8
2
+ #
3
+ ## Stupid small pure Ruby JSON parser & generator.
4
+ #
5
+ # Copyright © 2013 Mislav Marohnić
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this
8
+ # software and associated documentation files (the “Software”), to deal in the Software
9
+ # without restriction, including without limitation the rights to use, copy, modify,
10
+ # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to the following
12
+ # conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all copies or
15
+ # substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18
+ # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19
+ # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21
+ # OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ # OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ # We use this in Phusion Passenger at places where we cannot depend on the JSON
25
+ # gem being available, for example in 'passenger start' before the RuntimeInstaller
26
+ # has run.
27
+
28
+ require 'strscan'
29
+ require 'forwardable'
30
+
31
+ module UnionStationHooks
32
+ module SimpleJSON
33
+
34
+ # Usage:
35
+ #
36
+ # JSON.parse(json_string) => Array/Hash
37
+ # JSON.generate(object) => json string
38
+ #
39
+ # Run tests by executing this file directly. Pipe standard input to the script to have it
40
+ # parsed as JSON and to display the result in Ruby.
41
+ #
42
+ class JSON
43
+ def self.parse(data) new(data).parse end
44
+
45
+ WSP = /(\s|\/\/.*?\n|\/\*.*?\*\/)+/m
46
+ OBJ = /[{\[]/; HEN = /\}/; AEN = /\]/
47
+ COL = /\s*:\s*/; KEY = /\s*,\s*/
48
+ NUM = /-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/
49
+ BOL = /true|false/; NUL = /null/
50
+
51
+ extend Forwardable
52
+
53
+ attr_reader :scanner
54
+ alias_method :s, :scanner
55
+ def_delegators :scanner, :scan, :matched
56
+ private :s, :scan, :matched
57
+
58
+ def initialize data
59
+ @scanner = StringScanner.new data.to_s
60
+ end
61
+
62
+ def parse
63
+ space
64
+ object
65
+ end
66
+
67
+ private
68
+
69
+ def space() scan WSP end
70
+
71
+ def endkey() scan(KEY) or space end
72
+
73
+ def object
74
+ matched == '{' ? hash : array if scan(OBJ)
75
+ end
76
+
77
+ def value
78
+ object or string or
79
+ scan(NUL) ? nil :
80
+ scan(BOL) ? matched.size == 4:
81
+ scan(NUM) ? eval(matched) :
82
+ error
83
+ end
84
+
85
+ def hash
86
+ obj = {}
87
+ space
88
+ repeat_until(HEN) do
89
+ space
90
+ k = string
91
+ scan(COL)
92
+ obj[k] = value
93
+ endkey
94
+ end
95
+ obj
96
+ end
97
+
98
+ def array
99
+ ary = []
100
+ space
101
+ repeat_until(AEN) do
102
+ space
103
+ ary << value
104
+ endkey
105
+ end
106
+ ary
107
+ end
108
+
109
+ SPEC = {'b' => "\b", 'f' => "\f", 'n' => "\n", 'r' => "\r", 't' => "\t"}
110
+ UNI = 'u'; CODE = /[a-fA-F0-9]{4}/
111
+ STR = /"/; STE = '"'
112
+ ESC = '\\'
113
+
114
+ def string
115
+ if scan(STR)
116
+ str, esc = '', false
117
+ while c = s.getch
118
+ if esc
119
+ str << (c == UNI ? (s.scan(CODE) || error).to_i(16).chr : SPEC[c] || c)
120
+ esc = false
121
+ else
122
+ case c
123
+ when ESC then esc = true
124
+ when STE then break
125
+ else str << c
126
+ end
127
+ end
128
+ end
129
+ str
130
+ end
131
+ end
132
+
133
+ def error
134
+ raise "parse error at: #{scan(/.{1,20}/m).inspect}"
135
+ end
136
+
137
+ def repeat_until reg
138
+ until scan(reg)
139
+ pos = s.pos
140
+ yield
141
+ error unless s.pos > pos
142
+ end
143
+ end
144
+
145
+ module Generator
146
+ def generate(obj)
147
+ raise ArgumentError unless obj.is_a? Array or obj.is_a? Hash
148
+ generate_type(obj)
149
+ end
150
+ alias dump generate
151
+
152
+ private
153
+
154
+ def generate_type(obj)
155
+ type = obj.is_a?(Numeric) ? :Numeric : obj.class.name
156
+ begin send(:"generate_#{type}", obj)
157
+ rescue NoMethodError; raise ArgumentError, "can't serialize #{type}"
158
+ end
159
+ end
160
+
161
+ ESC_MAP = Hash.new {|h,k| k }.update \
162
+ "\r" => 'r',
163
+ "\n" => 'n',
164
+ "\f" => 'f',
165
+ "\t" => 't',
166
+ "\b" => 'b'
167
+
168
+ def quote(str) %("#{str}") end
169
+
170
+ def generate_String(str)
171
+ quote str.gsub(/[\r\n\f\t\b"\\]/) { "\\#{ESC_MAP[$&]}"}
172
+ end
173
+
174
+ def generate_simple(obj) obj.inspect end
175
+ alias generate_Numeric generate_simple
176
+ alias generate_TrueClass generate_simple
177
+ alias generate_FalseClass generate_simple
178
+
179
+ def generate_Symbol(sym) generate_String(sym.to_s) end
180
+
181
+ def generate_Time(time)
182
+ quote time.strftime(time.utc? ? "%F %T UTC" : "%F %T %z")
183
+ end
184
+ def generate_Date(date) quote date.to_s end
185
+
186
+ def generate_NilClass(*) 'null' end
187
+
188
+ def generate_Array(ary) '[%s]' % ary.map {|o| generate_type(o) }.join(', ') end
189
+
190
+ def generate_Hash(hash)
191
+ '{%s}' % hash.map { |key, value|
192
+ "#{generate_String(key.to_s)}: #{generate_type(value)}"
193
+ }.join(', ')
194
+ end
195
+ end
196
+
197
+ extend Generator
198
+ end
199
+
200
+ if __FILE__ == $0
201
+ if !$stdin.tty?
202
+ data = JSON.parse $stdin.read
203
+ require 'pp'
204
+ pp data
205
+ else
206
+ require 'test/unit'
207
+ require 'date'
208
+ class ParserTest < Test::Unit::TestCase
209
+ PARSED = JSON.parse DATA.read
210
+ def parsed() PARSED end
211
+ def parse_string(str) JSON.parse(%(["#{str}"]).gsub('\\\\', '\\')).first end
212
+ def test_string
213
+ assert_equal "Pagination library for \"Rails 3\", Sinatra, Merb, DataMapper, and more",
214
+ parsed['head']['repository']['description']
215
+ end
216
+ def test_string_specials
217
+ assert_equal "\r\n\t\f\b", parse_string('\r\n\t\f\b')
218
+ assert_equal "aA", parse_string('\u0061\u0041')
219
+ assert_equal "\e", parse_string('\u001B')
220
+ assert_equal "xyz", parse_string('\x\y\z')
221
+ assert_equal '"\\/', parse_string('\"\\\\\\/')
222
+ assert_equal 'no #{interpolation}', parse_string('no #{interpolation}')
223
+ end
224
+ def test_hash
225
+ assert_equal %w[label ref repository sha user], parsed['head'].keys.sort
226
+ end
227
+ def test_number
228
+ assert_equal 124.3e2, parsed['head']['repository']['size']
229
+ end
230
+ def test_bool
231
+ assert_equal true, parsed['head']['repository']['fork']
232
+ assert_equal false, parsed['head']['repository']['private']
233
+ end
234
+ def test_nil
235
+ assert_nil parsed['head']['user']['company']
236
+ end
237
+ def test_array
238
+ assert_equal ["4438f", {"a" => "b"}], parsed['head']['sha']
239
+ end
240
+ def test_invalid
241
+ assert_raises(RuntimeError) { JSON.parse %({) }
242
+ assert_raises(RuntimeError) { JSON.parse %({ "foo": }) }
243
+ assert_raises(RuntimeError) { JSON.parse %([ "foo": "bar" ]) }
244
+ assert_raises(RuntimeError) { JSON.parse %([ ~"foo" ]) }
245
+ assert_raises(RuntimeError) { JSON.parse %([ "foo ]) }
246
+ assert_raises(RuntimeError) { JSON.parse %([ "foo\\" ]) }
247
+ assert_raises(RuntimeError) { JSON.parse %([ "foo\\uabGd" ]) }
248
+ end
249
+ def test_single_line_comments
250
+ source = %Q{
251
+ // comment before document
252
+ {
253
+ // comment
254
+ "foo": "1",
255
+ "bar": "2",
256
+ // another comment
257
+ "baz": "3",
258
+ "array": [
259
+ // comment inside array
260
+ 1, 2, 3
261
+ // comment at end of array
262
+ ]
263
+ // comment at end of hash
264
+ }
265
+ // comment after document
266
+ }
267
+ doc = { "foo" => "1", "bar" => "2", "baz" => "3", "array" => [1, 2, 3] }
268
+ assert_equal(doc, JSON.parse(source))
269
+ end
270
+ def test_multi_line_comments
271
+ source = %Q{
272
+ /* comment before
273
+ * document */
274
+ {
275
+ /* comment */
276
+ "foo": "1",
277
+ "bar": "2",
278
+ /* another
279
+ comment
280
+ */
281
+ "baz": "3",
282
+ "array": [
283
+ /* comment inside array */
284
+ 1, 2, 3,
285
+ 4, /* comment inside an array */ 5,
286
+ /*
287
+ // "nested" comments
288
+ { "faux json": "inside comment" }
289
+ */
290
+ 6, 7
291
+ /**
292
+ * comment at end of array
293
+ */
294
+ ]
295
+ /**************************
296
+ comment at end of hash
297
+ **************************/
298
+ }
299
+ /* comment after
300
+ document */
301
+ }
302
+ doc = { "foo" => "1", "bar" => "2", "baz" => "3", "array" => [1, 2, 3, 4, 5, 6, 7] }
303
+ assert_equal(doc, JSON.parse(source))
304
+ end
305
+ end
306
+
307
+ class GeneratorTest < Test::Unit::TestCase
308
+ def generate(obj) JSON.generate(obj) end
309
+ def test_array
310
+ assert_equal %([1, 2, 3]), generate([1, 2, 3])
311
+ end
312
+ def test_bool
313
+ assert_equal %([true, false]), generate([true, false])
314
+ end
315
+ def test_null
316
+ assert_equal %([null]), generate([nil])
317
+ end
318
+ def test_string
319
+ assert_equal %(["abc\\n123"]), generate(["abc\n123"])
320
+ end
321
+ def test_string_unicode
322
+ assert_equal %(["ć\\"č\\nž\\tš\\\\đ"]), generate(["ć\"č\nž\tš\\đ"])
323
+ end
324
+ def test_time
325
+ time = Time.utc(2012, 04, 19, 1, 2, 3)
326
+ assert_equal %(["2012-04-19 01:02:03 UTC"]), generate([time])
327
+ end
328
+ def test_date
329
+ time = Date.new(2012, 04, 19)
330
+ assert_equal %(["2012-04-19"]), generate([time])
331
+ end
332
+ def test_symbol
333
+ assert_equal %(["abc"]), generate([:abc])
334
+ end
335
+ def test_hash
336
+ json = generate(:abc => 123, 123 => 'abc')
337
+ assert_match /^\{/, json
338
+ assert_match /\}$/, json
339
+ assert_equal [%("123": "abc"), %("abc": 123)], json[1...-1].split(', ').sort
340
+ end
341
+ def test_nested_structure
342
+ json = generate(:hash => {1=>2}, :array => [1,2])
343
+ assert json.include?(%("hash": {"1": 2}))
344
+ assert json.include?(%("array": [1, 2]))
345
+ end
346
+ def test_invalid_json
347
+ assert_raises(ArgumentError) { generate("abc") }
348
+ end
349
+ def test_invalid_object
350
+ err = assert_raises(ArgumentError) { generate("a" => Object.new) }
351
+ assert_equal "can't serialize Object", err.message
352
+ end
353
+ end
354
+ end
355
+ end
356
+
357
+ end # module SimpleJSON
358
+ end # module UnionStationHooks
359
+
360
+ __END__
361
+ {
362
+ "head": {
363
+ "ref": "master",
364
+ "repository": {
365
+ "forks": 0,
366
+ "integrate_branch": "rails3",
367
+ "watchers": 1,
368
+ "language": "Ruby",
369
+ "description": "Pagination library for \"Rails 3\", Sinatra, Merb, DataMapper, and more",
370
+ "has_downloads": true,
371
+ "fork": true,
372
+ "created_at": "2011/10/24 03:20:48 -0700",
373
+ "homepage": "http://github.com/mislav/will_paginate/wikis",
374
+ "size": 124.3e2,
375
+ "private": false,
376
+ "has_wiki": true,
377
+ "name": "will_paginate",
378
+ "owner": "dbackeus",
379
+ "url": "https://github.com/dbackeus/will_paginate",
380
+ "has_issues": false,
381
+ "open_issues": 0,
382
+ "pushed_at": "2011/10/25 05:44:05 -0700"
383
+ },
384
+ "label": "dbackeus:master",
385
+ "sha": ["4438f", { "a" : "b" }],
386
+ "user": {
387
+ "name": "David Backeus",
388
+ "company": null,
389
+ "gravatar_id": "ebe96524f5db9e92188f0542dc9d1d1a",
390
+ "location": "Stockholm (Sweden)",
391
+ "type": "User",
392
+ "login": "dbackeus"
393
+ }
394
+ }
395
+ }
@@ -31,9 +31,10 @@ module UnionStationHooks
31
31
  class Transaction
32
32
  attr_reader :txn_id
33
33
 
34
- def initialize(connection, txn_id)
34
+ def initialize(connection, txn_id, delta_monotonic = 0)
35
35
  @connection = connection
36
36
  @txn_id = txn_id
37
+ @delta_monotonic = delta_monotonic
37
38
  if connection
38
39
  raise ArgumentError, 'Transaction ID required' if txn_id.nil?
39
40
  connection.ref
@@ -83,35 +84,37 @@ module UnionStationHooks
83
84
  end
84
85
 
85
86
  def log_activity_begin(name, time = UnionStationHooks.now, extra_info = nil)
87
+ monotime = Utils.encoded_monotime_now(@delta_monotonic)
86
88
  if extra_info
87
89
  extra_info_base64 = Utils.base64(extra_info)
88
90
  else
89
91
  extra_info_base64 = nil
90
92
  end
91
93
  if time.is_a?(TimePoint)
92
- message "BEGIN: #{name} (#{Utils.encoded_timestamp(time)}," \
94
+ message "BEGIN: #{name} (#{monotime}," \
93
95
  "#{time.utime.to_s(36)},#{time.stime.to_s(36)}) " \
94
96
  "#{extra_info_base64}"
95
97
  else
96
- message "BEGIN: #{name} (#{Utils.encoded_timestamp(time)})" \
98
+ message "BEGIN: #{name} (#{monotime})" \
97
99
  " #{extra_info_base64}"
98
100
  end
99
101
  end
100
102
 
101
103
  def log_activity_end(name, time = UnionStationHooks.now, has_error = false)
104
+ monotime = Utils.encoded_monotime_now(@delta_monotonic)
102
105
  if time.is_a?(TimePoint)
103
106
  if has_error
104
- message "FAIL: #{name} (#{Utils.encoded_timestamp(time)}," \
107
+ message "FAIL: #{name} (#{monotime}," \
105
108
  "#{time.utime.to_s(36)},#{time.stime.to_s(36)})"
106
109
  else
107
- message "END: #{name} (#{Utils.encoded_timestamp(time)}," \
110
+ message "END: #{name} (#{monotime}," \
108
111
  "#{time.utime.to_s(36)},#{time.stime.to_s(36)})"
109
112
  end
110
113
  else
111
114
  if has_error
112
- message "FAIL: #{name} (#{Utils.encoded_timestamp(time)})"
115
+ message "FAIL: #{name} (#{monotime})"
113
116
  else
114
- message "END: #{name} (#{Utils.encoded_timestamp(time)})"
117
+ message "END: #{name} (#{monotime})"
115
118
  end
116
119
  end
117
120
  end
@@ -89,6 +89,20 @@ module UnionStationHooks
89
89
  end
90
90
  end
91
91
 
92
+ if Process.const_defined?(:CLOCK_MONOTONIC)
93
+ def encoded_monotime_now(delta_monotonic)
94
+ time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
95
+ timestamp = (time * 1_000_000).to_i
96
+ timestamp.to_s(36)
97
+ end
98
+ else
99
+ def encoded_monotime_now(delta_monotonic)
100
+ time = Time.now
101
+ timestamp = time.to_i * 1_000_000 + time.usec - delta_monotonic
102
+ timestamp.to_s(36)
103
+ end
104
+ end
105
+
92
106
  def encoded_timestamp(time = Time.now)
93
107
  timestamp = time.to_i * 1_000_000 + time.usec
94
108
  timestamp.to_s(36)
@@ -39,6 +39,6 @@
39
39
  {
40
40
  :major => 2,
41
41
  :minor => 0,
42
- :tiny => 2,
43
- :string => '2.0.2'
42
+ :tiny => 3,
43
+ :string => '2.0.3'
44
44
  }
@@ -273,34 +273,34 @@ module UnionStationHooks
273
273
 
274
274
  # Called by Passenger after loading the application, to check whether or
275
275
  # not the application developer forgot to call
276
- # {UnionStationHooks.initialize!}
276
+ # {UnionStationHooks.initialize!}. If so, it logs the problem and
277
+ # initializes now.
277
278
  #
278
279
  # @private
279
- # @raise RuntimeError
280
280
  def check_initialized
281
- if should_initialize? && !initialized?
282
- return if !config.fetch(:check_initialized, true)
283
-
284
- # We end each error message with two newlines so that in Passenger
285
- # error reports in the exception class is shown on a new line, after
286
- # the message.
287
- if defined?(::Rails)
288
- raise 'The Union Station hooks are not initialized. Please ensure ' \
289
- 'that you have an initializer file ' \
290
- '`config/initializers/union_station.rb` in which you call ' \
291
- "this:\n\n" \
292
- " if defined?(UnionStationHooks)\n" \
293
- " UnionStationHooks.initialize!\n" \
294
- " end\n\n"
295
- else
296
- raise 'The Union Station hooks are not initialized. Please ensure ' \
297
- 'that the following code is called during application ' \
298
- "startup:\n\n" \
299
- " if defined?(UnionStationHooks)\n" \
300
- " UnionStationHooks.initialize!\n" \
301
- " end\n\n"
302
- end
281
+ return if !should_initialize? || initialized?
282
+ return if !config.fetch(:check_initialized, true)
283
+
284
+ if defined?(::Rails)
285
+ message = 'The Union Station hooks are not initialized. Please ensure ' \
286
+ 'that you have an initializer file ' \
287
+ '`config/initializers/union_station.rb` in which you call ' \
288
+ "this:\n\n" \
289
+ " if defined?(UnionStationHooks)\n" \
290
+ " UnionStationHooks.initialize!\n" \
291
+ " end"
292
+ else
293
+ message = 'The Union Station hooks are not initialized. Please ensure ' \
294
+ 'that the following code is called during application ' \
295
+ "startup:\n\n" \
296
+ " if defined?(UnionStationHooks)\n" \
297
+ " UnionStationHooks.initialize!\n" \
298
+ " end"
303
299
  end
300
+
301
+ STDERR.puts(" *** WARNING: #{message}")
302
+ initialize!
303
+ report_internal_information('HOOKS_NOT_INITIALIZED', message)
304
304
  end
305
305
 
306
306
  def now
@@ -356,6 +356,48 @@ module UnionStationHooks
356
356
  "Union Station hooks configuration option required: #{key}"
357
357
  end
358
358
  end
359
+
360
+ def require_simple_json
361
+ if defined?(PhusionPassenger)
362
+ begin
363
+ PhusionPassenger.require_passenger_lib('utils/json')
364
+ UnionStationHooks.const_set(:SimpleJSON, PhusionPassenger::Utils)
365
+ rescue LoadError
366
+ end
367
+ end
368
+ if !defined?(UnionStationHooks::SimpleJSON)
369
+ require_lib('simple_json')
370
+ end
371
+ end
372
+
373
+ def report_internal_information(type, message, data = nil)
374
+ data ||= {}
375
+ data[:app_type] ||= :ruby
376
+ if defined?(::Rails)
377
+ data[:framework_type] = :rails
378
+ end
379
+
380
+ if defined?(PhusionPassenger)
381
+ data[:app_server] = {
382
+ :id => :passenger,
383
+ :version => PhusionPassenger::VERSION_STRING
384
+ }
385
+ end
386
+
387
+ body = SimpleJSON::JSON.generate(
388
+ :type => type,
389
+ :message => message,
390
+ :data => data
391
+ )
392
+
393
+ transaction = context.new_transaction(app_group_name,
394
+ :internal_information, key)
395
+ begin
396
+ transaction.message(body)
397
+ ensure
398
+ transaction.close
399
+ end
400
+ end
359
401
  end
360
402
  end
361
403
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: union_station_hooks_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hongli Lai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-12 00:00:00.000000000 Z
11
+ date: 2015-11-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Union Station Ruby hooks core code.
14
14
  email: info@phusion.nl
@@ -30,6 +30,7 @@ files:
30
30
  - lib/union_station_hooks_core/request_reporter/controllers.rb
31
31
  - lib/union_station_hooks_core/request_reporter/misc.rb
32
32
  - lib/union_station_hooks_core/request_reporter/view_rendering.rb
33
+ - lib/union_station_hooks_core/simple_json.rb
33
34
  - lib/union_station_hooks_core/spec_helper.rb
34
35
  - lib/union_station_hooks_core/time_point.rb
35
36
  - lib/union_station_hooks_core/transaction.rb
@@ -47,17 +48,17 @@ require_paths:
47
48
  - lib
48
49
  required_ruby_version: !ruby/object:Gem::Requirement
49
50
  requirements:
50
- - - ! '>='
51
+ - - ">="
51
52
  - !ruby/object:Gem::Version
52
53
  version: '0'
53
54
  required_rubygems_version: !ruby/object:Gem::Requirement
54
55
  requirements:
55
- - - ! '>='
56
+ - - ">="
56
57
  - !ruby/object:Gem::Version
57
58
  version: '0'
58
59
  requirements: []
59
60
  rubyforge_project:
60
- rubygems_version: 2.2.2
61
+ rubygems_version: 2.4.8
61
62
  signing_key:
62
63
  specification_version: 4
63
64
  summary: Union Station Ruby hooks core code