gitlab-exporter 11.4.0 → 12.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1629fb16b7451c28e085202cc001961dc3f1ac66308146ca992a7eeaf179c6e2
4
- data.tar.gz: a6158d5da5323e132a4a07e9b33238bb6b224658bc60a6885225c6d6a3729698
3
+ metadata.gz: 7c67811051d44f3c3e2fe6213eeb60f7a1c7997ddd2409008d52949cff5560fa
4
+ data.tar.gz: 0d750aef0b50d237783a09c22c35fdbcef0651a4998f6363cef2447786b9f8e1
5
5
  SHA512:
6
- metadata.gz: c1f03be5bb43004977d466b5dd4985a1626cc1cf430ee4d2137ad1c5983da527521ca3b3785e28e7daf20f18405b1bf43e9964106d60900b5c26e102e035d7c6
7
- data.tar.gz: 5433100b784c35f2c66e676e2c361ed6945fbcd4cc29d77be831f4f0c7d2f2d4a9b6271aeb8492c7af13f266975e053322a84e288c12502ba65ff03996f13a25
6
+ metadata.gz: 5e60b685e6005754b165963ad218462875a80eb26aef8f5a01753fb90c138f6e98e2ceb13a43aea058487579c2a9068328611af78b01ba6b58a391cc0d0e84a0
7
+ data.tar.gz: 611b3b9f1a66a50e7754a27f16f8fbd12f143559607fa1a4097c252823ace586043d334df8ae2a6a7060a52adb01b24ff6978141267bdddb2dc9b595d8f004e1
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  .bundle
2
+ vendor
2
3
  config/gitlab-exporter*.yml
data/.gitlab-ci.yml CHANGED
@@ -12,6 +12,7 @@ variables:
12
12
  stages:
13
13
  - test
14
14
  - dast
15
+ - publish
15
16
 
16
17
  default:
17
18
  image: ruby:${RUBY_VERSION}
@@ -24,7 +25,8 @@ default:
24
25
  .before_scripts: &before_scripts
25
26
  - git config --global user.email "bot@gitlab.com"
26
27
  - git config --global user.name "Bot User"
27
- - bundle install -j $(nproc) --path vendor
28
+ - bundle config set --local deployment true
29
+ - bundle install -j $(nproc)
28
30
 
29
31
  workflow:
30
32
  rules: &workflow_rules
@@ -41,8 +43,19 @@ rspec:
41
43
  before_script: *before_scripts
42
44
  parallel:
43
45
  matrix:
44
- - RUBY_VERSION: "2.7"
45
- - RUBY_VERSION: "3.0"
46
+ - RUBY_VERSION: ["2.7", "3.0"]
47
+
48
+ rspec_integration:
49
+ script:
50
+ - bundle exec rspec spec -t integration -f d -c
51
+ before_script: *before_scripts
52
+ services:
53
+ - redis:latest
54
+ variables:
55
+ REDIS_URL: "redis://redis"
56
+ parallel:
57
+ matrix:
58
+ - RUBY_VERSION: ["2.7", "3.0"]
46
59
 
47
60
  rubocop:
48
61
  script:
@@ -57,3 +70,16 @@ gemnasium-dependency_scanning:
57
70
 
58
71
  secret_detection:
59
72
  rules: *workflow_rules
73
+
74
+ publish_to_rubygems:
75
+ stage: publish
76
+ script:
77
+ - mkdir -p ~/.gem
78
+ - 'echo ":rubygems_api_key: ${RUBYGEMS_API_KEY}" > ~/.gem/credentials'
79
+ - chmod 0600 ~/.gem/credentials
80
+ - gem build gitlab-exporter.gemspec --output=gitlab-exporter.gem
81
+ - gem push gitlab-exporter.gem
82
+ before_script: *before_scripts
83
+ rules:
84
+ # Only push to RubyGems.org when we tag a new version
85
+ - if: '$CI_COMMIT_TAG'
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.4
1
+ 2.7.7
data/Gemfile.lock CHANGED
@@ -1,15 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gitlab-exporter (11.4.0)
4
+ gitlab-exporter (12.1.0)
5
5
  connection_pool (= 2.2.5)
6
+ faraday (~> 1.8.0)
6
7
  pg (= 1.2.3)
7
- puma (= 5.3.2)
8
+ puma (= 5.6.5)
8
9
  quantile (= 0.2.1)
9
- redis (= 4.1.4)
10
- redis-namespace (= 1.6.0)
11
- sidekiq (= 5.2.9)
12
- sinatra (~> 2.1.0)
10
+ redis (= 4.4.0)
11
+ redis-namespace (= 1.9.0)
12
+ sidekiq (= 6.4.0)
13
+ sinatra (~> 2.2.0)
13
14
 
14
15
  GEM
15
16
  remote: https://rubygems.org/
@@ -17,23 +18,43 @@ GEM
17
18
  ast (2.4.1)
18
19
  connection_pool (2.2.5)
19
20
  diff-lcs (1.3)
