Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dgottlieb committed Jan 28, 2025
1 parent c3daac7 commit 5c3446a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
12 changes: 7 additions & 5 deletions grpc/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type modNameKeyType int

const modNameKeyID = modNameKeyType(iota)

// GetName returns the debug log key included when enabling the context for debug logging.
// GetModuleName returns the module name (if any) the request came from. The module name will match
// a string from the robot config.
func GetModuleName(ctx context.Context) string {
valI := ctx.Value(modNameKeyID)
if val, ok := valI.(string); ok {
Expand All @@ -63,12 +64,13 @@ func GetModuleName(ctx context.Context) string {

const modNameMetadataKey = "modName"

// ModInterceptors takes a user input `ModName` and exposes an interceptor method that will attach
// it to outgoing gRPC requests.
type ModInterceptors struct {
ModName string
}

// UnaryClientInterceptor adds debug directives from the current context (if any) to the
// outgoing request's metadata.
// UnaryClientInterceptor adds a module name to any outgoing unary gRPC request.
func (mc *ModInterceptors) UnaryClientInterceptor(
ctx context.Context,
method string,
Expand All @@ -81,8 +83,8 @@ func (mc *ModInterceptors) UnaryClientInterceptor(
return invoker(ctx, method, req, reply, cc, opts...)
}

// UnaryServerInterceptor checks the incoming RPC metadata for a distributed tracing directive and
// attaches any information to a debug context.
// ModNameUnaryServerInterceptor checks the incoming RPC metadata for a module name and attaches any
// information to a context that can be retrieved with `GetModuleName`.
func ModNameUnaryServerInterceptor(
ctx context.Context,
req interface{},
Expand Down
4 changes: 2 additions & 2 deletions module/testmodule/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ func (h *helper) DoCommand(ctx context.Context, req map[string]interface{}) (map
case "get_num_reconfigurations":
return map[string]any{"num_reconfigurations": h.numReconfigurations}, nil
case "do_readings_on_dep":
h.dependsOnSensor.Readings(ctx, nil)
return nil, nil
_, err := h.dependsOnSensor.Readings(ctx, nil)
return nil, err
default:
return nil, fmt.Errorf("unknown command string %s", cmd)
}
Expand Down
2 changes: 2 additions & 0 deletions robot/client/client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func newFuncRobotClientOption(f func(*robotClientOpts)) *funcRobotClientOption {
}
}

// WithModName attaches a unary interceptor that attaches the module name for each outgoing gRPC
// request.
func WithModName(modName string) RobotClientOption {
return newFuncRobotClientOption(func(o *robotClientOpts) {
o.modName = modName
Expand Down
5 changes: 2 additions & 3 deletions robot/impl/local_robot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"go.viam.com/rdk/config"
"go.viam.com/rdk/examples/customresources/apis/gizmoapi"
"go.viam.com/rdk/examples/customresources/apis/summationapi"
"go.viam.com/rdk/grpc"
rgrpc "go.viam.com/rdk/grpc"
internalcloud "go.viam.com/rdk/internal/cloud"
"go.viam.com/rdk/logging"
Expand Down Expand Up @@ -4538,7 +4537,7 @@ func TestRemovingOfflineRemotes(t *testing.T) {

// TestModuleNamePassing asserts that module names are passed from viam-server -> module
// properly. Such that incoming requests from module -> viam-server identify themselves. And can be
// observed on contexts via `grpc.GetModuleName(ctx)`.
// observed on contexts via `[r]grpc.GetModuleName(ctx)`.
func TestModuleNamePassing(t *testing.T) {
logger := logging.NewTestLogger(t)

Expand All @@ -4553,7 +4552,7 @@ func TestModuleNamePassing(t *testing.T) {
moduleNameCh := make(chan string, 1)
callbackSensor := &inject.Sensor{
ReadingsFunc: func(ctx context.Context, extra map[string]any) (map[string]any, error) {
moduleNameCh <- grpc.GetModuleName(ctx)
moduleNameCh <- rgrpc.GetModuleName(ctx)
return map[string]any{
"reading": 42,
}, nil
Expand Down

0 comments on commit 5c3446a

Please sign in to comment.