/
appdash.go
68 lines (58 loc) · 1.88 KB
/
appdash.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
package tracing
import (
"fmt"
"log"
"net"
"net/http"
"net/url"
opentracing "github.com/opentracing/opentracing-go"
"sourcegraph.com/sourcegraph/appdash"
appdashot "sourcegraph.com/sourcegraph/appdash/opentracing"
"sourcegraph.com/sourcegraph/appdash/traceapp"
)
// StartTracing starts an OpenTracing UI on localhost:8700
// inspired by: https://medium.com/opentracing/distributed-tracing-in-10-minutes-51b378ee40f1
func StartTracing() func(opentracing.Span) string {
store := appdash.NewMemoryStore()
// Listen on any available TCP port locally.
l, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0})
if err != nil {
log.Fatal(err)
}
collectorPort := l.Addr().(*net.TCPAddr).Port
collectorAdd := fmt.Sprintf(":%d", collectorPort)
// Start an Appdash collection server that will listen for spans and
// annotations and add them to the local collector (stored in-memory).
cs := appdash.NewServer(l, appdash.NewLocalCollector(store))
go cs.Start()
tracer := appdashot.NewTracer(appdash.NewRemoteCollector(collectorAdd))
opentracing.InitGlobalTracer(tracer)
getURLFunc := func(s opentracing.Span) string {
// Start the webui for viewing traces
appdashPort := 8700
appdashURLStr := fmt.Sprintf("http://localhost:%d", appdashPort)
appdashURL, err := url.Parse(appdashURLStr)
if err != nil {
log.Fatalf("Error parsing %s: %s", appdashURLStr, err)
}
// Start the web UI in a separate goroutine.
tapp, err := traceapp.New(nil, appdashURL)
if err != nil {
log.Fatal(err)
}
tapp.Store = store
tapp.Queryer = store
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", appdashPort), tapp))
}()
traces, err := tapp.Queryer.Traces(appdash.TracesOpts{})
if err != nil {
log.Panic(err)
}
if len(traces) < 1 {
return appdashURLStr
}
return appdashURLStr + "/traces/" + traces[0].ID.Trace.String()
}
return getURLFunc
}