Class: Skylight::Probes::Sinatra::Probe Private

Inherits:
Object
  • Object
show all
Defined in:
lib/skylight/probes/sinatra.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#installObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
8
9
10
11
12
13
14
15
16
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/skylight/probes/sinatra.rb', line 5

def install
  class << ::Sinatra::Base
    alias build_without_sk build
    alias compile_without_sk! compile!

    def compile!(verb, path, *args, &block)
      compile_without_sk!(verb, path, *args, &block).tap do |_, _, _, wrapper|
        # Deal with the situation where the path is a regex, and the default behavior
        # of Ruby stringification produces an unreadable mess
        if path.is_a?(Regexp)
          human_readable = "<sk-regex>%r{#{path.source}}</sk-regex>"
          wrapper.instance_variable_set(:@route_name, "#{verb} #{human_readable}")
        else
          wrapper.instance_variable_set(:@route_name, "#{verb} #{path}")
        end

        # Newer versions of Sinatra populate env['sinatra.route']. Polyfill older
        # versions in a targeted but hackish way.
        if ::Sinatra::VERSION < '1.4.0'
          def wrapper.[](app, args)
            app.env['sinatra.route'] = @route_name
            super
          end
        end
      end
    end

    def build(*args, &block)
      self.use Skylight::Middleware
      build_without_sk(*args, &block)
    end
  end

  ::Sinatra::Base.class_eval do
    alias dispatch_without_sk! dispatch!
    alias compile_template_without_sk compile_template

    def dispatch!(*args, &block)
      dispatch_without_sk!(*args, &block).tap do
        instrumenter = Skylight::Instrumenter.instance
        next unless instrumenter
        trace = instrumenter.current_trace
        next unless trace

        # Set the endpoint name to the route name
        route = env['sinatra.route']
        trace.endpoint = route if route
      end
    end

    def compile_template(engine, data, options, *args, &block)
      # Pass along a useful "virtual path" to Tilt. The Tilt probe will handle
      # instrumenting correctly.
      case data
      when Symbol
        options[:sky_virtual_path] = data.to_s
      else
        options[:sky_virtual_path] = "Inline template (#{engine})"
      end

      compile_template_without_sk(engine, data, options, *args, &block)
    end
  end
end