Sha256: 7d9a40fcfbfa7583f954bf6527761f5c2f8f76f198dbf19896ad31675b7288d2
Contents?: true
Size: 1.93 KB
Versions: 1
Compression:
Stored size: 1.93 KB
Contents
module Datadog::Notifications::Plugins class Grape < Base DEFAULT_EXCEPTION_HANDLER = ->(e) { self.class.exception_status(e) } def self.exception_status(err) err.respond_to?(:status) ? err.status : 500 end attr_reader :metric_name, :exception_handler # Options: # # *<tt>:metric_name</tt> - the metric name, defaults to "grape.request" # *<tt>:exception_handler</tt> - a custom exception handler proc which accepts an exception object and returns a status # *<tt>:tags</tt> - additional tags def initialize(metric_name: 'grape.request', exception_handler: DEFAULT_EXCEPTION_HANDLER, **opts) super @metric_name = metric_name @exception_handler = exception_handler Datadog::Notifications.subscribe 'endpoint_run.grape' do |reporter, event| record reporter, event end end private def record(reporter, event) payload = event.payload endpoint = payload[:endpoint] method = endpoint.request.request_method status = endpoint.status status = exception_handler.call(payload[:exception_object]) if payload[:exception_object] path = extract_path(endpoint) path.gsub!(%r{[^\w/\-]+}, '_') tags = self.tags + %W[method:#{method} status:#{status}] tags.push "path:#{path}" if path tags.push "version:#{endpoint.version}" if endpoint.version reporter.batch do reporter.increment metric_name, tags: tags reporter.timing "#{metric_name}.time", event.duration, tags: tags end end def extract_path(endpoint) route = begin endpoint.route rescue NoMethodError nil end return endpoint.request.path unless route path = endpoint.route.path.dup path.sub!(/\(\.:format\)$/, '') path.sub!(':version/', '') if endpoint.version path.gsub!(/:(\w+)/) {|m| m[1..-1].upcase } path end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
datadog-notifications-0.6.4 | lib/datadog/notifications/plugins/grape.rb |