Class: Grape::Datadog::Middleware

Inherits:
Middleware::Base
  • Object
show all
Defined in:
lib/grape/datadog/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Middleware

Create a new Datadog middleware instance.

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 “grape.request”

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

  • :prefer_global - if set, tries to find global ‘$statsd` instance, otherwise connects to statsd_host:+statsd_port. Default: true



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/grape/datadog/middleware.rb', line 19

def initialize(app, 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

  @app    = app
  @metric = opts[:metric_name] || "grape.request"
  @statsd = opts[:prefer_global] == false || !defined?($statsd) ? ::Statsd.new(statsd_host, statsd_port) : $statsd
  @tags   = opts[:tags] || []
  @tags  += ["host:#{hostname}"]
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/grape/datadog/middleware.rb', line 31

def call(env)
  tags = prepare_tags(env)
  @statsd.time "#{@metric}.time", :tags => tags do
    resp = @app.call(env)
    tags.push "status:#{resp.status}"
    @statsd.increment @metric, :tags => tags
    resp
  end
end