Skip to content

Commit

Permalink
Test spanRowFormatter, and mark the gosec issue as a false-positive (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu authored Mar 29, 2024
1 parent 3ca855b commit 336d5b6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
16 changes: 13 additions & 3 deletions zpages/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func parseTemplate(name string) *template.Template {
return template.Must(template.New(name).Funcs(templateFunctions).Parse(string(text)))
}

//nolint:gosec // G203: The used method does not auto-escape HTML. Tracked under https://github.com/open-telemetry/opentelemetry-go-contrib/issues/4451.
func spanRowFormatter(r spanRow) template.HTML {
if !r.SpanContext.IsValid() {
return ""
Expand All @@ -69,10 +68,21 @@ func spanRowFormatter(r spanRow) template.HTML {
if r.SpanContext.IsSampled() {
col = "blue"
}

tpl := fmt.Sprintf(
`trace_id: <b style="color:%s">%s</b> span_id: %s`,
col,
r.SpanContext.TraceID(),
r.SpanContext.SpanID(),
)
if r.ParentSpanContext.IsValid() {
return template.HTML(fmt.Sprintf(`trace_id: <b style="color:%s">%s</b> span_id: %s parent_span_id: %s`, col, r.SpanContext.TraceID(), r.SpanContext.SpanID(), r.ParentSpanContext.SpanID()))
tpl += fmt.Sprintf(` parent_span_id: %s`, r.ParentSpanContext.SpanID())
}
return template.HTML(fmt.Sprintf(`trace_id: <b style="color:%s">%s</b> span_id: %s`, col, r.SpanContext.TraceID(), r.SpanContext.SpanID()))

//nolint:gosec // G203: None of the dynamic attributes (TraceID/SpanID) can
// contain characters that need escaping so this lint issue is a false
// positive.
return template.HTML(tpl)
}

func even(x int) bool {
Expand Down
59 changes: 59 additions & 0 deletions zpages/templates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package zpages

import (
"html/template"
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/trace"
)

func TestSpanRowFormatter(t *testing.T) {
for _, tt := range []struct {
name string
row spanRow

expectedTemplate template.HTML
}{
{
name: "with an invalid span context",
row: spanRow{
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{}),
},
expectedTemplate: "",
},
{
name: "with a valid span context",
row: spanRow{
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID{2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9},
SpanID: trace.SpanID{1, 2, 3, 4, 5, 6, 7, 8},
}),
},
expectedTemplate: "trace_id: <b style=\"color:black\">02030405060708090203040506070809</b> span_id: 0102030405060708",
},
{
name: "with a valid parent span context",
row: spanRow{
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID{2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9},
SpanID: trace.SpanID{1, 2, 3, 4, 5, 6, 7, 8},
}),
ParentSpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25},
SpanID: trace.SpanID{10, 11, 12, 13, 14, 15, 16, 18},
}),
},
expectedTemplate: "trace_id: <b style=\"color:black\">02030405060708090203040506070809</b> span_id: 0102030405060708 parent_span_id: 0a0b0c0d0e0f1012",
},
} {
t.Run(tt.name, func(t *testing.T) {
r := spanRowFormatter(tt.row)
assert.Equal(t, tt.expectedTemplate, r)
})
}
}

0 comments on commit 336d5b6

Please sign in to comment.