20
- mustermann (1.1.1)
21
+ faraday (1.8.0)
22
+ faraday-em_http (~> 1.0)
23
+ faraday-em_synchrony (~> 1.0)
24
+ faraday-excon (~> 1.1)
25
+ faraday-httpclient (~> 1.0.1)
26
+ faraday-net_http (~> 1.0)
27
+ faraday-net_http_persistent (~> 1.1)
28
+ faraday-patron (~> 1.0)
29
+ faraday-rack (~> 1.0)
30
+ multipart-post (>= 1.2, < 3)
31
+ ruby2_keywords (>= 0.0.4)
32
+ faraday-em_http (1.0.0)
33
+ faraday-em_synchrony (1.0.0)
34
+ faraday-excon (1.1.0)
35
+ faraday-httpclient (1.0.1)
36
+ faraday-net_http (1.0.1)
37
+ faraday-net_http_persistent (1.2.0)
38
+ faraday-patron (1.0.0)
39
+ faraday-rack (1.0.0)
40
+ multipart-post (2.2.3)
41
+ mustermann (2.0.2)
21
42
  ruby2_keywords (~> 0.0.1)
22
43
  nio4r (2.5.8)
23
44
  parallel (1.20.1)
24
45
  parser (3.0.0.0)
25
46
  ast (~> 2.4.1)
26
47
  pg (1.2.3)
27
- puma (5.3.2)
48
+ puma (5.6.5)
28
49
  nio4r (~> 2.0)
29
50
  quantile (0.2.1)
30
- rack (2.2.3)
31
- rack-protection (2.1.0)
51
+ rack (2.2.5)
52
+ rack-protection (2.2.4)
32
53
  rack
33
54
  rainbow (3.0.0)
34
- redis (4.1.4)
35
- redis-namespace (1.6.0)
36
- redis (>= 3.0.4)
55
+ redis (4.4.0)
56
+ redis-namespace (1.9.0)
57
+ redis (>= 4)
37
58
  regexp_parser (2.0.3)
38
59
  rexml (3.2.4)
39
60
  rspec (3.7.0)
@@ -62,17 +83,16 @@ GEM
62
83
  parser (>= 2.7.1.5)
63
84
  ruby-progressbar (1.11.0)
64
85
  ruby2_keywords (0.0.5)
65
- sidekiq (5.2.9)
66
- connection_pool (~> 2.2, >= 2.2.2)
86
+ sidekiq (6.4.0)
87
+ connection_pool (>= 2.2.2)
67
88
  rack (~> 2.0)
68
- rack-protection (>= 1.5.0)
69
- redis (>= 3.3.5, < 4.2)
70
- sinatra (2.1.0)
71
- mustermann (~> 1.0)
89
+ redis (>= 4.2.0)
90
+ sinatra (2.2.4)
91
+ mustermann (~> 2.0)
72
92
  rack (~> 2.2)
73
- rack-protection (= 2.1.0)
93
+ rack-protection (= 2.2.4)
74
94
  tilt (~> 2.0)
75
- tilt (2.0.10)
95
+ tilt (2.0.11)
76
96
  unicode-display_width (1.7.0)
77
97
  webrick (1.7.0)
78
98
 
data/README.md CHANGED
@@ -59,6 +59,8 @@ metrics.
59
59
  * `sidekiq_schedule_set_backlog_count`
60
60
  * `sidekiq_retry_set_processing_delay_seconds`
61
61
  * `sidekiq_retry_set_backlog_count`
