prometheus-config-builder 0.0.26 → 0.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56758de8c42a6bf76d657342fd895e8d5f0bf133
4
- data.tar.gz: 8fb4d608391a6c4bc60c6abea868bf0629845a75
3
+ metadata.gz: f97c7ca0818df250a1b5223d220c6e0b9c9f5ec9
4
+ data.tar.gz: 9d04ee4fe93d7b7b8dcb0927532993fa22b5c299
5
5
  SHA512:
6
- metadata.gz: 6f6597d9affe7f361a35e9d8b8f4701acb895147b5d790de81b624862d42c988b523ae81c0ee8b4854dea41530f0a201a8f22bf2b6ca8e699d1647fa29db7349
7
- data.tar.gz: 5080a4ff7a7ba2d1c87abaa095c65cc4e84a41c118e1801504f470f3d0c3c5165071cdd85f7361a87202d4a3415ef83df4e1e75c64c056dcd39e488fe610139d
6
+ metadata.gz: 9ea3df55d69a1ec404b11e475f84293f3512b2439baf71da01a539d22a91ea55d7621428972e7959a88ccfa1e04f8c56bec21dd7af3a46eb8a65b06314305a79
7
+ data.tar.gz: e47ffab6e099d533b00a35cd2868e9fc4007305c27f2745e83ba1e1cc7b9141bfaa6653375721de5c8fee96b75d4f145b20f10b07ce6a2eaa813670fbc63ff95
@@ -4,7 +4,6 @@ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
4
 
5
5
  require 'logger'
6
6
  require 'optparse'
7
- require 'prometheus-config-builder'
8
7
  require 'pp'
9
8
  require 'yaml'
10
9
  require 'json'
@@ -12,6 +11,11 @@ require 'prometheus_exporter'
12
11
  require 'prometheus_exporter/client'
13
12
  require 'prometheus_exporter/server'
14
13
 
14
+ server = PrometheusExporter::Server::WebServer.new port: 12345
15
+ PrometheusExporter::Client.default = PrometheusExporter::LocalClient.new(collector: server.collector)
16
+
17
+ require 'prometheus-config-builder'
18
+
15
19
  config = {}
16
20
  config[:paths] ||= []
17
21
  config[:root] ||= 'test/data'
@@ -67,8 +71,6 @@ if !File.exist?(config[:dst_dir])
67
71
  exit!(1)
68
72
  end
69
73
 
70
- server = PrometheusExporter::Server::WebServer.new port: 12345
71
- PrometheusExporter::Client.default = PrometheusExporter::LocalClient.new(collector: server.collector)
72
74
  server.start
73
75
 
74
76
  something_changed_count = PrometheusExporter::Client.default.register(:counter, "prometheusconfigbuilder_changes_count", "Number of times configuration has changed.")
@@ -17,6 +17,9 @@ require_relative './scrape_ecs.rb'
17
17
  require 'prometheus_exporter'
18
18
  require 'prometheus_exporter/client'
19
19
 
20
+ $config_file_status = PrometheusExporter::Client.default.register(:gauge, "prometheusconfigbuilder_config_file_status", ".")
21
+
22
+
20
23
  module PrometheusConfigBuilder
21
24
 
22
25
 
@@ -25,16 +28,17 @@ module PrometheusConfigBuilder
25
28
  @data = {}
26
29
 
27
30
  attr_reader :basename
31
+ attr_reader :source
28
32
 
29
33
  def open(filename)
30
- filename = File.expand_path(filename)
34
+ @filename = File.expand_path(filename)
31
35
  @source = "file://#{filename}"
32
36
  @basename = File.basename(filename)
33
37
  @data = YAML.load_file(filename)
34
38
  end
35
39
 
36
40
  def load(file)
37
- @source = "file://#{file[:filename]}"
41
+ @source = file[:source]
38
42
  @basename = file[:filename]
39
43
  @data = YAML.load(file[:contents])
40
44
  end
@@ -57,7 +61,7 @@ module PrometheusConfigBuilder
57
61
  when "ecs-tasks"
58
62
  configs << ScrapeConfigECS::handle(@basename, config, dst_prefix)
59
63
  else
60
- raise "Unknown scrape_config type #{config["type"]}"
64
+ raise "Unknown scrape_config type #{config["type"]} in file #{@basename}"
61
65
  end
62
66
  end
63
67
 
@@ -107,13 +111,20 @@ module PrometheusConfigBuilder
107
111
 
108
112
  logger.debug("Found file \"#{file.key}\" from S3 bucket #{bucket_name}")
109
113
  files << {
114
+ source: "s3://#{bucket_name}/#{file.key}",
110
115
  filename: File.basename(file.key),
111
116
  contents: contents.body.string,
112
117
  }
113
118
  end
114
119
  else
120
+ if glob[-1] == '/'
121
+ logger.warn("The file path #{glob} is not a glob. Adding a wilcard so that the path will be #{glob}*.yaml")
122
+ glob += "*.yaml"
123
+ end
115
124
  Dir.glob(glob).each do |file|
125
+ logger.debug("Found file from local filesystem: #{file}")
116
126
  files << {
127
+ source: "file://#{file}",
117
128
  filename: File.basename(file),
118
129
  contents: File.read(file)
119
130
  }
@@ -127,7 +138,7 @@ module PrometheusConfigBuilder
127
138
 
