forked from gravitational/teleport
/
latency.go
196 lines (169 loc) · 5.71 KB
/
latency.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Teleport
// Copyright (C) 2023 Gravitational, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package common
import (
"context"
"fmt"
"time"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/gravitational/trace"
"github.com/guptarohit/asciigraph"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/utils/diagnostics/latency"
)
// keyMap is used to display the help text at
// the bottom of the screen.
type keyMap struct {
quit key.Binding
}
func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k.quit}
}
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{{k.quit}}
}
var helpKeys = keyMap{
quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q", "quit"),
),
}
// latencyModel is a [tea.Model] that contains the state required
// to display the latency graph.
type latencyModel struct {
ready bool
h, w int
help help.Model
clientLabel, serverLabel string
clientLegend, serverLegend viewport.Model
clientMax, serverMax int64
clientLatency, serverLatency *utils.CircularBuffer
}
// newLatencyModel constructs a new latencyModel.
func newLatencyModel(clientLabel, serverLabel string) (*latencyModel, error) {
clientStats, err := utils.NewCircularBuffer(50)
if err != nil {
return nil, trace.Wrap(err)
}
serverStats, err := utils.NewCircularBuffer(50)
if err != nil {
return nil, trace.Wrap(err)
}
return &latencyModel{
clientLabel: clientLabel,
serverLabel: serverLabel,
help: help.New(),
clientLegend: viewport.New(1, 2),
serverLegend: viewport.New(1, 2),
clientLatency: clientStats,
serverLatency: serverStats,
}, nil
}
// Init is a noop but required to implement [tea.Model].
func (m *latencyModel) Init() tea.Cmd {
return nil
}
// Update reacts to screen resizing and new latency statistics.
func (m *latencyModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
}
case tea.WindowSizeMsg:
m.ready = true
m.h = msg.Height
m.w = msg.Width
m.clientLegend.Width = m.w / 2
m.serverLegend.Width = m.clientLegend.Width
case latency.Statistics:
m.clientLatency.Add(float64(msg.Client))
m.serverLatency.Add(float64(msg.Server))
m.clientMax = max(m.clientMax, msg.Client)
m.serverMax = max(m.serverMax, msg.Server)
m.clientLegend.SetContent(fmt.Sprintf("%s: last recorded %dms (max %dms)\t", m.clientLabel, msg.Client, m.clientMax))
m.serverLegend.SetContent(fmt.Sprintf("%s: last recorded %dms (max %dms)", m.serverLabel, msg.Server, m.serverMax))
}
return m, nil
}
var (
// clientColor is the color of the client plot and the client information in the legend.
clientColor = asciigraph.Blue
// serverColor is the color of the server plot and the client information in the legend.
serverColor = asciigraph.Goldenrod
clientStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(fmt.Sprintf("%d", clientColor)))
serverStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(fmt.Sprintf("%d", serverColor)))
)
// View renders the current latency graph to be displayed.
func (m *latencyModel) View() string {
if !m.ready {
return ""
}
clientData := m.clientLatency.Data(150)
serverData := m.serverLatency.Data(150)
if clientData == nil || serverData == nil {
return ""
}
legend := lipgloss.JoinHorizontal(
lipgloss.Top,
clientStyle.Render(m.clientLegend.View()),
serverStyle.Render(m.serverLegend.View()),
)
plot := asciigraph.PlotMany(
[][]float64{clientData, serverData},
asciigraph.Height(m.h-4),
asciigraph.Width(m.w),
asciigraph.SeriesColors(clientColor, serverColor),
asciigraph.Caption(legend),
)
return lipgloss.JoinVertical(lipgloss.Top, plot, lipgloss.PlaceHorizontal(m.w, lipgloss.Bottom, m.help.View(helpKeys)))
}
// showLatency creates a [latency.Monitor] using the provided [latency.Pinger]s to capture latency and
// render it to the terminal via a plot graph.
func showLatency(ctx context.Context, clientPinger, serverPinger latency.Pinger, clientLabel, serverLabel string) error {
m, err := newLatencyModel(clientLabel, serverLabel)
if err != nil {
return trace.Wrap(err)
}
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithContext(ctx), tea.WithoutSignalHandler())
monitor, err := latency.NewMonitor(latency.MonitorConfig{
InitialPingInterval: time.Millisecond,
InitialReportInterval: 500 * time.Millisecond,
PingInterval: time.Second,
ReportInterval: time.Second,
ClientPinger: clientPinger,
ServerPinger: serverPinger,
Reporter: latency.ReporterFunc(func(ctx context.Context, stats latency.Statistics) error {
p.Send(stats)
return nil
}),
})
if err != nil {
return trace.Wrap(err)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go monitor.Run(ctx)
if _, err := p.Run(); err != nil {
return trace.Wrap(err)
}
return nil
}