62
+ 1. [Elasticsearch](lib/gitlab_exporter/elasticsearch.rb)
63
+ * [Migrations](https://docs.gitlab.com/ee/integration/elasticsearch.html#advanced-search-migrations) -- `elasticsearch_migrations_state`
62
64
 
63
65
  ### Setup with GitLab Development Kit
64
66
 
@@ -6,11 +6,15 @@ db_common: &db_common
6
6
 
7
7
  # Web server config
8
8
  server:
9
- name: puma # cf. https://github.com/sinatra/sinatra#available-settings
9
+ name: webrick # cf. https://github.com/sinatra/sinatra#available-settings
10
10
  listen_address: 0.0.0.0
11
11
  listen_port: 9168
12
12
  # Maximum amount of memory to use in megabytes, after which the process is killed
13
13
  memory_threshold: 1024
14
+ # TLS settings
15
+ tls_enabled: false
16
+ tls_cert_path: /tmp/server.crt
17
+ tls_key_path: /tmp/server.key
14
18
 
15
19
  # Probes config
16
20
  probes:
@@ -99,6 +103,22 @@ probes:
99
103
  opts:
100
104
  quantiles: false
101
105
 
106
+
107
+ # Uncomment below to add GitLab specific Elasticsearch metrics.
108
+ #
109
+ # To support multiple authorization types, opts are passed to a Faraday connection object.
110
+ # See: https://www.rubydoc.info/github/lostisland/faraday/Faraday%2FConnection:initialize
111
+ #
112
+ # elasticsearch: &elasticsearch
113
+ # methods:
114
+ # - probe_migrations
115
+ #
116
+ # opts:
117
+ # - url: http://localhost:9200
118
+ # options:
119
+ # headers:
120
+ # Authorization: "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
121
+
102
122
  metrics:
103
123
  multiple: true
104
124
  git_process:
@@ -21,13 +21,14 @@ Gem::Specification.new do |s|
21
21
  s.license = "MIT"
22
22
 
23
23
  s.add_runtime_dependency "connection_pool", "2.2.5"
24
+ s.add_runtime_dependency "faraday", "~> 1.8.0"
24
25
  s.add_runtime_dependency "pg", "1.2.3"
25
- s.add_runtime_dependency "puma", "5.3.2"
26
+ s.add_runtime_dependency "puma", "5.6.5"
26
27
  s.add_runtime_dependency "quantile", "0.2.1"
27
- s.add_runtime_dependency "redis", "4.1.4"
28
- s.add_runtime_dependency "redis-namespace", "1.6.0"
29
- s.add_runtime_dependency "sidekiq", "5.2.9"
30
- s.add_runtime_dependency "sinatra", "~> 2.1.0"
28
+ s.add_runtime_dependency "redis", "4.4.0"
29
+ s.add_runtime_dependency "redis-namespace", "1.9.0"
30
+ s.add_runtime_dependency "sidekiq", "6.4.0"
31
+ s.add_runtime_dependency "sinatra", "~> 2.2.0"
31
32
 
32
33
  s.add_development_dependency "rspec", "~> 3.7.0"
33
34
  s.add_development_dependency "rspec-expectations", "~> 3.7.0"
@@ -19,11 +19,6 @@ module GitLab
19
19
  projects.mirror = true
20
20
  AND projects.archived = false
21
21
  AND project_mirror_data.retry_count <= 14
22
- AND (
23
- (projects.visibility_level = 20 AND root_namespaces.visibility_level = 20)
24
- OR
25
- plans.name IN ('early_adopter', 'bronze', 'silver', 'gold')
26
- )
27
22
  AND project_mirror_data.next_execution_timestamp > '2020-03-28'
28
23
  SQL
29
24
 
@@ -31,15 +26,6 @@ module GitLab
31
26
  select: :projects,
32
27
  joins: <<~SQL,
33
28
  INNER JOIN project_mirror_data ON project_mirror_data.project_id = projects.id
34
- INNER JOIN namespaces AS root_namespaces ON root_namespaces.id = (
35
- WITH RECURSIVE "base_and_ancestors" AS (
36
- (SELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."id" = projects.namespace_id)
37
- UNION
38
- (SELECT "namespaces".* FROM "namespaces", "base_and_ancestors" WHERE "namespaces"."id" = "base_and_ancestors"."parent_id")
39
- ) SELECT "namespaces".id FROM "base_and_ancestors" AS "namespaces" WHERE "namespaces"."parent_id" IS NULL
40
- )
41
- LEFT JOIN gitlab_subscriptions ON gitlab_subscriptions.namespace_id = root_namespaces.id
42
- LEFT JOIN plans ON plans.id = gitlab_subscriptions.hosted_plan_id
43
29
  SQL
44
30
  check: "SELECT 1 FROM information_schema.tables WHERE table_name='plans'"
45
31
  }.freeze
@@ -125,29 +111,146 @@ module GitLab
125
111
  archived: {}
126
112
  }
127
113
  },
