-
Notifications
You must be signed in to change notification settings - Fork 626
/
Copy pathexpvar.go
131 lines (108 loc) · 3.15 KB
/
expvar.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
// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
package main
import (
"expvar"
"fmt"
"io"
"log"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
// Import the "expvar" and "pprof" package >>>>>>>>>>
"net/http"
_ "net/http/pprof"
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
"golang.org/x/crypto/ssh/terminal"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/estransport"
)
var (
tWidth, _, _ = terminal.GetSize(int(os.Stdout.Fd()))
)
func init() {
runtime.SetMutexProfileFraction(10)
}
func main() {
log.SetFlags(0)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
aborted := make(chan os.Signal)
signal.Notify(aborted, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-aborted
log.Println("\nDone!\n")
os.Exit(0)
}()
log.Println(strings.Repeat("─", tWidth))
log.Println("Open <http://localhost:6060/debug/vars> to see all exported variables.")
log.Println(strings.Repeat("─", tWidth))
// Start the debug server >>>>>>>>>>>>>>>>>>>>>>>>>>>
go func() { log.Fatalln(http.ListenAndServe("localhost:6060", nil)) }()
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for i := 1; i <= 2; i++ {
go func(i int) {
log.Printf("==> Starting server on <localhost:1000%d>", i)
if err := http.ListenAndServe(
fmt.Sprintf("localhost:1000%d", i),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "OK\n") }),
); err != nil && err != http.ErrServerClosed {
log.Fatalf("Unable to start server: %s", err)
}
}(i)
}
es, err := elasticsearch.NewClient(
elasticsearch.Config{
Addresses: []string{
"http://localhost:10001",
"http://localhost:10002",
"http://localhost:10003",
},
Logger: &estransport.ColorLogger{Output: os.Stdout},
DisableRetry: true,
EnableDebugLogger: true,
// Enable metric collection >>>>>>>>>>>>>>>>>>>>>>>>>
EnableMetrics: true,
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
})
if err != nil {
log.Fatal("ERROR: %s", err)
}
// Publish client metrics to expvar >>>>>>>>>>>>>>>>>
expvar.Publish("go-elasticsearch", expvar.Func(func() interface{} { m, _ := es.Metrics(); return m }))
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for {
select {
case t := <-ticker.C:
go func() {
if res, _ := es.Info(); res != nil {
res.Body.Close()
}
}()
go func() {
if res, _ := es.Cluster.Health(); res != nil {
res.Body.Close()
}
}()
if t.Second()%5 == 0 {
m, err := es.Metrics()
if err != nil {
log.Printf("\x1b[31;1mUnable to get metrics: %s\x1b[0m", err)
continue
}
log.Println("███", fmt.Sprintf("\x1b[1m%s\x1b[0m", "Metrics"), strings.Repeat("█", tWidth-12))
log.Printf(
""+
" \x1b[2mRequests: \x1b[0m %d\n"+
" \x1b[2mFailures: \x1b[0m %d\n"+
" \x1b[2mConnections:\x1b[0m %s",
m.Requests, m.Failures, m.Connections)
log.Println(strings.Repeat("─", tWidth))
}
}
}
}