module Datadog::Contrib::Grape::Endpoint
rubocop:disable Metrics/ModuleLength Endpoint
module includes a list of subscribers to create traces when a Grape
endpoint is hit
Constants
- KEY_RENDER
- KEY_RUN
Public Class Methods
endpoint_render(name, start, finish, id, payload)
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 123 def endpoint_render(name, start, finish, id, payload) return unless Thread.current[KEY_RENDER] Thread.current[KEY_RENDER] = false return unless enabled? span = tracer.active_span return unless span # catch thrown exceptions begin # Measure service stats Contrib::Analytics.set_measured(span) span.set_error(payload[:exception_object]) if exception_is_error?(payload[:exception_object]) ensure span.start(start) span.finish(finish) end rescue StandardError => e Datadog.logger.error(e.message) end
endpoint_run(name, start, finish, id, payload)
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 63 def endpoint_run(name, start, finish, id, payload) return unless Thread.current[KEY_RUN] Thread.current[KEY_RUN] = false return unless enabled? span = tracer.active_span return unless span begin # collect endpoint details endpoint = payload.fetch(:endpoint) api_view = api_view(endpoint.options[:for]) request_method = endpoint.options.fetch(:method).first path = endpoint_expand_path(endpoint) try_setting_rack_request_resource(payload, span.resource) # Set analytics sample rate Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled? # Measure service stats Contrib::Analytics.set_measured(span) # catch thrown exceptions span.set_error(payload[:exception_object]) if exception_is_error?(payload[:exception_object]) # override the current span with this notification values span.set_tag(Ext::TAG_ROUTE_ENDPOINT, api_view) unless api_view.nil? span.set_tag(Ext::TAG_ROUTE_PATH, path) span.set_tag(Ext::TAG_ROUTE_METHOD, request_method) span.set_tag(Datadog::Ext::HTTP::METHOD, request_method) span.set_tag(Datadog::Ext::HTTP::URL, path) ensure span.start(start) span.finish(finish) end rescue StandardError => e Datadog.logger.error(e.message) end
endpoint_run_filters(name, start, finish, id, payload)
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 147 def endpoint_run_filters(name, start, finish, id, payload) return unless enabled? # safe-guard to prevent submitting empty filters zero_length = (finish - start).zero? filters = payload[:filters] type = payload[:type] return if (!filters || filters.empty?) || !type || zero_length span = tracer.trace( Ext::SPAN_ENDPOINT_RUN_FILTERS, service: service_name, span_type: Datadog::Ext::HTTP::TYPE_INBOUND, start_time: start ) begin # Set analytics sample rate Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled? # Measure service stats Contrib::Analytics.set_measured(span) # catch thrown exceptions span.set_error(payload[:exception_object]) if exception_is_error?(payload[:exception_object]) span.set_tag(Ext::TAG_FILTER_TYPE, type.to_s) ensure span.start(start) span.finish(finish) end rescue StandardError => e Datadog.logger.error(e.message) end
endpoint_start_process(_name, _start, _finish, _id, payload)
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 37 def endpoint_start_process(_name, _start, _finish, _id, payload) return if Thread.current[KEY_RUN] return unless enabled? # collect endpoint details endpoint = payload.fetch(:endpoint) api_view = api_view(endpoint.options[:for]) request_method = endpoint.options.fetch(:method).first path = endpoint_expand_path(endpoint) resource = "#{api_view} #{request_method} #{path}" # Store the beginning of a trace span = tracer.trace( Ext::SPAN_ENDPOINT_RUN, service: service_name, span_type: Datadog::Ext::HTTP::TYPE_INBOUND, resource: resource, ) try_setting_rack_request_resource(payload, span.resource) Thread.current[KEY_RUN] = true rescue StandardError => e Datadog.logger.error(e.message) end
endpoint_start_render(*)
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 107 def endpoint_start_render(*) return if Thread.current[KEY_RENDER] return unless enabled? # Store the beginning of a trace tracer.trace( Ext::SPAN_ENDPOINT_RENDER, service: service_name, span_type: Datadog::Ext::HTTP::TEMPLATE ) Thread.current[KEY_RENDER] = true rescue StandardError => e Datadog.logger.error(e.message) end
subscribe()
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 18 def subscribe # subscribe when a Grape endpoint is hit ::ActiveSupport::Notifications.subscribe('endpoint_run.grape.start_process') do |*args| endpoint_start_process(*args) end ::ActiveSupport::Notifications.subscribe('endpoint_run.grape') do |*args| endpoint_run(*args) end ::ActiveSupport::Notifications.subscribe('endpoint_render.grape.start_render') do |*args| endpoint_start_render(*args) end ::ActiveSupport::Notifications.subscribe('endpoint_render.grape') do |*args| endpoint_render(*args) end ::ActiveSupport::Notifications.subscribe('endpoint_run_filters.grape') do |*args| endpoint_run_filters(*args) end end
Private Class Methods
analytics_enabled?()
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 211 def analytics_enabled? Contrib::Analytics.enabled?(datadog_configuration[:analytics_enabled]) end
analytics_sample_rate()
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 215 def analytics_sample_rate datadog_configuration[:analytics_sample_rate] end
api_view(api)
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 184 def api_view(api) # If the API inherits from Grape::API in version >= 1.2.0 # then the API will be an instance and the name must be derived from the base. # See https://github.com/ruby-grape/grape/issues/1825 if defined?(::Grape::API::Instance) && api <= ::Grape::API::Instance api.base.to_s else api.to_s end end
datadog_configuration()
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 232 def datadog_configuration Datadog.configuration[:grape] end
enabled?()
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 228 def enabled? datadog_configuration[:enabled] == true end
endpoint_expand_path(endpoint)
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 195 def endpoint_expand_path(endpoint) route_path = endpoint.options[:path] namespace = endpoint.routes.first && endpoint.routes.first.namespace || '' parts = (namespace.split('/') + route_path).reject { |p| p.blank? || p.eql?('/') } parts.join('/').prepend('/') end
exception_is_error?(exception)
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 219 def exception_is_error?(exception) matcher = datadog_configuration[:error_statuses] return false unless exception return true unless matcher return true unless exception.respond_to?('status') matcher.include?(exception.status) end
service_name()
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 207 def service_name datadog_configuration[:service_name] end
tracer()
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 203 def tracer datadog_configuration[:tracer] end
try_setting_rack_request_resource(payload, resource)
click to toggle source
# File lib/ddtrace/contrib/grape/endpoint.rb, line 236 def try_setting_rack_request_resource(payload, resource) request_span = payload[:env][Datadog::Contrib::Rack::Ext::RACK_ENV_REQUEST_SPAN] if !request_span.nil? && request_span.name == Datadog::Contrib::Rack::Ext::SPAN_REQUEST request_span.resource = resource end end