-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (51 loc) · 1.33 KB
/
main.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
package main
import (
"embed"
_ "embed"
"encoding/json"
"flag"
"log"
"net/http"
"time"
)
var addr = flag.String("addr", ":7666", "address to listen on")
var buffSize = flag.Duration("lookback", 5*time.Minute, "time to look look into the past to build map")
//go:embed index.html
var static embed.FS
type appServer struct {
rb *RingBuffer
s http.Server
startTime time.Time
}
func main() {
flag.Parse()
a := appServer{
rb: NewBuffer(int((*buffSize).Milliseconds() / 1000)), // One slot for every second.
s: http.Server{Addr: *addr},
startTime: time.Now(),
}
mux := &http.ServeMux{}
mux.Handle("/v1/traces", a.TraceHandler())
mux.Handle("/api/map", a.handleServiceMap())
mux.Handle("/", http.FileServer(http.FS(static)))
a.s.Handler = mux
log.Printf("listening on %s", *addr)
log.Fatalf("error serving: %v", a.s.ListenAndServe())
}
func (a *appServer) handleServiceMap() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
serviceMap := NewServiceMap()
for i := 0; i <= a.rb.Size(); i++ {
m := a.rb.ReadAt(i)
if m.Paths == nil {
continue
}
serviceMap.Join(m)
}
e := json.NewEncoder(writer)
if err := e.Encode(serviceMap); err != nil {
log.Printf("error encoding map: %v", err)
writer.WriteHeader(http.StatusInternalServerError)
}
}
}