Class: Datadog::Notifications

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/datadog/notifications.rb,
lib/datadog/notifications/config.rb,
lib/datadog/notifications/plugins.rb,
lib/datadog/notifications/version.rb,
lib/datadog/notifications/reporter.rb

Defined Under Namespace

Modules: Plugins Classes: Config, Reporter

Constant Summary collapse

VERSION =
'0.5.4'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotifications

Returns a new instance of Notifications.



61
62
63
# File 'lib/datadog/notifications.rb', line 61

def initialize
  @config = Config.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



59
60
61
# File 'lib/datadog/notifications.rb', line 59

def config
  @config
end

Class Method Details

.configure(&block) ⇒ Object

Configure and install datadog instrumentation. Example:

Datadog::Notifications.configure do |c|
  c.hostname = "my-host"
  c.tags     = ["app:mine"]

  c.use Datadog::Notifications::Plugins::Grape, metric_name: "api.request", tags: ["grape:specific"]
end

Settings:

  • hostname - the hostname used for instrumentation, defaults to system hostname, respects INSTRUMENTATION_HOSTNAME env variable

  • namespace - set a namespace to be prepended to every metric name

  • tags - set an array of tags to be added to every metric

  • statsd_host - the statsD host, defaults to “localhost”, respects STATSD_HOST env variable

  • statsd_port - the statsD port, defaults to 8125, respects STATSD_PORT env variable

  • reporter - custom reporter class, defaults to ‘Datadog::Notifications::Reporter`



32
33
34
35
36
37
38
# File 'lib/datadog/notifications.rb', line 32

def self.configure(&block)
  if instance.instance_variable_defined?(:@reporter)
    warn "#{name} cannot be reconfigured once it has subscribed to notifications, called from: #{caller(2..2).first}"
    return
  end
  block.call instance.config if block
end

.subscribe(pattern, &block) ⇒ Object

You can subscribe to events exactly as with ActiveSupport::Notifications, but there will be an additional ‘statsd` block parameter available:

Datadog::Notifications.subscribe('render') do |reporter, event|
  reporter # => Reporter instance
  event    # => ActiveSupport::Notifications::Event object
end

Example:

Datadog::Notifications.subscribe('render') do |reporter, _, start, finish, _, payload|
  status = payload[:status]
  reporter.seconds('myapp.render', finish-start, tags: ["status:#{status}"])
end


55
56
57
# File 'lib/datadog/notifications.rb', line 55

def self.subscribe(pattern, &block)
  instance.subscribe(pattern, &block)
end

Instance Method Details

#subscribe(pattern) ⇒ Object



65
66
67
68
69
# File 'lib/datadog/notifications.rb', line 65

def subscribe(pattern)
  ActiveSupport::Notifications.subscribe(pattern) do |*args|
    yield reporter, ActiveSupport::Notifications::Event.new(*args)
  end
end