128
- groups: {
114
+ namespaces: {
129
115
  select: :namespaces,
130
116
  fields: {
117
+ type: {},
131
118
  visibility_level: {},
132
119
  root: { definition: "(parent_id IS NULL)" }
133
120
  }
134
121
  },
135
- registry_gc_manifest_review_queue: { select: :gc_manifest_review_queue },
136
- registry_gc_manifest_review_queue_overdue: {
137
- select: :gc_manifest_review_queue,
138
- where: "review_after < NOW()"
139
- },
140
- registry_gc_blob_review_queue: { select: :gc_blob_review_queue },
141
- registry_gc_blob_review_queue_overdue: {
142
- select: :gc_blob_review_queue,
143
- where: "review_after < NOW()"
144
- },
145
- registry_top_level_namespaces: { select: :top_level_namespaces },
146
- # Please note that the tables below are partitioned, so a SELECT COUNT(*)
147
- # will scale poorly. Avoid using these in production:
148
- registry_repositories: { select: :repositories },
149
- registry_manifests: { select: :manifests },
150
- registry_blobs: { select: :blobs }
122
+ container_repositories_migration_pending: {
123
+ select: :container_repositories,
124
+ where: <<~SQL
125
+ migration_state <> 'import_done'
126
+ AND created_at < '2022-08-17 00:00:00'
127
+ SQL
128
+ },
129
+ container_repositories_pre_importing: {
130
+ select: :container_repositories,
131
+ where: "migration_state = 'pre_importing'"
132
+ },
133
+ container_repositories_importing: {
134
+ select: :container_repositories,
135
+ where: "migration_state = 'importing'"
136
+ },
137
+ container_repositories_pre_import_done: {
138
+ select: :container_repositories,
139
+ where: "migration_state = 'pre_import_done'"
140
+ },
141
+ container_repositories_import_done: {
142
+ select: :container_repositories,
143
+ where: "migration_state = 'import_done'"
144
+ },
145
+ container_repositories_import_skipped: {
146
+ select: :container_repositories,
147
+ where: "migration_state = 'import_skipped'"
148
+ },
149
+ container_repositories_import_aborted: {
150
+ select: :container_repositories,
151
+ where: "migration_state = 'import_aborted'"
152
+ },
153
+ container_repositories_migration_all_free: {
154
+ select: :container_repositories,
155
+ where: <<~SQL
156
+ migration_plan IN ('free', 'early_adopter')
157
+ OR migration_plan IS NULL
158
+ SQL
159
+ },
160
+ container_repositories_migration_pending_free: {
161
+ select: :container_repositories,
162
+ where: <<~SQL
163
+ migration_state <> 'import_done'
164
+ AND created_at < '2022-08-17 00:00:00'
165
+ AND (migration_plan IN ('free', 'early_adopter')
166
+ OR migration_plan IS NULL)
167
+ SQL
168
+ },
169
+ container_repositories_import_done_free: {
170
+ select: :container_repositories,
171
+ where: <<~SQL
172
+ (migration_state = 'import_done' OR created_at >= '2022-08-17 00:00:00')
173
+ AND (migration_plan IN ('free', 'early_adopter')
174
+ OR migration_plan IS NULL)
175
+ SQL
176
+ },
177
+ container_repositories_stalled_pre_importing: {
178
+ select: :container_repositories,
179
+ where: <<~SQL
180
+ migration_state = 'pre_importing'
181
+ AND (COALESCE(migration_pre_import_started_at, TO_TIMESTAMP(0)) < (now() - INTERVAL '20 minutes'))
182
+ SQL
183
+ },
184
+ container_repositories_stalled_pre_import_done: {
185
+ select: :container_repositories,
186
+ where: <<~SQL
187
+ migration_state = 'pre_import_done'
188
+ AND (COALESCE(migration_pre_import_done_at, TO_TIMESTAMP(0)) < (now() - INTERVAL '5 minutes'))
189
+ SQL
190
+ },
191
+ container_repositories_stalled_importing: {
192
+ select: :container_repositories,
193
+ where: <<~SQL
194
+ migration_state = 'importing'
195
+ AND (COALESCE(migration_import_started_at, TO_TIMESTAMP(0)) < (now() - INTERVAL '5 minutes'))
196
+ SQL
197
+ },
198
+ container_repositories_skipped_not_in_plan: {
199
+ select: :container_repositories,
200
+ where: <<~SQL
201
+ migration_state = 'import_skipped'
202
+ AND migration_skipped_reason = 0
203
+ SQL
204
+ },
205
+ container_repositories_skipped_too_many_retries: {
206
+ select: :container_repositories,
207
+ where: <<~SQL
208
+ migration_state = 'import_skipped'
209
+ AND migration_skipped_reason = 1
210
+ SQL
211
+ },
212
+ container_repositories_skipped_too_many_tags: {
213
+ select: :container_repositories,
214
+ where: <<~SQL
215
+ migration_state = 'import_skipped'
216
+ AND migration_skipped_reason = 2
217
+ SQL
218
+ },
219
+ container_repositories_skipped_deny_list: {
220
+ select: :container_repositories,
221
+ where: <<~SQL
222
+ migration_state = 'import_skipped'
223
+ AND migration_skipped_reason = 3
224
+ SQL
225
+ },
226
+ container_repositories_skipped_canceled: {
227
+ select: :container_repositories,
228
+ where: <<~SQL
229
+ migration_state = 'import_skipped'
230
+ AND migration_skipped_reason IN (4, 8)
231
+ SQL
232
+ },
233
+ container_repositories_skipped_not_found: {
234
+ select: :container_repositories,
235
+ where: <<~SQL
236
+ migration_state = 'import_skipped'
237
+ AND migration_skipped_reason = 5
238
+ SQL
239
+ },
240
+ container_repositories_skipped_native: {
241
+ select: :container_repositories,
242
+ where: <<~SQL
243
+ migration_state = 'import_skipped'
244
+ AND migration_skipped_reason = 6
245
+ SQL
246
+ },
247
+ container_repositories_skipped_force_canceled: {
248
+ select: :container_repositories,
249
+ where: <<~SQL
250
+ migration_state = 'import_skipped'
251
+ AND migration_skipped_reason = 7
252
+ SQL
253
+ }
151
254
  }.freeze
152
255
 
