Module: Datadog::Contrib::Grape::Patcher

Defined in:
lib/ddtrace/contrib/grape/patcher.rb

Overview

Patcher that introduces more instrumentation for Grape endpoints, so that new signals are executed at the beginning of each step (filters, render and run)

Class Method Summary collapse

Class Method Details

.patchObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ddtrace/contrib/grape/patcher.rb', line 17

def patch
  if !@patched && defined?(::Grape)
    begin
      # do not require these by default, but only when actually patching
      require 'ddtrace'
      require 'ddtrace/ext/app_types'
      require 'ddtrace/contrib/grape/endpoint'

      @patched = true
      # patch all endpoints
      patch_endpoint_run()
      patch_endpoint_render()

      # attach a PIN object globally and set the service once
      pin = Datadog::Pin.new(SERVICE, app: 'grape', app_type: Datadog::Ext::AppTypes::WEB)
      pin.onto(::Grape)
      if pin.tracer && pin.service
        pin.tracer.set_service_info(pin.service, 'grape', pin.app_type)
      end

      # subscribe to ActiveSupport events
      Datadog::Contrib::Grape::Endpoint.subscribe()
    rescue StandardError => e
      Datadog::Tracer.log.error("Unable to apply Grape integration: #{e}")
    end
  end
  @patched
end

.patch_endpoint_renderObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ddtrace/contrib/grape/patcher.rb', line 56

def patch_endpoint_render
  ::Grape::Endpoint.class_eval do
    class << self
      alias_method :generate_api_method_without_datadog, :generate_api_method
      def generate_api_method(*params, &block)
        method_api = generate_api_method_without_datadog(*params, &block)
        proc do |*args|
          ::ActiveSupport::Notifications.instrument('endpoint_render.grape.start_render')
          method_api.call(*args)
        end
      end
    end
  end
end

.patch_endpoint_runObject



46
47
48
49
50
51
52
53
54
# File 'lib/ddtrace/contrib/grape/patcher.rb', line 46

def patch_endpoint_run
  ::Grape::Endpoint.class_eval do
    alias_method :run_without_datadog, :run
    def run(*args)
      ::ActiveSupport::Notifications.instrument('endpoint_run.grape.start_process')
      run_without_datadog(*args)
    end
  end
end

.patched?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/ddtrace/contrib/grape/patcher.rb', line 13

def patched?
  @patched
end