lambdakiq
lambdakiq copied to clipboard
Duplicate key warning in Metrics#message - JSON 3.0 compatibility concern
Hello, thank you for maintaining this excellent gem. I've encountered an issue that I'd like to bring to your attention.
Issue Description
When using lambdakiq 2.3.0 with Ruby 3.4, I'm seeing warnings about duplicate keys in the hash returned by Lambdakiq::Metrics#message. While this currently only produces warnings, it may cause errors in the future when JSON 3.0 is released.
Warning Message
/path/to/gems/lambdakiq-2.3.0/lib/lambdakiq/metrics.rb:20: warning: detected duplicate key "JobName" in {...}.
This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
Root Cause Analysis
Looking at lib/lambdakiq/metrics.rb (around lines 105-109), I noticed that the message method merges data from two sources:
def message
{
'_aws': { ... }
}.tap do |m|
dimensions.each { |d| m.merge!(d) } # Merges Symbol keys (e.g., JobName: "SampleJob")
m.merge!(@properties) # Merges String keys (e.g., "JobName" => "SampleJob")
end
end
The dimensions array contains hashes with Symbol keys (:JobName, :AppName, etc.), while @properties contains String keys ("JobName", "AppName", etc.). This results in duplicate keys in the final hash.
Suggested Fix
I believe converting the dimension keys to strings before merging would resolve this issue:
def message
{
'_aws': { ... }
}.tap do |m|
dimensions.each { |d| d.each { |key, value| m[key.to_s] = value } }
m.merge!(@properties)
end
end
Temporary Workaround
For now, I've implemented the following workaround in our application:
module Lambdakiq
class Metrics
private
alias original_message message
def message
original_message.transform_keys(&:to_s)
end
end
end
Environment
- lambdakiq version: 2.3.0
- Ruby version: 3.4.0
- Rails version: 8.0.3
Request
Would you be open to accepting a pull request to fix this issue? I'd be happy to submit one if that would be helpful.
Thank you for your time and consideration.