153
256
  def initialize(selected_queries: nil, **args)
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+
6
+ module GitLab
7
+ module Exporter
8
+ # Exports GitLab specific Elasticsearch metrics.
9
+ #
10
+ # For generic operational metrics, see elasticsearch_exporter.
11
+ # https://github.com/prometheus-community/elasticsearch_exporter
12
+ class ElasticsearchProber
13
+ MIGRATION_STATE_MAP = {
14
+ unknown: -9,
15
+ # TODO: failed: -1
16
+ pending: 0,
17
+ running: 1,
18
+ halted: 2,
19
+ completed: 3
20
+ }.freeze
21
+
22
+ def initialize(metrics: PrometheusMetrics.new, logger: nil, **opts)
23
+ @metrics = metrics
24
+ @logger = logger
25
+ @opts = opts
26
+ end
27
+
28
+ # Probes the state of Advanced Search Migrations
29
+ # https://docs.gitlab.com/ee/integration/elasticsearch.html#advanced-search-migrations
30
+ def probe_migrations
31
+ elastic_probe do |conn|
32
+ resp = conn.get "/gitlab-*-migrations/_search"
33
+ return unless resp.status == 200
34
+
35
+ JSON.parse(resp.body).dig("hits", "hits").each do |hit|
36
+ @metrics.add(
37
+ "elasticsearch_migrations_info", 1, # 1 is a noop.
38
+ state: inferred_migration_state(hit.fetch("_source")),
39
+ name: hit.fetch("_id")
40
+ )
41
+ end
42
+ end
43
+ rescue StandardError => e
44
+ @logger&.error "ElasticsearchProper encountered an error: #{e}"
45
+ end
46
+
47
+ private
48
+
49
+ def elastic_probe
50
+ yield Faraday.new(@opts.fetch(:url), @opts.fetch(:options, {}))
51
+ end
52
+
53
+ def inferred_migration_state(migration)
54
+ return :pending if migration["started_at"] == ""
55
+
56
+ if migration["started_at"] != "" && migration["completed_at"] == "" && !migration.dig("state", "halted")
57
+ return :running
58
+ end
59
+
60
+ return :completed if migration["completed"]
61
+ return :halted if migration.dig("state", "halted")
62
+
63
+ @logger&.error("Elasticsearch probe doesn't know the state of a migration")
64
+ :unknown
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ if Gem.loaded_specs["rack"].version >= Gem::Version.new("3.0.0")
4
+ fail <<~ERR
5
+ This patch is unnecessary in Rack versions 3.0.0 or newer.
6
+ Please remove this file and the associated spec.
7
+
8
+ See https://github.com/rack/rack/blob/main/CHANGELOG.md#security (issue #1733)
9
+ ERR
10
+ end
11
+
12
+ # Patches a cache poisoning attack vector in Rack by not allowing semicolons
13
+ # to delimit query parameters.
14
+ # See https://github.com/rack/rack/issues/1732.
15
+ #
16
+ # Solution is taken from the same issue.
17
+ #
18
+ # The actual patch is due for release in Rack 3.0.0.
19
+ module Rack
20
+ class Request # rubocop:disable Style/Documentation
21
+ Helpers.module_eval do
22
+ # rubocop: disable Naming/MethodName
23
+ def GET
24
+ if get_header(RACK_REQUEST_QUERY_STRING) == query_string
25
+ get_header(RACK_REQUEST_QUERY_HASH)
26
+ else
27
+ query_hash = parse_query(query_string, "&") # only allow ampersand here
28
+ set_header(RACK_REQUEST_QUERY_STRING, query_string)
29
+ set_header(RACK_REQUEST_QUERY_HASH, query_hash)
30
+ end
31
+ end
32
+ # rubocop: enable Naming/MethodName
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,39 @@
1
+ # Contains helper methods to generate TLS related configuration for web servers
2
+ module TLSHelper
3
+ CERT_REGEX = /-----BEGIN CERTIFICATE-----(?:.|\n)+?-----END CERTIFICATE-----/.freeze
4
+
5
+ def validate_tls_config(config)
6
+ %i[tls_cert_path tls_key_path].each do |key|
7
+ fail "TLS enabled, but #{key} not specified in config" unless config.key?(key)
8
+
9
+ fail "File specified via #{key} not found: #{config[key]}" unless File.exist?(config[key])
10
+ end
11
+ end
12
+
13
+ def webrick_tls_config(config)
14
+ # This monkey-patches WEBrick::GenericServer, so never require this unless TLS is enabled.
15
+ require "webrick/ssl"
16
+
17
+ certs = load_ca_certs_bundle(File.binread(config[:tls_cert_path]))
18
+
19
+ {
20
+ SSLEnable: true,
21
+ SSLCertificate: certs.shift,
22
+ SSLPrivateKey: OpenSSL::PKey.read(File.binread(config[:tls_key_path])),
23
+ # SSLStartImmediately is true by default according to the docs, but when WEBrick creates the
24
+ # SSLServer internally, the switch was always nil for some reason. Setting this explicitly fixes this.
25
+ SSLStartImmediately: true,
26
+ SSLExtraChainCert: certs
27
+ }
28
+ end
29
+
30
+ # In Ruby OpenSSL v3.0.0, this can be replaced by OpenSSL::X509::Certificate.load
31
+ # https://github.com/ruby/openssl/issues/254
32
+ def load_ca_certs_bundle(ca_certs_string)
33
+ return [] unless ca_certs_string
34
+
35
+ ca_certs_string.scan(CERT_REGEX).map do |ca_cert_string|
36
+ OpenSSL::X509::Certificate.new(ca_cert_string)
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  module GitLab
2
2
  module Exporter
3
- VERSION = "11.4.0".freeze
3
+ VERSION = "12.1.0".freeze
4
4
  end
5
5
  end
@@ -1,5 +1,9 @@
1
1
  require "sinatra/base"
2
2
  require "English"
3
+ require "cgi"
4
+
5
+ require_relative "rack_vulndb_255039_patch"
6
+ require_relative "tls_helper"
3
7
 
4
8
  module GitLab
5
9
  module Exporter
@@ -51,6 +55,8 @@ module GitLab
51
55
  end
