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



60
61
62
63
64
65
66
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 60

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



73
74
75
76
77
78
79
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 73

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



45
46
47
48
49
50
51
52
53
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 45

def metadata=()
  return unless transaction?

  transaction = ::Appsignal::Transaction.current

  stringify_hash().each do |key, value|
    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



101
102
103
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 101

def register_probe(name, probe)
  ::Appsignal::Minutely.probes.register(name, probe)
end

#send_error(error) ⇒ Object

Sends the error that occurred to Appsignal

Parameters:

  • error (Object)

    error we want to ship to Appsignal



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 84

def send_error(error)
  # 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
  if 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
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 25

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

  transaction.set_action_if_nil(action_name)
end

#stop_transactionObject

Stops the current transaction (if any)



36
37
38
39
40
# File 'lib/karafka/instrumentation/vendors/appsignal/client.rb', line 36

def stop_transaction
  return unless transaction?

  ::Appsignal::Transaction.complete_current!
end