Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to optionally opt-in on System metrics #82

Merged
merged 3 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _examples/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func main() {
h, _ := health.New()
h, _ := health.New(health.WithSystemInfo())
// custom health check example (fail)
h.Register(health.Config{
Name: "some-custom-check-fail",
Expand Down
19 changes: 13 additions & 6 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type (
// Failures holds the failed checks along with their messages.
Failures map[string]string `json:"failures,omitempty"`
// System holds information of the go process.
System `json:"system"`
*System `json:"system,omitempty"`
// Component holds information on the component for which checks are made
Component `json:"component"`
}
Expand Down Expand Up @@ -88,6 +88,8 @@ type (
instrumentationName string

component Component

systemInfoEnabled bool
}
)

Expand Down Expand Up @@ -232,24 +234,29 @@ func (h *Health) Measure(ctx context.Context) Check {
wg.Wait()
span.SetAttributes(attribute.String("status", string(status)))

return newCheck(h.component, status, failures)
var systemMetrics *System
if h.systemInfoEnabled {
systemMetrics = newSystemMetrics()
}

return newCheck(h.component, status, systemMetrics, failures)
}

func newCheck(c Component, s Status, failures map[string]string) Check {
func newCheck(c Component, s Status, system *System, failures map[string]string) Check {
return Check{
Status: s,
Timestamp: time.Now(),
Failures: failures,
System: newSystemMetrics(),
System: system,
Component: c,
}
}

func newSystemMetrics() System {
func newSystemMetrics() *System {
s := runtime.MemStats{}
runtime.ReadMemStats(&s)

return System{
return &System{
Version: runtime.Version(),
GoroutinesCount: runtime.NumGoroutine(),
TotalAllocBytes: int(s.TotalAlloc),
Expand Down
7 changes: 7 additions & 0 deletions health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ func TestHealth_Measure(t *testing.T) {
assert.Equal(t, StatusUnavailable, result.Status)
assert.Equal(t, string(StatusTimeout), result.Failures["check1"])
assert.Equal(t, string(StatusTimeout), result.Failures["check2"])
assert.Nil(t, result.System)

h, err = New(WithSystemInfo())
require.NoError(t, err)
result = h.Measure(context.Background())

assert.NotNil(t, result.System)
}
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ func WithMaxConcurrent(n int) Option {
return nil
}
}

// WithSystemInfo enables the option to return system information about the go process.
func WithSystemInfo() Option {
return func(h *Health) error {
h.systemInfoEnabled = true
return nil
}
}
10 changes: 10 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ func TestWithMaxConcurrent(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 13, h2.maxConcurrent)
}

func TestWithSystemInfo(t *testing.T) {
h1, err := New()
require.NoError(t, err)
assert.False(t, h1.systemInfoEnabled)

h2, err := New(WithSystemInfo())
require.NoError(t, err)
assert.True(t, h2.systemInfoEnabled)
}