structured-event-logger 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,16 +1,5 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
- require 'rubygems/package_task'
4
-
5
- gemspec = eval(File.read('structured-event-logger.gemspec'))
6
- Gem::PackageTask.new(gemspec) do |pkg|
7
- pkg.gem_spec = gemspec
8
- end
9
-
10
- desc "Build the gem and release it to rubygems.org"
11
- task :release => :gem do
12
- sh "gem push pkg/structured-event-logger-#{gemspec.version}.gem"
13
- end
14
3
 
15
4
  Rake::TestTask.new(:test) do |t|
16
5
  t.test_files = Dir.glob('test/**/*_test.rb')
@@ -1,4 +1,5 @@
1
1
  require 'logger'
2
+ require 'securerandom'
2
3
  require 'active_support/json'
3
4
  require 'active_support/log_subscriber'
4
5
 
@@ -69,8 +70,8 @@ class StructuredEventLogger
69
70
  def log_event(scope, event, hash)
70
71
  unstructured_logger.add(nil, format_hash(scope, event, hash)) if unstructured_logger
71
72
 
72
- hash = hash.merge(@default_context.merge(context))
73
- hash.update(event: event, scope: scope, timestamp: Time.now.utc)
73
+ hash = hash.merge(@default_context.merge(context)).merge(event_name: event, event_scope: scope, event_uuid: SecureRandom.uuid)
74
+ hash = { event_timestamp: Time.now.utc }.merge(hash)
74
75
  json_io.write("#{MultiJson.encode(hash)}\n")
75
76
  end
76
77
 
@@ -1,3 +1,3 @@
1
1
  class StructuredEventLogger
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -6,7 +6,7 @@ require 'structured_event_logger/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "structured-event-logger"
8
8
  spec.version = StructuredEventLogger::VERSION
9
- spec.authors = ["Emilie Noel", "Aaron Olson", "Willem van Bergen"]
9
+ spec.authors = ["Emilie Noel", "Aaron Olson", "Willem van Bergen", "Florian Weingarten"]
10
10
  spec.email = ["willem@shopify.com"]
11
11
  spec.description = %q{Structured event logging interface}
12
12
  spec.summary = %q{Structured event logger that writes events to both a human readable log and a JSON formatted log}
@@ -24,5 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "bundler", "~> 1.3"
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "minitest", "~> 5.0"
27
- spec.add_development_dependency "timecop"
27
+ spec.add_development_dependency "mocha"
28
28
  end
@@ -9,7 +9,9 @@ class StructuredEventLoggerTest < Minitest::Test
9
9
  @unstructured_logger = Logger.new(@nonstructured_io = StringIO.new)
10
10
  @unstructured_logger.formatter = proc { |_, _, _, msg| "#{msg}\n" }
11
11
  @event_logger = StructuredEventLogger.new(@json_io, @unstructured_logger)
12
- @time = Time.parse('2012-01-01T05:00:00Z')
12
+
13
+ Time.stubs(:now).returns(Time.parse('2012-01-01T05:00:00Z'))
14
+ SecureRandom.stubs(:uuid).returns('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')
13
15
  end
14
16
 
15
17
  def test_should_log_msg_to_buffered_logger
@@ -19,73 +21,100 @@ class StructuredEventLoggerTest < Minitest::Test
19
21
  end
20
22
 
21
23
  def test_should_log_event_to_both_loggers
