Class: PrometheusConfigBuilder::ConfigFile

Inherits:
Object
  • Object
show all
Includes:
PrometheusConfigBuilderLogger
Defined in:
lib/prometheus-config-builder/prometheus-config-builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PrometheusConfigBuilderLogger

#logger, logger, logger=

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



27
28
29
# File 'lib/prometheus-config-builder/prometheus-config-builder.rb', line 27

def basename
  @basename
end

Instance Method Details

#config_rulesObject



42
43
44
# File 'lib/prometheus-config-builder/prometheus-config-builder.rb', line 42

def config_rules
  @data['config_rules']
end

#get_scrape_configs(dst_prefix) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/prometheus-config-builder/prometheus-config-builder.rb', line 50

def get_scrape_configs(dst_prefix)
  configs = []

  @data['scrape_configs'].each do |config|
    case config["type"]
    when "passthrough"
      configs << ScrapeConfigPassthrough::handle(config)
    when "ecs-tasks"
      configs << ScrapeConfigECS::handle(@basename, config, dst_prefix)
    else
      raise "Unknown scrape_config type #{config["type"]}"
    end
  end

  return configs
end

#load(file) ⇒ Object



36
37
38
39
40
# File 'lib/prometheus-config-builder/prometheus-config-builder.rb', line 36

def load(file)
  @source = "file://#{file[:filename]}"
  @basename = file[:filename]
  @data = YAML.load(file[:contents])
end

#open(filename) ⇒ Object



29
30
31
32
33
34
# File 'lib/prometheus-config-builder/prometheus-config-builder.rb', line 29

def open(filename)
  filename = File.expand_path(filename)
  @source = "file://#{filename}"
  @basename = File.basename(filename)
  @data = YAML.load_file(filename)
end

#scrape_configsObject



46
47
48
# File 'lib/prometheus-config-builder/prometheus-config-builder.rb', line 46

def scrape_configs
  @data['scrape_configs']
end

#write_rules(path) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/prometheus-config-builder/prometheus-config-builder.rb', line 67

def write_rules(path)
  filename = path + "/" + @basename
  File.open(filename, "w") do |file|
    file.write("# AUTOGENERATED FILE, DO NOT MODIFY\n")
    file.write("#\n")
    file.write("# This file was automatically generated by prometheus-config-from-s3 at #{Time.now.utc.iso8601}\n")
    file.write("# based on #{@source}\n")
    file.write("#\n\n")
    file.write(config_rules.to_yaml)
  end

  return Digest::SHA256.digest config_rules.to_yaml
end