twiglet 3.4.3 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16ed3e64581c0d1c8908cf35d0dd77646a9e14be9f4fe39a6528a0c193f3bdfb
4
- data.tar.gz: 83a5aeb0da1c3d2d44c464fee1ea602189dbae971b01473ffd91bc7358a30a8b
3
+ metadata.gz: eaf4b42f0fd1e53803522582677f68f3a71835de59264853a8dac15bd385db7a
4
+ data.tar.gz: 56905f74d551eadb29c6ffbd6a1aa7e8ccba23e614c2ab36172b44c96e815c40
5
5
  SHA512:
6
- metadata.gz: f396edf27be32a886a2f08872632a1d5b19d236ce4eb20c84c1fc4fbe14889442c901e476d2b6def32329cf0149032c806d726d2a849da52f0e6a886ec6cf97d
7
- data.tar.gz: 4425f5082fe34808c45d9b7eaca355721c025db44e626cea2c2f958ec0ce2cd254ed1bfcb2fcdb39b975f60572560e1da3ca6293cc45a656c3d387411f918d68
6
+ metadata.gz: 36a81212d48fbd1d88b5fac2a6920cc1c6b03b5d4af03fc69ae3ec21d0d9109fd1c5e1afb7c07099e85f712a28bc319f05ed74fa5bc4a1ff3383e9de9a571a42
7
+ data.tar.gz: e727b067f3dce94ece0074108a704177ae101b3d78e13df801b147379f71041f19e3b5d519f60328783f27b9474ab3d8e9c96692da6c4144e94d8f9173c4b03e
@@ -38,7 +38,7 @@ jobs:
38
38
 
39
39
  steps:
40
40
  - name: Checkout repository
41
- uses: actions/checkout@v2
41
+ uses: actions/checkout@v3
42
42
 
43
43
  # Initializes the CodeQL tools for scanning.
44
44
  - name: Initialize CodeQL
@@ -3,6 +3,8 @@ name: "Dobby action"
3
3
  on:
4
4
  issue_comment:
5
5
  types: [created]
6
+ permissions:
7
+ contents: read
6
8
  jobs:
7
9
  pr_commented:
8
10
  runs-on: ubuntu-20.04
@@ -11,7 +13,7 @@ jobs:
11
13
  BUNDLE_WITHOUT: "development:test"
12
14
  steps:
13
15
  - name: Chekcout action
14
- uses: actions/checkout@v2
16
+ uses: actions/checkout@v3
15
17
  with:
16
18
  repository: 'simplybusiness/dobby'
17
19
  ref: 'v3.0.0'
@@ -20,7 +22,7 @@ jobs:
20
22
  with:
21
23
  bundler-cache: true
22
24
  - name: Bump version
23
- uses: simplybusiness/dobby@v3.0.0
25
+ uses: simplybusiness/dobby@v3.0.1
24
26
  env:
25
27
  DOBBY_APP_ID: ${{ secrets.DOBBY_APP_ID }}
26
28
  DOBBY_PRIVATE_KEY: ${{ secrets.DOBBY_PRIVATE_KEY }}
@@ -12,7 +12,7 @@ jobs:
12
12
  name: Build and Publish
13
13
  runs-on: ubuntu-latest
14
14
  steps:
15
- - uses: actions/checkout@v2
15
+ - uses: actions/checkout@v3
16
16
  - name: Set up Ruby
17
17
  uses: ruby/setup-ruby@v1
18
18
  with:
@@ -31,7 +31,7 @@ jobs:
31
31
  id: get-gem-version
32
32
  run: echo "::set-output name=GEM_VERSION::$(bundle exec ruby -e 'puts Twiglet::VERSION')"
33
33
  - name: Create Release
34
- uses: actions/github-script@v5
34
+ uses: actions/github-script@v6
35
35
  env:
36
36
  GEM_VERSION: ${{ steps.get-gem-version.outputs.GEM_VERSION }}
37
37
  with:
@@ -6,6 +6,8 @@ on:
6
6
 
7
7
  env:
8
8
  CI: true
9
+ permissions:
10
+ contents: read
9
11
 
10
12
  jobs:
11
13
  build:
@@ -19,7 +21,7 @@ jobs:
19
21
  ruby-version: [2.6, 2.7, 3.0]
20
22
 
21
23
  steps:
22
- - uses: actions/checkout@v2
24
+ - uses: actions/checkout@v3
23
25
  - name: Set up Ruby
