This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
component.go
182 lines (153 loc) · 3.95 KB
/
component.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package metricz
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/localip"
"github.com/cloudfoundry-incubator/metricz/auth"
"github.com/cloudfoundry-incubator/metricz/instrumentation"
)
type Component struct {
name string //Used by the collector to find data processing class
ipAddress string
healthMonitor HealthMonitor
index uint
uuid string
statusPort uint16
statusCredentials []string
instrumentables []instrumentation.Instrumentable
logger lager.Logger
listener net.Listener
quitChan chan bool
startChan chan bool
}
const (
username = iota
password
)
func NewComponent(
logger lager.Logger,
name string,
index uint,
heathMonitor HealthMonitor,
statusPort uint16,
statusCreds []string,
instrumentables []instrumentation.Instrumentable,
) (Component, error) {
ip, err := localip.LocalIP()
if err != nil {
return Component{}, err
}
if statusPort == 0 {
statusPort, err = localip.LocalPort()
if err != nil {
return Component{}, err
}
}
if len(statusCreds) == 0 || statusCreds[username] == "" || statusCreds[password] == "" {
randUser := make([]byte, 42)
randPass := make([]byte, 42)
rand.Read(randUser)
rand.Read(randPass)
en := base64.URLEncoding
user := en.EncodeToString(randUser)
pass := en.EncodeToString(randPass)
statusCreds = []string{user, pass}
}
componentLogger := logger.Session("component")
return Component{
name: name,
ipAddress: ip,
index: index,
healthMonitor: heathMonitor,
statusPort: statusPort,
statusCredentials: statusCreds,
instrumentables: instrumentables,
logger: componentLogger,
quitChan: make(chan bool, 1),
startChan: make(chan bool, 1),
}, nil
}
func (c *Component) StartMonitoringEndpoints() error {
mux := http.NewServeMux()
auth := auth.NewBasicAuth("Realm", c.statusCredentials)
mux.HandleFunc("/healthz", healthzHandlerFor(c))
mux.HandleFunc("/varz", auth.Wrap(varzHandlerFor(c)))
c.logger.Debug("start", lager.Data{
"uuid": c.uuid,
"ip": c.ipAddress,
"port": c.statusPort,
"username": c.statusCredentials[username],
"password": c.statusCredentials[password],
})
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", c.ipAddress, c.statusPort))
if err != nil {
return err
}
c.listener = listener
c.startChan <- true
server := &http.Server{Handler: mux}
err = server.Serve(listener)
select {
case <-c.quitChan:
return nil
default:
return err
}
}
func (c *Component) StopMonitoringEndpoints() {
<-c.startChan
c.quitChan <- true
c.listener.Close()
}
func (c *Component) Name() string {
return c.name
}
func (c *Component) Index() uint {
return c.index
}
func (c *Component) UUID() string {
return c.uuid
}
func (c *Component) URL() *url.URL {
return &url.URL{
Scheme: "http",
Host: net.JoinHostPort(c.ipAddress, fmt.Sprintf("%d", c.statusPort)),
User: url.UserPassword(c.statusCredentials[0], c.statusCredentials[1]),
}
}
func healthzHandlerFor(c *Component) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
if c.healthMonitor.Ok() {
fmt.Fprintf(w, "ok")
} else {
fmt.Fprintf(w, "bad")
}
}
}
func varzHandlerFor(c *Component) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
message, err := instrumentation.NewVarzMessage(c.name, c.instrumentables)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, err.Error())
return
}
json, err := json.Marshal(message)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(json)
}
}