Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 99a090b

Browse files
committed
chord: merge commit
2 parents 26c75a0 + 50d266d commit 99a090b

11 files changed

Lines changed: 1997 additions & 56 deletions

File tree

packages/opencensus-core/package-lock.json

Lines changed: 1463 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-core/package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
"main": "build/src/index.js",
66
"types": "build/src/index.d.js",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "npm run script run-unit-tests",
9+
"compile-all": "npm run script compile-auto",
10+
"compile-strict": "npm run script compile-auto-strict",
11+
"compile": "npm run script compile-auto compile-auto-strict",
12+
"clean": "rimraf build/*",
13+
"script": "ts-node -P ./scripts ./scripts"
914
},
1015
"keywords": [
1116
"opencensus",
@@ -27,7 +32,10 @@
2732
"@types/semver": "^5.5.0",
2833
"@types/shimmer": "^1.0.1",
2934
"@types/uuid": "^3.4.3",
35+
"gts": "^0.5.1",
36+
"ncp": "^2.0.0",
3037
"mocha": "^5.0.4",
38+
"ts-node": "^4.0.0",
3139
"typescript": "^2.7.2"
3240
},
3341
"dependencies": {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as path from 'path';
2+
import { forkP } from './utils';
3+
import * as ts from 'typescript';
4+
import * as semver from 'semver';
5+
6+
export interface CompileOptions {
7+
strict: boolean;
8+
languageLevel: string;
9+
}
10+
11+
export async function compile(options: CompileOptions) {
12+
let { strict, languageLevel } = options;
13+
if (languageLevel === 'auto') {
14+
languageLevel = semver.satisfies(process.version, '>=7.5') ? 'es2017' : 'es2015';
15+
}
16+
await forkP(`node_modules/typescript/lib/tsc`, [
17+
'-p',
18+
strict ? '.' : './tsconfig.full.json',
19+
'--target',
20+
languageLevel
21+
]);
22+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* The main entry point for cross-platform build scripts.
3+
* Usage (in repository root directory):
4+
* ts-node -P ./scripts ./scripts [step1] [step2 ... stepN]
5+
* Alias for above:
6+
* npm run script [step1] [step2 ... stepN]
7+
*/
8+
9+
const [, , ...steps] = process.argv;
10+
const {
11+
CI_PULL_REQUEST,
12+
TRACE_TEST_EXCLUDE_INTEGRATION,
13+
TRACE_SYSTEM_TEST_ENCRYPTED_CREDENTIALS_KEY,
14+
TRACE_SYSTEM_TEST_ENCRYPTED_CREDENTIALS_IV
15+
} = process.env;
16+
17+
import { compile } from './compile';
18+
import { runTests } from './run-tests';
19+
import { BUILD_DIRECTORY, existsP, spawnP } from './utils';
20+
21+
// The identifying string in the service account credentials file path.
22+
const keyID = 'de480e4f9023';
23+
24+
// Globs to exclude when running unit tests only.
25+
const unitTestExcludeGlobs: string[] = TRACE_TEST_EXCLUDE_INTEGRATION ? [
26+
`${BUILD_DIRECTORY}/test/plugins/test-*`,
27+
`${BUILD_DIRECTORY}/test/test-agent-stopped.js`,
28+
`${BUILD_DIRECTORY}/test/test-grpc-context.js`,
29+
`${BUILD_DIRECTORY}/test/test-mysql-pool.js`,
30+
`${BUILD_DIRECTORY}/test/test-plugins-*`,
31+
`${BUILD_DIRECTORY}/test/test-trace-web-frameworks.js`,
32+
`${BUILD_DIRECTORY}/test/test-unpatch.js`,
33+
`${BUILD_DIRECTORY}/test/test-trace.js`
34+
] : [];
35+
36+
/**
37+
* Sequentially runs a list of commands.
38+
*/
39+
async function run(steps: string[]) {
40+
for (const step of steps) {
41+
console.log(`> Running step: ${step}`);
42+
// If the step string is prefixed with "npm-", treat it as an "npm run"
43+
// command, and then short-circuit.
44+
if (step.indexOf('npm-') === 0) {
45+
const moduleAndArgs = step.split('-');
46+
await spawnP(
47+
'npm',
48+
[
49+
'run',
50+
moduleAndArgs.slice(1).join('-')
51+
]
52+
);
53+
continue;
54+
} else if (step.startsWith('compile-')) {
55+
const [, languageLevel, strict] = step.split('-');
56+
await compile({ strict: !!strict, languageLevel });
57+
continue;
58+
} else {
59+
switch (step) {
60+
case 'run-unit-tests':
61+
await runTests({
62+
includeGlobs: [
63+
`${BUILD_DIRECTORY}/test/test-*.js`,
64+
`${BUILD_DIRECTORY}/test/plugins/test-*.js`
65+
],
66+
excludeGlobs: unitTestExcludeGlobs,
67+
rootDir: BUILD_DIRECTORY,
68+
coverage: false,
69+
timeout: 4000
70+
});
71+
break;
72+
default:
73+
console.log(`> ${step}: not found`);
74+
break;
75+
}
76+
}
77+
}
78+
}
79+
80+
run(steps).catch((err) => {
81+
console.error(err);
82+
process.exit(1);
83+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as path from 'path';
2+
import { globP, forkP } from './utils';
3+
4+
export interface Options {
5+
includeGlobs: string[],
6+
excludeGlobs?: string[],
7+
rootDir: string,
8+
coverage?: boolean,
9+
timeout?: number
10+
}
11+
12+
export async function runTests(options: Options) {
13+
const { includeGlobs, excludeGlobs, rootDir, coverage, timeout } = options;
14+
function nodule(nodule: string) {
15+
return path.relative(rootDir, `node_modules/${nodule}`);
16+
}
17+
let testNum = 0;
18+
const excludedFiles = ([] as string[])
19+
.concat(...await Promise.all((excludeGlobs || []).map(glob => globP(glob))));
20+
const includedFiles = ([] as string[])
21+
.concat(...await Promise.all(includeGlobs.map(glob => globP(glob))));
22+
// Take the difference
23+
const files = includedFiles.filter(i => excludedFiles.indexOf(i) < 0);
24+
for (const file of files) {
25+
const moduleAndArgs = [
26+
...coverage ? [
27+
nodule('.bin/nyc'),
28+
'--reporter',
29+
'lcov',
30+
'--report-dir',
31+
`./coverage/${testNum++}`,
32+
'--exclude',
33+
'build/test/**'
34+
] : [],
35+
nodule('mocha/bin/_mocha'),
36+
'--require',
37+
'source-map-support/register',
38+
path.relative(rootDir, file),
39+
...timeout ? [
40+
'--timeout',
41+
`${timeout}`
42+
] : [
43+
'--no-timeouts'
44+
]
45+
];
46+
47+
await forkP(
48+
moduleAndArgs[0],
49+
moduleAndArgs.slice(1),
50+
{ cwd: rootDir }
51+
);
52+
}
53+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../node_modules/gts/tsconfig-google.json",
3+
"compilerOptions": {
4+
"rootDir": "..",
5+
"outDir": "../build"
6+
},
7+
"include": [
8+
"*.ts"
9+
]
10+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { mkdir, Stats, stat, readFile, writeFile } from 'fs';
2+
import * as glob from 'glob';
3+
import { ncp } from 'ncp';
4+
import * as path from 'path';
5+
import * as pify from 'pify';
6+
import { ChildProcess, ForkOptions, fork, SpawnOptions, spawn } from 'child_process';
7+
import * as once from 'once';
8+
import * as tmp from 'tmp';
9+
10+
export const BUILD_DIRECTORY = 'build';
11+
12+
export const globP: (pattern: string) => Promise<string[]> = pify(glob);
13+
export const mkdirP: (dir: string) => Promise<void> = pify(mkdir);
14+
export const ncpP: (src: string, dest: string) => Promise<void> = pify(ncp);
15+
export const readFileP: (path: string, encoding?: string) => Promise<Buffer|string> = pify(readFile);
16+
export const writeFileP: (path: string, data: Buffer|string, encoding?: string) => Promise<void> = pify(writeFile);
17+
export const statP: (path: string) => Promise<Stats> = pify(stat);
18+
export const tmpDirP: () => Promise<string> = pify(tmp.dir);
19+
20+
export function nodule(nodule: string) {
21+
return path.relative(BUILD_DIRECTORY, `node_modules/${nodule}`);
22+
}
23+
24+
export function flatten<T>(arr: Array<Array<T>>): Array<T> {
25+
return arr.reduce((acc, e) => acc.concat(e), []);
26+
}
27+
28+
export function existsP(path: string): Promise<boolean> {
29+
return statP(path).then(
30+
() => Promise.resolve(true),
31+
() => Promise.resolve(false)
32+
);
33+
}
34+
35+
function promisifyChildProcess(childProcess: ChildProcess): Promise<void> {
36+
return new Promise((resolve, reject) => {
37+
const exit = (err?: Error) => once(() => err ? reject(err) : resolve())();
38+
childProcess.on('error', exit);
39+
childProcess.on('close', (code) => {
40+
if (code === 0) {
41+
exit();
42+
} else {
43+
exit(new Error(`Process ${childProcess.pid} exited with code ${code}.`));
44+
}
45+
});
46+
});
47+
}
48+
49+
export function spawnP(command: string, args?: string[], options?: SpawnOptions): Promise<void> {
50+
const stringifiedCommand = `\`${command}${args ? (' ' + args.join(' ')) : ''}\``;
51+
console.log(`> Running: ${stringifiedCommand}`);
52+
return promisifyChildProcess(spawn(command, args, Object.assign({
53+
stdio: 'inherit',
54+
shell: true
55+
}, options)));
56+
}
57+
58+
export function forkP(moduleName: string, args?: string[], options?: ForkOptions): Promise<void> {
59+
const stringifiedCommand = `\`${moduleName}${args ? (' ' + args.join(' ')) : ''}\``;
60+
console.log(`> Running: ${stringifiedCommand}`);
61+
return promisifyChildProcess(fork(moduleName, args, Object.assign({
62+
stdio: 'inherit'
63+
}, options)));
64+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Span } from '../src/trace/span';
18+
import { Trace } from '../src/trace/trace';
19+
20+
var assert = require('assert');
21+
22+
describe('Span creation', function () {
23+
24+
let trace;
25+
let span;
26+
27+
before(function () {
28+
trace = new Trace();
29+
span = trace.startSpan('spanName', 'typeSpan');
30+
});
31+
32+
it('should create an span on the trace', function () {
33+
assert.ok(trace instanceof Trace);
34+
span = trace.startSpan('spanName', 'typeSpan');
35+
assert.ok(span instanceof Span);
36+
assert.ok(span.id);
37+
});
38+
39+
it('should start a span', function () {
40+
span.start();
41+
assert.ok(span.started);
42+
});
43+
44+
it('should end a span', function () {
45+
span.end();
46+
assert.ok(span.ended);
47+
});
48+
});
49+
50+
describe('Span checking creation', function () {
51+
52+
let trace;
53+
let span;
54+
55+
before(function () {
56+
trace = new Trace();
57+
span = trace.startSpan('spanName', 'typeSpan');
58+
});
59+
60+
it('should not start span after it ended', function () {
61+
span.end();
62+
assert.equal(span.ended, true);
63+
});
64+
});
65+
66+
describe('Span data', function () {
67+
68+
let trace;
69+
70+
before(function () {
71+
trace = new Trace();
72+
});
73+
74+
it('generates unique numeric span ID strings', function () {
75+
var numberOfSpansToCheck = 5;
76+
for (var i = 0; i < numberOfSpansToCheck; i++) {
77+
var span = trace.startSpan('spanName' + i, 'typeSpan' + i);
78+
var spanId = span.id;
79+
assert.ok(typeof spanId === 'string');
80+
assert.ok(spanId.match(/\d+/));
81+
assert.ok(Number(spanId) > 0);
82+
assert.strictEqual(Number(spanId).toString(), spanId);
83+
}
84+
});
85+
86+
// TODO
87+
it('truncates namespace', function(){
88+
this.skip();
89+
});
90+
});
91+

0 commit comments

Comments
 (0)