lib/opentelemetry/sdk/trace/span.rb in opentelemetry-sdk-1.4.1 vs lib/opentelemetry/sdk/trace/span.rb in opentelemetry-sdk-1.5.0

- old
+ new

@@ -116,10 +116,41 @@ end end self end + # Add a link to a {Span}. + # + # Adding links at span creation using the `links` option is preferred + # to calling add_link later, because head sampling decisions can only + # consider information present during span creation. + # + # Example: + # + # span.add_link(OpenTelemetry::Trace::Link.new(span_to_link_from.context)) + # + # Note that the OpenTelemetry project + # {https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md + # documents} certain "standard attributes" that have prescribed semantic + # meanings. + # + # @param [OpenTelemetry::Trace::Link] the link object to add on the {Span}. + # + # @return [self] returns itself + def add_link(link) + @mutex.synchronize do + if @ended + OpenTelemetry.logger.warn('Calling add_link on an ended Span.') + else + @links ||= [] + @links = trim_links(@links << link, @span_limits.link_count_limit, @span_limits.link_attribute_count_limit) + @total_recorded_links += 1 + end + end + self + end + # Add an Event to a {Span}. # # Example: # # span.add_event('event', attributes: {'eager' => true}) @@ -240,10 +271,11 @@ return self end @end_timestamp = relative_timestamp(end_timestamp) @attributes = validated_attributes(@attributes).freeze @events.freeze + @links.freeze @ended = true end @span_processors.each { |processor| processor.on_finish(self) } self end @@ -371,11 +403,11 @@ # Fast path (likely) common cases. return nil if links.nil? if links.size <= link_count_limit && links.all? { |link| link.span_context.valid? && link.attributes.size <= link_attribute_count_limit && Internal.valid_attributes?(name, 'link', link.attributes) } - return links.frozen? ? links : links.clone.freeze + return links end # Slow path: trim attributes for each Link. valid_links = links.select { |link| link.span_context.valid? } excess_link_count = valid_links.size - link_count_limit @@ -384,10 +416,10 @@ attrs = Hash[link.attributes] # link.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 - link_attribute_count_limit excess.times { attrs.shift } if excess.positive? OpenTelemetry::Trace::Link.new(link.span_context, attrs) - end.freeze + end end def append_event(events, event) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity event_count_limit = @span_limits.event_count_limit event_attribute_count_limit = @span_limits.event_attribute_count_limit