Skip to content

Commit

Permalink
Add support to optionally opt-in on System metrics (#82)
Browse files Browse the repository at this point in the history
Closes #79

Co-authored-by: Lucas Medeiros <[email protected]>
  • Loading branch information
Eli Flores and lucasmdrs authored Oct 19, 2022
1 parent 437d9c7 commit a1fcc22
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
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)
}

0 comments on commit a1fcc22

Please sign in to comment.