Skip to content

Commit

Permalink
Fix log interception to respect TF_LOG (#1537)
Browse files Browse the repository at this point in the history
Expose more TF-originating logs for SDKv2 based resources.

The log interceptor code is moving from an internal package in the pf module to the root module so
it can be shared across tfbridge proper and SDKv2.

Instances of context.TODO() in SDKv2 providers are eliminated in favor of propagating the right
Context object which is crucial for sharing Logger objects as payload values on Context.

Subsystem adapters are added to capture errors and warnings emitted by SDKv2 framework against
github.com/hashicorp/terraform-plugin-log APIs. Note that provider code (e.g. AWS provider) also
sometimes chooses to emit logs via this API.

Fixed a small bug in log filtering where TF_LOG=x would actually filter out x level, so for example
TF_LOG=DEBUG would show only ERROR level logs.
  • Loading branch information
t0yv0 authored Nov 27, 2023
1 parent 972ee37 commit 48a4952
Show file tree
Hide file tree
Showing 17 changed files with 607 additions and 299 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ require (
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.19.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-registry-address v0.2.2 // indirect
github.com/hashicorp/vault/api v1.8.2 // indirect
github.com/hashicorp/vault/sdk v0.6.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion pf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ replace (
require (
github.com/blang/semver v3.5.1+incompatible
github.com/golang/protobuf v1.5.3
github.com/hashicorp/go-hclog v1.5.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/terraform-plugin-framework v1.4.1
github.com/hashicorp/terraform-plugin-framework-validators v0.10.0
Expand Down Expand Up @@ -38,6 +37,7 @@ require (
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
Expand Down
221 changes: 0 additions & 221 deletions pf/internal/logging/logging.go

This file was deleted.

6 changes: 3 additions & 3 deletions pf/tfbridge/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import (

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"

logutils "github.com/pulumi/pulumi-terraform-bridge/pf/internal/logging"
"github.com/pulumi/pulumi-terraform-bridge/v3/unstable/logging"
)

// Configures logging. Note that urn is optional but useful to identify logs with resources.
//
// See https://developer.hashicorp.com/terraform/plugin/log/writing
func (p *provider) initLogging(ctx context.Context, sink logutils.LogSink, urn resource.URN) context.Context {
return logutils.InitLogging(ctx, logutils.LogOptions{
func (p *provider) initLogging(ctx context.Context, sink logging.Sink, urn resource.URN) context.Context {
return logging.InitLogging(ctx, logging.LogOptions{
LogSink: sink,
URN: urn,
ProviderName: p.info.Name,
Expand Down
6 changes: 3 additions & 3 deletions pf/tfbridge/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ import (
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"

"github.com/pulumi/pulumi-terraform-bridge/pf/internal/convert"
logutils "github.com/pulumi/pulumi-terraform-bridge/pf/internal/logging"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/pfutils"
pl "github.com/pulumi/pulumi-terraform-bridge/pf/internal/plugin"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/schemashim"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi-terraform-bridge/v3/unstable/logging"
)

// Provider implements the Pulumi resource provider operations for any
Expand All @@ -60,7 +60,7 @@ type provider struct {
configEncoder convert.Encoder
configType tftypes.Object
version semver.Version
logSink logutils.LogSink
logSink logging.Sink

// Used by CheckConfig to remember the current Provider configuration so that it can be recalled and used for
// populating defaults specified via DefaultInfo.Config.
Expand Down Expand Up @@ -170,7 +170,7 @@ func newProviderWithContext(ctx context.Context, info tfbridge.ProviderInfo,
// Internal. The signature of this function can change between major releases. Exposed to facilitate testing.
func NewProviderServer(
ctx context.Context,
logSink logutils.LogSink,
logSink logging.Sink,
info tfbridge.ProviderInfo,
meta ProviderMetadata,
) (pulumirpc.ResourceProviderServer, error) {
Expand Down
8 changes: 3 additions & 5 deletions pkg/tfbridge/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,9 @@ type Log interface {

// Get access to the [Logger] associated with this context.
func GetLogger(ctx context.Context) Logger {
logger, ok := ctx.Value(logging.CtxKey).(Logger)
if !ok {
panic("Cannot call GetLogger on a context that is not equipped with a Logger")
}
return logger
logger := ctx.Value(logging.CtxKey)
contract.Assertf(logger != nil, "Cannot call GetLogger on a context that is not equipped with a Logger")
return newLoggerAdapter(logger)
}

func (info *ProviderInfo) GetConfig() map[string]*SchemaInfo {
Expand Down
35 changes: 24 additions & 11 deletions pkg/tfbridge/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import (

"github.com/pulumi/pulumi/pkg/v3/resource/provider"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"

"github.com/pulumi/pulumi-terraform-bridge/v3/unstable/logging"
)

// LogRedirector creates a new redirection writer that takes as input plugin stderr output, and routes it to the
Expand Down Expand Up @@ -118,12 +115,28 @@ func (lr *LogRedirector) Write(p []byte) (n int, err error) {
return written, nil
}

func ctxWithHostLogger(
ctx context.Context, host *provider.HostClient, urn resource.URN,
) context.Context {
return context.WithValue(ctx, logging.CtxKey,
logging.NewHost(ctx, host, urn,
func(l *logging.Host[Log]) Log {
return l
}))
type loggerAdapter struct {
Log
untyped untypedLogger
}

func (a *loggerAdapter) Status() Log {
return a.untyped.StatusUntyped().(Log)
}

var _ Logger = (*loggerAdapter)(nil)

type untypedLogger interface {
Log
StatusUntyped() any
}

func newLoggerAdapter(logger any) Logger {
uLogger, ok := logger.(untypedLogger)
contract.Assertf(ok, "Context carries a logger that does not implement UntypedLogger")

return &loggerAdapter{
Log: uLogger,
untyped: uLogger,
}
}
Loading

0 comments on commit 48a4952

Please sign in to comment.