forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_endpoints.go
71 lines (58 loc) · 1.76 KB
/
http_endpoints.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"runtime"
"github.com/Jeffail/benthos/lib/util/service"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
)
//------------------------------------------------------------------------------
var endpoints []string
func registerEndpoint(path string, handler http.HandlerFunc) {
endpoints = append(endpoints, path)
http.HandleFunc(path, handler)
http.HandleFunc("/benthos"+path, handler)
}
func handleVersion(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("{\"version\":\"%v\", \"built\":\"%v\"}", service.Version, service.DateBuilt)))
}
func handleEndpoints(w http.ResponseWriter, r *http.Request) {
res := struct {
Endpoints []string `json:"endpoints"`
}{
Endpoints: endpoints,
}
resBytes, err := json.Marshal(res)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
} else {
w.Write(resBytes)
}
}
func registerHTTPEndpoints(
conf Config,
log log.Modular,
stats metrics.Type,
) {
handlePing := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
}
handleStackTrace := func(w http.ResponseWriter, r *http.Request) {
stackSlice := make([]byte, 1024*100)
s := runtime.Stack(stackSlice, true)
w.Write(stackSlice[:s])
}
registerEndpoint("/stack", handleStackTrace)
registerEndpoint("/ping", handlePing)
registerEndpoint("/version", handleVersion)
registerEndpoint("/endpoints", handleEndpoints)
// If we want to expose a JSON stats endpoint we register the endpoints.
if conf.Metrics.Type == "http_server" {
if httpStats, ok := stats.(*metrics.HTTP); ok {
registerEndpoint("/stats", httpStats.JSONHandler())
}
}
}
//------------------------------------------------------------------------------