Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context providers refactor #90

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/twiglet/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ def initialize(
validator:,
default_properties: {},
context_provider: nil,
context_providers: [],
now: -> { Time.now.utc }
)
@service_name = service_name
@now = now
@default_properties = default_properties
@context_provider = context_provider
@context_providers = context_provider ? [context_provider] : context_providers
@validator = validator

super()
Expand Down Expand Up @@ -45,7 +46,9 @@ def log(level:, message:)
}
}

context = @context_provider&.call || {}
context = @context_providers.reduce({}) do |c, context_provider|
c.deep_merge(context_provider.call)
end

JSON.generate(
base_message
Expand Down
13 changes: 5 additions & 8 deletions lib/twiglet/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def initialize(
formatter = Twiglet::Formatter.new(
service_name,
default_properties: args.fetch(:default_properties, {}),
context_provider: args[:context_provider],
context_providers: Array(args[:context_provider] || args[:context_providers]),
now: now,
validator: @validator
)
Expand Down Expand Up @@ -82,15 +82,12 @@ def with(default_properties)
end

def context_provider(&blk)
new_context_provider = blk
if @args[:context_provider]
new_context_provider = lambda do
@args[:context_provider].call.merge(blk.call)
end
end
new_context_providers = Array(@args[:context_providers])
new_context_providers << blk

self.class.new(
@service_name,
**@args.merge(context_provider: new_context_provider)
**@args.merge(context_providers: new_context_providers)
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/twiglet/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Twiglet
VERSION = '3.10.0'
VERSION = '3.11.0'
end
28 changes: 28 additions & 0 deletions test/formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,32 @@
}
assert_equal JSON.parse(msg), expected_log
end

it 'merges the outputs of all context providers into the messages log' do
provider_1 = -> { { 'request' => { 'id' => '1234567890' } } }
provider_2 = -> { { 'request' => { 'type' => 'test' } } }
formatter = Twiglet::Formatter.new(
'petshop', now: @now, validator: Twiglet::Validator.new({}.to_json),
context_providers: [provider_1, provider_2]
)
msg = formatter.call('warn', nil, nil, 'shop is running low on dog food')
expected_log = {
"ecs" => {
"version" => '1.5.0'
},
"@timestamp" => '2020-05-11T15:01:01.000Z',
"service" => {
"name" => 'petshop'
},
"log" => {
"level" => 'warn'
},
"message" => 'shop is running low on dog food',
"request" => {
'id' => '1234567890',
'type' => 'test'
}
}
assert_equal expected_log, JSON.parse(msg)
end
end
35 changes: 32 additions & 3 deletions test/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,37 @@
end

it "should be able to add contextual information to events with the context_provider" do
purchase_logger = @logger.context_provider do
{ 'context' => { 'id' => 'my-context-id' } }
end
provider = -> { { 'context' => { 'id' => 'my-context-id' } } }
purchase_logger = Twiglet::Logger.new(
'petshop',
now: @now,
output: @buffer,
context_provider: provider
)

# do stuff
purchase_logger.info(
{
message: 'customer bought a dog',
pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
}
)

log = read_json @buffer

assert_equal 'customer bought a dog', log[:message]
assert_equal 'my-context-id', log[:context][:id]
end

it "should be able to add contextual information to events with multiple context providers" do
provider_1 = -> { { 'context' => { 'id' => 'my-context-id' } } }
provider_2 = -> { { 'context' => { 'type' => 'test' } } }
purchase_logger = Twiglet::Logger.new(
'petshop',
now: @now,
output: @buffer,
context_providers: [provider_1, provider_2]
)

# do stuff
purchase_logger.info(
Expand All @@ -205,6 +233,7 @@

assert_equal 'customer bought a dog', log[:message]
assert_equal 'my-context-id', log[:context][:id]
assert_equal 'test', log[:context][:type]
end

it "chaining .with and .context_provider is possible" do
Expand Down
Loading