Class: Appsignal::Environment Private

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/environment.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.

Constant Summary collapse

ERROR_ON_YIELD =

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

:APPSIGNAL_ERROR_ON_YIELD
SUPPORTED_GEMS =

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

%w[
  actioncable
  actionmailer
  activejob
  activerecord
  capistrano
  celluloid
  data_mapper
  delayed_job
  dry-monitor
  elasticsearch
  excon
  faraday
  gvltools
  hanami
  hiredis
  mongo_ruby_driver
  padrino
  passenger
  puma
  que
  rack
  rails
  rake
  redis
  redis-client
  resque
  rom
  sequel
  shoryuken
  sidekiq
  sinatra
  unicorn
  webmachine
].freeze

Class Method Summary collapse

Class Method Details

.report(key) ⇒ void

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.

This method returns an undefined value.

Add environment metadata.

The key and value of the environment metadata must be a String, even if it’s actually of another type.

The value of the environment metadata is given as a block that captures errors that might be raised while fetching the value. It will not re-raise errors, but instead log them using the Appsignal.internal_logger. This ensures AppSignal will not cause an error in the application when collecting this metadata.

Examples:

Reporting a key and value

Appsignal::Environment.report("ruby_version") { RUBY_VERSION }

When a value is nil

Appsignal::Environment.report("ruby_version") { nil }
# Key and value do not get reported. A warning gets logged instead.

When an error occurs

Appsignal::Environment.report("ruby_version") { raise "uh oh" }
# Error does not get reraised. A warning gets logged instead.

Parameters:

  • key (String)

    The name of the key of the environment metadata value.

Yield Returns:

  • (String)

    The value of the key of the environment metadata.



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
69
70
71
72
73
# File 'lib/appsignal/environment.rb', line 33

def self.report(key)
  key =
    case key
    when String
      key
    else
      Appsignal.internal_logger.error "Unable to report on environment " \
        "metadata: Unsupported value type for #{key.inspect}"
      return
    end

  yielded_value =
    begin
      yield
    rescue => e
      Appsignal.internal_logger.error \
        "Unable to report on environment metadata #{key.inspect}:\n" \
          "#{e.class}: #{e}"
      ERROR_ON_YIELD
    end

  return if yielded_value == ERROR_ON_YIELD

  value =
    case yielded_value
    when TrueClass, FalseClass
      yielded_value.to_s
    when String
      yielded_value
    else
      Appsignal.internal_logger.error "Unable to report on environment " \
        "metadata #{key.inspect}: Unsupported value type for " \
        "#{yielded_value.inspect}"
      return
    end

  Appsignal::Extension.(key, value)
rescue => e
  Appsignal.internal_logger.error "Unable to report on environment " \
    "metadata:\n#{e.class}: #{e}"
end

.report_enabled(feature) ⇒ 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.



141
142
143
144
145
146
# File 'lib/appsignal/environment.rb', line 141

def self.report_enabled(feature)
  Appsignal::Environment.report("ruby_#{feature}_enabled") { true }
rescue => e
  Appsignal.internal_logger.error "Unable to report integration " \
    "enabled:\n#{e.class}: #{e}"
end

.report_supported_gemsObject

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.

Report on the list of AppSignal supported gems

This list is used to report if which AppSignal supported gems are present in this app and what version. This data will help AppSignal improve its support by knowing what gems and versions of gems it still needs to support or can drop support for.

It will ask Bundler to report name and version information from the gems that are present in the app bundle.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/appsignal/environment.rb', line 121

def self.report_supported_gems
  return unless defined?(Bundler) # Do nothing if Bundler is not present

  bundle_gem_specs =
    if ::Bundler.rubygems.respond_to?(:installed_specs)
      ::Bundler.rubygems.installed_specs
    else
      ::Bundler.rubygems.all_specs
    end
  SUPPORTED_GEMS.each do |gem_name|
    gem_spec = bundle_gem_specs.find { |spec| spec.name == gem_name }
    next unless gem_spec

    report("ruby_#{gem_name}_version") { gem_spec.version.to_s }
  end
rescue => e
  Appsignal.internal_logger.error "Unable to report supported gems:\n" \
    "#{e.class}: #{e}"
end