Debian Codegraph @

zpages/templates.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Copyright 2017, OpenCensus Authors
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18
19 package zpages // import "go.opentelemetry.io/contrib/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 // headerData contains data for the header template.
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) //nolint:revive // Called during initialization.
50 }
51 defer func() {
52 if err = f.Close(); err != nil {
53 log.Panicf("%v: %v", name, err) //nolint:revive // Called during initialization.
54 }
55 }()
56 text, err := io.ReadAll(f)
57 if err != nil {
58 log.Panicf("%v: %v", name, err) //nolint:revive // Called during initialization.
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 //nolint:gosec // G203: None of the dynamic attributes (TraceID/SpanID) can
83 // contain characters that need escaping so this lint issue is a false
84 // positive.
85 return template.HTML(tpl)
86 }
87
88 func even(x int) bool {
89 return x%2 == 0
90 }