Skip to content

Commit

Permalink
[prism] Add a debugz page to standalone web ui. (#28183)
Browse files Browse the repository at this point in the history
  • Loading branch information
lostluck authored Aug 28, 2023
1 parent 252c713 commit 2d5aced
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 2 deletions.
20 changes: 20 additions & 0 deletions sdks/go/pkg/beam/runners/prism/internal/web/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ header {
align-items: center;
}

footer {
width: 100%;
height: 50px;
position: absolute;
bottom: 0;
left: 0;
right: 0;
display: flex;
justify-content: space-between;
background-color: var(--beam-light-orange);
padding: 5px 10px;
align-items: center;
}

.logo {
color: var(--beam-white);
}
Expand Down Expand Up @@ -125,6 +139,12 @@ header {
padding-bottom: 10px;
}

footer {
flex-direction: column;
height: auto;
padding-top: 10px;
}

.logo {
display: inline-block;
margin-bottom: 10px;
Expand Down
150 changes: 150 additions & 0 deletions sdks/go/pkg/beam/runners/prism/internal/web/debugz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 web

import (
"fmt"
"runtime/debug"
"runtime/metrics"
"runtime/pprof"
"strings"
)

type debugzData struct {
Metrics []goRuntimeMetric

errorHolder
}

type goRuntimeMetric struct {
Name, Value, Description string
}

func dumpMetrics() debugzData {
// Get descriptions for all supported metrics.
descs := metrics.All()

// Create a sample for each metric.
samples := make([]metrics.Sample, len(descs))
for i := range samples {
samples[i].Name = descs[i].Name
}

// Sample the metrics. Re-use the samples slice if you can!
metrics.Read(samples)

var data debugzData

// Iterate over all results.
for i, sample := range samples {
// Pull out the name and value.
name, value := sample.Name, sample.Value

m := goRuntimeMetric{
Name: name,
Description: descs[i].Description,
}

// Handle each sample.
switch value.Kind() {
case metrics.KindUint64:
m.Value = fmt.Sprintf("%d", value.Uint64())
case metrics.KindFloat64:
m.Value = fmt.Sprintf("%f", value.Float64())
case metrics.KindFloat64Histogram:
m.Value = fmt.Sprintf("%f", medianBucket(value.Float64Histogram()))
// The histogram may be quite large, so let's just pull out
// a crude estimate for the median for the sake of this example.
case metrics.KindBad:
// This should never happen because all metrics are supported
// by construction.
m.Value = "bug in runtime/metrics package: KindBad"
default:
// This may happen as new metrics get added.
//
// The safest thing to do here is to simply log it somewhere
// as something to look into, but ignore it for now.
// In the worst case, you might temporarily miss out on a new metric.
m.Value = fmt.Sprintf("%s: unexpected metric Kind: %v - %s\n", name, value.Kind(), descs[i].Description)
}
data.Metrics = append(data.Metrics, m)
}

var b strings.Builder
buildInfo(&b)

data.Metrics = append(data.Metrics, goRuntimeMetric{
Name: "BUILD INFO",
Value: "n/a",
Description: b.String(),
})

b.Reset()
goroutineDump(&b)
data.Metrics = append(data.Metrics, goRuntimeMetric{
Name: "GOROUTINES",
Value: "n/a",
Description: b.String(),
})

b.Reset()
profiles(&b)

data.Metrics = append(data.Metrics, goRuntimeMetric{
Name: "PROFILES",
Value: b.String(),
Description: "List of available runtime/pprof profiles.",
})
return data
}

func goroutineDump(statusInfo *strings.Builder) {
profile := pprof.Lookup("goroutine")
if profile != nil {
profile.WriteTo(statusInfo, 1)
}
}

func buildInfo(statusInfo *strings.Builder) {
if info, ok := debug.ReadBuildInfo(); ok {
statusInfo.WriteString(info.String())
}
}

func profiles(statusInfo *strings.Builder) {
ps := pprof.Profiles()
statusInfo.WriteString(fmt.Sprintf("profiles %v\n", len(ps)))
for _, p := range ps {
statusInfo.WriteString(p.Name())
statusInfo.WriteRune('\n')
}
}

func medianBucket(h *metrics.Float64Histogram) float64 {
total := uint64(0)
for _, count := range h.Counts {
total += count
}
thresh := total / 2
total = 0
for i, count := range h.Counts {
total += count
if total >= thresh {
return h.Buckets[i]
}
}
panic("should not happen")
}
50 changes: 50 additions & 0 deletions sdks/go/pkg/beam/runners/prism/internal/web/debugz.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{{/*
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. See accompanying LICENSE file.
*/}}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Debug Metrics - Beam Prism</title>
<link rel="stylesheet" href="/assets/style.css" />
</head>

<body>
<main>
<header>
<a class="logo" href="/">Debug Metrics - Beam Prism</a>
</header>
<section class="container">
{{ if .Error}}{{.Error}}{{end}}
<table class="main-table">
<thead>
<td>ID</td>
<td>Name</td>
<td>State</td>
</thead>
{{ range .Metrics }}
<tr>
<td>{{ .Name }}</td>
<td style="white-space: pre">{{ .Value }}</td>
<td style="white-space: pre">{{ .Description }}</td>
</tr>
{{ else }}
<tr>
<td>No metrics have been returned.</td>
</tr>
{{ end }}
</table>
</section>
</main>
</body>
3 changes: 3 additions & 0 deletions sdks/go/pkg/beam/runners/prism/internal/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
{{ end }}
</table>
</section>
<footer>
<a class="logo" href="debugz">debugz</a>
</footer>
</main>
</body>
17 changes: 15 additions & 2 deletions sdks/go/pkg/beam/runners/prism/internal/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ var indexTemplate string
//go:embed jobdetails.html
var jobTemplate string

//go:embed debugz.html
var debugzTemplate string

//go:embed assets/*
var assets embed.FS

var (
indexPage = template.Must(template.New("index").Parse(indexTemplate))
jobPage = template.Must(template.New("job").Parse(jobTemplate))
indexPage = template.Must(template.New("index").Parse(indexTemplate))
jobPage = template.Must(template.New("job").Parse(jobTemplate))
debugzPage = template.Must(template.New("debugz").Parse(debugzTemplate))
)

type pTransform struct {
Expand Down Expand Up @@ -284,13 +288,22 @@ func (h *jobsConsoleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
renderPage(indexPage, data, w)
}

type debugzHandler struct {
}

func (h *debugzHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data := dumpMetrics()
renderPage(debugzPage, &data, w)
}

// Initialize the web client to talk to the given Job Management Client.
func Initialize(ctx context.Context, port int, jobcli jobpb.JobServiceClient) error {
assetsFs := http.FileServer(http.FS(assets))
mux := http.NewServeMux()

mux.Handle("/assets/", assetsFs)
mux.Handle("/job/", &jobDetailsHandler{Jobcli: jobcli})
mux.Handle("/debugz", &debugzHandler{})
mux.Handle("/", &jobsConsoleHandler{Jobcli: jobcli})

endpoint := fmt.Sprintf("localhost:%d", port)
Expand Down

0 comments on commit 2d5aced

Please sign in to comment.