diff --git a/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/message_attributes.rb b/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/message_attributes.rb index 4a4519f02..40598b977 100644 --- a/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/message_attributes.rb +++ b/instrumentation/aws_sdk/lib/opentelemetry/instrumentation/aws_sdk/message_attributes.rb @@ -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 diff --git a/instrumentation/aws_sdk/test/opentelemetry/instrumentation_test.rb b/instrumentation/aws_sdk/test/opentelemetry/instrumentation_test.rb index 1917da5e0..f44e81bc5 100644 --- a/instrumentation/aws_sdk/test/opentelemetry/instrumentation_test.rb +++ b/instrumentation/aws_sdk/test/opentelemetry/instrumentation_test.rb @@ -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