Skip to content

Commit

Permalink
Fill debug interceptor TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
t0yv0 committed Nov 1, 2022
1 parent ffa294d commit 84af240
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
58 changes: 58 additions & 0 deletions pkg/resource/deploy/debug_interceptors.go
@@ -0,0 +1,58 @@
// Copyright 2016-2022, Pulumi Corporation.
//
// 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 deploy

import (
debug "github.com/pulumi/grpc-debug-interceptors"
"os"

"github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil"
)

func serveWithDebugInterceptorsIfEnabled(opts rpcutil.ServeOptions) (rpcutil.ServeHandle, error) {
if debugInterceptorsEnabled() {
return serveWithDebugInterceptors(opts)
}
return rpcutil.ServeWithOptions(opts)
}

func debugInterceptorsEnabled() bool {
if logFile := os.Getenv("PULUMI_DEBUG_GRPC"); logFile != "" {
return true
}
return false
}

func serveWithDebugInterceptors(opts rpcutil.ServeOptions) (rpcutil.ServeHandle, error) {
metadata := map[string]interface{}{
"port": opts.Port,
"mode": "server",
}
opts = withDebugInterceptors(metadata, opts)
handle, err := rpcutil.ServeWithOptions(opts)
if err != nil {
return handle, err
}
metadata["port"] = handle.Port
return handle, err
}

func withDebugInterceptors(metadata interface{}, opts rpcutil.ServeOptions) rpcutil.ServeOptions {
i := debug.DebugServerInterceptor(metadata)
opts.Interceptors = append(opts.Interceptors, i)
s := debug.DebugStreamServerInterceptor(metadata)
opts.StreamInterceptors = append(opts.StreamInterceptors, s)
return opts
}
4 changes: 1 addition & 3 deletions pkg/resource/deploy/source_eval.go
Expand Up @@ -528,9 +528,7 @@ func newResourceMonitor(src *evalSource, provs ProviderSource, regChan chan *reg
}

// Fire up a gRPC server and start listening for incomings.

// TODO setup debug interceptors.
handle, err := rpcutil.ServeWithOptions(rpcutil.ServeOptions{
handle, err := serveWithDebugInterceptorsIfEnabled(rpcutil.ServeOptions{
Cancel: resmon.cancel,
Init: func(srv *grpc.Server) error {
pulumirpc.RegisterResourceMonitorServer(srv, resmon)
Expand Down
3 changes: 1 addition & 2 deletions pkg/resource/deploy/source_query.go
Expand Up @@ -249,8 +249,7 @@ func newQueryResourceMonitor(
}

// Fire up a gRPC server and start listening for incomings.
// TODO debug interceptors
handle, err := rpcutil.ServeWithOptions(rpcutil.ServeOptions{
handle, err := serveWithDebugInterceptorsIfEnabled(rpcutil.ServeOptions{
Cancel: queryResmon.cancel,
Init: func(srv *grpc.Server) error {
pulumirpc.RegisterResourceMonitorServer(srv, queryResmon)
Expand Down

0 comments on commit 84af240

Please sign in to comment.