24
26
  uses: ruby/setup-ruby@v1
25
27
  with:
@@ -5,6 +5,9 @@ on:
5
5
  branches:
6
6
  - master
7
7
  types: [opened, synchronize]
8
+ permissions:
9
+ contents: read
10
+ statuses: write
8
11
  jobs:
9
12
  build:
10
13
  runs-on: ubuntu-18.04
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Twiglet: Ruby version
2
2
  Like a log, only smaller.
3
3
 
4
- This library provides a minimal JSON logging interface suitable for use in (micro)services. See the [RATIONALE](RATIONALE.md) for design rationale and an explantion of the Elastic Common Schema that we are using for log attribute naming.
4
+ This library provides a minimal JSON logging interface suitable for use in (micro)services. See the [RATIONALE](docs/RATIONALE.md) for design rationale and an explantion of the Elastic Common Schema that we are using for log attribute naming.
5
5
 
6
6
  See [Full docs](docs/index.md)
@@ -7,11 +7,14 @@ module Twiglet
7
7
  Hash.include HashExtensions
8
8
 
9
9
  def initialize(service_name,
10
- validator:, default_properties: {},
10
+ validator:,
11
+ default_properties: {},
12
+ context_provider: nil,
11
13
  now: -> { Time.now.utc })
12
14
  @service_name = service_name
13
15
  @now = now
14
16
  @default_properties = default_properties
17
+ @context_provider = context_provider
15
18
  @validator = validator
16
19
 
17
20
  super()
@@ -40,8 +43,11 @@ module Twiglet
40
43
  }
41
44
  }
42
45
 
46
+ context = @context_provider&.call || {}
47
+
43
48
  base_message
44
49
  .deep_merge(@default_properties.to_nested)
50
+ .deep_merge(context.to_nested)
45
51
  .deep_merge(message.to_nested)
46
52
  .to_json
47
53
  .concat("\n")
@@ -16,7 +16,6 @@ module Twiglet
16
16
  **args
17
17
  )
18
18
  @service_name = service_name
19
- default_properties = args.delete(:default_properties) || {}
20
19
  @args = args
21
20
 
22
21
  now = args.fetch(:now, -> { Time.now.utc })
@@ -31,7 +30,8 @@ module Twiglet
31
30
 
32
31
  formatter = Twiglet::Formatter.new(
33
32
  service_name,
34
- default_properties: default_properties,
33
+ default_properties: args.fetch(:default_properties, {}),
34
+ context_provider: args[:context_provider],
35
35
  now: now,
36
36
  validator: @validator
37
37
  )
@@ -58,12 +58,19 @@ module Twiglet
58
58
  end
59
59
 
60
60
  def with(default_properties)
61
- Logger.new(
61
+ self.class.new(
62
62
  @service_name,
63
63
  **@args.merge(default_properties: default_properties)
64
64
  )
65
65
  end
66
66
 
67
+ def context_provider(&blk)
68
+ self.class.new(
69
+ @service_name,
70
+ **@args.merge(context_provider: blk)
71
+ )
72
+ end
73
+
67
74
  alias_method :warning, :warn
68
75
  alias_method :critical, :fatal
69
76
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Twiglet
4
- VERSION = '3.4.3'
4
+ VERSION = '3.5.0'
5
5
  end
@@ -32,4 +32,30 @@ describe Twiglet::Formatter do
32
32
  }
33
33
  assert_equal JSON.parse(msg), expected_log
34
34
  end
35
+
36
+ it 'merges the outputs of the context provider into messages logs' do
37
+ provider = -> { { 'request' => { 'id' => '1234567890' } } }
38
+ formatter = Twiglet::Formatter.new(
39
+ 'petshop', now: @now, validator: Twiglet::Validator.new({}.to_json),
40
+ context_provider: provider
41
+ )
42
+ msg = formatter.call('warn', nil, nil, 'shop is running low on dog food')
43
+ expected_log = {
44
+ "ecs" => {
45
+ "version" => '1.5.0'
46
+ },
47
+ "@timestamp" => '2020-05-11T15:01:01.000Z',
48
+ "service" => {
49
+ "name" => 'petshop'
50
+ },
51
+ "log" => {
52
+ "level" => 'warn'
53
+ },
54
+ "message" => 'shop is running low on dog food',
55
+ "request" => {
56
+ 'id' => '1234567890'
57
+ }
58
+ }
59
+ assert_equal JSON.parse(msg), expected_log
60
+ end
35
61
  end
