Class: Sidekiq::Middleware::Server::Datadog

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/middleware/server/datadog.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Datadog

Configure and install datadog instrumentation. Example:

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add Sidekiq::Middleware::Server::Datadog
  end
end

Options:

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

  • :metric_name - the metric name (prefix) to use, defaults to “sidekiq.job”

  • :tags - array of custom tags, these can be plain strings or lambda blocks accepting a rack env instance

  • :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

  • :statsd - custom statsd instance



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sidekiq/middleware/server/datadog.rb', line 26

def initialize(opts = {})
  hostname      = opts[:hostname] || ENV['INSTRUMENTATION_HOSTNAME'] || Socket.gethostname
  statsd_host   = opts[:statsd_host] || ENV['STATSD_HOST'] || "localhost"
  statsd_port   = (opts[:statsd_port] || ENV['STATSD_PORT'] || 8125).to_i

  @metric_name  = opts[:metric_name] || "sidekiq.job"
  @statsd       = opts[:statsd] || ::Datadog::Statsd.new(statsd_host, statsd_port)
  @tags         = opts[:tags] || []

  if @tags.none? {|t| t =~ /^host\:/ }
    @tags.push("host:#{hostname}")
  end

  env = Sidekiq.options[:environment] || ENV['RACK_ENV']
  if env && @tags.none? {|t| t =~ /^env\:/ }
    @tags.push("env:#{ENV['RACK_ENV']}")
  end
end

Instance Method Details

#call(worker, job, queue) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/sidekiq/middleware/server/datadog.rb', line 45

def call(worker, job, queue, *)
  start = Time.now
  begin
    yield
    record(worker, job, queue, start)
  rescue => e
    record(worker, job, queue, start, e)
    raise
  end
end