-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathconfig.rb
More file actions
269 lines (240 loc) · 11.3 KB
/
config.rb
File metadata and controls
269 lines (240 loc) · 11.3 KB
1
2
3
4
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
require "erb"
require "yaml"
require "uri"
require "socket"
require "tmpdir"
module Appsignal
class Config
DEFAULT_CONFIG = {
:debug => false,
:log => "file",
:ignore_actions => [],
:ignore_errors => [],
:ignore_namespaces => [],
:filter_parameters => [],
:send_params => true,
:endpoint => "https://push.appsignal.com",
:instrument_net_http => true,
:instrument_redis => true,
:instrument_sequel => true,
:skip_session_data => false,
:enable_frontend_error_catching => false,
:frontend_error_catching_path => "/appsignal_error_catcher",
:enable_allocation_tracking => true,
:enable_gc_instrumentation => false,
:enable_host_metrics => true,
:enable_minutely_probes => false,
:hostname => ::Socket.gethostname,
:ca_file_path => File.expand_path(File.join("../../../resources/cacert.pem"), __FILE__),
:dns_servers => [],
:files_world_accessible => true
}.freeze
ENV_TO_KEY_MAPPING = {
"APPSIGNAL_ACTIVE" => :active,
"APPSIGNAL_PUSH_API_KEY" => :push_api_key,
"APPSIGNAL_APP_NAME" => :name,
"APPSIGNAL_PUSH_API_ENDPOINT" => :endpoint,
"APPSIGNAL_FRONTEND_ERROR_CATCHING_PATH" => :frontend_error_catching_path,
"APPSIGNAL_DEBUG" => :debug,
"APPSIGNAL_LOG" => :log,
"APPSIGNAL_LOG_PATH" => :log_path,
"APPSIGNAL_INSTRUMENT_NET_HTTP" => :instrument_net_http,
"APPSIGNAL_INSTRUMENT_REDIS" => :instrument_redis,
"APPSIGNAL_INSTRUMENT_SEQUEL" => :instrument_sequel,
"APPSIGNAL_SKIP_SESSION_DATA" => :skip_session_data,
"APPSIGNAL_ENABLE_FRONTEND_ERROR_CATCHING" => :enable_frontend_error_catching,
"APPSIGNAL_IGNORE_ACTIONS" => :ignore_actions,
"APPSIGNAL_IGNORE_ERRORS" => :ignore_errors,
"APPSIGNAL_IGNORE_NAMESPACES" => :ignore_namespaces,
"APPSIGNAL_FILTER_PARAMETERS" => :filter_parameters,
"APPSIGNAL_SEND_PARAMS" => :send_params,
"APPSIGNAL_HTTP_PROXY" => :http_proxy,
"APPSIGNAL_ENABLE_ALLOCATION_TRACKING" => :enable_allocation_tracking,
"APPSIGNAL_ENABLE_GC_INSTRUMENTATION" => :enable_gc_instrumentation,
"APPSIGNAL_RUNNING_IN_CONTAINER" => :running_in_container,
"APPSIGNAL_WORKING_DIR_PATH" => :working_dir_path,
"APPSIGNAL_ENABLE_HOST_METRICS" => :enable_host_metrics,
"APPSIGNAL_ENABLE_MINUTELY_PROBES" => :enable_minutely_probes,
"APPSIGNAL_HOSTNAME" => :hostname,
"APPSIGNAL_CA_FILE_PATH" => :ca_file_path,
"APPSIGNAL_DNS_SERVERS" => :dns_servers,
"APPSIGNAL_FILES_WORLD_ACCESSIBLE" => :files_world_accessible
}.freeze
attr_reader :root_path, :env, :initial_config, :config_hash
attr_accessor :logger
def initialize(root_path, env, initial_config = {}, logger = Appsignal.logger)
@root_path = root_path
@env = ENV.fetch("APPSIGNAL_APP_ENV".freeze, env.to_s)
@initial_config = initial_config
@logger = logger
@valid = false
@config_hash = Hash[DEFAULT_CONFIG]
# Set config based on the system
detect_from_system
# Initial config
merge(@config_hash, initial_config)
# Load the config file if it exists
load_from_disk
# Load config from environment variables
load_from_environment
# Validate that we have a correct config
validate
end
# @api private
# @return [String] System's tmp directory.
def self.system_tmp_dir
if Gem.win_platform?
Dir.tmpdir
else
File.realpath("/tmp")
end
end
def [](key)
config_hash[key]
end
def []=(key, value)
config_hash[key] = value
end
def log_file_path
path = config_hash[:log_path] || root_path && File.join(root_path, "log")
if path && File.writable?(path)
return File.join(File.realpath(path), "appsignal.log")
end
system_tmp_dir = self.class.system_tmp_dir
if File.writable? system_tmp_dir
$stdout.puts "appsignal: Unable to log to '#{path}'. Logging to "\
"'#{system_tmp_dir}' instead. Please check the "\
"permissions for the application's (log) directory."
File.join(system_tmp_dir, "appsignal.log")
else
$stdout.puts "appsignal: Unable to log to '#{path}' or the "\
"'#{system_tmp_dir}' fallback. Please check the permissions "\
"for the application's (log) directory."
end
end
def valid?
@valid
end
def active?
@valid && config_hash[:active]
end
def write_to_environment # rubocop:disable Metrics/AbcSize
ENV["_APPSIGNAL_ACTIVE"] = active?.to_s
ENV["_APPSIGNAL_APP_PATH"] = root_path.to_s
ENV["_APPSIGNAL_AGENT_PATH"] = File.expand_path("../../../ext", __FILE__).to_s
ENV["_APPSIGNAL_ENVIRONMENT"] = env
ENV["_APPSIGNAL_AGENT_VERSION"] = Appsignal::Extension.agent_version
ENV["_APPSIGNAL_LANGUAGE_INTEGRATION_VERSION"] = "ruby-#{Appsignal::VERSION}"
ENV["_APPSIGNAL_DEBUG_LOGGING"] = config_hash[:debug].to_s
ENV["_APPSIGNAL_LOG"] = config_hash[:log]
ENV["_APPSIGNAL_LOG_FILE_PATH"] = log_file_path.to_s if log_file_path
ENV["_APPSIGNAL_PUSH_API_ENDPOINT"] = config_hash[:endpoint]
ENV["_APPSIGNAL_PUSH_API_KEY"] = config_hash[:push_api_key]
ENV["_APPSIGNAL_APP_NAME"] = config_hash[:name]
ENV["_APPSIGNAL_HTTP_PROXY"] = config_hash[:http_proxy]
ENV["_APPSIGNAL_IGNORE_ACTIONS"] = config_hash[:ignore_actions].join(",")
ENV["_APPSIGNAL_IGNORE_ERRORS"] = config_hash[:ignore_errors].join(",")
ENV["_APPSIGNAL_IGNORE_NAMESPACES"] = config_hash[:ignore_namespaces].join(",")
ENV["_APPSIGNAL_FILTER_PARAMETERS"] = config_hash[:filter_parameters].join(",")
ENV["_APPSIGNAL_SEND_PARAMS"] = config_hash[:send_params].to_s
ENV["_APPSIGNAL_RUNNING_IN_CONTAINER"] = config_hash[:running_in_container].to_s
ENV["_APPSIGNAL_WORKING_DIR_PATH"] = config_hash[:working_dir_path] if config_hash[:working_dir_path]
ENV["_APPSIGNAL_ENABLE_HOST_METRICS"] = config_hash[:enable_host_metrics].to_s
ENV["_APPSIGNAL_ENABLE_MINUTELY_PROBES"] = config_hash[:enable_minutely_probes].to_s
ENV["_APPSIGNAL_HOSTNAME"] = config_hash[:hostname].to_s
ENV["_APPSIGNAL_PROCESS_NAME"] = $PROGRAM_NAME
ENV["_APPSIGNAL_CA_FILE_PATH"] = config_hash[:ca_file_path].to_s
ENV["_APPSIGNAL_DNS_SERVERS"] = config_hash[:dns_servers].join(",")
ENV["_APPSIGNAL_FILES_WORLD_ACCESSIBLE"] = config_hash[:files_world_accessible].to_s
end
def validate
# Strip path from endpoint so we're backwards compatible with
# earlier versions of the gem.
# TODO: Move to its own method, maybe in `#[]=`?
endpoint_uri = URI(config_hash[:endpoint])
config_hash[:endpoint] =
if endpoint_uri.port == 443
"#{endpoint_uri.scheme}://#{endpoint_uri.host}"
else
"#{endpoint_uri.scheme}://#{endpoint_uri.host}:#{endpoint_uri.port}"
end
if config_hash[:push_api_key]
@valid = true
else
@valid = false
@logger.error "Push api key not set after loading config"
end
end
private
def config_file
@config_file ||=
root_path.nil? ? nil : File.join(root_path, "config", "appsignal.yml")
end
def detect_from_system
config_hash[:log] = "stdout" if Appsignal::System.heroku?
# Make active by default if APPSIGNAL_PUSH_API_KEY is present
config_hash[:active] = true if ENV["APPSIGNAL_PUSH_API_KEY"]
end
def load_from_disk
return if !config_file || !File.exist?(config_file)
configurations = YAML.load(ERB.new(IO.read(config_file)).result)
config_for_this_env = configurations[env]
if config_for_this_env
config_for_this_env = Hash[config_for_this_env.map do |key, value|
[key.to_sym, value]
end] # convert keys to symbols
# Backwards compatibility with config files generated by earlier
# versions of the gem
if !config_for_this_env[:push_api_key] && config_for_this_env[:api_key]
config_for_this_env[:push_api_key] = config_for_this_env[:api_key]
end
if !config_for_this_env[:ignore_errors] && config_for_this_env[:ignore_exceptions]
config_for_this_env[:ignore_errors] = config_for_this_env[:ignore_exceptions]
end
merge(@config_hash, config_for_this_env)
else
@logger.error "Not loading from config file: config for '#{env}' not found"
end
end
def load_from_environment
config = {}
# Configuration with string type
%w(APPSIGNAL_PUSH_API_KEY APPSIGNAL_APP_NAME APPSIGNAL_PUSH_API_ENDPOINT
APPSIGNAL_FRONTEND_ERROR_CATCHING_PATH APPSIGNAL_HTTP_PROXY
APPSIGNAL_LOG APPSIGNAL_LOG_PATH APPSIGNAL_WORKING_DIR_PATH
APPSIGNAL_HOSTNAME APPSIGNAL_CA_FILE_PATH).each do |var|
env_var = ENV[var]
next unless env_var
config[ENV_TO_KEY_MAPPING[var]] = env_var
end
# Configuration with boolean type
%w(APPSIGNAL_ACTIVE APPSIGNAL_DEBUG APPSIGNAL_INSTRUMENT_NET_HTTP
APPSIGNAL_INSTRUMENT_REDIS APPSIGNAL_INSTRUMENT_SEQUEL
APPSIGNAL_SKIP_SESSION_DATA APPSIGNAL_ENABLE_FRONTEND_ERROR_CATCHING
APPSIGNAL_ENABLE_ALLOCATION_TRACKING APPSIGNAL_ENABLE_GC_INSTRUMENTATION
APPSIGNAL_RUNNING_IN_CONTAINER APPSIGNAL_ENABLE_HOST_METRICS
APPSIGNAL_SEND_PARAMS APPSIGNAL_ENABLE_MINUTELY_PROBES
APPSIGNAL_FILES_WORLD_ACCESSIBLE).each do |var|
env_var = ENV[var]
next unless env_var
config[ENV_TO_KEY_MAPPING[var]] = env_var.casecmp("true").zero?
end
# Configuration with array of strings type
%w(APPSIGNAL_IGNORE_ACTIONS APPSIGNAL_IGNORE_ERRORS
APPSIGNAL_IGNORE_NAMESPACES APPSIGNAL_FILTER_PARAMETERS).each do |var|
env_var = ENV[var]
next unless env_var
config[ENV_TO_KEY_MAPPING[var]] = env_var.split(",")
end
merge(@config_hash, config)
end
def merge(original_config, new_config)
new_config.each do |key, value|
unless original_config[key].nil?
@logger.debug("Config key '#{key}' is being overwritten")
end
original_config[key] = value
end
end
end
end