health_bit 0.1.7 → 0.2.0

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
  SHA256:
3
- metadata.gz: 712c992c9824246024965917be0bf884a757b163a5b4d027226fa15d090c886b
4
- data.tar.gz: 3d9f192b7a93bed3dd706c8f7dddc965b446c40069b440d05353f585efac66be
3
+ metadata.gz: 7351c3a05b94e33b5349abd88cfd076680de3b01ab74e035f8d18f7c669222e4
4
+ data.tar.gz: be72a41bb73bbb372ce1b0416c9819dcdbe3f3ad05dee5dfdb9e25e9f16d258a
5
5
  SHA512:
6
- metadata.gz: 3731cea2f52998e7b54202dbcf7f2b4cae9ad82772ba23be92bb1290ac2858dc71250b0eec4d79651cbfb6073fb223f2f443af09a4e34a33dd945d87eb36b43e
7
- data.tar.gz: a59fa6db73a4888cdb3401abe2391d6ec73d44ac3a5023fd0806cf10d638e96e659a242f01de19fb7b8f9da349841613112b5e4eabe2d738ca7b837796a915f4
6
+ metadata.gz: e3604074bf177e2297062a284c38f100872de45815b3c6872c2b2f820150a8d1a89317ca7d58b312ca4edc95f8ce37db4934012b457e292bd2625b14b5c227a0
7
+ data.tar.gz: 308578eefc161b52aa99765842648e50a9748c96ae02ad86f9e93fd9d73723c95ee0a3fbbb94a824c58437645794d9e0f924104b0246ce47dc8c7aac0aee5051
@@ -0,0 +1,62 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ rspec:
7
+ runs-on: ubuntu-latest
8
+
9
+ strategy:
10
+ matrix:
11
+ ruby-version: [3.0, 2.7, 2.6]
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+
16
+ - name: Set up Ruby ${{ matrix.ruby-version }}
17
+ uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby-version }}
20
+
21
+ - name: Cache gems
22
+ uses: actions/cache@v1
23
+ with:
24
+ path: vendor/bundle
25
+ key: ${{ runner.os }}-${{ matrix.ruby-version }}-bundler-${{ hashFiles('**/Gemfile.lock') }}
26
+ restore-keys: |
27
+ ${{ runner.os }}-${{ matrix.ruby-version }}-bundler-
28
+
29
+ - name: Install gems
30
+ run: |
31
+ bundle config path vendor/bundle
32
+ bundle install --jobs 4 --retry 3
33
+
34
+ - name: Run tests
35
+ run: bundle exec rspec
36
+
37
+ rubocop:
38
+ runs-on: ubuntu-latest
39
+
40
+ steps:
41
+ - uses: actions/checkout@v2
42
+
43
+ - name: Set up Ruby
44
+ uses: ruby/setup-ruby@v1
45
+ with:
46
+ ruby-version: 3.0
47
+
48
+ - name: Cache gems
49
+ uses: actions/cache@v1
50
+ with:
51
+ path: vendor/bundle
52
+ key: ${{ runner.os }}-bundler-${{ hashFiles('**/Gemfile.lock') }}
53
+ restore-keys: |
54
+ ${{ runner.os }}-bundler-
55
+
56
+ - name: Install gems
57
+ run: |
58
+ bundle config path vendor/bundle
59
+ bundle install --jobs 4 --retry 3
60
+
61
+ - name: Run Rubocop
62
+ run: bundle exec rubocop
data/.rubocop.yml CHANGED
@@ -8,11 +8,58 @@ AllCops:
8
8
  - 'bin/*'
9
9
  - 'spec/**/*'
10
10
  - 'vendor/**/*'
11
- TargetRubyVersion: 2.7.1
11
+ TargetRubyVersion: 3
12
12
 
13
13
  Bundler/OrderedGems:
14
14
  Enabled: false
15
15
 
16
+ Gemspec/RequiredRubyVersion:
17
+ Enabled: false
18
+
19
+ # ============================== Gemspec ======================
20
+ Gemspec/DeprecatedAttributeAssignment:
21
+ Enabled: true
22
+ Gemspec/RequireMFA: # new in 1.23
23
+ Enabled: true
24
+
25
+ # ============================== Performance ======================
26
+ Performance/AncestorsInclude:
27
+ Enabled: true
28
+ Performance/BigDecimalWithNumericArgument:
29
+ Enabled: true
30
+ Performance/RedundantSortBlock:
31
+ Enabled: true
32
+ Performance/RedundantStringChars:
33
+ Enabled: true
34
+ Performance/ReverseFirst:
35
+ Enabled: true
36
+ Performance/SortReverse:
37
+ Enabled: true
38
+ Performance/Squeeze:
39
+ Enabled: true
40
+ Performance/StringInclude:
41
+ Enabled: true
42
+ Performance/Sum:
43
+ Enabled: true
44
+ Performance/BlockGivenWithExplicitBlock:
45
+ Enabled: true
46
+ Performance/CollectionLiteralInLoop:
47
+ Enabled: true
48
+ Performance/ConstantRegexp:
49
+ Enabled: true
50
+ Performance/MethodObjectAsBlock:
51
+ Enabled: true
52
+ Performance/RedundantEqualityComparisonBlock:
53
+ Enabled: true
54
+ Performance/RedundantSplitRegexpArgument:
55
+ Enabled: true
56
+ Performance/MapCompact:
57
+ Enabled: true
58
+ Performance/ConcurrentMonotonicTime: # new in 1.12
59
+ Enabled: true
60
+ Performance/StringIdentifierArgument: # new in 1.13
61
+ Enabled: true
62
+
16
63
  # ============================== Documentation ======================
