| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package zpages |
| 20 |
|
| 21 |
import ( |
| 22 |
"fmt" |
| 23 |
"html/template" |
| 24 |
"io" |
| 25 |
"log" |
| 26 |
|
| 27 |
"go.opentelemetry.io/contrib/zpages/internal" |
| 28 |
) |
| 29 |
|
| 30 |
var ( |
| 31 |
templateFunctions = template.FuncMap{ |
| 32 |
"even": even, |
| 33 |
"spanRow": spanRowFormatter, |
| 34 |
} |
| 35 |
headerTemplate = parseTemplate("header") |
| 36 |
summaryTableTemplate = parseTemplate("summary") |
| 37 |
tracesTableTemplate = parseTemplate("traces") |
| 38 |
footerTemplate = parseTemplate("footer") |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
type headerData struct { |
| 43 |
Title string |
| 44 |
} |
| 45 |
|
| 46 |
func parseTemplate(name string) *template.Template { |
| 47 |
f, err := internal.Templates.Open("templates/" + name + ".html") |
| 48 |
if err != nil { |
| 49 |
log.Panicf("%v: %v", name, err) |
| 50 |
} |
| 51 |
defer func() { |
| 52 |
if err = f.Close(); err != nil { |
| 53 |
log.Panicf("%v: %v", name, err) |
| 54 |
} |
| 55 |
}() |
| 56 |
text, err := io.ReadAll(f) |
| 57 |
if err != nil { |
| 58 |
log.Panicf("%v: %v", name, err) |
| 59 |
} |
| 60 |
return template.Must(template.New(name).Funcs(templateFunctions).Parse(string(text))) |
| 61 |
} |
| 62 |
|
| 63 |
func spanRowFormatter(r spanRow) template.HTML { |
| 64 |
if !r.IsValid() { |
| 65 |
return "" |
| 66 |
} |
| 67 |
col := "black" |
| 68 |
if r.IsSampled() { |
| 69 |
col = "blue" |
| 70 |
} |
| 71 |
|
| 72 |
tpl := fmt.Sprintf( |
| 73 |
`trace_id: <b style="color:%s">%s</b> span_id: %s`, |
| 74 |
col, |
| 75 |
r.TraceID(), |
| 76 |
r.SpanID(), |
| 77 |
) |
| 78 |
if r.ParentSpanContext.IsValid() { |
| 79 |
tpl += fmt.Sprintf(` parent_span_id: %s`, r.ParentSpanContext.SpanID()) |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
return template.HTML(tpl) |
| 86 |
} |
| 87 |
|
| 88 |
func even(x int) bool { |
| 89 |
return x%2 == 0 |
| 90 |
} |