Class: Karafka::Instrumentation::Vendors::Appsignal::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/instrumentation/vendors/appsignal/client.rb

Overview

Note:

This client is abstract, it has no notion of Karafka whatsoever

Appsignal client wrapper We wrap the native client so we can inject our own stub in specs when needed

It also abstracts away the notion of transactions and their management

Instance Method Summary collapse

Constructor Details

#initialize(namespace_name: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • namespace_name (String, nil) (defaults to: nil)

    Name of the AppSignal namespace we want to use or nil if it is to remain default. Defaults to ‘Appsignal::Transaction::BACKGROUND_JOB` in the execution flow.



17
18
19
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 17

def initialize(namespace_name: nil)
  @namespace_name = namespace_name
end

Instance Method Details

#count(key, value, tags) ⇒ Object

Increments counter with the given value and tags

Parameters:

  • key (String)

    key we want to use

  • value (Integer)

    increment value

  • tags (Hash)

    additional extra tags



65
66
67
68
69
70
71
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 65

def count(key, value, tags)
  ::Appsignal.increment_counter(
    key,
    value,
    stringify_hash(tags)
  )
end

#gauge(key, value, tags) ⇒ Object

Sets gauge with the given value and tags

Parameters:

  • key (String)

    key we want to use

  • value (Integer)

    gauge value

  • tags (Hash)

    additional extra tags



78
79
80
81
82
83
84
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 78

def gauge(key, value, tags)
  ::Appsignal.set_gauge(
    key,
    value,
    stringify_hash(tags)
  )
end

#metadata=(metadata_hash) ⇒ Object

Sets metadata on a current transaction (if any)

Parameters:

  • metadata_hash (Hash)

    hash with metadata we want to set



50
51
52
53
54
55
56
57
58
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 50

def metadata=()
  return unless transaction?

  current_transaction = transaction

  stringify_hash().each do |key, value|
    current_transaction.(key, value)
  end
end

#register_probe(name, probe) ⇒ Object

Registers the probe under a given name

Parameters:

  • name (Symbol)

    probe name

  • probe (Proc)

    code to run every minute



111
112
113
114
115
116
117
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 111

def register_probe(name, probe)
  if ::Appsignal::Probes.respond_to?(:register)
    ::Appsignal::Probes.register(name, probe)
  else
    ::Appsignal::Minutely.probes.register(name, probe)
  end
end

#report_error(error) ⇒ Object

Report the error that occurred to Appsignal

Parameters:

  • error (Object)

    error we want to ship to Appsignal



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 89

def report_error(error)
  if ::Appsignal.respond_to?(:report_error)
    # This helper will always report the error
    ::Appsignal.report_error(error) do |transaction|
      transaction.set_namespace(namespace_name)
    end
  # If we have an active transaction we should use it instead of creating a generic one
  # That way proper namespace and other data may be transferred
  #
  # In case there is no transaction, a new generic background job one will be used
  elsif transaction?
    transaction.set_error(error)
  else
    ::Appsignal.send_error(error) do |transaction|
      transaction.set_namespace(namespace_name)
    end
  end
end

#start_transaction(action_name) ⇒ Object

Starts an appsignal transaction with a given action name

Parameters:

  • action_name (String)

    action name. For processing this should be equal to consumer class + method name



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 25

def start_transaction(action_name)
  transaction =
    if version_4_or_newer?
      ::Appsignal::Transaction.create(namespace_name)
    else
      ::Appsignal::Transaction.create(
        SecureRandom.uuid,
        namespace_name,
        ::Appsignal::Transaction::GenericRequest.new({})
      )
    end

  transaction.set_action_if_nil(action_name)
end

#stop_transactionObject

Stops the current transaction (if any)



41
42
43
44
45
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 41

def stop_transaction
  return unless transaction?

  ::Appsignal::Transaction.complete_current!
end