-
Notifications
You must be signed in to change notification settings - Fork 12
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
Remove p.Context
in favor of context.Context
#227
Conversation
d5eea55
to
d37ebe7
Compare
The new logger is based around a private `logSink` interface. If the provider is aware of a host, then we use native Pulumi logging. Otherwise we use log/slog logging into stdout. The logger is designed to ensure that `GetLogger(context.Background)` will still work, even if it doesn't use Pulumi's preferred logging mechanism.
This simplifies the API of a provider and makes it much easier to go back and forth between code designed to work with pulumi-go-provider and other Go code. Migration guide: - Replace all instances of `"github.com/pulumi/pulumi-go-provider".Context` with `context.Context`. - Methods on a `ctx "github.com/pulumi/pulumi-go-provider".Context` have become free-standing functions on `"github.com/pulumi/pulumi-go-provider"` - `ctx.RuntimeInformation` has become `"github.com/pulumi/pulumi-go-provider".GetRunInfo(ctx)`. - `ctx.Log*` has been replaced by `"github.com/pulumi/pulumi-go-provider".GetLogger(ctx).*`. - `"github.com/pulumi/pulumi-go-provider/infer".CtxFromPulumiContext(ctx)` has been replaced by `ctx.Context()`. I'm sorry to break existing users but I think this design is strictly better in the long term. Go has standardized on a `context.Context` type and the more Go's ecosystem gets behind it the easier everyone's life will be.
d37ebe7
to
6ffd425
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would like a little bit more clarity on how this code discovers which logger to use but this seems like a good change.
The only question I'd have is how many users would be affected.
@@ -12,27 +12,16 @@ | |||
// See the License for the specific language governing permissions and | |||
// limitations under the License. | |||
|
|||
package provider | |||
package key |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a few lines how this package helps us figure out what is available in our context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a few lines detailing why the package exists:
// key provides an internal set of keys for use with [context.WithValue] and
// [context.Context.Value] that can be shared across packages source.
//
// Each key has a private type (a `struct{}`) and a public instance of that type.
Can we add a few lines how this package helps us figure out what is available in our context?
keys
doesn't help us figure out what is available in our context. It exists purely so we can share keys between packages in pulumi-go-provider without leaking that implementation to our users.
Below is a patch to automate most of the migration (note The patch can be applied like so:
@@
var goprovider identifier
@@
import goprovider "github.com/pulumi/pulumi-go-provider"
+import "context"
-goprovider.Context
+context.Context
@@
var goprovider identifier
@@
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.RuntimeInformation()
+goprovider.GetRunInfo(ctx)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.Log(diag.Info, ...)
+goprovider.GetLogger(ctx).Info(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.Logf(diag.Info, ...)
+goprovider.GetLogger(ctx).Infof(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.LogStatus(diag.Info, ...)
+goprovider.GetLogger(ctx).InfoStatus(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.LogStatusf(diag.Info, ...)
+goprovider.GetLogger(ctx).InfoStatusf(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.Log(diag.Error, ...)
+goprovider.GetLogger(ctx).Error(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.Logf(diag.Error, ...)
+goprovider.GetLogger(ctx).Errorf(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.LogStatus(diag.Error, ...)
+goprovider.GetLogger(ctx).ErrorStatus(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.LogStatusf(diag.Error, ...)
+goprovider.GetLogger(ctx).ErrorStatusf(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.Log(diag.Debug, ...)
+goprovider.GetLogger(ctx).Debug(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.Logf(diag.Debug, ...)
+goprovider.GetLogger(ctx).Debugf(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.LogStatus(diag.Debug, ...)
+goprovider.GetLogger(ctx).DebugStatus(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.LogStatusf(diag.Debug, ...)
+goprovider.GetLogger(ctx).DebugStatusf(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.Log(diag.Warning, ...)
+goprovider.GetLogger(ctx).Warning(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.Logf(diag.Warning, ...)
+goprovider.GetLogger(ctx).Warningf(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.LogStatus(diag.Warning, ...)
+goprovider.GetLogger(ctx).WarningStatus(...)
@@
var goprovider identifier
var diag identifier
var ctx identifier
@@
import diag "github.com/pulumi/pulumi/sdk/v3/go/common/diag"
import goprovider "github.com/pulumi/pulumi-go-provider"
-ctx.LogStatusf(diag.Warning, ...)
+goprovider.GetLogger(ctx).WarningStatusf(...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! Let's make sure to update the boilerplate repo to reflect the new API.
Why not a resource controller as recommended in #159?
That sounds fine. I'll still probably embed some Logger structs for convenience, but that's something consumers can do to their liking.
ctx = integration.InjectTestLogger(ctx)
My only feedback is that it would be helpful to be able to inject a Logger
interface, something like
ctx = integration.WithLogger(ctx, l)
and a default integration.TestLogger
could be available.
This isn't part of this PR so there's no need to block.
I'm sorry to break existing users but I think this design is strictly better in the long term.
Strongly agree!
Good call out! |
This upgrades pulumi-go-provider to latest. Code was automatically migrated using a slightly modified patch described [here](pulumi/pulumi-go-provider#227 (comment)). --------- Co-authored-by: Ian Wahbe <ian@wahbe.com>
There were two breaking changes since, pulumi/pulumi-go-provider#227 and pulumi/pulumi-go-provider#231. Hopefully fixes #435
Fixes #159
This change comes in two parts:
Logger
, accessed viap.GetLogger(ctx)
. Usage looks like thisp.GetLogger
function has two "sink"s: ahostSink
that equates to calling logging methods on the server host (which nicely displays to the user) and a fallbackslogSink
which useslog/slog
to output structured error messages. The fallback sink ensures thatp.GetLogger(context.Background()).Debug("Constructing Config")
will still log. This should be useful only when testing.p.Context
tocontext.Context
. The change is entirely mechanical.Migration Guide (to be included in the release that goes out with this PR):
"github.com/pulumi/pulumi-go-provider".Context
withcontext.Context
.ctx "github.com/pulumi/pulumi-go-provider".Context
have becomefree-standing functions on
"github.com/pulumi/pulumi-go-provider"
ctx.RuntimeInformation
has become"github.com/pulumi/pulumi-go-provider".GetRunInfo(ctx)
.ctx.Log*
has been replaced by"github.com/pulumi/pulumi-go-provider".GetLogger(ctx).*
."github.com/pulumi/pulumi-go-provider/infer".CtxFromPulumiContext(ctx)
has beenreplaced by
ctx.Context()
.Design considerations
I decided on using a plain
context.Context
with freestanding functions wrappingcontext.Context.Value
to keep usage idiomatic to Go and to allow arbitrary middleware to extend the values hanging oncontext.Context
.Why not a resource controller as recommended in #159?
A centralized resource controller doesn't decompose well between the main package and the
infer
package. Consider the example given in the issue (adjusted forprovider
->infer
):Nits & Problems:
infer.NewController
except in testing, so I would much rather see test functions in their own package:testinfer.NewController
.RetainOnDelete
simply means theDelete
method will be skipped. Pulumi providers never see it.I'd prefer a design that looks like this:
This way all packages can extend the context in the same way.
I'm sorry to break existing users but I think this design is strictly better in the long term. Go has standardized on a
context.Context
type and the more Go's ecosystem gets behind it the easier everyone's life will be.