Skip to content

Commit 8d9ffff

Browse files
committed
tracer: display connections in an html table
This reformats the connection list into an HTML table and adds display of completed transactions (on the pool of connections). Durations of completed transactions are also displayed. Here's an example (pasted as text): Transactions (12) RO user identification 0s RO read ssh port 0s RO ssh get host key 0s RO xds make clusters 1ms RO serving: name resolution 0s RO xds make listeners 7ms RO telemetry operator info 1ms RO lookup nomad url 0s RO telemetry read identifier 0s RO lookup envoy admin address 0s RO ext_proc lookup serving api 0s RW traffic logging 0s
1 parent 4cc9d90 commit 8d9ffff

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

sqlstats/sqlstats.go

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,17 @@ type connStats struct {
7777
why string
7878
at time.Time
7979
readOnly bool
80+
done bool
81+
finished time.Time
8082
}
8183

8284
func (t *Tracer) done(s *connStats) (why string, readOnly bool) {
8385
s.mu.Lock()
8486
why = s.why
8587
readOnly = s.readOnly
8688
at := s.at
87-
s.why = ""
88-
s.at = time.Time{}
89-
s.readOnly = false
89+
s.done = true
90+
s.finished = time.Now()
9091
s.mu.Unlock()
9192

9293
if t.TxTotalSeconds != nil {
@@ -233,6 +234,8 @@ func (t *Tracer) BeginTx(
233234
s.why = why
234235
s.at = time.Now()
235236
s.readOnly = readOnly
237+
s.done = false
238+
s.finished = time.Time{}
236239
s.mu.Unlock()
237240

238241
if t.TxCount != nil {
@@ -282,12 +285,16 @@ func (t *Tracer) Rollback(id sqliteh.TraceConnID, err error) {
282285
}
283286
}
284287

288+
type txSummary struct {
289+
name string
290+
start time.Time
291+
readOnly bool
292+
done bool
293+
finished time.Time
294+
}
295+
285296
func (t *Tracer) HandleConns(w http.ResponseWriter, r *http.Request) {
286-
type txSummary struct {
287-
name string
288-
start time.Time
289-
readOnly bool
290-
}
297+
291298
var summary []txSummary
292299

293300
t.curTxs.Range(func(k, v any) bool {
@@ -298,6 +305,8 @@ func (t *Tracer) HandleConns(w http.ResponseWriter, r *http.Request) {
298305
name: s.why,
299306
start: s.at,
300307
readOnly: s.readOnly,
308+
done: s.done,
309+
finished: s.finished,
301310
})
302311
s.mu.Unlock()
303312

@@ -311,22 +320,36 @@ func (t *Tracer) HandleConns(w http.ResponseWriter, r *http.Request) {
311320
w.Header().Set("Content-Type", "text/html; charset=utf-8")
312321
w.WriteHeader(200)
313322
fmt.Fprintf(w, "<!DOCTYPE html><html><title>sqlite conns</title><body>\n")
314-
fmt.Fprintf(w, "<p>outstanding sqlite transactions: %d</p>\n", len(summary))
315-
fmt.Fprintf(w, "<pre>\n")
323+
fmt.Fprintf(w, "<h1>Transactions (%d)</h1>\n", len(summary))
324+
fmt.Fprintf(w, "<table>\n")
316325
for _, s := range summary {
317-
rw := ""
326+
rw := "RO"
318327
if !s.readOnly {
319-
rw = " read-write"
328+
rw = "RW"
320329
}
321330
fmt.Fprintf(
322331
w,
323-
"\n\t%s (%v)%s",
324-
html.EscapeString(s.name),
325-
now.Sub(s.start).Round(time.Millisecond),
332+
"\n\t<tr><td>%s</td><td>%s</td><td>%v</td></tr>",
326333
rw,
334+
emphasizeIfTrue(s.done, html.EscapeString(s.name)),
335+
displayDuration(now, s),
327336
)
328337
}
329-
fmt.Fprintf(w, "</pre></body></html>\n")
338+
fmt.Fprintf(w, "</table></body></html>\n")
339+
}
340+
341+
func emphasizeIfTrue(v bool, s string) string {
342+
if v {
343+
return "<i>" + s + "</i>"
344+
}
345+
return "<strong>" + s + "</strong>"
346+
}
347+
348+
func displayDuration(now time.Time, s txSummary) time.Duration {
349+
if s.done {
350+
return s.finished.Sub(s.start).Round(time.Millisecond)
351+
}
352+
return now.Sub(s.start).Round(time.Millisecond)
330353
}
331354

332355
func (t *Tracer) Handle(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)