-
Notifications
You must be signed in to change notification settings - Fork 219
/
dashboard.go
313 lines (282 loc) · 8.98 KB
/
dashboard.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package status
import (
"bytes"
"context"
"embed"
"flag"
"fmt"
"html/template"
"net"
"net/http"
"net/url"
"os"
"sort"
"strings"
"time"
imetrics "github.com/ServiceWeaver/weaver/internal/metrics"
"github.com/ServiceWeaver/weaver/runtime/codegen"
"github.com/ServiceWeaver/weaver/runtime/logging"
"github.com/ServiceWeaver/weaver/runtime/metrics"
"github.com/ServiceWeaver/weaver/runtime/perfetto"
protos "github.com/ServiceWeaver/weaver/runtime/protos"
dtool "github.com/ServiceWeaver/weaver/runtime/tool"
"github.com/pkg/browser"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
var (
dashboardFlags = flag.NewFlagSet("dashboard", flag.ContinueOnError)
dashboardHost = dashboardFlags.String("host", "localhost", "Dashboard host")
dashboardPort = dashboardFlags.Int("port", 0, "Dashboart port")
//go:embed templates/index.html
indexHTML string
indexTemplate = template.Must(template.New("index").Parse(indexHTML))
//go:embed templates/deployment.html
deploymentHTML string
deploymentTemplate = template.Must(template.New("deployment").Funcs(template.FuncMap{
"shorten": logging.ShortenComponent,
"pidjoin": func(pids []int64) string {
s := make([]string, len(pids))
for i, x := range pids {
s[i] = fmt.Sprint(x)
}
return strings.Join(s, ", ")
},
"age": func(t *timestamppb.Timestamp) string {
return time.Since(t.AsTime()).Truncate(time.Second).String()
},
"dec": func(x int) int {
return x - 1
},
"traceurl": func(app, version string) string {
v := url.Values{}
v.Set("app", app)
v.Set("version", version)
tracerURL := url.QueryEscape("http://127.0.0.1:9001?" + v.Encode())
return "https://ui.perfetto.dev/#!/?url=" + tracerURL
},
}).Parse(deploymentHTML))
//go:embed assets/*
assets embed.FS
)
// A Command is a labeled terminal command that a user can run. We show these
// commands on the dashboard so that users can copy and run them.
type Command struct {
Label string // e.g., cat logs
Command string // e.g., weaver single logs '--version=="12345678"'
}
// DashboardSpec configures the command returned by DashboardCommand.
type DashboardSpec struct {
Tool string // tool name (e.g., "weaver single")
PerfettoFile string // perfetto database file
Registry func(context.Context) (*Registry, error) // registry of deployments
Commands func(deploymentId string) []Command // commands for a deployment
}
// DashboardCommand returns a "dashboard" subcommand that serves a dashboard
// with information about the active applications.
func DashboardCommand(spec *DashboardSpec) *dtool.Command {
const help = `Usage:
{{.Tool}} dashboard [--host=<host>] [--port=<port>]
Flags:
-h, --help Print this help message.
{{.Flags}}`
var b strings.Builder
t := template.Must(template.New("dashboard-help").Parse(help))
content := struct{ Tool, Flags string }{spec.Tool, dtool.FlagsHelp(dashboardFlags)}
if err := t.Execute(&b, content); err != nil {
panic(err)
}
return &dtool.Command{
Name: "dashboard",
Description: "Inspect Service Weaver applications",
Help: b.String(),
Flags: dashboardFlags,
Fn: func(ctx context.Context, _ []string) error {
r, err := spec.Registry(ctx)
if err != nil {
return err
}
dashboard := &dashboard{spec, r}
http.HandleFunc("/", dashboard.handleIndex)
http.HandleFunc("/favicon.ico", http.NotFound)
http.HandleFunc("/deployment", dashboard.handleDeployment)
http.HandleFunc("/metrics", dashboard.handleMetrics)
http.Handle("/assets/", http.FileServer(http.FS(assets)))
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *dashboardHost, *dashboardPort))
if err != nil {
return err
}
url := "http://" + lis.Addr().String()
traceDB, err := perfetto.Open(ctx, spec.PerfettoFile)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot open Perfetto database: %v\n", err)
} else {
go traceDB.Serve(ctx)
}
fmt.Fprintln(os.Stderr, "Dashboard available at:", url)
go browser.OpenURL(url) //nolint:errcheck // browser open is optional
return http.Serve(lis, nil)
},
}
}
// dashboard implements the "weaver dashboard" HTTP server.
type dashboard struct {
spec *DashboardSpec // e.g., "weaver multi" or "weaver single"
registry *Registry // registry of deployments
}
// handleIndex handles requests to /
func (d *dashboard) handleIndex(w http.ResponseWriter, r *http.Request) {
regs, err := d.registry.List(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var statuses []*Status
for _, reg := range regs {
status, err := NewClient(reg.Addr).Status(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
statuses = append(statuses, status)
}
content := struct {
Tool string
Statuses []*Status
}{
Tool: d.spec.Tool,
Statuses: statuses,
}
if err := indexTemplate.Execute(w, content); err != nil {
panic(err)
}
}
// handleDeployment handles requests to /deployment?id=<deployment id>
func (d *dashboard) handleDeployment(w http.ResponseWriter, r *http.Request) {
// TODO(mwhittaker): Change to /<deployment id>?
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "no deployment id provided", http.StatusBadRequest)
return
}
reg, err := d.registry.Get(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
client := NewClient(reg.Addr)
status, err := client.Status(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Sort components and methods so that they appear in a deterministic order
// on the deployment page.
sort.Slice(status.Components, func(i, j int) bool {
ci, cj := status.Components[i], status.Components[j]
if ci.Group != cj.Group {
return ci.Group < cj.Group
}
return ci.Name < cj.Name
})
for _, component := range status.Components {
sort.Slice(component.Methods, func(i, j int) bool {
return component.Methods[i].Name < component.Methods[j].Name
})
}
// Fetch metrics.
metrics, err := client.Metrics(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Display content.
content := struct {
*Status
Tool string
Traffic []edge
Commands []Command
}{
Status: status,
Tool: d.spec.Tool,
Traffic: computeTraffic(status, metrics.Metrics),
Commands: d.spec.Commands(id),
}
if err := deploymentTemplate.Execute(w, content); err != nil {
fmt.Println(err)
}
}
// An edge represents an edge in a traffic graph. If a component s calls n
// methods on component t, then an edge is formed from s to t with weight v.
type edge struct {
Source string // calling component
Target string // callee component
Value int // number of method calls
}
// computeTraffic calculates cross-component traffic.
func computeTraffic(status *Status, metrics []*protos.MetricSnapshot) []edge {
// Aggregate traffic by component.
type pair struct {
caller string
component string
}
byPair := map[pair]int{}
for _, metric := range metrics {
if metric.Name != codegen.MethodCounts.Name() {
continue
}
call := pair{
caller: metric.Labels["caller"],
component: metric.Labels["component"],
}
byPair[call] += int(metric.Value)
}
// Massage data into graph format.
var edges []edge
for call, value := range byPair {
edges = append(edges, edge{
Source: call.caller,
Target: call.component,
Value: value,
})
}
return edges
}
// handleMetrics handles requests to /metrics?id=<deployment id>
func (d *dashboard) handleMetrics(w http.ResponseWriter, r *http.Request) {
// TODO(mwhittaker): Change to /<deployment id>/metrics?
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "no deployment id provided", http.StatusBadRequest)
return
}
reg, err := d.registry.Get(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ms, err := NewClient(reg.Addr).Metrics(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
snapshots := make([]*metrics.MetricSnapshot, len(ms.Metrics))
for i, m := range ms.Metrics {
snapshots[i] = metrics.UnProto(m)
}
var b bytes.Buffer
imetrics.TranslateMetricsToPrometheusTextFormat(&b, snapshots, reg.Addr, prometheusEndpoint)
w.Write(b.Bytes()) //nolint:errcheck // response write error
}