data/test/logger_test.rb CHANGED
@@ -146,6 +146,115 @@ describe Twiglet::Logger do
146
146
  assert_equal 'Barker', log[:pet][:name]
147
147
  end
148
148
 
149
+ it "isn't possible to chain .with methods to gradually add messages" do
150
+ # Let's add some context to this customer journey
151
+ purchase_logger = @logger.with(
152
+ {
153
+ trace: { id: '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb' }
154
+ }
155
+ ).with(
156
+ {
157
+ customer: { full_name: 'Freda Bloggs' },
158
+ event: { action: 'pet purchase' }
159
+ }
160
+ )
161
+
162
+ # do stuff
163
+ purchase_logger.info(
164
+ {
165
+ message: 'customer bought a dog',
166
+ pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
167
+ }
168
+ )
169
+
170
+ log = read_json @buffer
171
+
172
+ assert_nil log[:trace]
173
+ assert_equal 'Freda Bloggs', log[:customer][:full_name]
174
+ assert_equal 'pet purchase', log[:event][:action]
175
+ assert_equal 'customer bought a dog', log[:message]
176
+ assert_equal 'Barker', log[:pet][:name]
177
+ end
178
+
179
+ it "should be able to add contextual information to events with the context_provider" do
180
+ purchase_logger = @logger.context_provider do
181
+ { 'context' => { 'id' => 'my-context-id' } }
182
+ end
183
+
184
+ # do stuff
185
+ purchase_logger.info(
186
+ {
187
+ message: 'customer bought a dog',
188
+ pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
189
+ }
190
+ )
191
+
192
+ log = read_json @buffer
193
+
194
+ assert_equal 'customer bought a dog', log[:message]
195
+ assert_equal 'my-context-id', log[:context][:id]
196
+ end
197
+
198
+ it "chaining .with and .context_provider is possible" do
199
+ # Let's add some context to this customer journey
200
+ purchase_logger = @logger.with(
201
+ {
202
+ trace: { id: '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb' },
203
+ customer: { full_name: 'Freda Bloggs' },
204
+ event: { action: 'pet purchase' }
205
+ }
206
+ ).context_provider do
207
+ { 'context' => { 'id' => 'my-context-id' } }
208
+ end
209
+
210
+ # do stuff
211
+ purchase_logger.info(
212
+ {
213
+ message: 'customer bought a dog',
214
+ pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
215
+ }
216
+ )
217
+
218
+ log = read_json @buffer
219
+
220
+ assert_equal '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb', log[:trace][:id]
221
+ assert_equal 'Freda Bloggs', log[:customer][:full_name]
222
+ assert_equal 'pet purchase', log[:event][:action]
223
+ assert_equal 'customer bought a dog', log[:message]
224
+ assert_equal 'Barker', log[:pet][:name]
225
+ assert_equal 'my-context-id', log[:context][:id]
226
+ end
227
+
228
+ it "chaining .context_provider and .with is possible" do
229
+ # Let's add some context to this customer journey
230
+ purchase_logger = @logger
231
+ .context_provider do
232
+ { 'context' => { 'id' => 'my-context-id' } }
233
+ end.with(
234
+ {
235
+ trace: { id: '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb' },
236
+ customer: { full_name: 'Freda Bloggs' },
237
+ event: { action: 'pet purchase' }
238
+ }
239
+ )
240
+ # do stuff
241
+ purchase_logger.info(
242
+ {
243
+ message: 'customer bought a dog',
244
+ pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
245
+ }
246
+ )
247
+
248
+ log = read_json @buffer
249
+
250
+ assert_equal '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb', log[:trace][:id]
251
+ assert_equal 'Freda Bloggs', log[:customer][:full_name]
252
+ assert_equal 'pet purchase', log[:event][:action]
253
+ assert_equal 'customer bought a dog', log[:message]
254
+ assert_equal 'Barker', log[:pet][:name]
255
+ assert_equal 'my-context-id', log[:context][:id]
256
+ end
257
+
149
258
  it "should log 'message' string property" do
150
259
  message = {}
151
260
  message['message'] = 'Guinea pigs arrived'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twiglet
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.3
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simply Business
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-01 00:00:00.000000000 Z
11
+ date: 2022-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema