Skip to content

Commit

Permalink
fix: return nil for non-existant key in AwsSdk::MessageAttributeGetter (
Browse files Browse the repository at this point in the history
#853)

Co-authored-by: Ariel Valentin <[email protected]>
  • Loading branch information
yoheyk and arielvalentin authored Feb 8, 2024
1 parent 716e901 commit 85c7f5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def self.set(carrier, key, value)
# OpenTelemetry.propagation.extract(message, getter: MessageAttributeGetter)
class MessageAttributeGetter
def self.get(carrier, key)
carrier[key][:string_value] if carrier[key][:data_type] == 'String'
message_attribute = carrier[key]
message_attribute[:string_value] if message_attribute && message_attribute[:data_type] == 'String'
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions instrumentation/aws_sdk/test/opentelemetry/instrumentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,25 @@
OpenTelemetry::Instrumentation::AwsSdk::MessageAttributeSetter.set(metadata_attributes, 'new10', 'value')
_(metadata_attributes.keys).must_equal(%w[existingKey0 existingKey1 existingKey2 existingKey3 existingKey4 existingKey5 existingKey6 existingKey7 existingKey8 existingKey9])
end

describe 'MessageAttributeGetter' do
let(:getter) { OpenTelemetry::Instrumentation::AwsSdk::MessageAttributeGetter }
let(:carrier) do
{
'traceparent' => { data_type: 'String', string_value: 'tp' },
'tracestate' => { data_type: 'String', string_value: 'ts' },
'x-source-id' => { data_type: 'String', string_value: '123' }
}
end

it 'reads key from carrier' do
_(getter.get(carrier, 'traceparent')).must_equal('tp')
_(getter.get(carrier, 'x-source-id')).must_equal('123')
end

it 'returns nil for non-existant key' do
_(getter.get(carrier, 'not-here')).must_be_nil
end
end
end
end

0 comments on commit 85c7f5c

Please sign in to comment.