22
- Timecop.travel(@time) do
23
- @event_logger.event "render", "error", {:status => "status", :message => "message"}
24
- assert_equal "{\"status\":\"status\",\"message\":\"message\",\"event\":\"error\",\"scope\":\"render\",\"timestamp\":\"2012-01-01T05:00:00Z\"}\n", @json_io.string
25
- assert_equal " [render] error: status=status, message=message\n", @nonstructured_io.string
26
- end
24
+ @event_logger.event "render", "error", {:status => "status", :message => "message"}
25
+
26
+ assert_equal " [render] error: status=status, message=message\n", @nonstructured_io.string
27
+ assert @json_io.string.end_with?("\n")
28
+ assert_kind_of Hash, JSON.parse(@json_io.string)
29
+ end
30
+
31
+ def test_default_json_properties
32
+ @event_logger.event :render, :error
33
+
34
+ assert_last_event_contains_value 'render', :event_scope
35
+ assert_last_event_contains_value 'error', :event_name
36
+ assert_last_event_contains_value 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', :event_uuid
37
+ assert_last_event_contains_value '2012-01-01T05:00:00Z', :event_timestamp
38
+ end
39
+
40
+ def test_overwriting_default_properties
41
+ @event_logger.event :original, :original, :event_scope => 'overwritten', :event_name => 'overwritten',
42
+ :event_timestamp => Time.parse('1912-01-01T04:00:00Z'), :event_uuid => 'overwritten'
43
+
44
+ assert_last_event_contains_value 'original', :event_scope
45
+ assert_last_event_contains_value 'original', :event_name
46
+ assert_last_event_contains_value 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', :event_uuid
47
+ assert_last_event_contains_value '1912-01-01T04:00:00Z', :event_timestamp
27
48
  end
28
49
 
29
50
  def test_should_log_flatten_hash
30
- Timecop.travel(@time) do
31
- @event_logger.event "render", "error", {:status => "status", :message => {:first => "first", :second => "second"}}
51
+ @event_logger.event "render", "error", {:status => "status", :message => {:first => "first", :second => "second"}}
32
52
 
33
- assert_equal "{\"status\":\"status\",\"message_first\":\"first\",\"message_second\":\"second\",\"event\":\"error\",\"scope\":\"render\",\"timestamp\":\"2012-01-01T05:00:00Z\"}\n", @json_io.string
34
- assert_equal " [render] error: status=status, message_first=first, message_second=second\n", @nonstructured_io.string
35
- end
53
+ assert_equal " [render] error: status=status, message_first=first, message_second=second\n", @nonstructured_io.string
54
+ assert_last_event_contains_value 'first', :message_first
55
+ assert_last_event_contains_value 'second', :message_second
56
+ assert_last_event_contains_value 'status', :status
36
57
  end
37
58
 
38
59
  def test_should_log_to_current_context
39
- Timecop.travel(@time) do
40
- Thread.new do
41
- @event_logger.context[:request_id] = '1'
60
+ Thread.new do
61
+ @event_logger.context[:request_id] = '1'
42
62
 
43
- Thread.new do
44
- @event_logger.context[:request_id] = '2'
45
- @event_logger.event :render, :error
46
- end.join
63
+ Thread.new do
64
+ @event_logger.context[:request_id] = '2'
65
+ @event_logger.event :render, :error
47
66
  end.join
48
- end
49
-
50
- assert_equal "{\"request_id\":\"2\",\"event\":\"error\",\"scope\":\"render\",\"timestamp\":\"2012-01-01T05:00:00Z\"}\n", @json_io.string
51
- end
67
+ end.join
52
68
 
53
- def assert_event_contains_value(value, key)
54
- @event_logger.event :some_scope, :some_event
55
- assert_equal value, JSON.parse(@json_io.string)[key.to_s]
69
+ assert_last_event_contains_value '2', :request_id
56
70
  end
57
71
 
58
72
  def test_default_context_gets_merged
59
73
  @event_logger.default_context[:foo] = 42
60
- assert_event_contains_value 42, :foo
74
+ @event_logger.event :some_scope, :some_event
75
+ assert_last_event_contains_value 42, :foo
61
76
  end
62
77
 
63
78
  def test_default_context_values_can_be_overriden
64
79
  @event_logger.default_context[:foo] = 42
65
80
  @event_logger.context[:foo] = 43
66
- assert_event_contains_value 43, :foo
81
+ @event_logger.event :some_scope, :some_event
82
+ assert_last_event_contains_value 43, :foo
67
83
  end
68
84
 
69
85
  def test_default_context_gets_merged_again_after_clear
70
86
  @event_logger.default_context[:foo] = 42
71
87
  @event_logger.context.clear
72
- assert_event_contains_value 42, :foo
88
+ @event_logger.event :some_scope, :some_event
89
+ assert_last_event_contains_value 42, :foo
73
90
  end
74
91
 
75
92
  def test_should_clear_context