128
139
 
129
140
  class ConfigFiles
130
-
141
+ include PrometheusConfigBuilderLogger
131
142
  def initialize(prometheus_yaml_filename)
132
143
  @files = []
133
144
  @prometheus_yaml_filename = prometheus_yaml_filename
@@ -167,8 +178,14 @@ module PrometheusConfigBuilder
167
178
  FileUtils.mkdir_p(scrape_files_dir) if !File.exist?(scrape_files_dir)
168
179
 
169
180
  @files.each do |file|
170
- configs = file.get_scrape_configs(scrape_files_dir + "/" + File.basename(file.basename, ".*"))
171
- data["scrape_configs"].push(*configs)
181
+ begin
182
+ configs = file.get_scrape_configs(scrape_files_dir + "/" + File.basename(file.basename, ".*"))
183
+ data["scrape_configs"].push(*configs)
184
+ $config_file_status.observe(1, file: file.source)
185
+ rescue Exception => e
186
+ logger.error("Unable to get scrape configs correctly from file #{file.source}. Error: #{e}")
187
+ $config_file_status.observe(0, file: file.source)
188
+ end
172
189
  end
173
190
 
174
191
  if !data["rule_files"] or data["rule_files"].class != Array
@@ -242,8 +259,10 @@ module PrometheusConfigBuilder
242
259
  # Determine if something had changed since the last write_out based on the generated hashes
243
260
  if hash != @last_hash
244
261
  @last_hash = hash
262
+ logger.debug("Wrote new #{@prometheus_src} file for Prometheus.")
245
263
  return true
246
264
  else
265
+ logger.debug("The Prometheus #{@prometheus_src} config file did not change.")
247
266
  return false
248
267
  end
249
268
  end
@@ -1,5 +1,6 @@
1
1
  require 'pp'
2
2
  require_relative './logger.rb'
3
+ require 'tempfile'
3
4
 
4
5
  module PrometheusConfigBuilder
5
6
 
@@ -32,18 +33,20 @@ module PrometheusConfigBuilder
32
33
  role_session_name = (assume_role["role_session_name"] or "prometheus-scrape-ecs")
33
34
 
34
35
  name = region + "/" + role_arn + "/" + role_session_name
36
+ client = @@ecs[name]
37
+ if client == nil
38
+ logger.info("Using sts:AssumeRole on #{role_arn} as client #{name}")
39
+ role_credentials = Aws::AssumeRoleCredentials.new(
40
+ client: Aws::STS::Client.new({
41
+ region: region
42
+ }),
43
+ role_arn: role_arn,
44
+ role_session_name: role_session_name
45
+ )
35
46
 
36
- logger.info("Using sts:AssumeRole on #{role_arn}")
37
- role_credentials = Aws::AssumeRoleCredentials.new(
38
- client: Aws::STS::Client.new({
39
- region: region
40
- }),
41
- role_arn: role_arn,
42
- role_session_name: role_session_name
43
- )
44
-
45
- client = Aws::ECS::Client.new(credentials: role_credentials)
46
- @@ecs[name] = client
47
+ client = Aws::ECS::Client.new(credentials: role_credentials)
48
+ @@ecs[name] = client
49
+ end
47
50
  return client
48
51
  end
49
52
 
@@ -77,10 +80,15 @@ module PrometheusConfigBuilder
77
80
  return nil
78
81
  end
79
82
 
83
+ # Prometheus might notice that the scrape file is changed in the middle of the write. That's why we'll
84
+ # first write the new contents into a temp file and atomically replace the file with the temp file.
80
85
  file = File.expand_path(dst_prefix + "_" + config["job_name"] + ".json")
81
- File.open(file, "w") do |f|
86
+ tmpfile = Tempfile.new().path
87
+ File.open(tmpfile, "w") do |f|
82
88
  f.write(endpoints.to_json)
83
89
  end
90
+ File.rename(tmpfile, file)
91
+
84
92
 
85
93
  # Make copy of the settings and remove our custom properties from it.
86
94
  # The rest user can set just as he wants according to the Prometheus schema.
@@ -106,6 +114,7 @@ module PrometheusConfigBuilder
106
114
  tasks = []
107
115
  last_result = Aws::ECS::Types::ListTasksResponse.new
108
116
  last_result.next_token = nil
117
+ retries = 3
109
118
  loop do
110
119
  begin
111
120
  options = {
@@ -119,6 +128,10 @@ module PrometheusConfigBuilder
119
128
  end
120
129
  rescue Aws::ECS::Errors::ServiceError => e
121
130
  logger.warn("Error listing ecs tasks for service #{service_name} in cluster #{cluster}: #{e}")
131
+ retries -= 1
132
+ if retries < 0
133
+ raise IOError.new("Unable to get ecs tasks for service #{service_name} in cluster #{cluster}: #{e}")
134
+ end
122
135
  sleep(1)
123
136
  next
124
137
  end
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'prometheus-config-builder'
6
- s.version = '0.0.26'
6
+ s.version = '0.0.27'
7
7
  s.date = Time.now
8
8
 
9
9
  s.summary = %q{Template based config generation}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prometheus-config-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.26
4
+ version: 0.0.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juho Mäkinen juho.makinen@gmail.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-28 00:00:00.000000000 Z
11
+ date: 2020-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk