Class: Appsignal::Span

Inherits:
Object show all
Defined in:
lib/appsignal/span.rb

Instance Method Summary collapse

Constructor Details

#initialize(namespace = nil, ext = nil) ⇒ Span

Returns a new instance of Span.



3
4
5
6
7
8
9
# File 'lib/appsignal/span.rb', line 3

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

Instance Method Details

#[]=(key, value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/appsignal/span.rb', line 43

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

#add_error(error) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/appsignal/span.rb', line 19

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

#childObject



11
12
13
# File 'lib/appsignal/span.rb', line 11

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

#closeObject



74
75
76
# File 'lib/appsignal/span.rb', line 74

def close
  @ext.close
end

#closed?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/appsignal/span.rb', line 78

def closed?
  to_h.nil?
end

#instrumentObject



68
69
70
71
72
# File 'lib/appsignal/span.rb', line 68

def instrument
  yield self
ensure
  close
end

#name=(value) ⇒ Object



15
16
17
# File 'lib/appsignal/span.rb', line 15

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

#set_sample_data(key, data) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/appsignal/span.rb', line 35

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

#to_hObject



62
63
64
65
66
# File 'lib/appsignal/span.rb', line 62

def to_h
  json = @ext.to_json
  return unless json
  JSON.parse(json)
end