cmd/wasmcloud: log querying

This commit is contained in:
Cadey Ratio 2019-12-08 21:44:12 +00:00
parent 275dc06a7b
commit 59d7356142
6 changed files with 87 additions and 2 deletions

View File

@ -0,0 +1,73 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/google/subcommands"
"within.website/x/writer"
)
type handlerLogsCmd struct{}
func (handlerLogsCmd) SetFlags(fs *flag.FlagSet) {}
func (handlerLogsCmd) Name() string { return "logs" }
func (handlerLogsCmd) Synopsis() string { return "shows logs for a handler" }
func (handlerLogsCmd) Usage() string {
return `wasmcloud logs <handler-name>
$ wasmcloud logs four-of-aether-60037
Returns all of the logs for a handler.
`
}
func (handlerLogsCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if fs.NArg() != 1 {
fmt.Println("usage: wasmcloud create [options] <filename>")
return subcommands.ExitUsageError
}
hname := fs.Arg(0)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, *apiServer+"/api/handler/logs?name="+hname, nil)
if err != nil {
log.Fatal(err)
}
withAPI(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
io.Copy(os.Stdout, resp.Body)
return subcommands.ExitFailure
}
type resultLine struct {
ExecID string `json:"exec_id"`
Comment string `json:"comment"`
Logs map[string]string `json:"logs"`
}
var result []resultLine
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
log.Fatal(err)
}
for _, line := range result {
wr := writer.LineSplitting(writer.PrefixWriter(line.ExecID[:5]+" | ", os.Stdout))
fmt.Fprintln(wr, line.Logs["logs.txt"])
}
return subcommands.ExitSuccess
}

View File

@ -21,6 +21,7 @@ func main() {
subcommands.Register(&loginCmd{}, "api")
subcommands.Register(&whoamiCmd{}, "api")
subcommands.Register(&handlerCreateCmd{}, "handlers")
subcommands.Register(handlerLogsCmd{}, "handlers")
subcommands.Register(namegenCmd{}, "utils")
subcommands.Register(&runCmd{}, "utils")
subcommands.ImportantFlag("api-server")

View File

@ -185,6 +185,11 @@ func invokeHandler(w http.ResponseWriter, r *http.Request, u *User) {
}
go func() {
f := ln.F{
"name": name,
"run_id": execID,
}
ln.Log(ctx, ln.Action("invoke-handler"), f)
msg, err := nc.Request(internal.TopicName, encData, 5*time.Minute)
if err != nil {
ln.Error(ctx, err)
@ -211,5 +216,7 @@ func invokeHandler(w http.ResponseWriter, r *http.Request, u *User) {
ln.Error(ctx, err)
return
}
ln.Log(ctx, ln.Action("saving-logs"), f)
}()
}

View File

@ -10,6 +10,7 @@ import (
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
nats "github.com/nats-io/nats.go"
"within.website/ln/ex"
)
var (
@ -66,5 +67,5 @@ func main() {
rtr.PathPrefix("/static/").Handler(http.FileServer(http.Dir(".")))
log.Printf("listening on http://wasmcloud.kahless.cetacean.club:%s", *port)
http.ListenAndServe(":"+*port, rtr)
http.ListenAndServe(":"+*port, ex.HTTPLog(rtr))
}

2
go.mod
View File

@ -23,5 +23,5 @@ require (
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392
within.website/ln v0.7.0
within.website/olin v0.1.1
within.website/x v1.2.0
within.website/x v1.2.1
)

3
go.sum
View File

@ -366,6 +366,7 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@ -448,3 +449,5 @@ within.website/olin v0.1.1 h1:y5kYkBMLcyZjt31d0bRiKhqVR4MEF3RTDk9uIjrvrds=
within.website/olin v0.1.1/go.mod h1:x+83GWNkYGcu7B9d+4CNk3+5KtaXrVzCJo0kq9tezXI=
within.website/x v1.2.0 h1:ZR/H2en2569p9og4Kx4hrDW2qyDtpVZ9upXOlCh/YGY=
within.website/x v1.2.0/go.mod h1:f6yg4hO/pprWxgRNHxKZU0Z/aKCsZq+WLg3iR8n1V6U=
within.website/x v1.2.1 h1:prNr9fWf/O7irD2hS/1na7GW/jiWdbuLnWNy1ZMkf+8=
within.website/x v1.2.1/go.mod h1:f6yg4hO/pprWxgRNHxKZU0Z/aKCsZq+WLg3iR8n1V6U=