Sha256: 679f9eee138d6491f3276cbda31a5e59d751bf6b967f7221ce242d85cff1ff53
Contents?: true
Size: 1.89 KB
Versions: 3
Compression:
Stored size: 1.89 KB
Contents
module Datadog::Notifications::Plugins class Grape < Base DEFAULT_EXCEPTION_HANDLER = ->(e) { Grape.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..].upcase } path end end end
Version data entries
3 entries across 3 versions & 1 rubygems