17
64
  Style/Documentation:
18
65
  Enabled: false
@@ -39,6 +86,10 @@ Naming/MethodParameterName:
39
86
  Enabled: false
40
87
  Naming/AccessorMethodName:
41
88
  Enabled: false
89
+ Naming/InclusiveLanguage:
90
+ Enabled: true
91
+ Naming/BlockForwarding: # new in 1.24
92
+ Enabled: true
42
93
 
43
94
  # ============================== Layout =============================
44
95
  Layout/HashAlignment:
@@ -73,6 +124,24 @@ Layout/SpaceAroundMethodCallOperator:
73
124
  Enabled: true
74
125
  Layout/LineLength:
75
126
  Max: 140
127
+ Layout/EmptyLinesAroundAttributeAccessor:
128
+ Enabled: true
129
+ Layout/BeginEndAlignment:
130
+ Enabled: true
131
+ Layout/SpaceBeforeBrackets:
132
+ Enabled: true
133
+ Layout/LineEndStringConcatenationIndentation:
134
+ Enabled: true
135
+ Layout/LineContinuationLeadingSpace: # new in 1.31
136
+ Enabled: true
137
+ Layout/LineContinuationSpacing: # new in 1.31
138
+ Enabled: true
139
+
140
+ # ============================== Security ==============================
141
+ Security/CompoundHash: # new in 1.28
142
+ Enabled: true
143
+ Security/IoMethods: # new in 1.22
144
+ Enabled: true
76
145
 
77
146
  # ============================== Style ==============================
78
147
  Style/RescueModifier:
@@ -127,7 +196,116 @@ Style/HashTransformKeys:
127
196
  Enabled: true
128
197
  Style/HashTransformValues:
129
198
  Enabled: true
130
-
199
+ Style/RedundantFetchBlock:
200
+ Enabled: true
201
+ Style/RedundantRegexpCharacterClass:
202
+ Enabled: true
203
+ Style/RedundantRegexpEscape:
204
+ Enabled: true
205
+ Style/SlicingWithRange:
206
+ Enabled: true
207
+ Style/AccessorGrouping:
208
+ Enabled: true
209
+ Style/ArrayCoercion:
210
+ Enabled: true
211
+ Style/BisectedAttrAccessor:
212
+ Enabled: true
213
+ Style/CaseLikeIf:
214
+ Enabled: true
215
+ Style/ExplicitBlockArgument:
216
+ Enabled: true
217
+ Style/GlobalStdStream:
218
+ Enabled: true
219
+ Style/HashAsLastArrayItem:
220
+ Enabled: true
221
+ Style/HashLikeCase:
222
+ Enabled: true
223
+ Style/OptionalBooleanParameter:
224
+ Enabled: true
225
+ Style/RedundantAssignment:
226
+ Enabled: true
227
+ Style/RedundantFileExtensionInRequire:
228
+ Enabled: true
229
+ Style/SingleArgumentDig:
230
+ Enabled: true
231
+ Style/StringConcatenation:
232
+ Enabled: true
233
+ Style/CombinableLoops:
234
+ Enabled: true
235
+ Style/KeywordParametersOrder:
236
+ Enabled: true
237
+ Style/RedundantSelfAssignment:
238
+ Enabled: true
239
+ Style/SoleNestedConditional:
240
+ Enabled: true
241
+ Style/ArgumentsForwarding:
242
+ Enabled: true
243
+ Style/CollectionCompact:
244
+ Enabled: true
245
+ Style/DocumentDynamicEvalDefinition:
246
+ Enabled: true
247
+ Style/EndlessMethod:
248
+ Enabled: true
249
+ Style/HashExcept:
250
+ Enabled: true
251
+ Style/IfWithBooleanLiteralBranches:
252
+ Enabled: true
253
+ Style/NegatedIfElseCondition:
254
+ Enabled: true
255
+ Style/NilLambda:
256
+ Enabled: true
257
+ Style/RedundantArgument:
258
+ Enabled: true
259
+ Style/SwapValues:
260
+ Enabled: true
261
+ Style/HashConversion:
262
+ Enabled: true
263
+ Style/StringChars:
264
+ Enabled: true
265
+ Style/InPatternThen:
266
+ Enabled: true
267
+ Style/MultilineInPatternThen:
268
+ Enabled: true
269
+ Style/QuotedSymbols:
270
+ Enabled: true
271
+ Style/EmptyHeredoc: # new in 1.32
272
+ Enabled: true
273
+ Style/EnvHome: # new in 1.29
274
+ Enabled: true
275
+ Style/FetchEnvVar: # new in 1.28
276
+ Enabled: true
277
+ Style/FileRead: # new in 1.24
278
+ Enabled: true
279
+ Style/FileWrite: # new in 1.24
280
+ Enabled: true
281
+ Style/MagicCommentFormat: # new in 1.35
282
+ Enabled: true
283
+ Style/MapCompactWithConditionalBlock: # new in 1.30
284
+ Enabled: true
285
+ Style/MapToHash: # new in 1.24
286
+ Enabled: true
287
+ Style/NestedFileDirname: # new in 1.26
288
+ Enabled: true
289
+ Style/NumberedParameters: # new in 1.22
290
+ Enabled: true
291
+ Style/NumberedParametersLimit: # new in 1.22
292
+ Enabled: true
293
+ Style/ObjectThen: # new in 1.28
294
+ Enabled: true
295
+ Style/OpenStructUse: # new in 1.23
296
+ Enabled: true
297
+ Style/OperatorMethodCall: # new in 1.37
298
+ Enabled: true
299
+ Style/RedundantEach: # new in 1.38
300
+ Enabled: true
301
+ Style/RedundantInitialize: # new in 1.27
302
+ Enabled: true
303
+ Style/RedundantSelfAssignmentBranch: # new in 1.19
304
+ Enabled: true
305
+ Style/RedundantStringEscape: # new in 1.37
306
+ Enabled: true
307
+ Style/SelectByRegexp: # new in 1.22
308
+ Enabled: true
131
309
  # ============================== Lint ==============================