52
56
 
53
57
  class << self
58
+ include TLSHelper
59
+
54
60
  DEFAULT_WEB_SERVER = "webrick".freeze
55
61
 
56
62
  def setup(config)
@@ -74,8 +80,47 @@ module GitLab
74
80
  config ||= {}
75
81
 
76
82
  set(:server, config.fetch(:name, DEFAULT_WEB_SERVER))
77
- set(:bind, config.fetch(:listen_address, "0.0.0.0"))
78
83
  set(:port, config.fetch(:listen_port, 9168))
84
+
85
+ # Depending on whether TLS is enabled or not, bind string
86
+ # will be different.
87
+ if config.fetch(:tls_enabled, "false").to_s == "true"
88
+ set_tls_config(config)
89
+ else
90
+ set(:bind, config.fetch(:listen_address, "0.0.0.0"))
91
+ end
92
+ end
93
+
94
+ def set_tls_config(config) # rubocop:disable Naming/AccessorMethodName
95
+ validate_tls_config(config)
96
+
97
+ web_server = config.fetch(:name, DEFAULT_WEB_SERVER)
98
+ if web_server == "webrick"
99
+ set_webrick_tls(config)
100
+ elsif web_server == "puma"
101
+ set_puma_tls(config)
102
+ else
103
+ fail "TLS not supported for web server `#{web_server}`."
104
+ end
105
+ end
106
+
107
+ def set_webrick_tls(config) # rubocop:disable Naming/AccessorMethodName
108
+ server_settings = {}
109
+ server_settings.merge!(webrick_tls_config(config))
110
+
111
+ set(:bind, config.fetch(:listen_address, "0.0.0.0"))
112
+ set(:server_settings, server_settings)
113
+ end
114
+
115
+ def set_puma_tls(config) # rubocop:disable Naming/AccessorMethodName
116
+ listen_address = config.fetch(:listen_address, "0.0.0.0")
117
+ listen_port = config.fetch(:listen_port, 8443)
118
+ tls_cert_path = CGI.escape(config.fetch(:tls_cert_path))
119
+ tls_key_path = CGI.escape(config.fetch(:tls_key_path))
120
+
121
+ bind_string = "ssl://#{listen_address}:#{listen_port}?cert=#{tls_cert_path}&key=#{tls_key_path}"
122
+
123
+ set(:bind, bind_string)
79
124
  end
80
125
 
81
126
  def setup_probes(config)
@@ -1,19 +1,20 @@
1
1
  module GitLab
2
2
  # GitLab Monitoring
3
3
  module Exporter
4
- autoload :CLI, "gitlab_exporter/cli"
5
- autoload :TimeTracker, "gitlab_exporter/util"
6
- autoload :Utils, "gitlab_exporter/util"
7
- autoload :PrometheusMetrics, "gitlab_exporter/prometheus"
8
- autoload :Utils, "gitlab_exporter/util"
9
- autoload :Git, "gitlab_exporter/git"
10
- autoload :GitProber, "gitlab_exporter/git"
11
- autoload :GitProcessProber, "gitlab_exporter/git"
12
- autoload :Database, "gitlab_exporter/database"
13
- autoload :ProcessProber, "gitlab_exporter/process"
14
- autoload :WebExporter, "gitlab_exporter/web_exporter"
15
- autoload :Prober, "gitlab_exporter/prober"
16
- autoload :SidekiqProber, "gitlab_exporter/sidekiq"
17
- autoload :RubyProber, "gitlab_exporter/ruby"
4
+ autoload :CLI, "gitlab_exporter/cli"
5
+ autoload :TimeTracker, "gitlab_exporter/util"
6
+ autoload :Utils, "gitlab_exporter/util"
7
+ autoload :PrometheusMetrics, "gitlab_exporter/prometheus"
8
+ autoload :Utils, "gitlab_exporter/util"
9
+ autoload :Git, "gitlab_exporter/git"
10
+ autoload :GitProber, "gitlab_exporter/git"
11
+ autoload :GitProcessProber, "gitlab_exporter/git"
12
+ autoload :Database, "gitlab_exporter/database"
13
+ autoload :ProcessProber, "gitlab_exporter/process"
14
+ autoload :WebExporter, "gitlab_exporter/web_exporter"
15
+ autoload :Prober, "gitlab_exporter/prober"
16
+ autoload :SidekiqProber, "gitlab_exporter/sidekiq"
17
+ autoload :RubyProber, "gitlab_exporter/ruby"
18
+ autoload :ElasticsearchProber, "gitlab_exporter/elasticsearch"
18
19
  end
19
20
  end
