Module: Rack::Honeycomb::AutoInstall Private

Defined in:
lib/rack-honeycomb/auto_install.rb

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.already_addedObject

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.



57
58
59
# File 'lib/rack-honeycomb/auto_install.rb', line 57

def already_added
  @already_added
end

.already_warnedObject

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.



58
59
60
# File 'lib/rack-honeycomb/auto_install.rb', line 58

def already_warned
  @already_warned
end

Class Method Details

.auto_install!(honeycomb_client:, logger: nil) ⇒ Object

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.



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
# File 'lib/rack-honeycomb/auto_install.rb', line 19

def auto_install!(honeycomb_client:, logger: nil)
  require 'rack'
  require 'sinatra/base'

  require 'rack-honeycomb'

  class << ::Sinatra::Base
    alias build_without_honeycomb build
  end

  ::Sinatra::Base.define_singleton_method(:build) do |*args, &block|
    if !AutoInstall.already_added
      logger.debug "Adding Rack::Honeycomb::Middleware to #{self}" if logger

      self.use Rack::Honeycomb::Middleware, client: honeycomb_client, logger: logger
      AutoInstall.already_added = true
    else
      # In the case of nested Sinatra apps - apps composed of other apps
      # (in addition to just handlers and middleware) - our .build hook
      # above will fire multiple times, for the parent app and also for
      # each child app. In that case, it's hard to hook in our
      # middleware reliably - so instead, we just want to warn the user
      # and avoid doing anything silly.
      unless AutoInstall.already_warned
        logger.warn "Honeycomb auto-instrumentation of Sinatra will probably not work, try manual installation" if logger
        AutoInstall.already_warned = true
      end
    end
    build_without_honeycomb(*args, &block)
  end

  ::Sinatra::Base.include(Module.new do
    def add_honeycomb_field(field, value)
      ::Rack::Honeycomb.add_field(env, field, value)
    end
  end)
end

.available?(logger: nil) ⇒ Boolean

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.

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rack-honeycomb/auto_install.rb', line 6

def available?(logger: nil)
  gem 'rack'
  logger.debug "#{self.name}: detected rack" if logger
  gem 'sinatra'
  logger.debug "#{self.name}: detected sinatra, okay to autoinitialise" if logger
  true
rescue Gem::LoadError => e
  if e.name == 'sinatra'
    logger.debug "Couldn't detect web framework, not autoinitialising rack-honeycomb" if logger
  end
  false
end