Skip to content

Commit 883ab17

Browse files
committed
[health] fix lint issues
1 parent 6a4de21 commit 883ab17

File tree

4 files changed

+42
-34
lines changed

4 files changed

+42
-34
lines changed

pkg/health/health.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
type health struct {
99
}
1010

11-
func NewHealth() *health {
11+
func newHealth() *health {
1212
return &health{}
1313
}
1414

@@ -18,48 +18,50 @@ func (h *health) Name() string {
1818
}
1919

2020
// LiveProbe implements HealthProvider.
21-
func (h *health) LiveProbe(ctx context.Context) (Checks, error) {
22-
const oneGiB uint64 = 1 << 30
21+
func (h *health) LiveProbe(_ context.Context) (Checks, error) {
22+
const oneMiB uint64 = 1 << 20
23+
const memoryThreshold uint64 = 128 * oneMiB
24+
const goroutineThreshold = 100
2325

2426
var m runtime.MemStats
2527
runtime.ReadMemStats(&m)
2628

2729
// Basic runtime health checks
2830
goroutineCheck := CheckDetail{
2931
Description: "Number of goroutines",
30-
ObservedValue: int(runtime.NumGoroutine()),
32+
ObservedValue: runtime.NumGoroutine(),
3133
ObservedUnit: "goroutines",
3234
Status: StatusPass,
3335
}
3436

3537
memoryCheck := CheckDetail{
3638
Description: "Memory usage",
37-
ObservedValue: int(m.Alloc / 1024 / 1024), // MiB
39+
ObservedValue: int(m.Alloc / oneMiB), //nolint:gosec // not a security issue
3840
ObservedUnit: "MiB",
3941
Status: StatusPass,
4042
}
4143

4244
// Check for potential memory issues
43-
if m.Alloc > oneGiB { // 1GB
45+
if m.Alloc > memoryThreshold {
4446
memoryCheck.Status = StatusWarn
4547
}
4648

4749
// Check for excessive goroutines
48-
if goroutineCheck.ObservedValue > 1000 {
50+
if goroutineCheck.ObservedValue > goroutineThreshold {
4951
goroutineCheck.Status = StatusWarn
5052
}
5153

5254
return Checks{"goroutines": goroutineCheck, "memory": memoryCheck}, nil
5355
}
5456

5557
// ReadyProbe implements HealthProvider.
56-
func (h *health) ReadyProbe(ctx context.Context) (Checks, error) {
57-
return nil, nil
58+
func (h *health) ReadyProbe(_ context.Context) (Checks, error) {
59+
return nil, nil //nolint:nilnil // empty result
5860
}
5961

6062
// StartedProbe implements HealthProvider.
61-
func (h *health) StartedProbe(ctx context.Context) (Checks, error) {
62-
return nil, nil
63+
func (h *health) StartedProbe(_ context.Context) (Checks, error) {
64+
return nil, nil //nolint:nilnil // empty result
6365
}
6466

65-
var _ HealthProvider = (*health)(nil)
67+
var _ Provider = (*health)(nil)

pkg/health/module.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func Module() fx.Option {
1010
"health",
1111
logger.WithNamedLogger("health"),
1212
fx.Provide(
13-
AsHealthProvider(NewHealth),
13+
AsHealthProvider(newHealth),
1414
fx.Private,
1515
),
1616
fx.Provide(
@@ -22,7 +22,7 @@ func Module() fx.Option {
2222
func AsHealthProvider(f any) any {
2323
return fx.Annotate(
2424
f,
25-
fx.As(new(HealthProvider)),
25+
fx.As(new(Provider)),
2626
fx.ResultTags(`group:"health-providers"`),
2727
)
2828
}

pkg/health/service.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ import (
77
)
88

99
type Service struct {
10-
providers []HealthProvider
10+
providers []Provider
1111

1212
logger *zap.Logger
1313
}
1414

15-
func NewService(providers []HealthProvider, logger *zap.Logger) *Service {
15+
func NewService(providers []Provider, logger *zap.Logger) *Service {
1616
return &Service{
1717
providers: providers,
1818

1919
logger: logger,
2020
}
2121
}
2222

23-
func (s *Service) checkProvider(ctx context.Context, probe func(context.Context, HealthProvider) (Checks, error)) CheckResult {
23+
func (s *Service) checkProvider(
24+
ctx context.Context,
25+
probe func(context.Context, Provider) (Checks, error),
26+
) CheckResult {
2427
check := CheckResult{
2528
Checks: map[string]CheckDetail{},
2629
}
@@ -34,7 +37,7 @@ func (s *Service) checkProvider(ctx context.Context, probe func(context.Context,
3437

3538
healthChecks, err := probe(ctx, p)
3639
if err != nil {
37-
s.logger.Error("Failed check", zap.String("provider", p.Name()), zap.Error(err))
40+
s.logger.Error("failed check", zap.String("provider", p.Name()), zap.Error(err))
3841
check.Checks[p.Name()] = CheckDetail{
3942
Description: "Failed check",
4043
ObservedUnit: "",
@@ -57,19 +60,19 @@ func (s *Service) checkProvider(ctx context.Context, probe func(context.Context,
5760
}
5861

5962
func (s *Service) CheckReadiness(ctx context.Context) CheckResult {
60-
return s.checkProvider(ctx, func(ctx context.Context, p HealthProvider) (Checks, error) {
63+
return s.checkProvider(ctx, func(ctx context.Context, p Provider) (Checks, error) {
6164
return p.ReadyProbe(ctx)
6265
})
6366
}
6467

6568
func (s *Service) CheckLiveness(ctx context.Context) CheckResult {
66-
return s.checkProvider(ctx, func(ctx context.Context, p HealthProvider) (Checks, error) {
69+
return s.checkProvider(ctx, func(ctx context.Context, p Provider) (Checks, error) {
6770
return p.LiveProbe(ctx)
6871
})
6972
}
7073

7174
func (s *Service) CheckStartup(ctx context.Context) CheckResult {
72-
return s.checkProvider(ctx, func(ctx context.Context, p HealthProvider) (Checks, error) {
75+
return s.checkProvider(ctx, func(ctx context.Context, p Provider) (Checks, error) {
7376
return p.StartedProbe(ctx)
7477
})
7578
}

pkg/health/types.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,13 @@ const (
1717
levelFail statusLevel = 2
1818
)
1919

20-
var statusLevels = map[statusLevel]Status{
21-
levelPass: StatusPass,
22-
levelWarn: StatusWarn,
23-
levelFail: StatusFail,
24-
}
25-
26-
// Health status of the application.
20+
// CheckResult represents the result of a set of health checks.
2721
type CheckResult struct {
2822
// A map of check names to their respective details.
29-
Checks Checks
23+
Checks Checks `json:"checks"`
3024
}
3125

32-
// Overall status of the application.
26+
// Status returns the overall status of the application.
3327
// It can be one of the following values: "pass", "warn", or "fail".
3428
func (c CheckResult) Status() Status {
3529
// Determine overall status
@@ -44,10 +38,19 @@ func (c CheckResult) Status() Status {
4438
}
4539
}
4640

47-
return statusLevels[level]
41+
switch level {
42+
case levelPass:
43+
return StatusPass
44+
case levelWarn:
45+
return StatusWarn
46+
case levelFail:
47+
return StatusFail
48+
}
49+
50+
return StatusFail
4851
}
4952

50-
// Details of a health check.
53+
// CheckDetail of a health check.
5154
type CheckDetail struct {
5255
// A human-readable description of the check.
5356
Description string
@@ -60,10 +63,10 @@ type CheckDetail struct {
6063
Status Status
6164
}
6265

63-
// Map of check names to their respective details.
66+
// Checks is a map of check names to their respective details.
6467
type Checks map[string]CheckDetail
6568

66-
type HealthProvider interface {
69+
type Provider interface {
6770
Name() string
6871

6972
StartedProbe(ctx context.Context) (Checks, error)

0 commit comments

Comments
 (0)