Skip to content

Commit

Permalink
chore(cli): add sentry error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed May 20, 2022
1 parent a23a56b commit a7b9821
Show file tree
Hide file tree
Showing 27 changed files with 442 additions and 50 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,27 @@ jobs:
- name: installing dependencies
run: |
yarn install
- name: Install the Sentry CLI
run: curl -sL https://sentry.io/get-cli/ | bash
- name: version
id: get_version
run: |
version=$(node -p "require('./package.json').version")
echo "::set-output name=version::${version}"
- name: Create a release
run: sentry-cli releases new ${{ steps.get_version.outputs.version }}
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_TOKEN }}
- name: compile
run: |
tools/align-version.sh
yarn build
env:
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
- name: Add sourcemaps
run: sentry-cli releases files ${{ steps.get_version.outputs.version }} upload-sourcemaps ./packages/cdktf-cli/bundle
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_TOKEN }}
- name: create bundle
run: yarn package
- name: test
Expand Down Expand Up @@ -207,6 +224,38 @@ jobs:
GIT_USER_NAME: "CDK for Terraform Team"
GIT_USER_EMAIL: "github-team-tf-cdk@hashicorp.com"

release_sentry:
name: Finalize the sentry release
needs:
- prepare-release
- integration-tests
- release_homebrew
- release_golang
- release_nuget
- release_maven
- release_pypi
- release_npm
runs-on: ubuntu-latest
container:
image: docker.mirror.hashicorp.services/hashicorp/jsii-terraform
steps:
- name: Download dist
uses: actions/download-artifact@v2
with:
name: dist
path: dist
- name: Install the Sentry CLI
run: curl -sL https://sentry.io/get-cli/ | bash
- name: version
id: get_version
run: |
version=$(node -p "require('./package.json').version")
echo "::set-output name=version::${version}"
- name: Create a release
run: sentry-cli releases finalize ${{ steps.get_version.outputs.version }}
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_TOKEN }}

release_homebrew:
name: Release to Homebrew
# The branch or tag ref that triggered the workflow run.
Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/release_next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,24 @@ jobs:
- run: |
yarn prepare-next-release
tools/align-version.sh
- name: Install the Sentry CLI
run: curl -sL https://sentry.io/get-cli/ | bash
- name: version
id: get_version
run: |
version=$(node -p "require('./package.json').version")
echo "::set-output name=version::${version}"
- name: Create a release
run: sentry-cli releases new ${{ steps.get_version.outputs.version }}
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_TOKEN }}
- run: yarn build
env:
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
- name: Add sourcemaps
run: sentry-cli releases files ${{ steps.get_version.outputs.version }} upload-sourcemaps ./packages/cdktf-cli/bundle
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_TOKEN }}
- run: yarn package
- run: yarn test:ci
- name: Upload artifact
Expand Down Expand Up @@ -188,3 +205,34 @@ jobs:
GITHUB_TOKEN: ${{ secrets.TERRAFORM_CDK_GO_REPO_GITHUB_TOKEN }}
GIT_USER_NAME: "CDK for Terraform Team"
GIT_USER_EMAIL: "github-team-tf-cdk@hashicorp.com"

release_sentry:
name: Finalize the sentry release
needs:
- prepare-release
- integration-tests
- release_golang
- release_nuget
- release_maven
- release_pypi
- release_npm
runs-on: ubuntu-latest
container:
image: docker.mirror.hashicorp.services/hashicorp/jsii-terraform
steps:
- name: Download dist
uses: actions/download-artifact@v2
with:
name: dist
path: dist
- name: Install the Sentry CLI
run: curl -sL https://sentry.io/get-cli/ | bash
- name: version
id: get_version
run: |
version=$(node -p "require('./package.json').version")
echo "::set-output name=version::${version}"
- name: Create a release
run: sentry-cli releases finalize ${{ steps.get_version.outputs.version }}
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_TOKEN }}
2 changes: 1 addition & 1 deletion packages/@cdktf/hcl2cdk/test/hcl2cdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe("convert", () => {
// Initialize a new project
projectDir = fs.mkdtempSync("cdktf-convert-test");
execSync(
`cd ${projectDir} && ${cdktfBin} init --local --dist=${cdktfDist} --project-name="hello" --project-description="world" --template=typescript`
`cd ${projectDir} && ${cdktfBin} init --local --dist=${cdktfDist} --project-name="hello" --project-description="world" --template=typescript --enable-crash-reporting=false`
);
const cdktfJson = JSON.parse(
fs.readFileSync(path.join(projectDir, "cdktf.json"), "utf8")
Expand Down
9 changes: 3 additions & 6 deletions packages/cdktf-cli/bin/cdktf.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
process.env.NODE_ENV = process.env.NODE_ENV || "production";

import * as yargs from "yargs";
import * as semver from "semver";
import * as path from "path";
import * as os from "os";
import * as fs from "fs-extra";
import { readCDKTFManifest } from "../lib/util";
import { IsErrorType } from "../lib/errors";
import { collectDebugInformation } from "../lib/debug";
import { CDKTF_DISABLE_PLUGIN_CACHE_ENV } from "../lib/environment";
import * as Sentry from "@sentry/node";

const ensurePluginCache = (): string => {
const pluginCachePath =
Expand All @@ -24,11 +24,6 @@ if (!CDKTF_DISABLE_PLUGIN_CACHE_ENV) {
process.env.TF_PLUGIN_CACHE_DIR = ensurePluginCache();
}

if (semver.lt(process.version, "10.12.0")) {
console.error("Need at least Node v10.12 to run");
process.exit(1);
}

const customCompletion = function (
_current: string,
argv: any,
Expand Down Expand Up @@ -141,11 +136,13 @@ yargs
console.error(error.stack);
console.error("Collecting Debug Information...");
const debugOutput = await collectDebugInformation();

console.error("Debug Information:");
Object.entries(debugOutput).forEach(([key, value]) => {
console.log(`${key}: ${value === null ? "null" : value}`);
});
}

await Sentry.close(4000);
process.exit(1);
}).argv;
14 changes: 12 additions & 2 deletions packages/cdktf-cli/bin/cmds/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {
verifySimilarLibraryVersion,
} from "./helper/check-environment";
import { collectDebugInformation } from "../../lib/debug";
import { logger } from "../../lib/logging";
import { initializErrorReporting } from "../../lib/error-reporting";

const chalkColour = new chalk.Instance();
const config = cfg.readConfigSync();
Expand All @@ -59,6 +61,7 @@ async function getProviderRequirements(provider: string[]) {
}

export async function convert({ language, provider }: any) {
await initializErrorReporting();
await displayVersionMessage();

const providerRequirements = await getProviderRequirements(provider);
Expand Down Expand Up @@ -89,6 +92,7 @@ export async function convert({ language, provider }: any) {
}

export async function deploy(argv: any) {
await initializErrorReporting(true);
throwIfNotProjectDirectory();
await displayVersionMessage();
await checkEnvironment();
Expand Down Expand Up @@ -124,6 +128,7 @@ export async function deploy(argv: any) {
}

export async function destroy(argv: any) {
await initializErrorReporting(true);
throwIfNotProjectDirectory();
await displayVersionMessage();
await checkEnvironment();
Expand All @@ -146,6 +151,7 @@ export async function destroy(argv: any) {
}

export async function diff(argv: any) {
await initializErrorReporting(true);
throwIfNotProjectDirectory();
await displayVersionMessage();
await checkEnvironment();
Expand All @@ -163,8 +169,7 @@ export async function diff(argv: any) {
}

export async function get(argv: any) {
throwIfNotProjectDirectory();
await displayVersionMessage();
await initializErrorReporting(true);
await checkEnvironment();
await verifySimilarLibraryVersion();
const args = argv as {
Expand All @@ -174,6 +179,7 @@ export async function get(argv: any) {
const providers = config.terraformProviders ?? [];
const modules = config.terraformModules ?? [];
const { output, language } = args;
logger.info(`Getting outputs for ${language}`);

const constraints: cfg.TerraformDependencyConstraint[] = [
...providers,
Expand Down Expand Up @@ -212,6 +218,7 @@ export async function init(argv: any) {
}

export async function list(argv: any) {
await initializErrorReporting(true);
throwIfNotProjectDirectory();
await displayVersionMessage();
await checkEnvironment();
Expand Down Expand Up @@ -260,6 +267,7 @@ export async function login(argv: any) {
}

export async function synth(argv: any) {
await initializErrorReporting(true);
throwIfNotProjectDirectory();
await displayVersionMessage();
await checkEnvironment();
Expand All @@ -283,6 +291,7 @@ export async function synth(argv: any) {
}

export async function watch(argv: any) {
await initializErrorReporting(true);
throwIfNotProjectDirectory();
await displayVersionMessage();
const command = argv.app;
Expand All @@ -308,6 +317,7 @@ export async function watch(argv: any) {
}

export async function output(argv: any) {
await initializErrorReporting(true);
throwIfNotProjectDirectory();
await displayVersionMessage();
await checkEnvironment();
Expand Down
6 changes: 6 additions & 0 deletions packages/cdktf-cli/bin/cmds/helper/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from "@cdktf/provider-generator";
import { templates, templatesDir } from "./init-templates";
import { init, Project } from "../../../lib";
import { askForCrashReportingConsent } from "../../../lib/error-reporting";

const chalkColour = new chalk.Instance();

Expand Down Expand Up @@ -57,6 +58,7 @@ type Options = {
dist?: string;
destination: string;
fromTerraformProject?: string;
enableCrashReporting?: boolean;
};
export async function runInit(argv: Options) {
const telemetryData: Record<string, unknown> = {};
Expand Down Expand Up @@ -122,13 +124,17 @@ This means that your Terraform state file will be stored locally on disk in a fi
}
}

const sendCrashReports =
argv.enableCrashReporting ?? (await askForCrashReportingConsent());

await init({
cdktfVersion: argv.cdktfVersion,
destination,
dist: argv.dist,
projectId,
projectInfo,
templatePath: templateInfo.Path,
sendCrashReports: sendCrashReports,
});

if (argv.fromTerraformProject) {
Expand Down
4 changes: 4 additions & 0 deletions packages/cdktf-cli/bin/cmds/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Command extends BaseCommand {
type: "string",
desc: "Use a terraform project as the basis, CDK constructs will be generated based on the .tf files in the path",
})
.option("enable-crash-reporting", {
type: "boolean",
desc: "Enable crash reporting for the CLI, see https://www.terraform.io/cdktf/telemetry#crash-reporting for more details",
})
.strict();

public async handleCommand(argv: any) {
Expand Down
3 changes: 3 additions & 0 deletions packages/cdktf-cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ const nativeNodeModulesPlugin = {
"constructs",
],
plugins: [nativeNodeModulesPlugin],
define: {
"process.env.SENTRY_DSN": JSON.stringify(process.env.SENTRY_DSN),
},
tsconfig: "tsconfig.json",
});

Expand Down
4 changes: 2 additions & 2 deletions packages/cdktf-cli/lib/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ function getId(
}
}

function getProjectId(projectPath = process.cwd()): string {
export function getProjectId(projectPath = process.cwd()): string {
return getId(path.resolve(projectPath, "cdktf.json"), "projectId");
}

function getUserId(): string {
export function getUserId(): string {
return getId(
path.resolve(homeDir(), "config.json"),
"userId",
Expand Down
36 changes: 26 additions & 10 deletions packages/cdktf-cli/lib/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,26 +300,41 @@ export async function collectDebugInformation() {
const language = getLanguage();
debugOutput["language"] = language ?? null;
debugOutput["cdktf-cli"] = DISPLAY_VERSION;
debugOutput["node"] = (await getNodeVersion()) ?? null;

const node = getNodeVersion();

debugOutput["node"] = (await node) ?? null;
if (language) {
debugOutput["cdktf"] = (await getPackageVersion(language, "cdktf")) ?? null;
debugOutput["constructs"] =
(await getPackageVersion(language, "constructs")) ?? null;
debugOutput["jsii"] = (await getPackageVersion(language, "jsii")) ?? null;
const cdktf = getPackageVersion(language, "cdktf");
const constructs = getPackageVersion(language, "constructs");
const jsii = getPackageVersion(language, "jsii");

debugOutput["cdktf"] = (await cdktf) ?? null;
debugOutput["constructs"] = (await constructs) ?? null;
debugOutput["jsii"] = (await jsii) ?? null;
}
debugOutput["terraform"] = await terraformVersion;
debugOutput["arch"] = os.arch();
debugOutput["os"] = `${os.platform()} ${os.release()}`;

switch (language) {
case "python":
debugOutput["python"] = (await getPythonVersion()) ?? null;
debugOutput["pip"] = (await getPipVersion()) ?? null;
debugOutput["pipenv"] = (await getPipenvVersion()) ?? null;
{
const python = getPythonVersion();
const pip = getPipVersion();
const pipenv = getPipenvVersion();
debugOutput["python"] = (await python) ?? null;
debugOutput["pip"] = (await pip) ?? null;
debugOutput["pipenv"] = (await pipenv) ?? null;
}
break;
case "java":
debugOutput["java"] = (await getJavaVersion()) ?? null;
debugOutput["maven"] = (await getMavenVersion()) ?? null;
{
const java = getJavaVersion();
const maven = getMavenVersion();
debugOutput["java"] = (await java) ?? null;
debugOutput["maven"] = (await maven) ?? null;
}
break;
case "csharp":
debugOutput["dotnet"] = (await getDotnetVersion()) ?? null;
Expand All @@ -328,5 +343,6 @@ export async function collectDebugInformation() {
debugOutput["go"] = (await getGoVersion()) ?? null;
break;
}

return debugOutput;
}
Loading

0 comments on commit a7b9821

Please sign in to comment.