lib/opentelemetry/sdk/trace/span.rb in opentelemetry-sdk-0.17.0 vs lib/opentelemetry/sdk/trace/span.rb in opentelemetry-sdk-1.0.0.rc1

- old
+ new

@@ -15,12 +15,13 @@ # and should consider {Span} to be write-only. # # rubocop:disable Metrics/ClassLength class Span < OpenTelemetry::Trace::Span DEFAULT_STATUS = OpenTelemetry::Trace::Status.new(OpenTelemetry::Trace::Status::UNSET) + EMPTY_ATTRIBUTES = {}.freeze - private_constant(:DEFAULT_STATUS) + private_constant :DEFAULT_STATUS, :EMPTY_ATTRIBUTES # The following readers are intended for the use of SpanProcessors and # should not be considered part of the public interface for instrumentation. attr_reader :name, :status, :kind, :parent_span_id, :start_timestamp, :end_timestamp, :links, :resource, :instrumentation_library @@ -68,11 +69,10 @@ # Array values must not contain nil elements and all elements must be of # the same basic type (string, numeric, boolean). # # @return [self] returns itself def set_attribute(key, value) - super @mutex.synchronize do if @ended OpenTelemetry.logger.warn('Calling set_attribute on an ended Span.') else @attributes ||= {} @@ -97,11 +97,10 @@ # Array values must not contain nil elements and all elements must be of # the same basic type (string, numeric, boolean). # # @return [self] returns itself def add_attributes(attributes) - super @mutex.synchronize do if @ended OpenTelemetry.logger.warn('Calling add_attributes on an ended Span.') else @attributes ||= {} @@ -130,12 +129,11 @@ # values may be (array of) string, boolean or numeric type. # @param [optional Time] timestamp Optional timestamp for the event. # # @return [self] returns itself def add_event(name, attributes: nil, timestamp: nil) - super - event = Event.new(name: name, attributes: truncate_attribute_values(attributes), timestamp: timestamp || Time.now) + event = Event.new(name, truncate_attribute_values(attributes), wall_clock(timestamp)) @mutex.synchronize do if @ended OpenTelemetry.logger.warn('Calling add_event on an ended Span.') else @@ -177,11 +175,10 @@ # @param [Status] status The new status, which overrides the default Span # status, which is OK. # # @return [void] def status=(status) - super @mutex.synchronize do if @ended OpenTelemetry.logger.warn('Calling status= on an ended Span.') else @status = status @@ -197,11 +194,10 @@ # @param [String] new_name The new operation name, which supersedes # whatever was passed in when the Span was started # # @return [void] def name=(new_name) - super @mutex.synchronize do if @ended OpenTelemetry.logger.warn('Calling name= on an ended Span.') else @name = new_name @@ -232,11 +228,11 @@ @mutex.synchronize do if @ended OpenTelemetry.logger.warn('Calling finish on an ended Span.') return self end - @end_timestamp = end_timestamp || Time.now + @end_timestamp = wall_clock(end_timestamp) @attributes = validated_attributes(@attributes).freeze @events.freeze @ended = true end @span_processor.on_finish(self) @@ -290,11 +286,11 @@ @ended = false @status = DEFAULT_STATUS @total_recorded_events = 0 @total_recorded_links = links&.size || 0 @total_recorded_attributes = attributes&.size || 0 - @start_timestamp = start_timestamp + @start_timestamp = wall_clock(start_timestamp) @end_timestamp = nil @attributes = attributes.nil? ? nil : Hash[attributes] # We need a mutable copy of attributes. trim_span_attributes(@attributes) @events = nil @links = trim_links(links, trace_config.max_links_count, trace_config.max_attributes_per_link) @@ -304,11 +300,11 @@ # 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) + return attrs if Internal.valid_attributes?(name, 'span', attrs) attrs.keep_if { |key, value| Internal.valid_key?(key) && Internal.valid_value?(value) } end def trim_span_attributes(attrs) @@ -319,11 +315,11 @@ truncate_attribute_values(attrs) nil end def truncate_attribute_values(attrs) - return if attrs.nil? + return EMPTY_ATTRIBUTES if attrs.nil? max_attributes_length = @trace_config.max_attributes_length attrs.each { |key, value| attrs[key] = OpenTelemetry::Common::Utilities.truncate(value, max_attributes_length) } if max_attributes_length attrs end @@ -331,11 +327,11 @@ def trim_links(links, max_links_count, max_attributes_per_link) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity # Fast path (likely) common cases. return nil if links.nil? if links.size <= max_links_count && - links.all? { |link| link.attributes.size <= max_attributes_per_link && Internal.valid_attributes?(link.attributes) } + links.all? { |link| link.attributes.size <= max_attributes_per_link && Internal.valid_attributes?(name, 'link', link.attributes) } return links.frozen? ? links : links.clone.freeze end # Slow path: trim attributes for each Link. links.last(max_links_count).map! do |link| @@ -348,11 +344,11 @@ 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) + valid_attributes = Internal.valid_attributes?(name, 'event', event.attributes) # Fast path (likely) common case. if events.size < max_events_count && event.attributes.size <= max_attributes_per_event && valid_attributes @@ -367,12 +363,17 @@ 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) + event = Event.new(event.name, attrs.freeze, event.timestamp) end events << event + end + + def wall_clock(timestamp) + timestamp = (timestamp.to_r * 1_000_000_000).to_i unless timestamp.nil? + timestamp || Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond) end end # rubocop:enable Metrics/ClassLength end end