Module: Datadog::Contrib::Grape::Endpoint
- Defined in:
- lib/ddtrace/contrib/grape/endpoint.rb
Overview
rubocop:disable Metrics/ModuleLength Endpoint module includes a list of subscribers to create traces when a Grape endpoint is hit
Constant Summary collapse
- KEY_RUN =
'datadog_grape_endpoint_run'.freeze
- KEY_RENDER =
'datadog_grape_endpoint_render'.freeze
Class Method Summary collapse
- .endpoint_render(name, start, finish, id, payload) ⇒ Object
- .endpoint_run(name, start, finish, id, payload) ⇒ Object
- .endpoint_run_filters(name, start, finish, id, payload) ⇒ Object
- .endpoint_start_process ⇒ Object
- .endpoint_start_render ⇒ Object
- .subscribe ⇒ Object
Class Method Details
.endpoint_render(name, start, finish, id, payload) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ddtrace/contrib/grape/endpoint.rb', line 111 def self.endpoint_render(name, start, finish, id, payload) return unless Thread.current[KEY_RENDER] Thread.current[KEY_RENDER] = false # retrieve the tracer from the PIN object pin = Datadog::Pin.get_from(::Grape) return unless pin && pin.enabled? tracer = pin.tracer span = tracer.active_span() return unless span # catch thrown exceptions begin span.set_error(payload[:exception_object]) unless payload[:exception_object].nil? ensure span.start_time = start span.finish(finish) end rescue StandardError => e Datadog::Tracer.log.error(e.) end |
.endpoint_run(name, start, finish, id, payload) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ddtrace/contrib/grape/endpoint.rb', line 54 def self.endpoint_run(name, start, finish, id, payload) return unless Thread.current[KEY_RUN] Thread.current[KEY_RUN] = false # retrieve the tracer from the PIN object pin = Datadog::Pin.get_from(::Grape) return unless pin && pin.enabled? tracer = pin.tracer span = tracer.active_span() return unless span begin # collect endpoint details api_view = payload[:endpoint].[:for].to_s path = payload[:endpoint].[:path].join('/') resource = "#{api_view}##{path}" span.resource = resource # set the request span resource if it's a `rack.request` span request_span = payload[:env][:datadog_rack_request_span] if !request_span.nil? && request_span.name == 'rack.request' request_span.resource = resource end # catch thrown exceptions span.set_error(payload[:exception_object]) unless payload[:exception_object].nil? # override the current span with this notification values span.set_tag('grape.route.endpoint', api_view) span.set_tag('grape.route.path', path) ensure span.start_time = start span.finish(finish) end rescue StandardError => e Datadog::Tracer.log.error(e.) end |
.endpoint_run_filters(name, start, finish, id, payload) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ddtrace/contrib/grape/endpoint.rb', line 134 def self.endpoint_run_filters(name, start, finish, id, payload) # retrieve the tracer from the PIN object pin = Datadog::Pin.get_from(::Grape) return unless pin && pin.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 tracer = pin.tracer service = pin.service type = Datadog::Ext::HTTP::TYPE span = tracer.trace('grape.endpoint_run_filters', service: service, span_type: type) begin # catch thrown exceptions span.set_error(payload[:exception_object]) unless payload[:exception_object].nil? span.set_tag('grape.filter.type', type.to_s) ensure span.start_time = start span.finish(finish) end rescue StandardError => e Datadog::Tracer.log.error(e.) end |
.endpoint_start_process ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ddtrace/contrib/grape/endpoint.rb', line 36 def self.endpoint_start_process(*) return if Thread.current[KEY_RUN] # retrieve the tracer from the PIN object pin = Datadog::Pin.get_from(::Grape) return unless pin && pin.enabled? # store the beginning of a trace tracer = pin.tracer service = pin.service type = Datadog::Ext::HTTP::TYPE tracer.trace('grape.endpoint_run', service: service, span_type: type) Thread.current[KEY_RUN] = true rescue StandardError => e Datadog::Tracer.log.error(e.) end |
.endpoint_start_render ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ddtrace/contrib/grape/endpoint.rb', line 93 def self.endpoint_start_render(*) return if Thread.current[KEY_RENDER] # retrieve the tracer from the PIN object pin = Datadog::Pin.get_from(::Grape) return unless pin && pin.enabled? # store the beginning of a trace tracer = pin.tracer service = pin.service type = Datadog::Ext::HTTP::TYPE tracer.trace('grape.endpoint_render', service: service, span_type: type) Thread.current[KEY_RENDER] = true rescue StandardError => e Datadog::Tracer.log.error(e.) end |
.subscribe ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ddtrace/contrib/grape/endpoint.rb', line 14 def self.subscribe # Grape is instrumented only if it's available return unless defined?(::Grape) && defined?(::ActiveSupport::Notifications) # 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 |