-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checkpoint telemetry integration (#326)
* checkpoint telemetry reporting for cdktf get command * added date to report options and removed unwanted parameters * changes and fixes after pr review and documentation for telemetry collection * checkpoint is disabled for ci * checkpoint is disabled for release next workflow * change checkpoint disable environment variable name * adding integration tests
- Loading branch information
1 parent
03694d9
commit ab46998
Showing
14 changed files
with
237 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Telemetry | ||
|
||
CDK for Terraform CLI ([cdktf-cli](../../packages/cdktf-cli)) interacts with a HashiCorp service called [Checkpoint](https://checkpoint.hashicorp.com) | ||
to report project metrics such as cdktf version, project language, provider name, platform name, and other details that help guide the project maintainers with | ||
feature and roadmap decisions. The code that interacts with Checkpoint is part of CDK for Terraform CLI and can be read [here](../../packages/cdktf-cli/bin/lib/checkpoint.ts). | ||
|
||
All HashiCorp projects including Terraform that is used by CDK for Terraform use Checkpoint. | ||
Read more about project metrics [here](https://github.com/hashicorp/terraform-cdk/issues/325). | ||
|
||
The information that is sent to Checkpoint is anonymous and cannot be used to identify the user or host. The use of Checkpoint is completely optional | ||
and it can be disabled at any time by setting the `CHECKPOINT_DISABLE` environment variable to a non-empty value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ReportParams, ReportRequest } from '../../../lib/checkpoint' | ||
import { versionNumber } from '../version-check'; | ||
import { readConfigSync } from '../../../lib/config'; | ||
|
||
const product = "cdktf" | ||
const config = readConfigSync() | ||
|
||
export async function Report(command: string, language: string, dateTime: Date, payload: {}): Promise<void> { | ||
if (language == '') { | ||
if (config.language) { | ||
language = config.language | ||
} | ||
} | ||
const reportParams: ReportParams = { command: command, product: product, version: versionNumber(), dateTime: dateTime, payload: payload, language: language }; | ||
await ReportRequest(reportParams); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import https = require('https'); | ||
import { format } from 'url'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import * as os from 'os'; | ||
import { processLogger } from './logging'; | ||
|
||
const BASE_URL = `https://checkpoint-api.hashicorp.com/v1/`; | ||
|
||
const VALID_STATUS_CODES = [200, 201]; | ||
|
||
export interface ReportParams { | ||
dateTime?: Date; | ||
arch?: string; | ||
os?: string; | ||
payload: {}; | ||
product: string; | ||
runID?: string; | ||
version?: string; | ||
command?: string; | ||
language?: string; | ||
} | ||
|
||
async function post(url: string, data: string) { | ||
return new Promise<any>((ok, ko) => { | ||
const req = https.request(format(url), { | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Length': data.length, | ||
'User-Agent': 'HashiCorp/cdktf-cli' | ||
}, | ||
method: 'POST' | ||
}, res => { | ||
if (res.statusCode) { | ||
const statusCode = res.statusCode; | ||
if (!VALID_STATUS_CODES.includes(statusCode)) { | ||
return ko(new Error(res.statusMessage)); | ||
} | ||
} | ||
const data = new Array<Buffer>(); | ||
res.on('data', chunk => data.push(chunk)); | ||
|
||
res.once('error', err => ko(err)); | ||
res.once('end', () => { | ||
return ok(); | ||
}); | ||
}); | ||
|
||
req.write(data); | ||
|
||
req.end(); | ||
}) | ||
} | ||
|
||
export async function ReportRequest(reportParams: ReportParams): Promise<void> { | ||
// we won't report when checkpoint is disabled. | ||
if (process.env.CHECKPOINT_DISABLE) { | ||
return | ||
} | ||
|
||
if (!reportParams.runID) { | ||
reportParams.runID = uuidv4(); | ||
} | ||
|
||
if (!reportParams.dateTime) { | ||
reportParams.dateTime = new Date(); | ||
} | ||
|
||
if (!reportParams.arch) { | ||
reportParams.arch = os.arch(); | ||
} | ||
|
||
if (!reportParams.os) { | ||
reportParams.os = os.platform(); | ||
} | ||
|
||
const postData = JSON.stringify(reportParams); | ||
|
||
try { | ||
await post(`${BASE_URL}telemetry/${reportParams.product}`, postData) | ||
} catch (e) { | ||
// Log errors writing to checkpoint | ||
processLogger(e.message) | ||
} | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"//": { | ||
"metadata": { | ||
"version": "stubbed", | ||
"stackName": "hello-terra" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Construct } from 'constructs'; | ||
import { App, TerraformStack, Testing } from 'cdktf'; | ||
|
||
export class HelloTerra extends TerraformStack { | ||
constructor(scope: Construct, id: string) { | ||
super(scope, id); | ||
|
||
// define resources here | ||
|
||
} | ||
} | ||
|
||
const app = Testing.stubVersion(new App({stackTraces: false})); | ||
new HelloTerra(app, 'hello-terra'); | ||
app.synth(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/sh | ||
set -e | ||
scriptdir=$(cd $(dirname $0) && pwd) | ||
|
||
cd $(mktemp -d) | ||
mkdir test && cd test | ||
|
||
# hidden files should be ignored | ||
touch .foo | ||
mkdir .bar | ||
|
||
# unset CHECKPOINT_DISABLE | ||
export CHECKPOINT_DISABLE="" | ||
|
||
# initialize an empty project | ||
cdktf init --template typescript --project-name="typescript-test" --project-description="typescript test app" --local | ||
|
||
# put some code in it | ||
cp ${scriptdir}/main.ts . | ||
|
||
# build | ||
yarn compile | ||
yarn synth > /dev/null | ||
|
||
# get rid of downloaded Terraform providers, no point in diffing them | ||
rm -rf cdktf.out/.terraform | ||
|
||
# show output | ||
diff cdktf.out ${scriptdir}/expected | ||
|
||
echo "PASS" |