...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package zpages
17
18 import (
19 "fmt"
20 "html/template"
21 "io/ioutil"
22 "log"
23 "strconv"
24 "time"
25
26 "go.opencensus.io/trace"
27 "go.opencensus.io/zpages/internal"
28 )
29
30 var (
31 fs = internal.FS(false)
32 templateFunctions = template.FuncMap{
33 "count": countFormatter,
34 "ms": msFormatter,
35 "rate": rateFormatter,
36 "datarate": dataRateFormatter,
37 "even": even,
38 "traceid": traceIDFormatter,
39 }
40 headerTemplate = parseTemplate("header")
41 summaryTableTemplate = parseTemplate("summary")
42 statsTemplate = parseTemplate("rpcz")
43 tracesTableTemplate = parseTemplate("traces")
44 footerTemplate = parseTemplate("footer")
45 )
46
47 func parseTemplate(name string) *template.Template {
48 f, err := fs.Open("/templates/" + name + ".html")
49 if err != nil {
50 log.Panicf("%v: %v", name, err)
51 }
52 defer f.Close()
53 text, err := ioutil.ReadAll(f)
54 if err != nil {
55 log.Panicf("%v: %v", name, err)
56 }
57 return template.Must(template.New(name).Funcs(templateFunctions).Parse(string(text)))
58 }
59
60 func countFormatter(num uint64) string {
61 if num <= 0 {
62 return " "
63 }
64 var floatVal float64
65 var suffix string
66
67 if num >= 1e18 {
68 floatVal = float64(num) / 1e18
69 suffix = " E "
70 } else if num >= 1e15 {
71 floatVal = float64(num) / 1e15
72 suffix = " P "
73 } else if num >= 1e12 {
74 floatVal = float64(num) / 1e12
75 suffix = " T "
76 } else if num >= 1e9 {
77 floatVal = float64(num) / 1e9
78 suffix = " G "
79 } else if num >= 1e6 {
80 floatVal = float64(num) / 1e6
81 suffix = " M "
82 }
83
84 if floatVal != 0 {
85 return fmt.Sprintf("%1.3f%s", floatVal, suffix)
86 }
87 return fmt.Sprint(num)
88 }
89
90 func msFormatter(d time.Duration) string {
91 if d == 0 {
92 return "0"
93 }
94 if d < 10*time.Millisecond {
95 return fmt.Sprintf("%.3f", float64(d)*1e-6)
96 }
97 return strconv.Itoa(int(d / time.Millisecond))
98 }
99
100 func rateFormatter(r float64) string {
101 return fmt.Sprintf("%.3f", r)
102 }
103
104 func dataRateFormatter(b float64) string {
105 return fmt.Sprintf("%.3f", b/1e6)
106 }
107
108 func traceIDFormatter(r traceRow) template.HTML {
109 sc := r.SpanContext
110 if sc == (trace.SpanContext{}) {
111 return ""
112 }
113 col := "black"
114 if sc.TraceOptions.IsSampled() {
115 col = "blue"
116 }
117 if r.ParentSpanID != (trace.SpanID{}) {
118 return template.HTML(fmt.Sprintf(`trace_id: <b style="color:%s">%s</b> span_id: %s parent_span_id: %s`, col, sc.TraceID, sc.SpanID, r.ParentSpanID))
119 }
120 return template.HTML(fmt.Sprintf(`trace_id: <b style="color:%s">%s</b> span_id: %s`, col, sc.TraceID, sc.SpanID))
121 }
122
123 func even(x int) bool {
124 return x%2 == 0
125 }
126
View as plain text