76
- Timecop.travel(@time) do
77
- Thread.new do
78
- @event_logger.context[:request_id] = '1'
79
- @event_logger.event :render, :in_thread
80
- @event_logger.context.clear
81
- end.join
93
+ Thread.new do
94
+ @event_logger.context[:request_id] = '1'
95
+ @event_logger.event :render, :in_thread
96
+ @event_logger.context.clear
97
+ end.join
82
98
 
83
- @event_logger.event :render, :out_thread
99
+ assert_last_event_contains_value '1', :request_id
84
100
 
85
- log_lines = @json_io.string.lines.entries
86
- assert_equal "{\"request_id\":\"1\",\"event\":\"in_thread\",\"scope\":\"render\",\"timestamp\":\"2012-01-01T05:00:00Z\"}\n", log_lines[0]
87
- assert_equal "{\"event\":\"out_thread\",\"scope\":\"render\",\"timestamp\":\"2012-01-01T05:00:00Z\"}\n", log_lines[1]
88
- end
101
+ @event_logger.event :render, :out_thread
102
+ log_lines = @json_io.string.lines.entries
103
+
104
+ assert_last_event_does_not_contain :request_id
89
105
  end
90
- end
91
106
 
107
+ private
108
+
109
+ def assert_last_event_contains_value(value, key)
110
+ assert_equal value, last_parsed_event[key.to_s]
111
+ end
112
+
113
+ def assert_last_event_does_not_contain(key)
114
+ assert !last_parsed_event.has_key?(key.to_s)
115
+ end
116
+
117
+ def last_parsed_event
118
+ JSON.parse(@json_io.string.lines.entries[-1])
119
+ end
120
+ end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'bundler/setup'
2
2
  require 'minitest/autorun'
3
3
  require 'minitest/pride'
4
- require 'timecop'
4
+ require 'mocha/setup'
5
5
 
6
6
  $LOAD_PATH.unshift File.expand_path('../lib', File.dirname(__FILE__))
7
7
  require 'structured_event_logger'
metadata CHANGED
@@ -1,20 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structured-event-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Emilie Noel
8
9
  - Aaron Olson
9
10
  - Willem van Bergen
11
+ - Florian Weingarten
10
12
  autorequire:
11
13
  bindir: bin
12
14
  cert_chain: []
13
- date: 2013-08-08 00:00:00.000000000 Z
15
+ date: 2013-08-09 00:00:00.000000000 Z
14
16
  dependencies:
15
17
  - !ruby/object:Gem::Dependency
16
18
  name: activesupport
17
19
  requirement: !ruby/object:Gem::Requirement
20
+ none: false
18
21
  requirements:
19
22
  - - ~>
20
23
  - !ruby/object:Gem::Version
@@ -22,6 +25,7 @@ dependencies:
22
25
  type: :runtime
23
26
  prerelease: false
24
27
  version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
25
29
  requirements:
26
30
  - - ~>
27
31
  - !ruby/object:Gem::Version
@@ -29,6 +33,7 @@ dependencies:
29
33
  - !ruby/object:Gem::Dependency
30
34
  name: multi_json
31
35
  requirement: !ruby/object:Gem::Requirement
36
+ none: false
32
37
  requirements:
33
38
  - - ! '>='
34
39
  - !ruby/object:Gem::Version
@@ -36,6 +41,7 @@ dependencies:
36
41
  type: :runtime
37
42
  prerelease: false
38
43
  version_requirements: !ruby/object:Gem::Requirement
44
+ none: false
39
45
  requirements:
40
46
  - - ! '>='
41
47
  - !ruby/object:Gem::Version
@@ -43,6 +49,7 @@ dependencies:
43
49
  - !ruby/object:Gem::Dependency
44
50
  name: bundler
45
51
  requirement: !ruby/object:Gem::Requirement
52
+ none: false
46
53
  requirements:
47
54
  - - ~>
48
55
  - !ruby/object:Gem::Version
@@ -50,6 +57,7 @@ dependencies:
50
57
  type: :development
51
58
  prerelease: false
52
59
  version_requirements: !ruby/object:Gem::Requirement
60
+ none: false
53
61
  requirements:
54
62
  - - ~>
55
63
  - !ruby/object:Gem::Version
