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.



79
80
81
# File 'lib/rack-honeycomb/auto_install.rb', line 79

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.



80
81
82
# File 'lib/rack-honeycomb/auto_install.rb', line 80

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.



25
26
27
28
29
30
31
32
33
# File 'lib/rack-honeycomb/auto_install.rb', line 25

def auto_install!(honeycomb_client:, logger: nil)
  @logger = logger

  require 'rack'
  require 'rack-honeycomb'

  auto_install_sinatra!(honeycomb_client, logger) if @has_sinatra
  auto_install_rails!(honeycomb_client, logger) if @has_rails
end

.auto_install_rails!(honeycomb_client, logger) ⇒ 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.



70
71
72
73
74
75
76
77
# File 'lib/rack-honeycomb/auto_install.rb', line 70

def auto_install_rails!(honeycomb_client, logger)
  require 'rack-honeycomb/railtie'
  ::Rack::Honeycomb::Railtie.init(
    honeycomb_client: honeycomb_client,
    logger: logger,
  )
  debug 'Loaded Railtie'
end

.auto_install_sinatra!(honeycomb_client, logger) ⇒ 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.



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/rack-honeycomb/auto_install.rb', line 35

def auto_install_sinatra!(honeycomb_client, logger)
  require 'sinatra/base'

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

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

      self.use Rack::Honeycomb::Middleware, client: honeycomb_client, logger: logger, is_sinatra: true
      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
        AutoInstall.warn 'Honeycomb auto-instrumentation of Sinatra will probably not work, try manual installation'
        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
18
19
20
21
22
23
# File 'lib/rack-honeycomb/auto_install.rb', line 6

def available?(logger: nil)
  @logger = logger

  unless has_gem? 'rack'
    debug 'not autoinitialising rack-honeycomb'
    return false
  end

  @has_sinatra = has_gem? 'sinatra'
  @has_rails = has_gem? 'rails'

  unless @has_sinatra || @has_rails
    debug "Couldn't detect web framework, not autoinitialising rack-honeycomb"
    return false
  end

  true
end

.debug(msg) ⇒ 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.



82
83
84
# File 'lib/rack-honeycomb/auto_install.rb', line 82

def debug(msg)
  @logger.debug "#{self.name}: #{msg}" if @logger
end

.warn(msg) ⇒ 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.



86
87
88
# File 'lib/rack-honeycomb/auto_install.rb', line 86

def warn(msg)
  @logger.warn "#{self.name}: #{msg}" if @logger
end