pulumi/sdk/nodejs/log/index.ts

139 lines
4.8 KiB
TypeScript
Raw Permalink Normal View History

2018-05-22 19:43:36 +00:00
// Copyright 2016-2018, 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.
// The log module logs messages in a way that tightly integrates with the resource engine's interface.
import * as engrpc from "../proto/engine_grpc_pb";
import * as engproto from "../proto/engine_pb";
import * as resourceTypes from "../resource";
2017-11-26 18:04:15 +00:00
import { getEngine, rpcKeepAlive } from "../runtime/settings";
Recover after a failed automatation run (#14702) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes #12521 The log module for the nodejs SDK keeps track of an error count in a global, this should be stored in the stack's local store. ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-01 17:35:07 +00:00
import { getStore } from "../runtime/state";
let lastLog: Promise<any> = Promise.resolve();
const messageLevels = {
[engproto.LogSeverity.DEBUG]: "debug",
[engproto.LogSeverity.INFO]: "info",
[engproto.LogSeverity.WARNING]: "warn",
[engproto.LogSeverity.ERROR]: "error",
};
/**
* Returns true if any errors have occurred in the program.
*/
export function hasErrors(): boolean {
Recover after a failed automatation run (#14702) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes #12521 The log module for the nodejs SDK keeps track of an error count in a global, this should be stored in the stack's local store. ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-01 17:35:07 +00:00
return getStore().logErrorCount > 0;
}
/**
* Logs a debug-level message that is generally hidden from end-users.
*/
export function debug(msg: string, resource?: resourceTypes.Resource, streamId?: number, ephemeral?: boolean) {
const engine = getEngine();
if (engine) {
return log(engine, engproto.LogSeverity.DEBUG, msg, resource, streamId, ephemeral);
} else {
return Promise.resolve();
}
}
/**
* Logs an informational message that is generally printed to standard output
* during resource operations.
*/
export function info(msg: string, resource?: resourceTypes.Resource, streamId?: number, ephemeral?: boolean) {
const engine = getEngine();
if (engine) {
return log(engine, engproto.LogSeverity.INFO, msg, resource, streamId, ephemeral);
} else {
console.log(`info: [runtime] ${msg}`);
return Promise.resolve();
}
}
/**
* Logs a warning to indicate that something went wrong, but not
* catastrophically so.
*/
export function warn(msg: string, resource?: resourceTypes.Resource, streamId?: number, ephemeral?: boolean) {
const engine = getEngine();
if (engine) {
return log(engine, engproto.LogSeverity.WARNING, msg, resource, streamId, ephemeral);
} else {
console.warn(`warning: [runtime] ${msg}`);
return Promise.resolve();
}
}
/**
* Logs a fatal condition. Consider raising an exception after calling error to
* stop the Pulumi program.
*/
export function error(msg: string, resource?: resourceTypes.Resource, streamId?: number, ephemeral?: boolean) {
Recover after a failed automatation run (#14702) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes #12521 The log module for the nodejs SDK keeps track of an error count in a global, this should be stored in the stack's local store. ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-01 17:35:07 +00:00
getStore().logErrorCount++; // remember the error so we can suppress leaks.
const engine = getEngine();
if (engine) {
return log(engine, engproto.LogSeverity.ERROR, msg, resource, streamId, ephemeral);
} else {
console.error(`error: [runtime] ${msg}`);
return Promise.resolve();
}
}
function log(
Move nodejs feature checks to startup (#14856) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> We need to synchronously check for transform support in `registerStackTransformation` and resource constructors when adding remote transform support to node (c.f. https://github.com/pulumi/pulumi/pull/14303). This change changes all the feature checks to be done at startup and then accessed via just a field lookup. Adding the "transform" feature to this set is clearly simple. Sadly there's no single entry point to make these changes in. So we need to update entry point of construct/call, the entry point of programs, and test setup. Miss any one of these and current tests start failing. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-19 14:35:23 +00:00
engine: engrpc.IEngineClient,
sev: engproto.LogSeverity,
msg: string,
resource: resourceTypes.Resource | undefined,
streamId: number | undefined,
ephemeral: boolean | undefined,
): Promise<void> {
// Ensure we log everything in serial order.
const keepAlive: () => void = rpcKeepAlive();
const urnPromise = resource ? resource.urn.promise() : Promise.resolve("");
lastLog = Promise.all([lastLog, urnPromise]).then(([_, urn]) => {
return new Promise<void>((resolve, reject) => {
try {
const req = new engproto.LogRequest();
req.setSeverity(sev);
req.setMessage(msg);
req.setUrn(urn);
req.setStreamid(streamId === undefined ? 0 : streamId);
req.setEphemeral(ephemeral === true);
engine.log(req, () => {
resolve(); // let the next log through
keepAlive(); // permit RPC channel tear-downs
});
} catch (err) {
reject(err);
}
});
});
return lastLog.catch((err) => {
// debug messages never go to stdout/err
if (sev !== engproto.LogSeverity.DEBUG) {
// if we're unable to deliver the log message, deliver to stderr instead
console.error(
`failed to deliver log message. \nerror: ${err} \noriginal message: ${msg}\n message severity: ${messageLevels[sev]}`,
);
}
// we still need to free up the outstanding promise chain, whether or not delivery succeeded.
keepAlive();
});
}