132
310
  Lint/DuplicateMethods:
133
311
  Enabled: false
@@ -137,3 +315,95 @@ Lint/RaiseException:
137
315
  Enabled: true
138
316
  Lint/StructNewOverride:
139
317
  Enabled: true
318
+ Lint/DeprecatedOpenSSLConstant:
319
+ Enabled: true
320
+ Lint/MixedRegexpCaptureTypes:
321
+ Enabled: true
322
+ Lint/BinaryOperatorWithIdenticalOperands:
323
+ Enabled: true
324
+ Lint/DuplicateElsifCondition:
325
+ Enabled: true
326
+ Lint/DuplicateRescueException:
327
+ Enabled: true
328
+ Lint/EmptyConditionalBody:
329
+ Enabled: true
330
+ Lint/FloatComparison:
331
+ Enabled: true
332
+ Lint/MissingSuper:
333
+ Enabled: false
334
+ Lint/OutOfRangeRegexpRef:
335
+ Enabled: true
336
+ Lint/SelfAssignment:
337
+ Enabled: true
338
+ Lint/TopLevelReturnWithArgument:
339
+ Enabled: true
340
+ Lint/UnreachableLoop:
341
+ Enabled: true
342
+ Lint/ConstantDefinitionInBlock:
343
+ Enabled: true
344
+ Lint/DuplicateRequire:
345
+ Enabled: true
346
+ Lint/EmptyFile:
347
+ Enabled: true
348
+ Lint/IdentityComparison:
349
+ Enabled: true
350
+ Lint/TrailingCommaInAttributeDeclaration:
351
+ Enabled: true
352
+ Lint/UselessMethodDefinition:
353
+ Enabled: true
354
+ Lint/UselessTimes:
355
+ Enabled: true
356
+ Lint/AmbiguousAssignment:
357
+ Enabled: true
358
+ Lint/DeprecatedConstants:
359
+ Enabled: true
360
+ Lint/DuplicateBranch:
361
+ Enabled: true
362
+ Lint/DuplicateRegexpCharacterClassElement:
363
+ Enabled: true
364
+ Lint/EmptyBlock:
365
+ Enabled: true
366
+ Lint/EmptyClass:
367
+ Enabled: true
368
+ Lint/LambdaWithoutLiteralBlock:
369
+ Enabled: true
370
+ Lint/NoReturnInBeginEndBlocks:
371
+ Enabled: true
372
+ Lint/NumberedParameterAssignment:
373
+ Enabled: true
374
+ Lint/OrAssignmentToConstant:
375
+ Enabled: true
376
+ Lint/RedundantDirGlobSort:
377
+ Enabled: true
378
+ Lint/SymbolConversion:
379
+ Enabled: true
380
+ Lint/ToEnumArguments:
381
+ Enabled: true
382
+ Lint/TripleQuotes:
383
+ Enabled: true
384
+ Lint/UnexpectedBlockArity:
385
+ Enabled: true
386
+ Lint/UnmodifiedReduceAccumulator:
387
+ Enabled: true
388
+ Lint/EmptyInPattern:
389
+ Enabled: true
390
+ Lint/AmbiguousOperatorPrecedence: # new in 1.21
391
+ Enabled: true
392
+ Lint/AmbiguousRange: # new in 1.19
393
+ Enabled: true
394
+ Lint/ConstantOverwrittenInRescue: # new in 1.31
395
+ Enabled: true
396
+ Lint/DuplicateMagicComment: # new in 1.37
397
+ Enabled: true
398
+ Lint/IncompatibleIoSelectWithFiberScheduler: # new in 1.21
399
+ Enabled: true
400
+ Lint/NonAtomicFileOperation: # new in 1.31
401
+ Enabled: true
402
+ Lint/RefinementImportMethods: # new in 1.27
403
+ Enabled: true
404
+ Lint/RequireRangeParentheses: # new in 1.32
405
+ Enabled: true
406
+ Lint/RequireRelativeSelfPath: # new in 1.22
407
+ Enabled: true
408
+ Lint/UselessRuby2Keywords: # new in 1.23
409
+ Enabled: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.9
2
+ - [13](https://github.com/shlima/health_bit/pull/13) Pass env to check blocks or #call methods
3
+ - [11](https://github.com/shlima/health_bit/issues/11) Added custom formatter
data/Gemfile CHANGED
@@ -1,13 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  source 'https://rubygems.org'
4
- ruby '2.7.1'
5
4
 
6
5
  # Specify your gem's dependencies in health_bit.gemspec
7
6
  gemspec
8
7
 
9
8
  gem 'rake', '~> 13.0'
10
9
  gem 'rspec', '~> 3.0'
11
- gem 'pry'
12
- gem 'rubocop'
13
- gem 'rubocop-performance'
data/Gemfile.lock CHANGED
@@ -1,57 +1,64 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- health_bit (0.1.7)
4
+ health_bit (0.2.0)
5
5
  rack
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- ast (2.4.0)
11
- coderay (1.1.2)
12
- diff-lcs (1.3)
13
- jaro_winkler (1.5.4)
10
+ ast (2.4.2)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.5.0)
13
+ json (2.6.2)
14
14
  method_source (1.0.0)
15
- parallel (1.19.1)
16
- parser (2.7.1.2)
17
- ast (~> 2.4.0)
18
- pry (0.13.1)
15
+ parallel (1.22.1)
16
+ parser (3.1.2.1)
17
+ ast (~> 2.4.1)
18
+ pry (0.14.1)
19
19
  coderay (~> 1.1)
20
20
  method_source (~> 1.0)
21
- rack (2.2.2)
22
- rainbow (3.0.0)
23
- rake (13.0.1)
24
- rexml (3.2.4)
25
- rspec (3.9.0)
26
- rspec-core (~> 3.9.0)
27
- rspec-expectations (~> 3.9.0)
28
- rspec-mocks (~> 3.9.0)
29
- rspec-core (3.9.2)
30
- rspec-support (~> 3.9.3)
31
- rspec-expectations (3.9.1)
21
+ rack (3.0.0)
22
+ rainbow (3.1.1)
23
+ rake (13.0.6)
24
+ regexp_parser (2.6.0)
25
+ rexml (3.2.5)
26
+ rspec (3.12.0)
27
+ rspec-core (~> 3.12.0)
28
+ rspec-expectations (~> 3.12.0)
29
+ rspec-mocks (~> 3.12.0)
30
+ rspec-core (3.12.0)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-expectations (3.12.0)
32
33
  diff-lcs (>= 1.2.0, < 2.0)
33
- rspec-support (~> 3.9.0)
34
- rspec-mocks (3.9.1)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-mocks (3.12.0)
35
36
  diff-lcs (>= 1.2.0, < 2.0)
36
- rspec-support (~> 3.9.0)
37
- rspec-support (3.9.3)
38
- rubocop (0.82.0)
39
- jaro_winkler (~> 1.5.1)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-support (3.12.0)
39
+ rubocop (1.38.0)
40
+ json (~> 2.3)
40
41
  parallel (~> 1.10)
41
- parser (>= 2.7.0.1)
42
+ parser (>= 3.1.2.1)
42
43
  rainbow (>= 2.2.2, < 4.0)
43
- rexml
44
+ regexp_parser (>= 1.8, < 3.0)
45
+ rexml (>= 3.2.5, < 4.0)
46
+ rubocop-ast (>= 1.23.0, < 2.0)
44
47
  ruby-progressbar (~> 1.7)
45
- unicode-display_width (>= 1.4.0, < 2.0)
46
- rubocop-performance (1.5.2)
47
- rubocop (>= 0.71.0)
48
- ruby-progressbar (1.10.1)
49
- unicode-display_width (1.7.0)
48
+ unicode-display_width (>= 1.4.0, < 3.0)
49
+ rubocop-ast (1.23.0)
50
+ parser (>= 3.1.1.0)
51
+ rubocop-performance (1.15.0)
52
+ rubocop (>= 1.7.0, < 2.0)
53
+ rubocop-ast (>= 0.4.0)
54
+ ruby-progressbar (1.11.0)
55
+ unicode-display_width (2.3.0)
50
56
 
51
57
  PLATFORMS
52
58
  ruby
53
59
 
54
60
  DEPENDENCIES
61
+ bundler
55
62
  health_bit!
56
63
  pry
57
64
  rake (~> 13.0)
@@ -59,8 +66,5 @@ DEPENDENCIES
59
66
  rubocop
60
67
  rubocop-performance
61
68
 
62
- RUBY VERSION
63
- ruby 2.7.1p83
64
-
65
69
  BUNDLED WITH
66
- 2.1.4
70
+ 2.2.15
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
- [![pipeline status](https://travis-ci.com/shlima/health_bit.svg?branch=master)](https://travis-ci.com/shlima/health_bit)
1
+ ![CI](https://github.com/shlima/health_bit/workflows/CI/badge.svg)
2
2
  [![gem version](https://badge.fury.io/rb/health_bit.svg)](https://rubygems.org/gems/health_bit)
3
3
 
4
4
  # HealthBit
5
5
 
6
6
  ![](./doc/logo.png?sanitize=true)
7
7
 
8
- This gem was inspired by the [health_check](https://github.com/ianheggie/health_check), but is simpler and more
8
+ This gem was inspired by the [health_check](https://github.com/ianheggie/health_check), but is simpler and more
9
9
  extensible and contains up to 95% less code.
10
10
 
11
11
  Key differences:
@@ -22,14 +22,22 @@ Key differences:
22
22
  * [Add Checks](#add-checks)
23
23
  * [Add a Route](#add-a-route)
24
24
  * [Password Protection](#password-protection)
25
+ * [Multiple endpoints](#multiple-endpoints)
26
+ * [Custom formatter](#custom-formatter)
27
+
28
+ ## Check Examples
29
+
25
30
  * [Database check](#database-check)
26
31
  * [Redis check](#redis-check)
32
+ * [Sidekiq check](#sidekiq-check)
27
33
  * [Rails cache check](#rails-cache-check)
28
34
  * [Elasticsearch check](#elasticsearch-check)
29
- * [Multiple endpoints](#multiple-endpoints)
35
+ * [RabbitMQ check](#rabbitmq-check)
36
+ * [HTTP check](#http-check)
37
+ * [ClickHouse check](#clickhouse-check)
30
38
 
31
39
  ## Installation
32
-
40
+
33
41
  Add this line to your application's Gemfile:
34
42
 
35
43
  ```ruby
@@ -40,56 +48,62 @@ gem 'health_bit'
40
48
 
41
49
  ```ruby
42
50
  # config/initializers/health_bit.rb
43
- # DEFAULT SETTINGS ARE SHOWN BELOW
51
+
44
52
  HealthBit.configure do |c|
53
+ # DEFAULT SETTINGS ARE SHOWN BELOW
45
54
  c.success_text = '%<count>d checks passed 🎉'
46
- c.headers = {
47
- 'Content-Type' => 'text/plain;charset=utf-8',
48
- 'Cache-Control' => 'private,max-age=0,must-revalidate,no-store'
55
+ c.headers = {
56
+ 'Content-Type' => 'text/plain;charset=utf-8',
57
+ 'Cache-Control' => 'private,max-age=0,must-revalidate,no-store'
49
58
  }
50
59
  c.success_code = 200
51
60
  c.fail_code = 500
52
61
  c.show_backtrace = false
62
+ c.formatter = HealthBit::Formatter.new
63
+ # DEFAULT SETTINGS ARE SHOWN ABOVE
53
64
 
54
65
  c.add('Check name') do
55
- # Body check, should returns `true`
66
+ # Body check, should returns `true`
56
67
  true
57
68
  end
58
69
  end
59
70
  ```
60
-
71
+
61
72
  ## Add Checks
62
73
 
63
- By default, the **gem does not contain any checks**, **you should add the
64
- necessary checks by yourself**. The check should return `false` or `nil`
65
- to be considered unsuccessful or throw an exception, any other
74
+ By default, the **gem does not contain any checks**, **you should add the
75
+ necessary checks by yourself**. The check should return `false` or `nil`
76
+ to be considered unsuccessful or throw an exception, any other
66
77
  values are considered satisfactory.
67
78
 
68
79
  Example checks:
69
80
 
70
81
  ```ruby
71
82
  ## Successful checks
72
- HealthBit.add('PostgreSQL') do
83
+ HealthBit.add('PostgreSQL') do |env|
73
84
  ApplicationRecord.connection.select_value('SELECT 1') == 1
74
85
  end
75
86
 
76
- HealthBit.add('Custom') do
87
+ HealthBit.add('Custom') do |env|
88
+ next(false) if 1 != 0
89
+
77
90
  true
78
91
  end
79
92
 
80
93
  ## Failed checks
81
- HealthBit.add('Database') do
94
+ HealthBit.add('Database') do |env|
82
95
  false
83
96
  end
84
97
 
85
- HealthBit.add('Docker service') do
98
+ HealthBit.add('Docker service') do |env|
86
99
  raise 'not responding'
87
100
  end
88
101
 
89
102
  # The Check can be added as an object responding to a call
90
- class Covid19Check
91
- def self.call
92
- true
103
+ # (to be able to test your check)
104
+ class Covid19Check
105
+ def self.call(env)
106
+ false
93
107
  end
94
108
  end
95
109
 
@@ -98,7 +112,7 @@ HealthBit.add('COVID-19 Checker', Covid19Check)
98
112
 
99
113
  ## Add a Route
100
114
 
101
- Since the gem is a rack application, you must mount it to app's
115
+ Since the gem is a rack application, you must mount it to app's
102
116
  routes. Below is an example for the Rails.
103
117
 
104
118
  ```ruby
@@ -109,19 +123,33 @@ Rails.application.routes.draw do
109
123
  end
110
124
  ```
111
125
 
126
+ ```bash
127
+ curl --verbose http://localhost:3000/health
128
+
129
+ < HTTP/1.1 200 OK
130
+ < Content-Type: text/plain;charset=utf-8
131
+ < Cache-Control: private,max-age=0,must-revalidate,no-store
132
+ < X-Request-Id: 59a796b9-29f7-4302-b1ff-5d0b06dd6637
133
+ < X-Runtime: 0.006007
134
+ < Vary: Origin
135
+ < Transfer-Encoding: chunked
136
+
137
+ 4 checks passed 🎉
138
+ ```
139
+
112
140
  ## Password Protection
113
141
 
114
142
  Since the gem is a common rack application, you can add any rack
115
- middleware. Below is an example with HTTP-auth for the Rails.
143
+ middleware to it. Below is an example with HTTP-auth for the Rails.
116
144
 
117
145
  ```ruby
118
146
  # config/routes.rb
119
147
 
120
148
  Rails.application.routes.draw do
121
149
  HealthBit.rack.use Rack::Auth::Basic do |username, password|
122
- ActiveSupport::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(username), ::Digest::SHA256.hexdigest('user')) & ActiveSupport::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(password), ::Digest::SHA256.hexdigest('password'))
150
+ ActiveSupport::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(username), Digest::SHA256.hexdigest('user')) & ActiveSupport::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(password), Digest::SHA256.hexdigest('password'))
123
151
  end
124
-
152
+
125
153
  mount HealthBit.rack => '/health'
126
154
  end
127
155
  ```
@@ -129,7 +157,7 @@ end
129
157
  ## Database check
130
158
 
131
159
  ```ruby
132
- HealthBit.add('Database') do
160
+ HealthBit.add('Database') do |env|
133
161
  ApplicationRecord.connection.select_value('SELECT 1') == 1
134
162
  end
135
163
  ```
@@ -137,32 +165,64 @@ end
137
165
  ## Redis check
138
166
 
139
167
  ```ruby
140
- HealthBit.add('Redis') do
141
- Redis.current.ping == 'PONG'
168
+ HealthBit.add('Redis') do |env|
169
+ Redis.current.ping == 'PONG'
170
+ end
171
+ ```
172
+
173
+ ## Sidekiq check
174
+
175
+ ```ruby
176
+ HealthBit.add('Sidekiq') do |env|
177
+ Sidekiq.redis(&:ping) == 'PONG'
142
178
  end
143
179
  ```
144
180
 
145
181
  ## Rails cache check
146
182
 
147
183
  ```ruby
148
- HealthBit.add('Rails cache') do
149
- Rails.cache.read('1').nil?
184
+ HealthBit.add('Rails cache') do |env|
185
+ Rails.cache.write('__health_bit__', '1', expires_in: 1.second)
150
186
  end
151
187
  ```
152
188
 
153
189
  ## Elasticsearch check
154
190
 
155
191
  ```ruby
156
- HealthBit.add('Elasticsearch') do
192
+ HealthBit.add('Elasticsearch') do |env|
157
193
  Elasticsearch::Client.new.ping
158
194
  end
159
195
  ```
160
196
 
197
+ ## RabbitMQ check
198
+
199
+ ```ruby
200
+ HealthBit.add('RabbitMQ') do |env|
201
+ Bunny::Connection.connect(&:connection)
202
+ end
203
+ ```
204
+
205
+ ## HTTP check
206
+
207
+ ```ruby
208
+ HealthBit.add('HTTP check') do |env|
209
+ Net::HTTP.new('www.example.com', 80).request_get('/').kind_of?(Net::HTTPSuccess)
210
+ end
211
+ ```
212
+
213
+ ## ClickHouse check
214
+
215
+ ```ruby
216
+ HealthBit.add('ClickHouse') do |env|
217
+ ClickHouse.connection.ping
218
+ end
219
+ ```
220
+
161
221
  ## Multiple endpoints
162
222
 
163
- Sometimes you have to add several health check endpoints. Let's say
164
- you have to check the docker container health and the health
165
- of your application as a whole. Below is an example for rails.
223
+ Sometimes you have to add several health check endpoints. Let's say
224
+ you have to check the docker container health and the health
225
+ of your application as a whole. Below is an example for the Rails.
166
226
 
167
227
  ```ruby
168
228
  # config/initializers/health_bit.rb
@@ -170,11 +230,11 @@ of your application as a whole. Below is an example for rails.
170
230
  DockerCheck = HealthBit.clone
171
231
  AppCheck = HealthBit.clone
172
232
 
173
- DockerCheck.add('Docker Health') do
233
+ DockerCheck.add('Docker Health') do |env|
174
234
  true
175
235
  end
176
236
 
177
- AppCheck.add('App Health') do
237
+ AppCheck.add('App Health') do |env|
178
238
  ApplicationRecord.connection.select_value("SELECT 't'::boolean")
179
239
  end
180
240
  ```
@@ -187,3 +247,32 @@ Rails.application.routes.draw do
187
247
  mount AppCheck.rack => '/app'
188
248
  end
189
249
  ```
250
+
251
+ ## Custom formatter
252
+
253
+ You can easily [configure](https://github.com/shlima/health_bit/blob/master/lib/health_bit/formatter.rb) custom format of response body, headers and
254
+ http statuses.
255
+
256
+ ```ruby
257
+ class JsonFormatter < HealthBit::Formatter
258
+ # @param error HealthBit::CheckError
259
+ # @param env Hash
260
+ # @param health_bit HealthBit
261
+ def format_failure(error, env, health_bit)
262
+ { 'status': 'error' }.to_json
263
+ end
264
+
265
+ # @param error HealthBit::CheckError
266
+ # @param env Hash
267
+ # @param health_bit HealthBit
268
+ def headers_failure(error, env, health_bit)
269
+ {
270
+ 'Content-Type' => 'application/json'
271
+ }
272
+ end
273
+ end
274
+
275
+ HealthBit.configure do |c|
276
+ c.formatter = JsonFormatter.new
277
+ end
278
+ ```
data/health_bit.gemspec CHANGED
@@ -13,7 +13,8 @@ Gem::Specification.new do |spec|
13
13
  spec.description = 'Tiny health check of Rack apps like Rails, Sinatra for use with uptime checking systems like Kubernetes, Docker or Uptimerobot'
14
14
  spec.homepage = 'https://github.com/shlima/health_bit'
15
15
  spec.license = 'MIT'
16
- spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
17
+ spec.metadata['rubygems_mfa_required'] = 'true'
17
18
  spec.metadata['homepage_uri'] = spec.homepage
18
19
  spec.metadata['source_code_uri'] = spec.homepage
19
20
  spec.metadata['changelog_uri'] = 'https://github.com/shlima/health_bit/releases'
@@ -25,5 +26,11 @@ Gem::Specification.new do |spec|
25
26
  spec.require_paths = %w[lib]
26
27
 
27
28
  spec.add_dependency 'rack'
29
+ spec.add_development_dependency 'bundler'
30
+ spec.add_development_dependency 'pry'
31
+ spec.add_development_dependency 'rake'
32
+ spec.add_development_dependency 'rspec'
33
+ spec.add_development_dependency 'rubocop'
34
+ spec.add_development_dependency 'rubocop-performance'
28
35
  end
29
36
  # rubocop:enable Layout/LineLength
@@ -11,8 +11,11 @@ module HealthBit
11
11
 
12
12
  # @return [nil] if its ok
13
13
  # @return [CheckError] if not
14
- def call
15
- raise('The check has returned a negative value') unless handler.call
14
+ def call(env = {})
15
+ arity = handler.is_a?(Proc) ? handler.arity : handler.method(:call).arity
16
+ return if arity.abs == 1 ? handler.call(env) : handler.call
17
+
18
+ raise('The check has returned a negative value')
16
19
  rescue Exception => e # rubocop:disable Lint/RescueException
17
20
  CheckError.new(name, exception: e)
18
21
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rack'
4
+ require 'health_bit/version'
5
+
6
+ module HealthBit
7
+ # rubocop:disable Lint/UnusedMethodArgument
8
+ class Formatter
9
+ # @param error HealthBit::CheckError
10
+ # @param env Hash
11
+ # @param health_bit HealthBit
12
+ def format_success(error, env, health_bit)
13
+ health_bit.success_text
14
+ end
15
+
16
+ # @param error HealthBit::CheckError
17
+ # @param env Hash
18
+ # @param health_bit HealthBit
19
+ def format_failure(error, env, health_bit)
20
+ format = health_bit.show_backtrace ? CheckError::FORMAT_FULL : CheckError::FORMAT_SHORT
21
+ error.to_s(format)
22
+ end
23
+
24
+ # @param env Hash
25
+ # @param health_bit HealthBit
26
+ def headers_success(env, health_bit)
27
+ health_bit.headers
28
+ end
29
+
30
+ # @param error HealthBit::CheckError
31
+ # @param env Hash
32
+ # @param health_bit HealthBit
33
+ def headers_failure(error, env, health_bit)
34
+ health_bit.headers
35
+ end
36
+
37
+ # @param env Hash
38
+ # @param health_bit HealthBit
39
+ def code_success(env, health_bit)
40
+ health_bit.success_code
41
+ end
42
+
43
+ # @param error HealthBit::CheckError
44
+ # @param env Hash
45
+ # @param health_bit HealthBit
46
+ def code_failure(error, env, health_bit)
47
+ health_bit.fail_code
48
+ end
49
+ end
50
+ # rubocop:enable Lint/UnusedMethodArgument
51
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HealthBit
4
- VERSION = '0.1.7'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/health_bit.rb CHANGED
@@ -4,6 +4,10 @@ require 'rack'
4
4
  require 'health_bit/version'
5
5
 
6
6
  module HealthBit
7
+ autoload :Check, 'health_bit/check'
8
+ autoload :CheckError, 'health_bit/check_error'
9
+ autoload :Formatter, 'health_bit/formatter'
10
+
7
11
  DEFAULT_SUCCESS_TEXT = '%<count>d checks passed 🎉'
8
12
  DEFAULT_HEADERS = {
9
13
  'Content-Type' => 'text/plain;charset=utf-8',
@@ -11,13 +15,11 @@ module HealthBit
11
15
  }.freeze
12
16
  DEFAULT_SUCCESS_CODE = 200
13
17
  DEFAULT_FAIL_CODE = 500
14
-
15
- autoload :Check, 'health_bit/check'
16
- autoload :CheckError, 'health_bit/check_error'
18
+ DEFAULT_FORMATTER = Formatter.new
17
19
 
18
20
  extend self # rubocop:disable Style/ModuleFunction
19
21
 
20
- attr_writer :success_text, :success_code, :fail_code, :headers
22
+ attr_writer :success_text, :success_code, :fail_code, :headers, :formatter
21
23
  attr_accessor :show_backtrace
22
24
 
23
25
  def success_text
@@ -36,6 +38,11 @@ module HealthBit
36
38
  (@headers || DEFAULT_HEADERS).dup
37
39
  end
38
40
 
41
+ # @return [Formatter]
42
+ def formatter
43
+ @formatter || DEFAULT_FORMATTER
44
+ end
45
+
39
46
  def checks
40
47
  @checks ||= []
41
48
  end
@@ -60,27 +67,37 @@ module HealthBit
60
67
  end
61
68
 
62
69
  # @return [nil, CheckError]
63
- def check
70
+ def check(env)
64
71
  checks.each do |check|
65
- (exception = check.call).nil? ? next : (return exception)
72
+ (exception = check.call(env)).nil? ? next : (return exception)
66
73
  end
67
74
 
68
75
  nil
69
76
  end
70
77
 
71
78
  def rack(this = self)
72
- @rack ||= begin
73
- format = this.show_backtrace ? CheckError::FORMAT_FULL : CheckError::FORMAT_SHORT
74
-
75
- Rack::Builder.new do
76
- run ->(_env) do
77
- if (error = this.check)
78
- [this.fail_code, this.headers, [error.to_s(format)]]
79
- else
80
- [this.success_code, this.headers, [this.success_text]]
81
- end
79
+ @rack ||= Rack::Builder.new do
80
+ run ->(env) do
81
+ if (error = this.check(env))
82
+ [
83
+ this.formatter.code_failure(error, env, this),
84
+ this.formatter.headers_failure(error, env, this),
85
+ [this.formatter.format_failure(error, env, this)]
86
+ ]
87
+ else
88
+ [
89
+ this.formatter.code_success(env, this),
90
+ this.formatter.headers_success(env, this),
91
+ [this.formatter.format_success(error, env, this)]
92
+ ]
82
93
  end
83
94
  end
84
95
  end
85
96
  end
97
+
98
+ def clone
99
+ Module.new.tap do |dolly|
100
+ dolly.singleton_class.include(HealthBit)
101
+ end
102
+ end
86
103
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: health_bit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aliaksandr Shylau
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-07 00:00:00.000000000 Z
11
+ date: 2022-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -24,6 +24,90 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-performance
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
27
111
  description: Tiny health check of Rack apps like Rails, Sinatra for use with uptime
28
112
  checking systems like Kubernetes, Docker or Uptimerobot
29
113
  email:
@@ -32,10 +116,11 @@ executables: []
32
116
  extensions: []
33
117
  extra_rdoc_files: []
34
118
  files:
119
+ - ".github/workflows/main.yml"
35
120
  - ".gitignore"
36
121
  - ".rspec"
37
122
  - ".rubocop.yml"
38
- - ".travis.yml"
123
+ - CHANGELOG.md
39
124
  - Gemfile
40
125
  - Gemfile.lock
41
126
  - LICENSE.txt
@@ -49,15 +134,17 @@ files:
49
134
  - lib/health_bit.rb
50
135
  - lib/health_bit/check.rb
51
136
  - lib/health_bit/check_error.rb
137
+ - lib/health_bit/formatter.rb
52
138
  - lib/health_bit/version.rb
53
139
  homepage: https://github.com/shlima/health_bit
54
140
  licenses:
55
141
  - MIT
56
142
  metadata:
143
+ rubygems_mfa_required: 'true'
57
144
  homepage_uri: https://github.com/shlima/health_bit
58
145
  source_code_uri: https://github.com/shlima/health_bit
59
146
  changelog_uri: https://github.com/shlima/health_bit/releases
60
- post_install_message:
147
+ post_install_message:
61
148
  rdoc_options: []
62
149
  require_paths:
63
150
  - lib
@@ -65,15 +152,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
152
  requirements:
66
153
  - - ">="
67
154
  - !ruby/object:Gem::Version
68
- version: 2.3.0
155
+ version: 2.6.0
69
156
  required_rubygems_version: !ruby/object:Gem::Requirement
70
157
  requirements:
71
158
  - - ">="
72
159
  - !ruby/object:Gem::Version
73
160
  version: '0'
74
161
  requirements: []
75
- rubygems_version: 3.1.2
76
- signing_key:
162
+ rubygems_version: 3.3.7
163
+ signing_key:
77
164
  specification_version: 4
78
165
  summary: Tiny health check of Rack apps like Rails, Sinatra
79
166
  test_files: []
data/.travis.yml DELETED
@@ -1,14 +0,0 @@
1
- language: ruby
2
-
3
- cache: bundler
4
-
5
- rvm:
6
- - 2.7.1
7
-
8
- before_install:
9
- - gem install bundler -v 2.1.4
10
- - bundle install
11
-
12
- script:
13
- - bundle exec rubocop
14
- - bundle exec rspec