Skip to content

Commit 82ef32e

Browse files
feat: Update error message for invalid log record attribute (#1825)
* feat: Use logger.warn for log attribute validation Update the way users are notified about invalid log record attribute keys and values by using OpenTelemetry.logger.warn instead of OpenTelemetry.handle_error. Also, include the key with the invalid type in the message. * test: Update tests for new log message * chore: Rubocop Bundler/OrderedGems * fix: Use a handle_error call to match Traces * chore: Add missing :message kwarg * chore: Add missing empty line at EOF * chore: Remove mutex_m install * Update logs_sdk/Gemfile
1 parent ddb2a3c commit 82ef32e

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ def validate_attributes(attrs)
139139
# Future refactor opportunity: https://github.com/open-telemetry/opentelemetry-ruby/issues/1739
140140
attrs.keep_if do |k, v|
141141
if !Internal.valid_key?(k)
142-
OpenTelemetry.handle_error(message: "invalid log record attribute key type #{k.class} on record: '#{body}'")
142+
OpenTelemetry.handle_error(message: "Invalid log record attribute key type #{k.class} for " \
143+
"key #{k.inspect} on record: '#{body}'. Attribute keys must be Strings. Dropping attribute.")
143144
return false
144145
elsif !Internal.valid_value?(v)
145-
OpenTelemetry.handle_error(message: "invalid log record attribute value type #{v.class} for key '#{k}' on record: '#{body}'")
146+
OpenTelemetry.handle_error(message: "Invalid log record attribute value type #{v.class} for key '#{k}' " \
147+
"on record: '#{body}'. Dropping attribute.")
146148
return false
147149
end
148150

logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@
123123
it 'emits an error message if attribute key is invalid' do
124124
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
125125
logger.on_emit(attributes: { a: 'a' })
126-
assert_match(/invalid log record attribute key type Symbol/, log_stream.string)
126+
assert_match(/Invalid log record attribute key type Symbol/, log_stream.string)
127127
end
128128
end
129129

130130
it 'emits an error message if the attribute value is invalid' do
131131
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
132132
logger.on_emit(attributes: { 'a' => Class.new })
133-
assert_match(/invalid log record attribute value type Class/, log_stream.string)
133+
assert_match(/Invalid log record attribute value type Class/, log_stream.string)
134134
end
135135
end
136136

0 commit comments

Comments
 (0)