Class: Skywalking::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/skywalking/configuration.rb

Constant Summary collapse

DEFAULTS =
{
  :service_name => {
    type: :string,
    default: 'Your_ApplicationName',
    desc: 'The name of your awesome Ruby service'
  },
  :instance_name => {
    type: :string,
    default: 'Your_InstanceName',
    desc: 'The name of this particular awesome Ruby service instance'
  },
  :namespace => {
    type: :string,
    default: '',
    desc: 'The namespace of the service'
  },
  :environment => {
    type: :string,
    default: '',
    desc: 'The name of the environment this service is deployed in'
  },
  :collector_backend_services => {
    type: :string,
    default: '127.0.0.1:11800',
    desc: 'Backend service addresses'
  },
  :config_file => {
    type: :string,
    default: '',
    desc: 'The absolute path to the configuration file'
  },
  :log_file_name => {
    type: :string,
    default: 'skywalking',
    desc: 'The name of the log file'
  },
  :log_file_path => {
    type: :string,
    default: '',
    desc: 'The path to the log file'
  },
  :log_level => {
    type: :string,
    default: 'info',
    desc: 'The log level'
  },
  :disable_plugins => {
    type: :string,
    default: '',
    desc: "The plugins to disable, multiple names should be split by comma, e.g. 'redis5,elasticsearch'"
  },
  :report_protocol => {
    type: :string,
    default: 'grpc',
    desc: 'The protocol to use for reporting'
  },
  :re_ignore_operation => {
    type: :string,
    default: '',
    desc: 'Ignore specific URL paths'
  },
  :instance_properties_json => {
    type: :string,
    default: '',
    desc: 'A custom JSON string to be reported as service instance properties, e.g. `{"key": "value"}`'
  },
  :collector_heartbeat_period => {
    type: :int,
    default: 30,
    desc: 'The agent will send heartbeat to OAP every `collector_heartbeat_period` seconds'
  },
  :properties_report_period_factor => {
    type: :int,
    default: 10,
    desc: 'The agent will report service instance properties every 
          `collector_heartbeat_period * properties_report_period_factor` seconds'
  },
  :max_queue_size => {
    type: :int,
    default: 10000,
    desc: 'The maximum queue size for reporting data'
  },
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Configuration

Returns a new instance of Configuration.



108
109
110
111
112
113
114
# File 'lib/skywalking/configuration.rb', line 108

def initialize(opts = {})
  @agent_config = {}
  initialize_config(opts)
  if @logger.nil?
    @logger ||= Mutex.new.synchronize { build_logger }
  end
end

Instance Attribute Details

#agent_configObject (readonly)

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.



106
107
108
# File 'lib/skywalking/configuration.rb', line 106

def agent_config
  @agent_config
end

#loggerObject (readonly)

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.



106
107
108
# File 'lib/skywalking/configuration.rb', line 106

def logger
  @logger
end

Instance Method Details

#build_loggerObject

LOAD LOG



216
217
218
219
220
221
# File 'lib/skywalking/configuration.rb', line 216

def build_logger
  return @logger if @logger

  log_dest = log_destination
  create_log(log_dest, get_log_level)
end

#create_log(log_dest, level) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/skywalking/configuration.rb', line 223

def create_log(log_dest, level)
  if log_dest.is_a?(String)
    log_dest = File.expand_path(log_dest, Pathname.new(Dir.pwd).realpath)
    FileUtils.mkdir_p(File.dirname(log_dest))
  end

  begin
    logger = ::Logger.new(log_dest, progname: "Skywalking", level: level)
    logger.formatter = log_formatter

    logger
  rescue => e
    logger = ::Logger.new($stdout, progname: "Skywalking", level: level)
    logger.warn "Create logger for file #{log_dest} failed, using standard out for logging error=#{e.message}"
  end
end

#freezeObject



197
198
199
200
201
202
203
# File 'lib/skywalking/configuration.rb', line 197

def freeze
  super
  @agent_config.freeze
  @agent_config.transform_values(&:freeze)

  self
end

#get_log_levelObject



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/skywalking/configuration.rb', line 262

def get_log_level
  candidate = @agent_config[:log_level].downcase

  case candidate
  when "debug" then ::Logger::DEBUG
  when "info" then ::Logger::INFO
  when "warn" then ::Logger::WARN
  when "error" then ::Logger::ERROR
  when "fatal" then ::Logger::FATAL
  when ::Logger::DEBUG, ::Logger::INFO, ::Logger::WARN, ::Logger::ERROR, ::Logger::FATAL then candidate
  else ::Logger::INFO
  end
end

#initialize_config(opts) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/skywalking/configuration.rb', line 116

def initialize_config(opts)
  # from the default value
  merge_config(DEFAULTS.transform_values { |v| v[:default] })
  # start parameters
  merge_config(opts)
  # from the custom config file
  merge_config(override_config_by_file)
  # environment variables
  merge_config(override_config_by_env)
end

#key_to_env_key(key) ⇒ Object



193
194
195
# File 'lib/skywalking/configuration.rb', line 193

def key_to_env_key(key)
  'SW_AGENT_' + key.to_s.upcase
end

#log_destinationObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/skywalking/configuration.rb', line 247

def log_destination
  candidate = @agent_config[:log_file_path].upcase

  case candidate
  when "STDOUT"
    $stdout
  when "STDERR"
    $stderr
  when nil? || ''
    $stdout
  else
    "#{@agent_config[:log_file_path]}/#{@agent_config[:log_file_name]}.log"
  end
end

#log_formatterObject



240
241
242
243
244
245
# File 'lib/skywalking/configuration.rb', line 240

def log_formatter
  ->(severity, datetime, program, message) do
    datetime = datetime.strftime("%Y-%m-%dT%H:%M:%S")
    "[#{datetime}] - #{severity} - [#{program}] #{message}\n"
  end
end

#merge_config(new_config) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/skywalking/configuration.rb', line 127

def merge_config(new_config)
  return if new_config.nil?

  new_config.each do |k, v|
    @agent_config[k.to_sym] = v
  end
end

#override_config_by_envObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/skywalking/configuration.rb', line 169

def override_config_by_env
  new_config = {}
  DEFAULTS.each do |env_key, env_schema|
    env_value = ENV.fetch(key_to_env_key(env_key), nil)
    next if env_value.nil?

    type = env_schema[:type]
    case type
    when :string
      new_config[env_key] = env_value.to_s
    when :bool
      # rubocop:disable Performance/CollectionLiteralInLoop
      new_config[env_key] = !%w[0 false].include?(env_value.strip.downcase)
      # rubocop:enable Performance/CollectionLiteralInLoop
    when :int
      new_config[env_key] = env_value.to_s
    else
      env_value
    end
  end

  new_config
end

#override_config_by_fileObject

Raises:

  • (StandardError)


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
# File 'lib/skywalking/configuration.rb', line 135

def override_config_by_file
  config_yaml = @agent_config[:config_file]
  if config_yaml.nil? || config_yaml.empty?
    config_yaml = File.join(srv_root, "config", "skywalking.yml")
  end

  unless File.exist?(config_yaml)
    return
  end

  unless srv_environment
    return
  end

  error = nil
  begin
    raw_file = File.read(config_yaml)
    erb_file = ERB.new(raw_file).result(binding)
    loaded_yaml = if YAML.respond_to?(:unsafe_load)
                    YAML.unsafe_load(erb_file)
                  else
                    YAML.safe_load(erb_file, permitted_classes: [], permitted_symbols: [], aliases: true)
                  end
    loaded_yaml = loaded_yaml[srv_environment]
    error = "Invalid format in config file" if loaded_yaml && !loaded_yaml.is_a?(Hash)
  rescue Exception => e
    error = e.message
    nil
  end
  raise StandardError, "Error loading config file: #{config_yaml} - #{error}" if error

  loaded_yaml
end

#srv_environmentObject



205
206
207
# File 'lib/skywalking/configuration.rb', line 205

def srv_environment
  @agent_config[:environment].to_s.empty? ? Skywalking::Environment.instance.framework_env : @agent_config[:environment]
end

#srv_rootObject



209
210
211
# File 'lib/skywalking/configuration.rb', line 209

def srv_root
  Skywalking::Environment.instance.framework_root
end