@@ -285,14 +285,6 @@ func (s *Server) PrometheusMiddleware(c *gin.Context) {
285
285
timer .ObserveDuration ()
286
286
}
287
287
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
-
296
288
// IndexHandler displays the main page of the allocator. It shows the number of jobs and targets.
297
289
// It also displays a table with the collectors and the number of jobs and targets for each collector.
298
290
// 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) {
304
296
305
297
WriteHTMLPropertiesTable (c .Writer , PropertiesTableData {
306
298
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 ())))},
311
303
},
312
304
})
313
305
WriteHTMLPropertiesTable (c .Writer , PropertiesTableData {
314
306
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
317
309
collectorNames := []string {}
318
310
for k := range s .allocator .Collectors () {
319
311
collectorNames = append (collectorNames , k )
@@ -323,16 +315,19 @@ func (s *Server) IndexHandler(c *gin.Context) {
323
315
for _ , colName := range collectorNames {
324
316
jobCount := strconv .Itoa (s .getJobCountForCollector (colName ))
325
317
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 )})
327
319
}
328
320
return rows
329
321
}(),
330
322
})
331
323
WriteHTMLPageFooter (c .Writer )
332
324
}
333
325
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
+ }
336
331
}
337
332
338
333
// 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) {
348
343
349
344
WriteHTMLPropertiesTable (c .Writer , PropertiesTableData {
350
345
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
353
348
for _ , v := range s .sortedTargetItems () {
354
- rows = append (rows , []template. HTML {
349
+ rows = append (rows , []Cell {
355
350
jobAnchorLink (v .JobName ),
356
351
targetAnchorLink (v ),
357
352
collectorAnchorLink (v .CollectorName ),
358
- template . HTML (v .GetEndpointSliceName ()),
353
+ NewCell (v .GetEndpointSliceName ()),
359
354
})
360
355
}
361
356
return rows
@@ -364,8 +359,11 @@ func (s *Server) TargetsHTMLHandler(c *gin.Context) {
364
359
WriteHTMLPageFooter (c .Writer )
365
360
}
366
361
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
+ }
369
367
}
370
368
371
369
// TargetHTMLHandler displays information about a target in a table format.
@@ -413,34 +411,37 @@ func (s *Server) TargetHTMLHandler(c *gin.Context) {
413
411
})
414
412
WriteHTMLPropertiesTable (c .Writer , PropertiesTableData {
415
413
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 ())},
427
425
},
428
426
})
429
427
WriteHTMLPropertiesTable (c .Writer , PropertiesTableData {
430
428
Headers : []string {"Label" , "Value" },
431
- Rows : func () [][]template. HTML {
432
- var rows [][]template. HTML
429
+ Rows : func () [][]Cell {
430
+ var rows [][]Cell
433
431
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 )})
435
433
}
436
434
return rows
437
435
}(),
438
436
})
439
437
WriteHTMLPageFooter (c .Writer )
440
438
}
441
439
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
+ }
444
445
}
445
446
446
447
// 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) {
454
455
})
455
456
WriteHTMLPropertiesTable (c .Writer , PropertiesTableData {
456
457
Headers : []string {"Job" , "Target Count" },
457
- Rows : func () [][]template. HTML {
458
- var rows [][]template. HTML
458
+ Rows : func () [][]Cell {
459
+ var rows [][]Cell
459
460
jobs := make (map [string ]int )
460
461
for _ , v := range s .allocator .TargetItems () {
461
462
jobs [v .JobName ]++
@@ -469,16 +470,19 @@ func (s *Server) JobsHTMLHandler(c *gin.Context) {
469
470
470
471
for _ , j := range jobNames {
471
472
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 ))})
473
474
}
474
475
return rows
475
476
}(),
476
477
})
477
478
WriteHTMLPageFooter (c .Writer )
478
479
}
479
480
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
+ }
482
486
}
483
487
func (s * Server ) JobHTMLHandler (c * gin.Context ) {
484
488
c .Writer .Header ().Set ("X-Content-Type-Options" , "nosniff" )
@@ -496,8 +500,8 @@ func (s *Server) JobHTMLHandler(c *gin.Context) {
496
500
})
497
501
WriteHTMLPropertiesTable (c .Writer , PropertiesTableData {
498
502
Headers : []string {"Collector" , "Target Count" },
499
- Rows : func () [][]template. HTML {
500
- var rows [][]template. HTML
503
+ Rows : func () [][]Cell {
504
+ var rows [][]Cell
501
505
targets := map [string ]* target.Item {}
502
506
for k , v := range s .allocator .TargetItems () {
503
507
if v .JobName == jobId {
@@ -516,16 +520,19 @@ func (s *Server) JobHTMLHandler(c *gin.Context) {
516
520
count ++
517
521
}
518
522
}
519
- rows = append (rows , []template. HTML {collectorAnchorLink (colName ), template . HTML (strconv .Itoa (count ))})
523
+ rows = append (rows , []Cell {collectorAnchorLink (colName ), NewCell (strconv .Itoa (count ))})
520
524
}
521
525
return rows
522
526
}(),
523
527
})
524
528
WriteHTMLPageFooter (c .Writer )
525
529
}
526
530
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
+ }
529
536
}
530
537
531
538
func (s * Server ) CollectorHTMLHandler (c * gin.Context ) {
@@ -581,14 +588,14 @@ func (s *Server) CollectorHTMLHandler(c *gin.Context) {
581
588
})
582
589
WriteHTMLPropertiesTable (c .Writer , PropertiesTableData {
583
590
Headers : []string {"Job" , "Target" , "Endpoint Slice" },
584
- Rows : func () [][]template. HTML {
585
- var rows [][]template. HTML
591
+ Rows : func () [][]Cell {
592
+ var rows [][]Cell
586
593
for _ , v := range s .sortedTargetItems () {
587
594
if v .CollectorName == collectorId {
588
- rows = append (rows , []template. HTML {
595
+ rows = append (rows , []Cell {
589
596
jobAnchorLink (v .JobName ),
590
597
targetAnchorLink (v ),
591
- template . HTML (v .GetEndpointSliceName ()),
598
+ NewCell (v .GetEndpointSliceName ()),
592
599
})
593
600
}
594
601
}
@@ -598,8 +605,11 @@ func (s *Server) CollectorHTMLHandler(c *gin.Context) {
598
605
WriteHTMLPageFooter (c .Writer )
599
606
}
600
607
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
+ }
603
613
}
604
614
func (s * Server ) ScrapeConfigsHTMLHandler (c * gin.Context ) {
605
615
c .Writer .Header ().Set ("X-Content-Type-Options" , "nosniff" )
@@ -620,23 +630,16 @@ func (s *Server) ScrapeConfigsHTMLHandler(c *gin.Context) {
620
630
621
631
WriteHTMLPropertiesTable (c .Writer , PropertiesTableData {
622
632
Headers : []string {"Job" , "Scrape Config" },
623
- Rows : func () [][]template. HTML {
624
- var rows [][]template. HTML
633
+ Rows : func () [][]Cell {
634
+ var rows [][]Cell
625
635
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
- }
631
636
// pretty print the JSON
632
- scrapeConfigJSON , err = json .MarshalIndent (scrapeConfig , "" , " " )
637
+ scrapeConfigJSON , err : = json .MarshalIndent (scrapeConfig , "" , " " )
633
638
if err != nil {
634
639
s .errorHandler (c .Writer , err )
635
640
return nil
636
641
}
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 }})
640
643
}
641
644
return rows
642
645
}(),
0 commit comments