Skip to content

Commit 1e01164

Browse files
committed
Fix test lint
Signed-off-by: Charlie Le <[email protected]>
1 parent b4f43e2 commit 1e01164

File tree

4 files changed

+96
-69
lines changed

4 files changed

+96
-69
lines changed

cmd/otel-allocator/internal/server/server.go

+69-66
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,6 @@ func (s *Server) PrometheusMiddleware(c *gin.Context) {
285285
timer.ObserveDuration()
286286
}
287287

288-
func header(data ...string) string {
289-
return "<thead><td>" + strings.Join(data, "</td><td>") + "</td></thead>\n"
290-
}
291-
292-
func row(data ...string) string {
293-
return "<tr><td>" + strings.Join(data, "</td><td>") + "</td></tr>\n"
294-
}
295-
296288
// IndexHandler displays the main page of the allocator. It shows the number of jobs and targets.
297289
// It also displays a table with the collectors and the number of jobs and targets for each collector.
298290
// The collector names are links to the respective pages. The table is sorted by collector name.
@@ -304,16 +296,16 @@ func (s *Server) IndexHandler(c *gin.Context) {
304296

305297
WriteHTMLPropertiesTable(c.Writer, PropertiesTableData{
306298
Headers: []string{"Category", "Count"},
307-
Rows: [][]template.HTML{
308-
{scrapeConfigAnchorLink(), template.HTML(strconv.Itoa(s.getScrapeConfigCount()))},
309-
{jobsAnchorLink(), template.HTML(strconv.Itoa(s.getJobCount()))},
310-
{targetsAnchorLink(), template.HTML(strconv.Itoa(len(s.allocator.TargetItems())))},
299+
Rows: [][]Cell{
300+
{scrapeConfigAnchorLink(), Text(strconv.Itoa(s.getScrapeConfigCount()))},
301+
{jobsAnchorLink(), Text(strconv.Itoa(s.getJobCount()))},
302+
{targetsAnchorLink(), Text(strconv.Itoa(len(s.allocator.TargetItems())))},
311303
},
312304
})
313305
WriteHTMLPropertiesTable(c.Writer, PropertiesTableData{
314306
Headers: []string{"Collector", "Job Count", "Target Count"},
315-
Rows: func() [][]template.HTML {
316-
var rows [][]template.HTML
307+
Rows: func() [][]Cell {
308+
var rows [][]Cell
317309
collectorNames := []string{}
318310
for k := range s.allocator.Collectors() {
319311
collectorNames = append(collectorNames, k)
@@ -323,16 +315,19 @@ func (s *Server) IndexHandler(c *gin.Context) {
323315
for _, colName := range collectorNames {
324316
jobCount := strconv.Itoa(s.getJobCountForCollector(colName))
325317
targetCount := strconv.Itoa(s.getTargetCountForCollector(colName))
326-
rows = append(rows, []template.HTML{collectorAnchorLink(colName), template.HTML(jobCount), template.HTML(targetCount)})
318+
rows = append(rows, []Cell{collectorAnchorLink(colName), NewCell(jobCount), NewCell(targetCount)})
327319
}
328320
return rows
329321
}(),
330322
})
331323
WriteHTMLPageFooter(c.Writer)
332324
}
333325

334-
func targetsAnchorLink() template.HTML {
335-
return `<a href="/targets">Targets</a>`
326+
func targetsAnchorLink() Cell {
327+
return Cell{
328+
Link: "/targets",
329+
Text: "Targets",
330+
}
336331
}
337332

338333
// TargetsHTMLHandler displays the targets in a table format. Each target is a row in the table.
@@ -348,14 +343,14 @@ func (s *Server) TargetsHTMLHandler(c *gin.Context) {
348343

349344
WriteHTMLPropertiesTable(c.Writer, PropertiesTableData{
350345
Headers: []string{"Job", "Target", "Collector", "Endpoint Slice"},
351-
Rows: func() [][]template.HTML {
352-
var rows [][]template.HTML
346+
Rows: func() [][]Cell {
347+
var rows [][]Cell
353348
for _, v := range s.sortedTargetItems() {
354-
rows = append(rows, []template.HTML{
349+
rows = append(rows, []Cell{
355350
jobAnchorLink(v.JobName),
356351
targetAnchorLink(v),
357352
collectorAnchorLink(v.CollectorName),
358-
template.HTML(v.GetEndpointSliceName()),
353+
NewCell(v.GetEndpointSliceName()),
359354
})
360355
}
361356
return rows
@@ -364,8 +359,11 @@ func (s *Server) TargetsHTMLHandler(c *gin.Context) {
364359
WriteHTMLPageFooter(c.Writer)
365360
}
366361

367-
func targetAnchorLink(t *target.Item) template.HTML {
368-
return template.HTML(fmt.Sprintf("<a href=\"/target?target_hash=%s\">%s</a>", t.Hash(), t.TargetURL))
362+
func targetAnchorLink(t *target.Item) Cell {
363+
return Cell{
364+
Link: fmt.Sprintf("/target?target_hash=%s", t.Hash()),
365+
Text: t.TargetURL,
366+
}
369367
}
370368

371369
// TargetHTMLHandler displays information about a target in a table format.
@@ -413,34 +411,37 @@ func (s *Server) TargetHTMLHandler(c *gin.Context) {
413411
})
414412
WriteHTMLPropertiesTable(c.Writer, PropertiesTableData{
415413
Headers: []string{"", ""},
416-
Rows: [][]template.HTML{
417-
{"Collector", collectorAnchorLink(target.CollectorName)},
418-
{"Job", jobAnchorLink(target.JobName)},
419-
{"Namespace", template.HTML(target.Labels.Get("__meta_kubernetes_namespace"))},
420-
{"Service Name", template.HTML(target.Labels.Get("__meta_kubernetes_service_name"))},
421-
{"Service Port", template.HTML(target.Labels.Get("__meta_kubernetes_service_port"))},
422-
{"Pod Name", template.HTML(target.Labels.Get("__meta_kubernetes_pod_name"))},
423-
{"Container Name", template.HTML(target.Labels.Get("__meta_kubernetes_pod_container_name"))},
424-
{"Container Port Name", template.HTML(target.Labels.Get("__meta_kubernetes_pod_container_port_name"))},
425-
{"Node Name", template.HTML(target.GetNodeName())},
426-
{"Endpoint Slice Name", template.HTML(target.GetEndpointSliceName())},
414+
Rows: [][]Cell{
415+
{NewCell("Collector"), collectorAnchorLink(target.CollectorName)},
416+
{NewCell("Job"), jobAnchorLink(target.JobName)},
417+
{NewCell("Namespace"), NewCell(target.Labels.Get("__meta_kubernetes_namespace"))},
418+
{NewCell("Service Name"), NewCell(target.Labels.Get("__meta_kubernetes_service_name"))},
419+
{NewCell("Service Port"), NewCell(target.Labels.Get("__meta_kubernetes_service_port"))},
420+
{NewCell("Pod Name"), NewCell(target.Labels.Get("__meta_kubernetes_pod_name"))},
421+
{NewCell("Container Name"), NewCell(target.Labels.Get("__meta_kubernetes_pod_container_name"))},
422+
{NewCell("Container Port Name"), NewCell(target.Labels.Get("__meta_kubernetes_pod_container_port_name"))},
423+
{NewCell("Node Name"), NewCell(target.GetNodeName())},
424+
{NewCell("Endpoint Slice Name"), NewCell(target.GetEndpointSliceName())},
427425
},
428426
})
429427
WriteHTMLPropertiesTable(c.Writer, PropertiesTableData{
430428
Headers: []string{"Label", "Value"},
431-
Rows: func() [][]template.HTML {
432-
var rows [][]template.HTML
429+
Rows: func() [][]Cell {
430+
var rows [][]Cell
433431
for _, l := range target.Labels {
434-
rows = append(rows, []template.HTML{template.HTML(l.Name), template.HTML(l.Value)})
432+
rows = append(rows, []Cell{NewCell(l.Name), NewCell(l.Value)})
435433
}
436434
return rows
437435
}(),
438436
})
439437
WriteHTMLPageFooter(c.Writer)
440438
}
441439

442-
func jobsAnchorLink() template.HTML {
443-
return `<a href="/jobs">Jobs</a>`
440+
func jobsAnchorLink() Cell {
441+
return Cell{
442+
Link: "/jobs",
443+
Text: "Jobs",
444+
}
444445
}
445446

446447
// JobsHTMLHandler displays the jobs in a table format. Each job is a row in the table.
@@ -454,8 +455,8 @@ func (s *Server) JobsHTMLHandler(c *gin.Context) {
454455
})
455456
WriteHTMLPropertiesTable(c.Writer, PropertiesTableData{
456457
Headers: []string{"Job", "Target Count"},
457-
Rows: func() [][]template.HTML {
458-
var rows [][]template.HTML
458+
Rows: func() [][]Cell {
459+
var rows [][]Cell
459460
jobs := make(map[string]int)
460461
for _, v := range s.allocator.TargetItems() {
461462
jobs[v.JobName]++
@@ -469,16 +470,19 @@ func (s *Server) JobsHTMLHandler(c *gin.Context) {
469470

470471
for _, j := range jobNames {
471472
v := jobs[j]
472-
rows = append(rows, []template.HTML{jobAnchorLink(j), template.HTML(strconv.Itoa(v))})
473+
rows = append(rows, []Cell{jobAnchorLink(j), NewCell(strconv.Itoa(v))})
473474
}
474475
return rows
475476
}(),
476477
})
477478
WriteHTMLPageFooter(c.Writer)
478479
}
479480

480-
func jobAnchorLink(jobId string) template.HTML {
481-
return template.HTML(fmt.Sprintf("<a href=\"/job?job_id=%s\">%s</a>", jobId, jobId))
481+
func jobAnchorLink(jobId string) Cell {
482+
return Cell{
483+
Link: fmt.Sprintf("/job?job_id=%s", url.QueryEscape(jobId)),
484+
Text: jobId,
485+
}
482486
}
483487
func (s *Server) JobHTMLHandler(c *gin.Context) {
484488
c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
@@ -496,8 +500,8 @@ func (s *Server) JobHTMLHandler(c *gin.Context) {
496500
})
497501
WriteHTMLPropertiesTable(c.Writer, PropertiesTableData{
498502
Headers: []string{"Collector", "Target Count"},
499-
Rows: func() [][]template.HTML {
500-
var rows [][]template.HTML
503+
Rows: func() [][]Cell {
504+
var rows [][]Cell
501505
targets := map[string]*target.Item{}
502506
for k, v := range s.allocator.TargetItems() {
503507
if v.JobName == jobId {
@@ -516,16 +520,19 @@ func (s *Server) JobHTMLHandler(c *gin.Context) {
516520
count++
517521
}
518522
}
519-
rows = append(rows, []template.HTML{collectorAnchorLink(colName), template.HTML(strconv.Itoa(count))})
523+
rows = append(rows, []Cell{collectorAnchorLink(colName), NewCell(strconv.Itoa(count))})
520524
}
521525
return rows
522526
}(),
523527
})
524528
WriteHTMLPageFooter(c.Writer)
525529
}
526530

527-
func collectorAnchorLink(collectorId string) template.HTML {
528-
return template.HTML(fmt.Sprintf("<a href=\"/collector?collector_id=%s\">%s</a>", collectorId, collectorId))
531+
func collectorAnchorLink(collectorId string) Cell {
532+
return Cell{
533+
Link: fmt.Sprintf("/collector?collector_id=%s", url.QueryEscape(collectorId)),
534+
Text: collectorId,
535+
}
529536
}
530537

531538
func (s *Server) CollectorHTMLHandler(c *gin.Context) {
@@ -581,14 +588,14 @@ func (s *Server) CollectorHTMLHandler(c *gin.Context) {
581588
})
582589
WriteHTMLPropertiesTable(c.Writer, PropertiesTableData{
583590
Headers: []string{"Job", "Target", "Endpoint Slice"},
584-
Rows: func() [][]template.HTML {
585-
var rows [][]template.HTML
591+
Rows: func() [][]Cell {
592+
var rows [][]Cell
586593
for _, v := range s.sortedTargetItems() {
587594
if v.CollectorName == collectorId {
588-
rows = append(rows, []template.HTML{
595+
rows = append(rows, []Cell{
589596
jobAnchorLink(v.JobName),
590597
targetAnchorLink(v),
591-
template.HTML(v.GetEndpointSliceName()),
598+
NewCell(v.GetEndpointSliceName()),
592599
})
593600
}
594601
}
@@ -598,8 +605,11 @@ func (s *Server) CollectorHTMLHandler(c *gin.Context) {
598605
WriteHTMLPageFooter(c.Writer)
599606
}
600607

601-
func scrapeConfigAnchorLink() template.HTML {
602-
return `<a href="/scrape_configs">Scrape Configs</a>`
608+
func scrapeConfigAnchorLink() Cell {
609+
return Cell{
610+
Link: "/scrape_configs",
611+
Text: "Scrape Configs",
612+
}
603613
}
604614
func (s *Server) ScrapeConfigsHTMLHandler(c *gin.Context) {
605615
c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
@@ -620,23 +630,16 @@ func (s *Server) ScrapeConfigsHTMLHandler(c *gin.Context) {
620630

621631
WriteHTMLPropertiesTable(c.Writer, PropertiesTableData{
622632
Headers: []string{"Job", "Scrape Config"},
623-
Rows: func() [][]template.HTML {
624-
var rows [][]template.HTML
633+
Rows: func() [][]Cell {
634+
var rows [][]Cell
625635
for job, scrapeConfig := range scrapeConfigs {
626-
scrapeConfigJSON, err := json.Marshal(scrapeConfig)
627-
if err != nil {
628-
s.errorHandler(c.Writer, err)
629-
return nil
630-
}
631636
// pretty print the JSON
632-
scrapeConfigJSON, err = json.MarshalIndent(scrapeConfig, "", " ")
637+
scrapeConfigJSON, err := json.MarshalIndent(scrapeConfig, "", " ")
633638
if err != nil {
634639
s.errorHandler(c.Writer, err)
635640
return nil
636641
}
637-
// Wrap the JSON in a <pre> tag to preserve formatting
638-
scrapeConfigJSON = []byte(fmt.Sprintf("<pre>%s</pre>", scrapeConfigJSON))
639-
rows = append(rows, []template.HTML{template.HTML(jobAnchorLink(job)), template.HTML(scrapeConfigJSON)})
642+
rows = append(rows, []Cell{jobAnchorLink(job), {Text: string(scrapeConfigJSON), Preformatted: true}})
640643
}
641644
return rows
642645
}(),

cmd/otel-allocator/internal/server/templates.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,25 @@ func WriteHTMLPageHeader(w io.Writer, hd HeaderData) {
4747
// PropertiesTableData contains data for properties table template.
4848
type PropertiesTableData struct {
4949
Headers []string
50-
Rows [][]template.HTML
50+
Rows [][]Cell
51+
}
52+
53+
// Cell represents a cell in a row.
54+
type Cell struct {
55+
// Link is the URL to link to. If empty, no link is created.
56+
Link string
57+
// Text is the text to display in the cell.
58+
Text string
59+
// Preformatted indicates if the text should be displayed as preformatted text.
60+
Preformatted bool
61+
}
62+
63+
func NewCell(text string) Cell {
64+
return Cell{Text: text}
65+
}
66+
67+
func Text(text string) Cell {
68+
return Cell{Text: text}
5169
}
5270

5371
// WriteHTMLPropertiesTable writes the HTML for properties table.

cmd/otel-allocator/internal/server/templates/properties_table.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
<tr {{ if (even $index) }}style="background: #eee"{{end}}>
1212
{{- range $index, $cell := $row}}
1313
<td style="vertical-align: top;">
14-
{{$cell}}
14+
{{- if $cell.Link }}
15+
<a href="{{$cell.Link}}">{{$cell.Text}}</a>
16+
{{- else if $cell.Preformatted }}
17+
<pre>{{$cell.Text}}</pre>
18+
{{- else}}
19+
{{$cell.Text}}
20+
{{- end}}
1521
</td>
1622
{{- end}}
1723
</tr>

cmd/otel-allocator/internal/server/templates_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestNoCrash(t *testing.T) {
3939
buf := new(bytes.Buffer)
4040
assert.NotPanics(t, func() { WriteHTMLPageHeader(buf, HeaderData{Title: "Foo"}) })
4141
assert.NotPanics(t, func() {
42-
WriteHTMLPropertiesTable(buf, PropertiesTableData{Headers: []string{"foo"}, Rows: [][]template.HTML{{"bar"}}})
42+
WriteHTMLPropertiesTable(buf, PropertiesTableData{Headers: []string{"foo"}, Rows: [][]Cell{{NewCell("bar")}}})
4343
})
4444
assert.NotPanics(t, func() { WriteHTMLPageFooter(buf) })
4545
}

0 commit comments

Comments
 (0)