Sha256: 6dad53454285e53335a258b3eaac494644c58c8350de0ce9fe5b3ace7ac82713
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
module Datadog::Notifications::Plugins class Grape < Base 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:, **opts) super @metric_name = metric_name @exception_handler = exception_handler || ->(e) { self.class.exception_status(e) } 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.3 | lib/datadog/notifications/plugins/grape.rb |