Skip to content

Commit

Permalink
Introspection 7/7, /debug/yarpc debug page (#643)
Browse files Browse the repository at this point in the history
Finally, the suspens killing part of this serie of PRs, the yarpc debug page,
pretty printing the introspection data into HTML.
  • Loading branch information
bombela authored Dec 28, 2016
1 parent 164a6e1 commit 15bf66e
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 2 deletions.
197 changes: 197 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package yarpc

import (
"html/template"
"io"
"log"
"net/http"
"sync"
)

var (
dispatchersLock sync.RWMutex
dispatchers []*Dispatcher
)

func addDispatcherToDebugPages(disp *Dispatcher) {
dispatchersLock.Lock()
defer dispatchersLock.Unlock()

dispatchers = append(dispatchers, disp)
}

func removeDispatcherFromDebugPages(disp *Dispatcher) {
dispatchersLock.Lock()
defer dispatchersLock.Unlock()

for i, x := range dispatchers {
if x == disp {
copy(dispatchers[i:], dispatchers[i+1:])
dispatchers[len(dispatchers)-1] = nil
dispatchers = dispatchers[:len(dispatchers)-1]
break
}
}
}

func init() {
http.HandleFunc("/debug/yarpc", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
render(w, req)
})
}

func render(w io.Writer, req *http.Request) {
var data struct {
Dispatchers []dispatcherStatus
}

for _, disp := range dispatchers {
data.Dispatchers = append(data.Dispatchers, disp.introspect())
}

if err := pageTmpl.ExecuteTemplate(w, "Page", data); err != nil {
log.Printf("yarpc/debug: Failed executing template: %v", err)
}
}

var pageTmpl = template.Must(template.New("Page").Funcs(template.FuncMap{}).Parse(pageHTML))

const pageHTML = `
<html>
<head>
<title>/debug/yarpc</title>
<style type="text/css">
body {
font-family: "Courier New", Courier, monospace;
}
table {
color:#333333;
border-width: 1px;
border-color: #3A3A3A;
border-collapse: collapse;
}
table th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #3A3A3A;
background-color: #B3B3B3;
}
table td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #3A3A3A;
background-color: #ffffff;
}
</style>
</head>
<body>
<h1>/debug/yarpc</h1>
{{range .Dispatchers}}
<hr />
<h2>Dispatcher "{{.Name}}" <small>({{.ID}})</small></h2>
<table>
<tr>
<th>Service</th>
<th>Procedure</th>
<th>Encoding</th>
<th>Signature</th>
<th>Flavor</th>
</tr>
{{range .Procedures}}
<tr>
<td>{{.Service}}</td>
<td>{{.Name}}</td>
<td>{{.Encoding}}</td>
<td>{{.Signature}}</td>
<td>{{.HandlerSpec.Type}}</td>
</tr>
{{end}}
</table>
<h3>Inbounds</h3>
<table>
<tr>
<th>Transport</th>
<th>Endpoint</th>
<th>State</th>
</tr>
{{range .Inbounds}}
<tr>
<td>{{.Transport}}</td>
<td>{{.Endpoint}}</td>
<td>{{.State}}</td>
</tr>
{{end}}
</table>
<h3>Outbounds</h3>
<table>
<thead>
<tr>
<th>Service</th>
<th>Transport</th>
<th>Flavor</th>
<th>Endpoint</th>
<th>State</th>
<th colspan="3">Chooser</th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th>Name</th>
<th>State</th>
<th>Peers</th>
</tr>
</thead>
<tbody>
{{range .Outbounds}}
<tr>
<td>{{.Service}}</td>
<td>{{.Transport}}</td>
<td>{{.Flavor}}</td>
<td>{{.Endpoint}}</td>
<td>{{.State}}</td>
<td>{{.Chooser.Name}}</td>
<td>{{.Chooser.State}}</td>
<td>
<ul>
{{range .Chooser.Peers}}
<li>{{.Identifier}} ({{.State}})</li>
{{end}}
</ul>
</td>
</tr>
</tbody>
{{end}}
</table>
{{end}}
</body>
</html>
`
3 changes: 3 additions & 0 deletions dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ func (d *Dispatcher) Start() error {
return abort(errs)
}

addDispatcherToDebugPages(d)
return nil
}

Expand Down Expand Up @@ -380,6 +381,8 @@ func (d *Dispatcher) Stop() error {
if len(allErrs) > 0 {
return errors.ErrorGroup(allErrs)
}

removeDispatcherFromDebugPages(d)
return nil
}

Expand Down
12 changes: 10 additions & 2 deletions internal/examples/thrift-keyvalue/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
.PHONY: all
all:
.PHONY: all hello kvserver kvclient

all: hello kvserver kvclient

hello:
cd hello; go build -i

kvclient:
cd keyvalue/client; go build -i

kvserver:
cd keyvalue/server; go build -i

0 comments on commit 15bf66e

Please sign in to comment.