lib/opentelemetry/sdk/trace/span.rb in opentelemetry-sdk-0.5.1 vs lib/opentelemetry/sdk/trace/span.rb in opentelemetry-sdk-0.6.0

- old
+ new

@@ -16,14 +16,14 @@ # # rubocop:disable Metrics/ClassLength class Span < OpenTelemetry::Trace::Span # 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, :library_resource, :instrumentation_library + attr_reader :name, :status, :kind, :parent_span_id, :start_timestamp, :end_timestamp, :links, :resource, :instrumentation_library # Return a frozen copy of the current attributes. This is intended for - # use of SpanProcesses and should not be considered part of the public + # use of SpanProcessors and should not be considered part of the public # interface for instrumentation. # # @return [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] may be nil. def attributes # Don't bother synchronizing. Access by SpanProcessors is expected to @@ -57,11 +57,11 @@ # {https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md # documents} certain "standard attributes" that have prescribed semantic # meanings. # # @param [String] key - # @param [String, Boolean, Numeric] value + # @param [String, Boolean, Numeric, Array<String, Numeric, Boolean>] value # # @return [self] returns itself def set_attribute(key, value) super @mutex.synchronize do @@ -74,41 +74,33 @@ @total_recorded_attributes += 1 end end self end + alias []= set_attribute - # Add an Event to a {Span}. This can be accomplished eagerly or lazily. - # Lazy evaluation is useful when the event attributes are expensive to - # build and where the cost can be avoided for an unsampled {Span}. + # Add an Event to a {Span}. # - # Eager example: + # Example: # - # span.add_event(name: 'event', attributes: {'eager' => true}) + # span.add_event('event', attributes: {'eager' => true}) # - # Lazy example: - # - # span.add_event { OpenTelemetry::Trace::Event.new(name: 'event', attributes: {'eager' => false}) } - # # Note that the OpenTelemetry project # {https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md # documents} certain "standard event names and keys" which have # prescribed semantic meanings. # - # @param [optional String] name Optional name of the event. This is - # required if a block is not given. + # @param [String] name Name of the event. # @param [optional Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] attributes # One or more key:value pairs, where the keys must be strings and the - # values may be string, boolean or numeric type. This argument should - # only be used when passing in a name. + # values may be string, boolean or numeric type. # @param [optional Time] timestamp Optional timestamp for the event. - # This argument should only be used when passing in a name. # # @return [self] returns itself - def add_event(name: nil, attributes: nil, timestamp: nil) + def add_event(name, attributes: nil, timestamp: nil) super - event = block_given? ? yield : OpenTelemetry::Trace::Event.new(name: name, attributes: attributes, timestamp: timestamp || Time.now) + event = Event.new(name: name, attributes: attributes, timestamp: timestamp || Time.now) @mutex.synchronize do if @ended OpenTelemetry.logger.warn('Calling add_event on an ended Span.') else @@ -118,22 +110,22 @@ end end self end - # Record an error during the execution of this span. Multiple errors + # Record an exception during the execution of this span. Multiple exceptions # can be recorded on a span. # - # @param [Exception] error The error to be recorded + # @param [Exception] exception The exception to be recorded # # @return [void] - def record_error(error) - add_event(name: 'error', + def record_exception(exception) + add_event('exception', attributes: { - 'error.type' => error.class.to_s, - 'error.message' => error.message, - 'error.stack' => error.backtrace.join("\n") + 'exception.type' => exception.class.to_s, + 'exception.message' => exception.message, + 'exception.stacktrace' => exception.full_message(highlight: false, order: :top) }) end # Sets the Status to the Span # @@ -234,29 +226,29 @@ @start_timestamp, @end_timestamp, @attributes, @links, @events, - @library_resource, + @resource, @instrumentation_library, context.span_id, context.trace_id, context.trace_flags, context.tracestate ) end # @api private - def initialize(context, name, kind, parent_span_id, trace_config, span_processor, attributes, links, start_timestamp, library_resource, instrumentation_library) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + def initialize(context, name, kind, parent_span_id, trace_config, span_processor, attributes, links, start_timestamp, resource, instrumentation_library) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength super(span_context: context) @mutex = Mutex.new @name = name @kind = kind @parent_span_id = parent_span_id.freeze || OpenTelemetry::Trace::INVALID_SPAN_ID @trace_config = trace_config @span_processor = span_processor - @library_resource = library_resource + @resource = resource @instrumentation_library = instrumentation_library @ended = false @status = nil @child_count = 0 @total_recorded_events = 0 @@ -323,10 +315,10 @@ if excess.positive? || !Internal.valid_attributes?(event.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 = OpenTelemetry::Trace::Event.new(name: event.name, attributes: attrs, timestamp: event.timestamp) + event = Event.new(name: event.name, attributes: attrs, timestamp: event.timestamp) end events << event end end # rubocop:enable Metrics/ClassLength