Class: Grape::Datadog::Middleware
- Inherits:
-
Middleware::Base
- Object
- Middleware::Base
- Grape::Datadog::Middleware
- Defined in:
- lib/grape/datadog/middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Middleware
constructor
Create a new
Datadogmiddleware instance.
Constructor Details
#initialize(app, opts = {}) ⇒ Middleware
Create a new Datadog middleware instance.
Options
-
:hostname- the hostname used for instrumentation, defaults to system hostname, respectsINSTRUMENTATION_HOSTNAMEenv 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”, respectsSTATSD_HOSTenv variable -
:statsd_port- the statsD port, defaults to 8125, respectsSTATSD_PORTenv variable -
:prefer_global- if set, tries to find global ‘$statsd` instance, otherwise connects tostatsd_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) = (env) @statsd.time "#{@metric}.time", :tags => do resp = @app.call(env) .push "status:#{resp.status}" @statsd.increment @metric, :tags => resp end end |