@@ -57,6 +65,7 @@ dependencies:
57
65
  - !ruby/object:Gem::Dependency
58
66
  name: rake
59
67
  requirement: !ruby/object:Gem::Requirement
68
+ none: false
60
69
  requirements:
61
70
  - - ! '>='
62
71
  - !ruby/object:Gem::Version
@@ -64,6 +73,7 @@ dependencies:
64
73
  type: :development
65
74
  prerelease: false
66
75
  version_requirements: !ruby/object:Gem::Requirement
76
+ none: false
67
77
  requirements:
68
78
  - - ! '>='
69
79
  - !ruby/object:Gem::Version
@@ -71,6 +81,7 @@ dependencies:
71
81
  - !ruby/object:Gem::Dependency
72
82
  name: minitest
73
83
  requirement: !ruby/object:Gem::Requirement
84
+ none: false
74
85
  requirements:
75
86
  - - ~>
76
87
  - !ruby/object:Gem::Version
@@ -78,13 +89,15 @@ dependencies:
78
89
  type: :development
79
90
  prerelease: false
80
91
  version_requirements: !ruby/object:Gem::Requirement
92
+ none: false
81
93
  requirements:
82
94
  - - ~>
83
95
  - !ruby/object:Gem::Version
84
96
  version: '5.0'
85
97
  - !ruby/object:Gem::Dependency
86
- name: timecop
98
+ name: mocha
87
99
  requirement: !ruby/object:Gem::Requirement
100
+ none: false
88
101
  requirements:
89
102
  - - ! '>='
90
103
  - !ruby/object:Gem::Version
@@ -92,6 +105,7 @@ dependencies:
92
105
  type: :development
93
106
  prerelease: false
94
107
  version_requirements: !ruby/object:Gem::Requirement
108
+ none: false
95
109
  requirements:
96
110
  - - ! '>='
97
111
  - !ruby/object:Gem::Version
@@ -118,26 +132,27 @@ files:
118
132
  homepage: https://github.com/Shopify/structured-event-logger
119
133
  licenses:
120
134
  - MIT
121
- metadata: {}
122
135
  post_install_message:
123
136
  rdoc_options: []
124
137
  require_paths:
125
138
  - lib
126
139
  required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
127
141
  requirements:
128
142
  - - ! '>='
129
143
  - !ruby/object:Gem::Version
130
144
  version: '0'
131
145
  required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
132
147
  requirements:
133
148
  - - ! '>='
134
149
  - !ruby/object:Gem::Version
135
150
  version: '0'
136
151
  requirements: []
137
152
  rubyforge_project:
138
- rubygems_version: 2.0.6
153
+ rubygems_version: 1.8.23
139
154
  signing_key:
140
- specification_version: 4
155
+ specification_version: 3
141
156
  summary: Structured event logger that writes events to both a human readable log and
142
157
  a JSON formatted log
143
158
  test_files:
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZDcyNDFkNzJlNWFlYTc0NzUxNDE5YzNhMmE3MjgzNmRiOTUwYTlmYw==
5
- data.tar.gz: !binary |-
6
- YTE4MDdiMDZjNWJmOTAwODk2YTE5YWJiZTMwMjQ1YjA0N2I4NjQ1MA==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- MTQwNmQzNGJmMjdjNTZiY2I3ZjYwMjA5NWY4ZGE0NjY0MTk3MWJjM2U1ZWI5
10
- OGM2Mzc0MjA1NDdmOTE3NjFjYmJhN2ZmNjI4OGNlN2RjYjk3OTk0YmQ4NzU2
11
- MWE0MmRmY2VhOTMzYjczYWYzMmZkMjE0NDhmMzdkYjNiMzc0ZDc=
12
- data.tar.gz: !binary |-
13
- NWZmMTMyMWU2NWVmZjBiMjJlOTNhODQ0NjVjMmQyM2ZiYmFhOWU2MjM0Mzhi
14
- YzVlYWFhNDdkZmYxMGY3OTliNzZiNjMzM2VjMmQ0MDRkOTMxNmJlMzIwNGQw
15
- OTQwOGZjNzU2NjA3MmY1NGM3MzlmNjIxMWYzYThlNjJjNWU0NmI=