forked from palantir/witchcraft-go-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.go
123 lines (104 loc) · 4.19 KB
/
reporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2018 Palantir Technologies. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package reporter
import (
"context"
"regexp"
"sync"
werror "github.com/palantir/witchcraft-go-error"
"github.com/palantir/witchcraft-go-server/conjure/witchcraft/api/health"
"github.com/palantir/witchcraft-go-server/status"
)
const slsHealthNameRegex = "^[A-Z_]+$"
var _ HealthReporter = &healthReporter{}
type HealthReporter interface {
status.HealthCheckSource
InitializeHealthComponent(name string) (HealthComponent, error)
GetHealthComponent(name string) (HealthComponent, bool)
UnregisterHealthComponent(name string) bool
}
type healthReporter struct {
mutex sync.RWMutex
healthComponents map[health.CheckType]HealthComponent
}
// NewHealthReporter - creates a new HealthReporter; an implementation of status.HealthCheckSource
// which initializes HealthComponents to report on the health of each individual health.CheckType.
func NewHealthReporter() HealthReporter {
return newHealthReporter()
}
func newHealthReporter() *healthReporter {
return &healthReporter{
healthComponents: make(map[health.CheckType]HealthComponent),
}
}
// InitializeHealthComponent - Creates a health component for the given name where the component should be stated as
// initializing until a future call modifies the initializing status. The created health component is stored in the
// HealthReporter and can be fetched later by name via GetHealthComponent.
// Returns ErrorState if the component name is non-SLS compliant, or the name is already in use
func (r *healthReporter) InitializeHealthComponent(name string) (HealthComponent, error) {
isSLSCompliant := regexp.MustCompile(slsHealthNameRegex).MatchString
if !isSLSCompliant(name) {
return nil, werror.Error("component name is not a valid SLS health component name",
werror.SafeParam("name", name),
werror.SafeParam("validPattern", slsHealthNameRegex))
}
componentName := health.CheckType(name)
healthComponent := &healthComponent{
name: componentName,
state: StartingState,
}
r.mutex.Lock()
defer r.mutex.Unlock()
if _, ok := r.healthComponents[componentName]; ok {
return nil, werror.Error("Health component name already exists", werror.SafeParam("name", name))
}
r.healthComponents[componentName] = healthComponent
return healthComponent, nil
}
// GetHealthComponent - Gets an initialized health component by name.
func (r *healthReporter) GetHealthComponent(name string) (HealthComponent, bool) {
r.mutex.RLock()
defer r.mutex.RUnlock()
c, ok := r.healthComponents[health.CheckType(name)]
return c, ok
}
// UnregisterHealthComponent - Removes a health component by name if already initialized.
func (r *healthReporter) UnregisterHealthComponent(name string) bool {
r.mutex.Lock()
defer r.mutex.Unlock()
var changesMade bool
componentName := health.CheckType(name)
if _, present := r.healthComponents[componentName]; present {
delete(r.healthComponents, componentName)
changesMade = true
}
return changesMade
}
// HealthStatus returns a copy of the current HealthStatus, and cannot be used to modify the current state
func (r *healthReporter) HealthStatus(ctx context.Context) health.HealthStatus {
checks := make(map[health.CheckType]health.HealthCheckResult, len(r.healthComponents))
for checkType, component := range r.healthComponents {
checks[checkType] = component.GetHealthCheck()
}
return health.HealthStatus{Checks: checks}
}
func (r *healthReporter) getHealthCheck(check health.CheckType) (health.HealthCheckResult, bool) {
r.mutex.RLock()
defer r.mutex.RUnlock()
component, found := r.healthComponents[check]
if !found {
return health.HealthCheckResult{}, false
}
return component.GetHealthCheck(), found
}