Sha256: 2929da3ad10275d0c675772f54c278879cdafec741876780580e56a73b3b28af

Contents?: true

Size: 1.98 KB

Versions: 42

Compression:

Stored size: 1.98 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.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

42 entries across 42 versions & 1 rubygems

Version Path
appsignal-3.5.4-java lib/appsignal/span.rb
appsignal-3.5.4 lib/appsignal/span.rb
appsignal-3.5.3-java lib/appsignal/span.rb
appsignal-3.5.3 lib/appsignal/span.rb
appsignal-3.5.2-java lib/appsignal/span.rb
appsignal-3.5.2 lib/appsignal/span.rb
appsignal-3.5.1-java lib/appsignal/span.rb
appsignal-3.5.1 lib/appsignal/span.rb
appsignal-3.5.0-java lib/appsignal/span.rb
appsignal-3.5.0 lib/appsignal/span.rb
appsignal-3.4.16-java lib/appsignal/span.rb
appsignal-3.4.16 lib/appsignal/span.rb
appsignal-3.4.15-java lib/appsignal/span.rb
appsignal-3.4.15 lib/appsignal/span.rb
appsignal-3.4.14-java lib/appsignal/span.rb
appsignal-3.4.14 lib/appsignal/span.rb
appsignal-3.4.13-java lib/appsignal/span.rb
appsignal-3.4.13 lib/appsignal/span.rb
appsignal-3.4.12-java lib/appsignal/span.rb
appsignal-3.4.12 lib/appsignal/span.rb