Sha256: e1cf9b0b2f13fc2741e8bc2036cd4dc692759c034cfa8a1765bf943ce788a895

Contents?: true

Size: 1.99 KB

Versions: 38

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Appsignal
  class Span
    def initialize(namespace = nil, ext = nil)
      @ext = ext || Appsignal::Extension::Span.root(namespace || "")
    end

    def child
      Span.new(nil, @ext.child)
    end

    def name=(value)
      @ext.set_name(value)
    end

    def add_error(error)
      unless error.is_a?(Exception)
        Appsignal.internal_logger.error "Appsignal::Span#add_error: Cannot " \
          "add error. The given value is not an exception: #{error.inspect}"
        return
      end
      return unless error

      backtrace = cleaned_backtrace(error.backtrace)
      @ext.add_error(
        error.class.name,
        error.message.to_s,
        backtrace ? Appsignal::Utils::Data.generate(backtrace) : Appsignal::Extension.data_array_new
      )
    end

    def set_sample_data(key, data)
      return unless key && data && (data.is_a?(Array) || data.is_a?(Hash))

      @ext.set_sample_data(
        key.to_s,
        Appsignal::Utils::Data.generate(data)
      )
    end

    def []=(key, value)
      case value
      when String
        @ext.set_attribute_string(key.to_s, value)
      when Integer
        begin
          @ext.set_attribute_int(key.to_s, value)
        rescue RangeError
          @ext.set_attribute_string(key.to_s, "bigint:#{value}")
        end
      when TrueClass, FalseClass
        @ext.set_attribute_bool(key.to_s, value)
      when Float
        @ext.set_attribute_double(key.to_s, value)
      else
        raise TypeError, "value needs to be a string, int, bool or float"
      end
    end

    def to_h
      json = @ext.to_json
      return unless json

      JSON.parse(json)
    end

    def instrument
      yield self
    ensure
      close
    end

    def close
      @ext.close
    end

    def closed?
      to_h.nil?
    end

    private

    def cleaned_backtrace(backtrace)
      if defined?(::Rails) && backtrace
        ::Rails.backtrace_cleaner.clean(backtrace, nil)
      else
        backtrace
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
appsignal-3.10.0-java lib/appsignal/span.rb
appsignal-3.10.0 lib/appsignal/span.rb
appsignal-3.9.3-java lib/appsignal/span.rb
appsignal-3.9.3 lib/appsignal/span.rb
appsignal-3.9.2-java lib/appsignal/span.rb
appsignal-3.9.2 lib/appsignal/span.rb
appsignal-3.9.1-java lib/appsignal/span.rb
appsignal-3.9.1 lib/appsignal/span.rb
appsignal-3.9.0-java lib/appsignal/span.rb
appsignal-3.9.0 lib/appsignal/span.rb
appsignal-3.8.1-java lib/appsignal/span.rb
appsignal-3.8.1 lib/appsignal/span.rb
appsignal-3.8.0-java lib/appsignal/span.rb
appsignal-3.8.0 lib/appsignal/span.rb
appsignal-3.7.6-java lib/appsignal/span.rb
appsignal-3.7.6 lib/appsignal/span.rb
appsignal-3.7.2-java lib/appsignal/span.rb
appsignal-3.7.2 lib/appsignal/span.rb
appsignal-3.7.1-java lib/appsignal/span.rb
appsignal-3.7.1 lib/appsignal/span.rb