Class: Hypertrace::Config::Config

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/hypertrace/config/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initializeConfig

Returns a new instance of Config.



5
6
7
# File 'lib/hypertrace/config/config.rb', line 5

def initialize
  @config = load_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/hypertrace/config/config.rb', line 4

def config
  @config
end

Instance Method Details

#load_configObject



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
# File 'lib/hypertrace/config/config.rb', line 9

def load_config
  # Order of loading:
  # 1.) Defaults
  # 2.) Overriden by config file
  # 3.) Overriden by env vars
  config = Marshal.load(Marshal.dump(DEFAULT_AGENT_CONFIG))

  file_config = load_file
  config = merge_config(config, file_config)

  env_config = Hypertrace::Config::Environment.load_config_from_env
  config = merge_config(config, env_config)

  log.info "Finalized config loaded"
  log.info config

  proto_config = Hypertrace::Agent::Config::V1::AgentConfig.new
  enabled = Google::Protobuf::BoolValue.new({value: config[:enabled]})
  proto_config.enabled = enabled
  proto_config.propagation_formats += config[:propagation_formats]
  service_name = Google::Protobuf::StringValue.new({value: config[:service_name]})
  proto_config.service_name = service_name

  proto_config.reporting = Hypertrace::Agent::Config::V1::Reporting.new
  endpoint = Google::Protobuf::StringValue.new({value: config[:reporting][:endpoint]})
  proto_config.reporting.endpoint = endpoint
  secure = Google::Protobuf::BoolValue.new({value: config[:reporting][:secure]})
  proto_config.reporting.secure = secure

  proto_config.reporting.trace_reporter_type = config[:reporting][:trace_reporter_type]

  proto_config.data_capture = Hypertrace::Agent::Config::V1::DataCapture.new
  %i[http_headers http_body rpc_metadata rpc_body].each do |field|
    message_instance = Hypertrace::Agent::Config::V1::Message.new
    message_instance.request = Google::Protobuf::BoolValue.new({value:config[:data_capture][field][:request]})
    message_instance.response = Google::Protobuf::BoolValue.new({value:config[:data_capture][field][:response]})
    proto_config.data_capture[field.to_s] = message_instance
  end
  proto_config.data_capture.body_max_size_bytes = Google::Protobuf::Int32Value.new({value: config[:data_capture][:body_max_size_bytes]})

  proto_config.resource_attributes.merge(config[:resource_attributes])
  proto_config
end

#load_fileObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hypertrace/config/config.rb', line 53

def load_file
  file_path = Hypertrace::EnvVarSettings.env_value('CONFIG_FILE')
  return {} if file_path.nil?

  begin
    yaml_content = File.read(file_path)
    config_file_contents = YAML.load(yaml_content, symbolize_names: true)
  rescue => e
    log.warn "failed to load config file: #{file_path}"
  end
  config_file_contents || {}
end

#merge_config(base_config, overriding_config) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hypertrace/config/config.rb', line 66

def merge_config(base_config, overriding_config)
  overriding_config.each_key do |key|
    if base_config.key?(key) && base_config[key].instance_of?(Hash)
      base_config[key] = merge_config(base_config[key], overriding_config[key])
    else
      base_config[key] = overriding_config[key]
    end
  end

  base_config
end