Skip to content

Added gRPC debugger #1443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions cli/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ func NewCommand() *cobra.Command {
cmd.PersistentFlags().String("port", "", tr("The TCP port the daemon will listen to"))
configuration.Settings.BindPFlag("daemon.port", cmd.PersistentFlags().Lookup("port"))
cmd.Flags().BoolVar(&daemonize, "daemonize", false, tr("Do not terminate daemon process if the parent process dies"))
cmd.Flags().BoolVar(&debug, "debug", false, tr("Enable debug logging of gRPC calls"))
cmd.Flags().StringSliceVar(&debugFilters, "debug-filter", []string{}, tr("Display only the provided gRPC calls"))
return cmd
}

var daemonize bool
var debug bool
var debugFilters []string

func runDaemonCommand(cmd *cobra.Command, args []string) {

Expand All @@ -69,8 +73,14 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
defer stats.Flush()
}
port := configuration.Settings.GetString("daemon.port")
s := grpc.NewServer()

gRPCOptions := []grpc.ServerOption{}
if debug {
gRPCOptions = append(gRPCOptions,
grpc.UnaryInterceptor(unaryLoggerInterceptor),
grpc.StreamInterceptor(streamLoggerInterceptor),
)
}
s := grpc.NewServer(gRPCOptions...)
// Set specific user-agent for the daemon
configuration.Settings.Set("network.user_agent_ext", "daemon")

Expand Down
98 changes: 98 additions & 0 deletions cli/daemon/interceptors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package daemon

import (
"context"
"encoding/json"
"fmt"
"strings"

"google.golang.org/grpc"
)

func log(isRequest bool, msg interface{}) {
j, _ := json.MarshalIndent(msg, "| ", " ")
inOut := map[bool]string{true: "REQ: ", false: "RESP: "}
fmt.Println("| " + inOut[isRequest] + string(j))
}

func logError(err error) {
if err != nil {
fmt.Println("| ERROR: ", err)
}
}

func logSelector(method string) bool {
if len(debugFilters) == 0 {
return true
}
for _, filter := range debugFilters {
if strings.Contains(method, filter) {
return true
}
}
return false
}

func unaryLoggerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if !logSelector(info.FullMethod) {
return handler(ctx, req)
}
fmt.Println("CALLED:", info.FullMethod)
log(true, req)
resp, err := handler(ctx, req)
logError(err)
log(false, resp)
fmt.Println()
return resp, err
}

func streamLoggerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if !logSelector(info.FullMethod) {
return handler(srv, stream)
}
streamReq := ""
if info.IsClientStream {
streamReq = "STREAM_REQ "
}
if info.IsServerStream {
streamReq += "STREAM_RESP"
}
fmt.Println("CALLED:", info.FullMethod, streamReq)
err := handler(srv, &loggingServerStream{ServerStream: stream})
logError(err)
fmt.Println()
return err
}

type loggingServerStream struct {
grpc.ServerStream
}

func (l *loggingServerStream) RecvMsg(m interface{}) error {
err := l.ServerStream.RecvMsg(m)
logError(err)
log(true, m)
return err
}

func (l *loggingServerStream) SendMsg(m interface{}) error {
err := l.ServerStream.SendMsg(m)
logError(err)
log(false, m)
return err
}
16 changes: 12 additions & 4 deletions i18n/data/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ msgstr "Disable completion description for shells that support it"
msgid "Disconnected"
msgstr "Disconnected"

#: cli/daemon/daemon.go:60
msgid "Display only the provided gRPC calls"
msgstr "Display only the provided gRPC calls"

#: cli/lib/install.go:49
msgid "Do not install dependencies."
msgstr "Do not install dependencies."
Expand Down Expand Up @@ -586,6 +590,10 @@ msgstr "Downloads one or more cores and corresponding tool dependencies."
msgid "Downloads one or more libraries without installing them."
msgstr "Downloads one or more libraries without installing them."

#: cli/daemon/daemon.go:59
msgid "Enable debug logging of gRPC calls"
msgstr "Enable debug logging of gRPC calls"

#: cli/lib/install.go:51
msgid "Enter a path to zip file"
msgstr "Enter a path to zip file"
Expand Down Expand Up @@ -1019,19 +1027,19 @@ msgstr "Failed to create data directory"
msgid "Failed to create downloads directory"
msgstr "Failed to create downloads directory"

#: cli/daemon/daemon.go:114
#: cli/daemon/daemon.go:124
msgid "Failed to listen on TCP port: %[1]s. %[2]s is an invalid port."
msgstr "Failed to listen on TCP port: %[1]s. %[2]s is an invalid port."

#: cli/daemon/daemon.go:108
#: cli/daemon/daemon.go:118
msgid "Failed to listen on TCP port: %[1]s. %[2]s is unknown name."
msgstr "Failed to listen on TCP port: %[1]s. %[2]s is unknown name."

#: cli/daemon/daemon.go:123
#: cli/daemon/daemon.go:133
msgid "Failed to listen on TCP port: %[1]s. Unexpected error: %[2]v"
msgstr "Failed to listen on TCP port: %[1]s. Unexpected error: %[2]v"

#: cli/daemon/daemon.go:120
#: cli/daemon/daemon.go:130
msgid "Failed to listen on TCP port: %s. Address already in use."
msgstr "Failed to listen on TCP port: %s. Address already in use."

Expand Down
8 changes: 4 additions & 4 deletions i18n/rice-box.go

Large diffs are not rendered by default.