lib/opentelemetry/sdk/trace/span.rb in opentelemetry-sdk-0.11.1 vs lib/opentelemetry/sdk/trace/span.rb in opentelemetry-sdk-0.12.0
- old
+ new
@@ -197,11 +197,11 @@
if @ended
OpenTelemetry.logger.warn('Calling finish on an ended Span.')
return self
end
@end_timestamp = end_timestamp || Time.now
- @attributes.freeze
+ @attributes = validated_attributes(@attributes).freeze
@events.freeze
@ended = true
end
@span_processor.on_finish(self)
self
@@ -267,16 +267,20 @@
# TODO: Java implementation overrides finalize to log if a span isn't finished.
private
+ def validated_attributes(attrs)
+ return attrs if Internal.valid_attributes?(attrs)
+
+ attrs.keep_if { |key, value| Internal.valid_key?(key) && Internal.valid_value?(value) }
+ end
+
def trim_span_attributes(attrs)
return if attrs.nil?
excess = attrs.size - @trace_config.max_attributes_count
- # TODO: with Ruby 2.5, replace with the more efficient
- # attrs.shift(excess) if excess.positive?
excess.times { attrs.shift } if excess.positive?
nil
end
def trim_links(links, max_links_count, max_attributes_per_link) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@@ -299,23 +303,24 @@
end
def append_event(events, event) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
max_events_count = @trace_config.max_events_count
max_attributes_per_event = @trace_config.max_attributes_per_event
+ valid_attributes = Internal.valid_attributes?(event.attributes)
# Fast path (likely) common case.
if events.size < max_events_count &&
event.attributes.size <= max_attributes_per_event &&
- Internal.valid_attributes?(event.attributes)
+ valid_attributes
return events << event
end
# Slow path.
excess = events.size + 1 - max_events_count
events.shift(excess) if excess.positive?
excess = event.attributes.size - max_attributes_per_event
- if excess.positive? || !Internal.valid_attributes?(event.attributes)
+ if excess.positive? || !valid_attributes
attrs = Hash[event.attributes] # event.attributes is frozen, so we need an unfrozen copy to adjust.
attrs.keep_if { |key, value| Internal.valid_key?(key) && Internal.valid_value?(value) }
excess = attrs.size - max_attributes_per_event
excess.times { attrs.shift } if excess.positive?
event = Event.new(name: event.name, attributes: attrs, timestamp: event.timestamp)