github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/cortex/status.go (about)

     1  package cortex
     2  
     3  import (
     4  	"html/template"
     5  	"net/http"
     6  	"sort"
     7  	"time"
     8  
     9  	"github.com/cortexproject/cortex/pkg/util"
    10  )
    11  
    12  const tpl = `
    13  <!DOCTYPE html>
    14  <html>
    15  	<head>
    16  		<meta charset="UTF-8">
    17  		<title>Cortex Services Status</title>
    18  	</head>
    19  	<body>
    20  		<h1>Cortex Services Status</h1>
    21  		<p>Current time: {{ .Now }}</p>
    22  		<table border="1">
    23  			<thead>
    24  				<tr>
    25  					<th>Service</th>
    26  					<th>Status</th>
    27  				</tr>
    28  			</thead>
    29  			<tbody>
    30  				{{ range .Services }}
    31  				<tr>
    32  					<td>{{ .Name }}</td>
    33  					<td>{{ .Status }}</td>
    34  				</tr>
    35  				{{ end }}
    36  			</tbody>
    37  		</table>
    38  	</body>
    39  </html>`
    40  
    41  var tmpl *template.Template
    42  
    43  type renderService struct {
    44  	Name   string `json:"name"`
    45  	Status string `json:"status"`
    46  }
    47  
    48  func init() {
    49  	tmpl = template.Must(template.New("webpage").Parse(tpl))
    50  }
    51  
    52  func (t *Cortex) servicesHandler(w http.ResponseWriter, r *http.Request) {
    53  	w.WriteHeader(200)
    54  	w.Header().Set("Content-Type", "text/plain")
    55  
    56  	svcs := make([]renderService, 0)
    57  	for mod, s := range t.ServiceMap {
    58  		svcs = append(svcs, renderService{
    59  			Name:   mod,
    60  			Status: s.State().String(),
    61  		})
    62  	}
    63  	sort.Slice(svcs, func(i, j int) bool {
    64  		return svcs[i].Name < svcs[j].Name
    65  	})
    66  
    67  	// TODO: this could be extended to also print sub-services, if given service has any
    68  	util.RenderHTTPResponse(w, struct {
    69  		Now      time.Time       `json:"now"`
    70  		Services []renderService `json:"services"`
    71  	}{
    72  		Now:      time.Now(),
    73  		Services: svcs,
    74  	}, tmpl, r)
    75  }