|
| 1 | +// Licensed to Elasticsearch B.V. under one or more agreements. |
| 2 | +// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "expvar" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "log" |
| 12 | + "os" |
| 13 | + "os/signal" |
| 14 | + "runtime" |
| 15 | + "strings" |
| 16 | + "syscall" |
| 17 | + "time" |
| 18 | + |
| 19 | + // Import the "expvar" and "pprof" package >>>>>>>>>> |
| 20 | + |
| 21 | + "net/http" |
| 22 | + _ "net/http/pprof" |
| 23 | + // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 24 | + |
| 25 | + "golang.org/x/crypto/ssh/terminal" |
| 26 | + |
| 27 | + "github.com/elastic/go-elasticsearch/v8" |
| 28 | + "github.com/elastic/go-elasticsearch/v8/estransport" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + tWidth, _, _ = terminal.GetSize(int(os.Stdout.Fd())) |
| 33 | +) |
| 34 | + |
| 35 | +func init() { |
| 36 | + runtime.SetMutexProfileFraction(10) |
| 37 | +} |
| 38 | + |
| 39 | +func main() { |
| 40 | + log.SetFlags(0) |
| 41 | + |
| 42 | + ticker := time.NewTicker(time.Second) |
| 43 | + defer ticker.Stop() |
| 44 | + |
| 45 | + aborted := make(chan os.Signal) |
| 46 | + signal.Notify(aborted, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) |
| 47 | + |
| 48 | + go func() { |
| 49 | + <-aborted |
| 50 | + |
| 51 | + log.Println("\nDone!\n") |
| 52 | + os.Exit(0) |
| 53 | + }() |
| 54 | + |
| 55 | + log.Println(strings.Repeat("─", tWidth)) |
| 56 | + log.Println("Open <http://localhost:6060/debug/vars> to see all exported variables.") |
| 57 | + log.Println(strings.Repeat("─", tWidth)) |
| 58 | + |
| 59 | + // Start the debug server >>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 60 | + go func() { log.Fatalln(http.ListenAndServe("localhost:6060", nil)) }() |
| 61 | + // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 62 | + |
| 63 | + for i := 1; i <= 2; i++ { |
| 64 | + go func(i int) { |
| 65 | + log.Printf("==> Starting server on <localhost:1000%d>", i) |
| 66 | + if err := http.ListenAndServe( |
| 67 | + fmt.Sprintf("localhost:1000%d", i), |
| 68 | + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "OK\n") }), |
| 69 | + ); err != nil && err != http.ErrServerClosed { |
| 70 | + log.Fatalf("Unable to start server: %s", err) |
| 71 | + } |
| 72 | + }(i) |
| 73 | + } |
| 74 | + |
| 75 | + es, err := elasticsearch.NewClient( |
| 76 | + elasticsearch.Config{ |
| 77 | + Addresses: []string{ |
| 78 | + "http://localhost:10001", |
| 79 | + "http://localhost:10002", |
| 80 | + "http://localhost:10003", |
| 81 | + }, |
| 82 | + |
| 83 | + Logger: &estransport.ColorLogger{Output: os.Stdout}, |
| 84 | + DisableRetry: true, |
| 85 | + EnableDebugLogger: true, |
| 86 | + |
| 87 | + // Enable metric collection >>>>>>>>>>>>>>>>>>>>>>>>> |
| 88 | + EnableMetrics: true, |
| 89 | + // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 90 | + }) |
| 91 | + if err != nil { |
| 92 | + log.Fatal("ERROR: %s", err) |
| 93 | + } |
| 94 | + |
| 95 | + // Publish client metrics to expvar >>>>>>>>>>>>>>>>> |
| 96 | + expvar.Publish("go-elasticsearch", expvar.Func(func() interface{} { m, _ := es.Metrics(); return m })) |
| 97 | + // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 98 | + |
| 99 | + for { |
| 100 | + select { |
| 101 | + case t := <-ticker.C: |
| 102 | + |
| 103 | + go func() { |
| 104 | + if res, _ := es.Info(); res != nil { |
| 105 | + res.Body.Close() |
| 106 | + } |
| 107 | + }() |
| 108 | + |
| 109 | + go func() { |
| 110 | + if res, _ := es.Cluster.Health(); res != nil { |
| 111 | + res.Body.Close() |
| 112 | + } |
| 113 | + }() |
| 114 | + |
| 115 | + if t.Second()%5 == 0 { |
| 116 | + m, err := es.Metrics() |
| 117 | + if err != nil { |
| 118 | + log.Printf("\x1b[31;1mUnable to get metrics: %s\x1b[0m", err) |
| 119 | + continue |
| 120 | + } |
| 121 | + log.Println("███", fmt.Sprintf("\x1b[1m%s\x1b[0m", "Metrics"), strings.Repeat("█", tWidth-12)) |
| 122 | + log.Printf( |
| 123 | + ""+ |
| 124 | + " \x1b[2mRequests: \x1b[0m %d\n"+ |
| 125 | + " \x1b[2mFailures: \x1b[0m %d\n"+ |
| 126 | + " \x1b[2mLive nodes:\x1b[0m %s", |
| 127 | + m.Requests, m.Failures, m.Live) |
| 128 | + log.Println(strings.Repeat("─", tWidth)) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments