Skip to content

Commit

Permalink
Update weaver-gke to the base weaver version 0.18.0.
Browse files Browse the repository at this point in the history
Changes required for the upgrade:
  * Use the new versioning libraries.
  * Local copy of the version command, since the corresponding
    command in weaver has been moved to its internal/.
  * Change the protocol to use `protos.TraceSpans`.
  * Trace dashboard changes.
  • Loading branch information
spetrovic77 committed Jul 24, 2023
1 parent d3ed949 commit 3848a74
Show file tree
Hide file tree
Showing 15 changed files with 484 additions and 117 deletions.
124 changes: 110 additions & 14 deletions cmd/weaver-gke-local/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@ package main

import (
"context"
_ "embed"
"flag"
"fmt"
"html/template"
"net/http"
"net/url"
"os"
"time"

"github.com/ServiceWeaver/weaver-gke/internal/local"
"github.com/ServiceWeaver/weaver-gke/internal/local/metricdb"
"github.com/ServiceWeaver/weaver-gke/internal/tool"
"github.com/ServiceWeaver/weaver/runtime/logging"
"github.com/ServiceWeaver/weaver/runtime/perfetto"
"github.com/ServiceWeaver/weaver/runtime/traces"
)

var (
//go:embed templates/traces.html
tracesHTML string
tracesTemplate = template.Must(template.New("traces").Funcs(template.FuncMap{
"sub": func(endTime, startTime time.Time) string {
return endTime.Sub(startTime).String()
},
}).Parse(tracesHTML))
)

var dashboardSpec = tool.DashboardSpec{
Expand All @@ -43,26 +56,25 @@ var dashboardSpec = tool.DashboardSpec{
}
mux.Handle("/metrics", local.NewPrometheusHandler(metricDB, logger))

// Start a separate Perfetto server, which has to run on
// a specific port.
db, err := perfetto.Open(ctx, "gke-local")
// Add the trace handlers.
traceDB, err := traces.OpenDB(ctx, local.TracesFile)
if err != nil {
return err
}
go func() {
defer db.Close()
if db.Serve(ctx); err != nil {
fmt.Fprintln(os.Stderr, "Error serving local traces", err)
}
}()
mux.HandleFunc("/traces", func(w http.ResponseWriter, r *http.Request) {
handleTraces(w, r, traceDB)
})
mux.HandleFunc("/tracefetch", func(w http.ResponseWriter, r *http.Request) {
handleTraceFetch(w, r, traceDB)
})

return nil
},
AppLinks: func(ctx context.Context, app string) (tool.Links, error) {
v := url.Values{}
v.Set("app", app)
tracerURL := url.QueryEscape("http://127.0.0.1:9001?" + v.Encode())
return tool.Links{
Traces: "https://ui.perfetto.dev/#!/?url=" + tracerURL,
Traces: "/traces?" + v.Encode(),
Metrics: "/metrics?" + v.Encode(),
}, nil
},
Expand All @@ -71,9 +83,8 @@ var dashboardSpec = tool.DashboardSpec{
v := url.Values{}
v.Set("app", app)
v.Set("version", version)
tracerURL := url.QueryEscape("http://127.0.0.1:9001?" + v.Encode())
return tool.Links{
Traces: "https://ui.perfetto.dev/#!/?url=" + tracerURL,
Traces: "/traces?" + v.Encode(),
Metrics: "/metrics?" + v.Encode(),
}, nil
},
Expand All @@ -94,3 +105,88 @@ var dashboardSpec = tool.DashboardSpec{
}
},
}

// handleTraces handles requests to /traces?app=<app>&version=<app_version>
func handleTraces(w http.ResponseWriter, r *http.Request, db *traces.DB) {
app := r.URL.Query().Get("app")
version := r.URL.Query().Get("version")
if app == "" && version == "" {
http.Error(w, "neither application name or version id provided", http.StatusBadRequest)
}
parseDuration := func(arg string) (time.Duration, bool) {
str := r.URL.Query().Get(arg)
if str == "" {
return 0, true
}
dur, err := time.ParseDuration(str)
if err != nil {
http.Error(w, fmt.Sprintf("invalid duration %q", str), http.StatusBadRequest)
return 0, false
}
return dur, true
}
latencyLower, ok := parseDuration("lat_low")
if !ok {
return
}
latencyUpper, ok := parseDuration("lat_hi")
if !ok {
return
}
onlyErrors := r.URL.Query().Get("errs") != ""

// Weavelets export traces every 5 seconds. In order to (semi-)guarantee
// that the database contains all spans for the selected traces, we only
// fetch traces that ended more than 5+ seconds ago (all spans for such
// traces should have been exported to the database by now).
const exportInterval = 5 * time.Second
const gracePeriod = time.Second
endTime := time.Now().Add(-1 * (exportInterval + gracePeriod))

const maxNumTraces = 100
ts, err := db.QueryTraces(r.Context(), app, version, time.Time{} /*startTime*/, endTime, latencyLower, latencyUpper, onlyErrors, maxNumTraces)
if err != nil {
http.Error(w, fmt.Sprintf("cannot query trace database: %v", err), http.StatusInternalServerError)
return
}

content := struct {
Tool string
App string
Version string
Traces []traces.TraceSummary
}{
Tool: "gke-local",
App: app,
Version: version,
Traces: ts,
}
if err := tracesTemplate.Execute(w, content); err != nil {
http.Error(w, fmt.Sprintf("cannot display traces: %v", err), http.StatusInternalServerError)
return
}
}

// handleTraceFetch handles requests to /tracefetch?trace_id=<trace_id>.
func handleTraceFetch(w http.ResponseWriter, r *http.Request, db *traces.DB) {
traceID := r.URL.Query().Get("trace_id")
if traceID == "" {
http.Error(w, fmt.Sprintf("invalid trace id %q", traceID), http.StatusBadRequest)
return
}
spans, err := db.FetchSpans(r.Context(), traceID)
if err != nil {
http.Error(w, fmt.Sprintf("cannot fetch spans: %v", err), http.StatusInternalServerError)
return
}
if len(spans) == 0 {
http.Error(w, "no matching spans", http.StatusNotFound)
return
}
data, err := perfetto.EncodeSpans(spans)
if err != nil {
http.Error(w, fmt.Sprintf("cannot encode spans: %v", err), http.StatusInternalServerError)
return
}
w.Write(data) //nolint:errcheck // response write error
}
2 changes: 1 addition & 1 deletion cmd/weaver-gke-local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
"kill": gketool.KillCmd(&killSpec),
"profile": gketool.ProfileCmd(&profileSpec),
"dashboard": gketool.DashboardCmd(&dashboardSpec),
"version": tool.VersionCmd("weaver gke-local"),
"version": &versionCmd,
"purge": tool.PurgeCmd(&purgeSpec),

// Hidden commands.
Expand Down
122 changes: 122 additions & 0 deletions cmd/weaver-gke-local/templates/traces.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<!DOCTYPE html>
<!--
Copyright 2023 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.
-->

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.Tool}} Dashboard</title>
<link href="/assets/main.css" rel="stylesheet" />
<!-- https://css-tricks.com/emoji-as-a-favicon/ -->
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🧶</text></svg>">
<style>
/* Style for the trace table. */
#traces {
width: 100%;
}
#traces th, #traces td {
border: 1pt solid black;
}
</style>
</head>

<body>
<header class="navbar">
<a href="/">{{.Tool}} dashboard</a>
</header>
<script type="text/javascript">
// The code below largely taken from:
// https://perfetto.dev/docs/visualization/deep-linking-to-perfetto-ui
const ORIGIN = 'https://ui.perfetto.dev';

async function fetchAndOpen(traceUrl) {
const resp = await fetch(traceUrl);
const blob = await resp.blob();
const arrayBuffer = await blob.arrayBuffer();
openTrace(arrayBuffer, traceUrl);
}

function openTrace(arrayBuffer, traceId, traceUrl) {
const win = window.open(ORIGIN);
if (!win) {
alert('Popups blocked. Please allow popups in order to be able to' +
'see traces');
return
}
const timer = setInterval(() => win.postMessage('PING', ORIGIN), 50);
const onMessageHandler = (evt) => {
if (evt.data !== 'PONG') return;

// We got a PONG, the UI is ready.
window.clearInterval(timer);
window.removeEventListener('message', onMessageHandler);

const reopenUrl = new URL(location.href);
reopenUrl.hash = `#reopen=${traceUrl}`;
win.postMessage({
perfetto: {
buffer: arrayBuffer,
title: 'Trace Id ' + traceId,
url: reopenUrl.toString(),
}}, ORIGIN);
};

window.addEventListener('message', onMessageHandler);
}
</script>
<div class="container">
<div class="card">
<div class="card-title">Traces</div>
<div class="card-body">
<table id = buckets class = "data-table">
<tbody>
<tr>
<td><a href="/traces?app={{.App}}&version={{.Version}}&lat_hi=1ms">0-1ms</a></td>
<td><a href="/traces?app={{.App}}&version={{.Version}}&lat_low=1ms&lat_hi=10ms">1-10ms</a></td>
<td><a href="/traces?app={{.App}}&version={{.Version}}&lat_low=10ms&lat_hi=100ms">10-100ms</a></td>
<td><a href="/traces?app={{.App}}&version={{.Version}}&lat_low=100ms&lat_hi=1s">100ms-1s</a></td>
<td><a href="/traces?app={{.App}}&version={{.Version}}&lat_low=1s&lat_hi=10s">1-10s</a></td>
<td><a href="/traces?app={{.App}}&version={{.Version}}">all</a></td>
<td><a href="/traces?app={{.App}}&version={{.Version}}&errs=true">errors</a></td>
</tr>
</tbody>
</table>
<br>
<table id="traces" class="data-table">
<thead>
<tr>
<th scope="col">Trace URL</th>
<th scope="col">Start Time</th>
<th scope="col">Latency</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
{{range .Traces}}
<tr>
<td><a href="javascript:fetchAndOpen('/tracefetch?trace_id={{.TraceID}}')">link</a></td>
<td>{{.StartTime}}</td>
<td>{{sub .EndTime .StartTime}}</td>
<td>{{.Status}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
</body>
</html>
36 changes: 36 additions & 0 deletions cmd/weaver-gke-local/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023 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 main

import (
"context"
"flag"
"fmt"
"runtime"

"github.com/ServiceWeaver/weaver-gke/internal/version"
"github.com/ServiceWeaver/weaver/runtime/tool"
)

var versionCmd = tool.Command{
Name: "version",
Flags: flag.NewFlagSet("version", flag.ContinueOnError),
Description: "Show weaver gke-local version",
Help: "Usage:\n weaver gke-local version",
Fn: func(context.Context, []string) error {
fmt.Printf("weaver gke-local v%d.%d.%d %s/%s\n", version.Major, version.Minor, version.Patch, runtime.GOOS, runtime.GOARCH)
return nil
},
}
2 changes: 1 addition & 1 deletion cmd/weaver-gke/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
"kill": gketool.KillCmd(&killSpec),
"store": gketool.StoreCmd(&storeSpec),
"profile": gketool.ProfileCmd(&profileSpec),
"version": tool.VersionCmd("weaver gke"),
"version": &versionCmd,
"purge": &purgeCmd,

// Hidden commands.
Expand Down
36 changes: 36 additions & 0 deletions cmd/weaver-gke/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023 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 main

import (
"context"
"flag"
"fmt"
"runtime"

"github.com/ServiceWeaver/weaver-gke/internal/version"
"github.com/ServiceWeaver/weaver/runtime/tool"
)

var versionCmd = tool.Command{
Name: "version",
Flags: flag.NewFlagSet("version", flag.ContinueOnError),
Description: "Show weaver gke version",
Help: "Usage:\n weaver gke version",
Fn: func(context.Context, []string) error {
fmt.Printf("weaver gke v%d.%d.%d %s/%s\n", version.Major, version.Minor, version.Patch, runtime.GOOS, runtime.GOARCH)
return nil
},
}
Loading

0 comments on commit 3848a74

Please sign in to comment.