@@ -0,0 +1,132 @@
1
+ require "spec_helper"
2
+ require "gitlab_exporter/elasticsearch"
3
+
4
+ describe GitLab::Exporter::ElasticsearchProber do
5
+ subject(:prober) { GitLab::Exporter::Prober.new(**options) }
6
+ let(:options) do
7
+ {
8
+ elasticsearch: {
9
+ class_name: described_class.to_s,
10
+ methods: %i[probe_migrations],
11
+ opts: {
12
+ url: "http://elasticsearch"
13
+ }
14
+ }
15
+ }
16
+ end
17
+
18
+ let(:connection) { instance_double(Faraday::Connection) }
19
+
20
+ describe "probe_migrations" do
21
+ let(:response) { double(:response, body: results, status: 200) }
22
+ let(:metric_name) { "elasticsearch_migrations_info" }
23
+
24
+ let(:results) do
25
+ <<-RESULTS
26
+ {
27
+ "took": 5,
28
+ "timed_out": false,
29
+ "_shards": {
30
+ "total": 1,
31
+ "successful": 1,
32
+ "skipped": 0,
33
+ "failed": 0
34
+ },
35
+ "hits": {
36
+ "total": {
37
+ "value": 1,
38
+ "relation": "eq"
39
+ },
40
+ "max_score": 1,
41
+ "hits": [#{document.to_json}]
42
+ }
43
+ }
44
+ RESULTS
45
+ end
46
+
47
+ let(:document) do
48
+ { "_id" => "123", "_source" => migration }
49
+ end
50
+
51
+ let(:migration) do
52
+ {
53
+ "started_at" => "",
54
+ "completed_at" => "",
55
+ "completed" => false,
56
+ "state" => {}
57
+ }.tap do |m|
58
+ case migration_state
59
+ when "running"
60
+ m["started_at"] = "2021-09-23T11:47:48,890Z"
61
+ when "completed"
62
+ m["started_at"] = "2021-09-23T11:47:48,890Z"
63
+ m["completed_at"] = "2021-09-24T11:47:48,890Z"
64
+ m["completed"] = true
65
+ when "halted"
66
+ m["started_at"] = "2021-09-23T11:47:48,890Z"
67
+ m["state"]["halted"] = true
68
+ end
69
+ end
70
+ end
71
+
72
+ let(:output) do
73
+ prober.probe_all
74
+ StringIO.new.tap do |s|
75
+ prober.write_to(s)
76
+ end
77
+ end
78
+
79
+ before do
80
+ allow_any_instance_of(described_class).to receive(:elastic_probe).and_yield(connection)
81
+ end
82
+
83
+ describe "when an Exception is raised" do
84
+ before do
85
+ allow(connection).to receive(:get).with("/gitlab-*-migrations/_search").and_raise("boom")
86
+ end
87
+
88
+ it "does NOT raise an error" do
89
+ expect { output }.to_not raise_error
90
+ end
91
+
92
+ it "does NOT export any metrics" do
93
+ expect(output.string).not_to match(/#{metric_name}/)
94
+ end
95
+ end
96
+
97
+ describe "when elasticsearch is reachable" do
98
+ let(:expected_output) { /#{metric_name}{state="#{migration_state}",name="123"} 1 \d+/ }
99
+ before do
100
+ allow(connection).to receive(:get).with("/gitlab-*-migrations/_search").and_return response
101
+ end
102
+
103
+ context "when there is a pending migration" do
104
+ let(:migration_state) { "pending" }
105
+ it "exports state correctly" do
106
+ expect(output.string).to match(expected_output)
107
+ end
108
+ end
109
+
110
+ context "when there is a running migration" do
111
+ let(:migration_state) { "running" }
112
+ it "exports state correctly" do
113
+ expect(output.string).to match(expected_output)
114
+ end
115
+ end
116
+
117
+ context "when there is a halted migration" do
118
+ let(:migration_state) { "halted" }
119
+ it "exports state correctly" do
120
+ expect(output.string).to match(expected_output)
121
+ end
122
+ end
123
+
124
+ context "when there is a completed migration" do
125
+ let(:migration_state) { "completed" }
126
+ it "exports state correctly" do
127
+ expect(output.string).to match(expected_output)
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+ require "gitlab_exporter/cli"
3
+
4
+ module GitLab
5
+ module Exporter
6
+ module CLI
7
+ describe SidekiqRunner, :integration do
8
+ let(:redis_url) { ENV.fetch("REDIS_URL", "redis://localhost:6379") }
9
+ let(:io) { StringIO.new }
10
+
11
+ it "can properly reach out to redis" do
12
+ args = CLIArgs.new([io], options: { /^--redis-url/ => redis_url })
13
+ runner = SidekiqRunner.new(args)
14
+
15
+ expect { runner.run }.not_to raise_error
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,10 @@ require "tmpdir"
7
7
  $LOAD_PATH.unshift File.expand_path(".")
8
8
  Dir["spec/support/**/*.rb"].each do |f| require f end
9
9
 
10
+ RSpec.configure do |config|
11
+ config.filter_run_excluding integration: true
12
+ end
13
+
10
14
  class GitRepoBuilder
11
15
  def origin
12
16
  @origin ||= create_origin
@@ -41,19 +45,30 @@ class GitRepoBuilder
41
45
  end
42
46
 
43
47
  class CLIArgs
44
- def initialize(args)
48
+ def initialize(args, options: {})
45
49
  @arguments = args
50
+ @options = options
46
51
  end
47
52
 
48
53
  def options
49
54
  yield self
55
+
56
+ self
50
57
  end
51
58
 
52
- def on(*args); end
59
+ def on(flag, *_)
60
+ match = @options.find { |regex, _| regex.match?(flag) }
61
+
62
+ yield match[1] if match
63
+ end
53
64
 
54
65
  def banner=(banner); end
55
66
 
56
67
  def parse!
57
68
  @arguments
58
69
  end
70
+
71
+ def shift
72
+ @arguments.shift
73
+ end
59
74
  end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-exporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 11.4.0
4
+ version: 12.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Carranza
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-07-27 00:00:00.000000000 Z
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.2.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: pg
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +58,14 @@ dependencies:
44
58
  requirements:
45
59
  - - '='
46
60
  - !ruby/object:Gem::Version
47
- version: 5.3.2
61
+ version: 5.6.5
48
62
  type: :runtime
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - '='
53
67
  - !ruby/object:Gem::Version
54
- version: 5.3.2
68
+ version: 5.6.5
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: quantile
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -72,56 +86,56 @@ dependencies:
72
86
  requirements:
73
87
  - - '='
74
88
  - !ruby/object:Gem::Version
75
- version: 4.1.4
89
+ version: 4.4.0
76
90
  type: :runtime
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - '='
81
95
  - !ruby/object:Gem::Version
82
- version: 4.1.4
96
+ version: 4.4.0
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: redis-namespace
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - '='
88
102
  - !ruby/object:Gem::Version
89
- version: 1.6.0
103
+ version: 1.9.0
90
104
  type: :runtime
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
108
  - - '='
95
109
  - !ruby/object:Gem::Version
96
- version: 1.6.0
110
+ version: 1.9.0
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: sidekiq
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - '='
102
116
  - !ruby/object:Gem::Version
103
- version: 5.2.9
117
+ version: 6.4.0
104
118
  type: :runtime
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - '='
109
123
  - !ruby/object:Gem::Version
110
- version: 5.2.9
124
+ version: 6.4.0
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: sinatra
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
129
  - - "~>"
116
130
  - !ruby/object:Gem::Version
117
- version: 2.1.0
131
+ version: 2.2.0
118
132
  type: :runtime
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
136
  - - "~>"
123
137
  - !ruby/object:Gem::Version
124
- version: 2.1.0
138
+ version: 2.2.0
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: rspec
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -181,14 +195,17 @@ files:
181
195
  - lib/gitlab_exporter/database/remote_mirrors.rb
182
196
  - lib/gitlab_exporter/database/row_count.rb
183
197
  - lib/gitlab_exporter/database/tuple_stats.rb
198
+ - lib/gitlab_exporter/elasticsearch.rb
184
199
  - lib/gitlab_exporter/git.rb
185
200
  - lib/gitlab_exporter/memstats.rb
186
201
  - lib/gitlab_exporter/memstats/mapping.rb
187
202
  - lib/gitlab_exporter/prober.rb
188
203
  - lib/gitlab_exporter/process.rb
189
204
  - lib/gitlab_exporter/prometheus.rb
205
+ - lib/gitlab_exporter/rack_vulndb_255039_patch.rb
190
206
  - lib/gitlab_exporter/ruby.rb
191
207
  - lib/gitlab_exporter/sidekiq.rb
208
+ - lib/gitlab_exporter/tls_helper.rb
192
209
  - lib/gitlab_exporter/util.rb
193
210
  - lib/gitlab_exporter/version.rb
194
211
  - lib/gitlab_exporter/web_exporter.rb
@@ -196,9 +213,11 @@ files:
196
213
  - spec/database/bloat_spec.rb
197
214
  - spec/database/ci_builds_spec.rb
198
215
  - spec/database/row_count_spec.rb
216
+ - spec/elasticsearch_spec.rb
199
217
  - spec/fixtures/smaps/sample.txt
200
218
  - spec/git_process_proper_spec.rb
201
219
  - spec/git_spec.rb
220
+ - spec/integration/cli_spec.rb
202
221
  - spec/memstats_spec.rb
203
222
  - spec/prometheus_metrics_spec.rb
204
223
  - spec/ruby_spec.rb
@@ -208,7 +227,7 @@ homepage: http://gitlab.com
208
227
  licenses:
209
228
  - MIT
210
229
  metadata: {}
211
- post_install_message:
230
+ post_install_message:
212
231
  rdoc_options: []
213
232
  require_paths:
214
233
  - lib
@@ -223,8 +242,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
242
  - !ruby/object:Gem::Version
224
243
  version: '0'
225
244
  requirements: []
226
- rubygems_version: 3.1.4
227
- signing_key:
245
+ rubygems_version: 3.1.6
246
+ signing_key:
228
247
  specification_version: 4
229
248
  summary: GitLab metrics exporter
230
249
  test_files:
@@ -232,9 +251,11 @@ test_files:
232
251
  - spec/database/bloat_spec.rb
233
252
  - spec/database/ci_builds_spec.rb
234
253
  - spec/database/row_count_spec.rb
254
+ - spec/elasticsearch_spec.rb
235
255
  - spec/fixtures/smaps/sample.txt
236
256
  - spec/git_process_proper_spec.rb
237
257
  - spec/git_spec.rb
258
+ - spec/integration/cli_spec.rb
238
259
  - spec/memstats_spec.rb
239
260
  - spec/prometheus_metrics_spec.rb
240
261
  - spec/ruby_spec.rb