From ecb41d3face5161857b182c6af2867d8f2554914 Mon Sep 17 00:00:00 2001 From: Naseem Date: Sun, 22 Dec 2019 02:46:18 -0500 Subject: [PATCH 1/6] feat: test-utils --- .../opentelemetry-plugin-mysql/package.json | 1 + .../test/mysql.test.ts | 6 +-- .../opentelemetry-plugin-pg/package.json | 1 + .../opentelemetry-plugin-pg/test/pg.test.ts | 6 +-- .../opentelemetry-plugin-pg/test/testUtils.ts | 54 ------------------- .../opentelemetry-plugin-redis/package.json | 1 + .../test/redis.test.ts | 6 +-- .../test/testUtils.ts | 54 ------------------- .../opentelemetry-test-utils/package.json | 29 ++++++++++ .../testUtils.ts | 28 +++++++--- .../opentelemetry-test-utils/tsconfig.json | 10 ++++ packages/opentelemetry-test-utils/tslint.json | 4 ++ 12 files changed, 75 insertions(+), 125 deletions(-) delete mode 100644 packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/testUtils.ts delete mode 100644 packages/opentelemetry-plugin-redis/test/testUtils.ts create mode 100644 packages/opentelemetry-test-utils/package.json rename packages/{opentelemetry-plugin-mysql/test => opentelemetry-test-utils}/testUtils.ts (64%) create mode 100644 packages/opentelemetry-test-utils/tsconfig.json create mode 100644 packages/opentelemetry-test-utils/tslint.json diff --git a/packages/opentelemetry-plugin-mysql/package.json b/packages/opentelemetry-plugin-mysql/package.json index 9d02fd7377..2efc94f94c 100644 --- a/packages/opentelemetry-plugin-mysql/package.json +++ b/packages/opentelemetry-plugin-mysql/package.json @@ -41,6 +41,7 @@ }, "devDependencies": { "@opentelemetry/node": "^0.3.1", + "@opentelemetry/test-utils": "^0.3.1", "@opentelemetry/tracing": "^0.3.1", "@types/mocha": "^5.2.7", "@types/mysql": "^2.15.4", diff --git a/packages/opentelemetry-plugin-mysql/test/mysql.test.ts b/packages/opentelemetry-plugin-mysql/test/mysql.test.ts index 600b3aa9b3..cd207a7216 100644 --- a/packages/opentelemetry-plugin-mysql/test/mysql.test.ts +++ b/packages/opentelemetry-plugin-mysql/test/mysql.test.ts @@ -24,7 +24,7 @@ import { import * as assert from 'assert'; import * as mysql from 'mysql'; import { MysqlPlugin, plugin } from '../src'; -import * as testUtils from './testUtils'; +import * as testUtils from '@opentelemetry/test-utils'; import { AttributeNames } from '../src/enums'; import { CanonicalCode } from '@opentelemetry/types'; @@ -54,7 +54,7 @@ describe('mysql@2.x', () => { } tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); if (testMysqlLocally) { - testUtils.startDocker(); + testUtils.startDocker('mysql'); // wait 15 seconds for docker container to start this.timeout(20000); setTimeout(done, 15000); @@ -66,7 +66,7 @@ describe('mysql@2.x', () => { after(function() { if (testMysqlLocally) { this.timeout(5000); - testUtils.cleanUpDocker(); + testUtils.cleanUpDocker('mysql'); } }); diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json index 2e722b3096..ea027a18b7 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/package.json @@ -44,6 +44,7 @@ "access": "public" }, "devDependencies": { + "@opentelemetry/test-utils": "^0.3.1", "@types/mocha": "^5.2.7", "@types/node": "^12.6.9", "@types/pg": "^7.11.2", diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts index b260131a1b..bec13efe79 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts @@ -33,7 +33,7 @@ import { AttributeNames } from '../src/enums'; import * as assert from 'assert'; import * as pg from 'pg'; import * as assertionUtils from './assertionUtils'; -import * as testUtils from './testUtils'; +import * as testUtils from '@opentelemetry/test-utils'; const memoryExporter = new InMemorySpanExporter(); @@ -103,7 +103,7 @@ describe('pg@7.x', () => { } tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); if (testPostgresLocally) { - testUtils.startDocker(); + testUtils.startDocker('postgres'); } client = new pg.Client(CONFIG); @@ -116,7 +116,7 @@ describe('pg@7.x', () => { after(async () => { if (testPostgresLocally) { - testUtils.cleanUpDocker(); + testUtils.cleanUpDocker('postgres'); } await client.end(); }); diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/testUtils.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/testUtils.ts deleted file mode 100644 index eec866fc41..0000000000 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/testUtils.ts +++ /dev/null @@ -1,54 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import * as childProcess from 'child_process'; -export function startDocker() { - const tasks = [ - run('docker run -d -p 54320:5432 --name otpostgres postgres:alpine'), - ]; - - for (let i = 0; i < tasks.length; i++) { - const task = tasks[i]; - if (task && task.code !== 0) { - console.error('Failed to start container!'); - console.error(task.output); - return false; - } - } - return true; -} - -export function cleanUpDocker() { - run('docker stop otpostgres'); - run('docker rm otpostgres'); -} - -function run(cmd: string) { - try { - const proc = childProcess.spawnSync(cmd, { - shell: true, - }); - return { - code: proc.status, - output: proc.output - .map(v => String.fromCharCode.apply(null, v as any)) - .join(''), - }; - } catch (e) { - console.log(e); - return; - } -} diff --git a/packages/opentelemetry-plugin-redis/package.json b/packages/opentelemetry-plugin-redis/package.json index 1c1f7226c0..f9c910fd4c 100644 --- a/packages/opentelemetry-plugin-redis/package.json +++ b/packages/opentelemetry-plugin-redis/package.json @@ -43,6 +43,7 @@ }, "devDependencies": { "@opentelemetry/node": "^0.3.1", + "@opentelemetry/test-utils": "^0.3.1", "@opentelemetry/tracing": "^0.3.1", "@types/mocha": "^5.2.7", "@types/node": "^12.6.9", diff --git a/packages/opentelemetry-plugin-redis/test/redis.test.ts b/packages/opentelemetry-plugin-redis/test/redis.test.ts index 72d6730a69..ec68cbfad7 100644 --- a/packages/opentelemetry-plugin-redis/test/redis.test.ts +++ b/packages/opentelemetry-plugin-redis/test/redis.test.ts @@ -23,7 +23,7 @@ import { NodeTracer } from '@opentelemetry/node'; import { plugin, RedisPlugin } from '../src'; import * as redisTypes from 'redis'; import { NoopLogger } from '@opentelemetry/core'; -import * as dockerUtils from './testUtils'; +import * as dockerUtils from '@opentelemetry/test-utils'; import * as assertionUtils from './assertionUtils'; import { SpanKind, Status, CanonicalCode } from '@opentelemetry/types'; import { AttributeNames } from '../src/enums'; @@ -64,7 +64,7 @@ describe('redis@2.x', () => { } if (shouldTestLocal) { - dockerUtils.startDocker(); + dockerUtils.startDocker('redis'); } redis = require('redis'); @@ -74,7 +74,7 @@ describe('redis@2.x', () => { after(() => { if (shouldTestLocal) { - dockerUtils.cleanUpDocker(); + dockerUtils.cleanUpDocker('redis'); } }); diff --git a/packages/opentelemetry-plugin-redis/test/testUtils.ts b/packages/opentelemetry-plugin-redis/test/testUtils.ts deleted file mode 100644 index a40b0b4aab..0000000000 --- a/packages/opentelemetry-plugin-redis/test/testUtils.ts +++ /dev/null @@ -1,54 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import * as childProcess from 'child_process'; -export function startDocker() { - const tasks = [ - run('docker run -d -p 63790:6379 --name otjsredis redis:alpine'), - ]; - - for (let i = 0; i < tasks.length; i++) { - const task = tasks[i]; - if (task && task.code !== 0) { - console.error('Failed to start container!'); - console.error(task.output); - return false; - } - } - return true; -} - -export function cleanUpDocker() { - run('docker stop otjsredis'); - run('docker rm otjsredis'); -} - -function run(cmd: string) { - try { - const proc = childProcess.spawnSync(cmd, { - shell: true, - }); - return { - code: proc.status, - output: proc.output - .map(v => String.fromCharCode.apply(null, v as any)) - .join(''), - }; - } catch (e) { - console.log(e); - return; - } -} diff --git a/packages/opentelemetry-test-utils/package.json b/packages/opentelemetry-test-utils/package.json new file mode 100644 index 0000000000..e5bd4c0c98 --- /dev/null +++ b/packages/opentelemetry-test-utils/package.json @@ -0,0 +1,29 @@ +{ + "name": "@opentelemetry/test-utils", + "version": "0.3.1", + "description": "Test utilities.", + "main": "build/testUtils.js", + "scripts": { + "precompile": "tsc --version", + "compile": "tsc -p .", + "prepare": "npm run compile" + }, + "repository": "open-telemetry/opentelemetry-js", + "keywords": [ + "opentelemetry", + "test-utils" + ], + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/open-telemetry/opentelemetry-js/issues" + }, + "homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", + "devDependencies": { + "gts": "^1.1.2", + "ts-node": "^8.5.4", + "tslint-consistent-codestyle": "^1.16.0", + "tslint-microsoft-contrib": "^6.2.0", + "typescript": "3.7.4" + } +} diff --git a/packages/opentelemetry-plugin-mysql/test/testUtils.ts b/packages/opentelemetry-test-utils/testUtils.ts similarity index 64% rename from packages/opentelemetry-plugin-mysql/test/testUtils.ts rename to packages/opentelemetry-test-utils/testUtils.ts index a569ff57d1..111fa751da 100644 --- a/packages/opentelemetry-plugin-mysql/test/testUtils.ts +++ b/packages/opentelemetry-test-utils/testUtils.ts @@ -15,12 +15,23 @@ */ import * as childProcess from 'child_process'; -export function startDocker() { - const tasks = [ - run( - 'docker run --rm -d -e MYSQL_ROOT_PASSWORD=rootpw -e MYSQL_DATABASE=test_db -e MYSQL_USER=otel -e MYSQL_PASSWORD=secret -p 33306:3306 --name otmysql circleci/mysql:5.7' - ), - ]; +export function startDocker(db: 'redis' | 'mysql' | 'postgres') { + let dockerRunCmd; + switch (db) { + case 'redis': + dockerRunCmd = `docker run -d -p 63790:6379 --name ot${db} ${db}:alpine`; + break; + + case 'mysql': + dockerRunCmd = `docker run --rm -d -e MYSQL_ROOT_PASSWORD=rootpw -e MYSQL_DATABASE=test_db -e MYSQL_USER=otel -e MYSQL_PASSWORD=secret -p 33306:3306 --name ot${db} circleci/${db}:5.7`; + break; + + case 'postgres': + dockerRunCmd = `docker run -d -p 54320:5432 --name ot${db} ${db}:alpine`; + break; + } + + const tasks = [run(dockerRunCmd)]; for (let i = 0; i < tasks.length; i++) { const task = tasks[i]; @@ -33,8 +44,9 @@ export function startDocker() { return true; } -export function cleanUpDocker() { - run('docker stop otmysql'); +export function cleanUpDocker(db: 'redis' | 'mysql' | 'postgres') { + run(`docker stop ot${db}`); + run(`docker rm ot${db}`); } function run(cmd: string) { diff --git a/packages/opentelemetry-test-utils/tsconfig.json b/packages/opentelemetry-test-utils/tsconfig.json new file mode 100644 index 0000000000..eba808d7cd --- /dev/null +++ b/packages/opentelemetry-test-utils/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": [ + "*.ts" + ] +} diff --git a/packages/opentelemetry-test-utils/tslint.json b/packages/opentelemetry-test-utils/tslint.json new file mode 100644 index 0000000000..0710b135d0 --- /dev/null +++ b/packages/opentelemetry-test-utils/tslint.json @@ -0,0 +1,4 @@ +{ + "rulesDirectory": ["node_modules/tslint-microsoft-contrib"], + "extends": ["../../tslint.base.js", "./node_modules/tslint-consistent-codestyle"] +} From 200f49922c36ad3f2590e96e76e5ff0f68dabd6a Mon Sep 17 00:00:00 2001 From: Naseem Date: Mon, 23 Dec 2019 09:26:04 -0500 Subject: [PATCH 2/6] fix: add check,fix scripts --- packages/opentelemetry-test-utils/package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-test-utils/package.json b/packages/opentelemetry-test-utils/package.json index e5bd4c0c98..4160fb1226 100644 --- a/packages/opentelemetry-test-utils/package.json +++ b/packages/opentelemetry-test-utils/package.json @@ -4,8 +4,10 @@ "description": "Test utilities.", "main": "build/testUtils.js", "scripts": { - "precompile": "tsc --version", + "check": "gts check", "compile": "tsc -p .", + "fix": "gts fix", + "precompile": "tsc --version", "prepare": "npm run compile" }, "repository": "open-telemetry/opentelemetry-js", From d6fde29f9b4287c8dd52720c908168d72ed0ebb3 Mon Sep 17 00:00:00 2001 From: Naseem Date: Mon, 23 Dec 2019 17:49:33 -0500 Subject: [PATCH 3/6] feat: use test-utils package in pg pool plugin --- .../opentelemetry-plugin-pg-pool/package.json | 1 + .../test/pg-pool.test.ts | 6 +-- .../test/testUtils.ts | 54 ------------------- 3 files changed, 4 insertions(+), 57 deletions(-) delete mode 100644 packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/testUtils.ts diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/package.json b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/package.json index 4697636985..d02fb84ddc 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/package.json +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/package.json @@ -47,6 +47,7 @@ }, "devDependencies": { "@opentelemetry/plugin-pg": "^0.3.1", + "@opentelemetry/test-utils": "^0.3.1", "@types/mocha": "^5.2.7", "@types/node": "^12.6.9", "@types/pg": "^7.11.2", diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts index 73ca6650a1..eb46f689ec 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts @@ -28,7 +28,7 @@ import * as assert from 'assert'; import * as pg from 'pg'; import * as pgPool from 'pg-pool'; import * as assertionUtils from './assertionUtils'; -import * as testUtils from './testUtils'; +import * as testUtils from '@opentelemetry/test-utils'; const memoryExporter = new InMemorySpanExporter(); @@ -97,14 +97,14 @@ describe('pg-pool@2.x', () => { pool = new pgPool(CONFIG); tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); if (testPostgresLocally) { - testUtils.startDocker(); + testUtils.startDocker('postgres'); } done(); }); after(function(done) { if (testPostgresLocally) { - testUtils.cleanUpDocker(); + testUtils.cleanUpDocker('postgres'); } pool.end(() => { done(); diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/testUtils.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/testUtils.ts deleted file mode 100644 index eec866fc41..0000000000 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/testUtils.ts +++ /dev/null @@ -1,54 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import * as childProcess from 'child_process'; -export function startDocker() { - const tasks = [ - run('docker run -d -p 54320:5432 --name otpostgres postgres:alpine'), - ]; - - for (let i = 0; i < tasks.length; i++) { - const task = tasks[i]; - if (task && task.code !== 0) { - console.error('Failed to start container!'); - console.error(task.output); - return false; - } - } - return true; -} - -export function cleanUpDocker() { - run('docker stop otpostgres'); - run('docker rm otpostgres'); -} - -function run(cmd: string) { - try { - const proc = childProcess.spawnSync(cmd, { - shell: true, - }); - return { - code: proc.status, - output: proc.output - .map(v => String.fromCharCode.apply(null, v as any)) - .join(''), - }; - } catch (e) { - console.log(e); - return; - } -} From 86985f9860a4a3b91ba48ac3e5173e7c661b71f6 Mon Sep 17 00:00:00 2001 From: Naseem Date: Mon, 23 Dec 2019 18:24:14 -0500 Subject: [PATCH 4/6] feat: include assertionUtils in test-utils package And use for redis and pg --- .../test/assertionUtils.ts | 79 ------------------ .../test/pg-pool.test.ts | 35 +++++--- .../test/assertionUtils.ts | 82 ------------------- .../opentelemetry-plugin-pg/test/pg.test.ts | 5 +- .../test/assertionUtils.ts | 76 ----------------- .../test/redis.test.ts | 11 ++- .../opentelemetry-test-utils/package.json | 3 + .../opentelemetry-test-utils/testUtils.ts | 61 ++++++++++++++ 8 files changed, 94 insertions(+), 258 deletions(-) delete mode 100644 packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts delete mode 100644 packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/assertionUtils.ts delete mode 100644 packages/opentelemetry-plugin-redis/test/assertionUtils.ts diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts deleted file mode 100644 index a964d47a2f..0000000000 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/assertionUtils.ts +++ /dev/null @@ -1,79 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import { - SpanKind, - Attributes, - Event, - Span, - TimedEvent, -} from '@opentelemetry/types'; -import * as assert from 'assert'; -import { ReadableSpan } from '@opentelemetry/tracing'; -import { - hrTimeToMilliseconds, - hrTimeToMicroseconds, -} from '@opentelemetry/core'; - -export const assertSpan = ( - span: ReadableSpan, - kind: SpanKind, - attributes: Attributes, - events: Event[] -) => { - assert.strictEqual(span.spanContext.traceId.length, 32); - assert.strictEqual(span.spanContext.spanId.length, 16); - assert.strictEqual(span.kind, kind); - - // check all the AttributeNames fields - Object.keys(span.attributes).forEach(key => { - assert.deepStrictEqual(span.attributes[key], attributes[key]); - }); - - assert.ok(span.endTime); - assert.strictEqual(span.links.length, 0); - - assert.ok( - hrTimeToMicroseconds(span.startTime) < hrTimeToMicroseconds(span.endTime) - ); - assert.ok(hrTimeToMilliseconds(span.endTime) > 0); - - // events - assert.strictEqual( - span.events.length, - events.length, - 'Should contain same number of events' - ); - span.events.forEach((_: TimedEvent, index: number) => { - assert.deepStrictEqual(span.events[index], events[index]); - }); -}; - -// Check if sourceSpan was propagated to targetSpan -export const assertPropagation = ( - childSpan: ReadableSpan, - parentSpan: Span -) => { - const targetSpanContext = childSpan.spanContext; - const sourceSpanContext = parentSpan.context(); - assert.strictEqual(targetSpanContext.traceId, sourceSpanContext.traceId); - assert.strictEqual(childSpan.parentSpanId, sourceSpanContext.spanId); - assert.strictEqual( - targetSpanContext.traceFlags, - sourceSpanContext.traceFlags - ); - assert.notStrictEqual(targetSpanContext.spanId, sourceSpanContext.spanId); -}; diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts index eb46f689ec..28ad86113a 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts @@ -20,14 +20,20 @@ import { InMemorySpanExporter, SimpleSpanProcessor, } from '@opentelemetry/tracing'; -import { SpanKind, Attributes, TimedEvent, Span } from '@opentelemetry/types'; +import { + SpanKind, + Attributes, + TimedEvent, + Span, + CanonicalCode, + Status, +} from '@opentelemetry/types'; import { plugin as pgPlugin, PostgresPlugin } from '@opentelemetry/plugin-pg'; import { plugin, PostgresPoolPlugin } from '../src'; import { AttributeNames } from '../src/enums'; import * as assert from 'assert'; import * as pg from 'pg'; import * as pgPool from 'pg-pool'; -import * as assertionUtils from './assertionUtils'; import * as testUtils from '@opentelemetry/test-utils'; const memoryExporter = new InMemorySpanExporter(); @@ -65,18 +71,23 @@ const DEFAULT_PG_ATTRIBUTES = { [AttributeNames.DB_USER]: CONFIG.user, }; +const okStatus: Status = { + code: CanonicalCode.OK, +}; + const runCallbackTest = ( parentSpan: Span, attributes: Attributes, events: TimedEvent[], + status: Status = okStatus, spansLength = 1, spansIndex = 0 ) => { const spans = memoryExporter.getFinishedSpans(); assert.strictEqual(spans.length, spansLength); const pgSpan = spans[spansIndex]; - assertionUtils.assertSpan(pgSpan, SpanKind.CLIENT, attributes, events); - assertionUtils.assertPropagation(pgSpan, parentSpan); + testUtils.assertSpan(pgSpan, SpanKind.CLIENT, attributes, events, status); + testUtils.assertPropagation(pgSpan, parentSpan); }; describe('pg-pool@2.x', () => { @@ -144,11 +155,11 @@ describe('pg-pool@2.x', () => { const span = tracer.startSpan('test span'); await tracer.withSpan(span, async () => { const client = await pool.connect(); - runCallbackTest(span, pgPoolattributes, events, 1, 0); + runCallbackTest(span, pgPoolattributes, events, okStatus, 1, 0); assert.ok(client, 'pool.connect() returns a promise'); try { await client.query('SELECT NOW()'); - runCallbackTest(span, pgAttributes, events, 2, 1); + runCallbackTest(span, pgAttributes, events, okStatus, 2, 1); } catch (e) { throw e; } finally { @@ -175,13 +186,13 @@ describe('pg-pool@2.x', () => { } release(); assert.ok(client); - runCallbackTest(parentSpan, pgPoolattributes, events, 1, 0); + runCallbackTest(parentSpan, pgPoolattributes, events, okStatus, 1, 0); client.query('SELECT NOW()', (err, ret) => { if (err) { return done(err); } assert.ok(ret); - runCallbackTest(parentSpan, pgAttributes, events, 2, 1); + runCallbackTest(parentSpan, pgAttributes, events, okStatus, 2, 1); done(); }); }); @@ -205,8 +216,8 @@ describe('pg-pool@2.x', () => { await tracer.withSpan(span, async () => { try { const result = await pool.query('SELECT NOW()'); - runCallbackTest(span, pgPoolattributes, events, 2, 0); - runCallbackTest(span, pgAttributes, events, 2, 1); + runCallbackTest(span, pgPoolattributes, events, okStatus, 2, 0); + runCallbackTest(span, pgAttributes, events, okStatus, 2, 1); assert.ok(result, 'pool.query() returns a promise'); } catch (e) { throw e; @@ -230,8 +241,8 @@ describe('pg-pool@2.x', () => { if (err) { return done(err); } - runCallbackTest(parentSpan, pgPoolattributes, events, 2, 0); - runCallbackTest(parentSpan, pgAttributes, events, 2, 1); + runCallbackTest(parentSpan, pgPoolattributes, events, okStatus, 2, 0); + runCallbackTest(parentSpan, pgAttributes, events, okStatus, 2, 1); done(); }); assert.strictEqual(resNoPromise, undefined, 'No promise is returned'); diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/assertionUtils.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/assertionUtils.ts deleted file mode 100644 index 2c81918305..0000000000 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/assertionUtils.ts +++ /dev/null @@ -1,82 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import { - SpanKind, - Attributes, - Event, - Span, - Status, -} from '@opentelemetry/types'; -import * as assert from 'assert'; -import { PostgresPlugin } from '../src'; -import { ReadableSpan } from '@opentelemetry/tracing'; -import { - hrTimeToMilliseconds, - hrTimeToMicroseconds, -} from '@opentelemetry/core'; -import { AttributeNames } from '../src/enums'; - -export const assertSpan = ( - span: ReadableSpan, - kind: SpanKind, - attributes: Attributes, - events: Event[], - status: Status -) => { - assert.strictEqual(span.spanContext.traceId.length, 32); - assert.strictEqual(span.spanContext.spanId.length, 16); - assert.strictEqual(span.kind, kind); - - assert.strictEqual( - span.attributes[AttributeNames.COMPONENT], - PostgresPlugin.COMPONENT - ); - assert.ok(span.endTime); - assert.strictEqual(span.links.length, 0); - - assert.ok( - hrTimeToMicroseconds(span.startTime) < hrTimeToMicroseconds(span.endTime) - ); - assert.ok(hrTimeToMilliseconds(span.endTime) > 0); - - // attributes - assert.deepStrictEqual(span.attributes, attributes); - - // events - assert.deepStrictEqual(span.events, events); - - assert.strictEqual(span.status.code, status.code); - if (status.message) { - assert.strictEqual(span.status.message, status.message); - } -}; - -// Check if sourceSpan was propagated to targetSpan -export const assertPropagation = ( - childSpan: ReadableSpan, - parentSpan: Span -) => { - const targetSpanContext = childSpan.spanContext; - const sourceSpanContext = parentSpan.context(); - assert.strictEqual(targetSpanContext.traceId, sourceSpanContext.traceId); - assert.strictEqual(childSpan.parentSpanId, sourceSpanContext.spanId); - assert.strictEqual( - targetSpanContext.traceFlags, - sourceSpanContext.traceFlags - ); - assert.notStrictEqual(targetSpanContext.spanId, sourceSpanContext.spanId); -}; diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts index bec13efe79..4ccb2db947 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts @@ -32,7 +32,6 @@ import { plugin, PostgresPlugin } from '../src'; import { AttributeNames } from '../src/enums'; import * as assert from 'assert'; import * as pg from 'pg'; -import * as assertionUtils from './assertionUtils'; import * as testUtils from '@opentelemetry/test-utils'; const memoryExporter = new InMemorySpanExporter(); @@ -74,7 +73,7 @@ const runCallbackTest = ( const spans = memoryExporter.getFinishedSpans(); assert.strictEqual(spans.length, spansLength); const pgSpan = spans[spansIndex]; - assertionUtils.assertSpan( + testUtils.assertSpan( pgSpan, SpanKind.CLIENT, attributes, @@ -82,7 +81,7 @@ const runCallbackTest = ( status ); if (span) { - assertionUtils.assertPropagation(pgSpan, span); + testUtils.assertPropagation(pgSpan, span); } }; diff --git a/packages/opentelemetry-plugin-redis/test/assertionUtils.ts b/packages/opentelemetry-plugin-redis/test/assertionUtils.ts deleted file mode 100644 index fdb517c7a0..0000000000 --- a/packages/opentelemetry-plugin-redis/test/assertionUtils.ts +++ /dev/null @@ -1,76 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * 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 - * - * https://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. - */ - -import { - SpanKind, - Attributes, - Event, - Span, - Status, -} from '@opentelemetry/types'; -import * as assert from 'assert'; -import { ReadableSpan } from '@opentelemetry/tracing'; -import { - hrTimeToMilliseconds, - hrTimeToMicroseconds, -} from '@opentelemetry/core'; - -export const assertSpan = ( - span: ReadableSpan, - kind: SpanKind, - attributes: Attributes, - events: Event[], - status: Status -) => { - assert.strictEqual(span.spanContext.traceId.length, 32); - assert.strictEqual(span.spanContext.spanId.length, 16); - assert.strictEqual(span.kind, kind); - - assert.ok(span.endTime); - assert.strictEqual(span.links.length, 0); - - assert.ok( - hrTimeToMicroseconds(span.startTime) < hrTimeToMicroseconds(span.endTime) - ); - assert.ok(hrTimeToMilliseconds(span.endTime) > 0); - - // attributes - assert.deepStrictEqual(span.attributes, attributes); - - // events - assert.deepStrictEqual(span.events, events); - - assert.strictEqual(span.status.code, status.code); - if (status.message) { - assert.strictEqual(span.status.message, status.message); - } -}; - -// Check if childSpan was propagated from parentSpan -export const assertPropagation = ( - childSpan: ReadableSpan, - parentSpan: Span -) => { - const targetSpanContext = childSpan.spanContext; - const sourceSpanContext = parentSpan.context(); - assert.strictEqual(targetSpanContext.traceId, sourceSpanContext.traceId); - assert.strictEqual(childSpan.parentSpanId, sourceSpanContext.spanId); - assert.strictEqual( - targetSpanContext.traceFlags, - sourceSpanContext.traceFlags - ); - assert.notStrictEqual(targetSpanContext.spanId, sourceSpanContext.spanId); -}; diff --git a/packages/opentelemetry-plugin-redis/test/redis.test.ts b/packages/opentelemetry-plugin-redis/test/redis.test.ts index ec68cbfad7..422cfa995a 100644 --- a/packages/opentelemetry-plugin-redis/test/redis.test.ts +++ b/packages/opentelemetry-plugin-redis/test/redis.test.ts @@ -23,8 +23,7 @@ import { NodeTracer } from '@opentelemetry/node'; import { plugin, RedisPlugin } from '../src'; import * as redisTypes from 'redis'; import { NoopLogger } from '@opentelemetry/core'; -import * as dockerUtils from '@opentelemetry/test-utils'; -import * as assertionUtils from './assertionUtils'; +import * as testUtils from '@opentelemetry/test-utils'; import { SpanKind, Status, CanonicalCode } from '@opentelemetry/types'; import { AttributeNames } from '../src/enums'; @@ -64,7 +63,7 @@ describe('redis@2.x', () => { } if (shouldTestLocal) { - dockerUtils.startDocker('redis'); + testUtils.startDocker('redis'); } redis = require('redis'); @@ -74,7 +73,7 @@ describe('redis@2.x', () => { after(() => { if (shouldTestLocal) { - dockerUtils.cleanUpDocker('redis'); + testUtils.cleanUpDocker('redis'); } }); @@ -174,14 +173,14 @@ describe('redis@2.x', () => { endedSpans[0].name, `redis-${operation.command}` ); - assertionUtils.assertSpan( + testUtils.assertSpan( endedSpans[0], SpanKind.CLIENT, attributes, [], okStatus ); - assertionUtils.assertPropagation(endedSpans[0], span); + testUtils.assertPropagation(endedSpans[0], span); done(); }); }); diff --git a/packages/opentelemetry-test-utils/package.json b/packages/opentelemetry-test-utils/package.json index 4160fb1226..b120cdfbf5 100644 --- a/packages/opentelemetry-test-utils/package.json +++ b/packages/opentelemetry-test-utils/package.json @@ -27,5 +27,8 @@ "tslint-consistent-codestyle": "^1.16.0", "tslint-microsoft-contrib": "^6.2.0", "typescript": "3.7.4" + }, + "dependencies": { + "@opentelemetry/tracing": "^0.3.1" } } diff --git a/packages/opentelemetry-test-utils/testUtils.ts b/packages/opentelemetry-test-utils/testUtils.ts index 111fa751da..f0eb0aa702 100644 --- a/packages/opentelemetry-test-utils/testUtils.ts +++ b/packages/opentelemetry-test-utils/testUtils.ts @@ -15,6 +15,20 @@ */ import * as childProcess from 'child_process'; +import { + SpanKind, + Attributes, + Event, + Span, + Status, +} from '@opentelemetry/types'; +import * as assert from 'assert'; +import { ReadableSpan } from '@opentelemetry/tracing'; +import { + hrTimeToMilliseconds, + hrTimeToMicroseconds, +} from '@opentelemetry/core'; + export function startDocker(db: 'redis' | 'mysql' | 'postgres') { let dockerRunCmd; switch (db) { @@ -65,3 +79,50 @@ function run(cmd: string) { return; } } + +export const assertSpan = ( + span: ReadableSpan, + kind: SpanKind, + attributes: Attributes, + events: Event[], + status: Status +) => { + assert.strictEqual(span.spanContext.traceId.length, 32); + assert.strictEqual(span.spanContext.spanId.length, 16); + assert.strictEqual(span.kind, kind); + + assert.ok(span.endTime); + assert.strictEqual(span.links.length, 0); + + assert.ok( + hrTimeToMicroseconds(span.startTime) < hrTimeToMicroseconds(span.endTime) + ); + assert.ok(hrTimeToMilliseconds(span.endTime) > 0); + + // attributes + assert.deepStrictEqual(span.attributes, attributes); + + // events + assert.deepStrictEqual(span.events, events); + + assert.strictEqual(span.status.code, status.code); + if (status.message) { + assert.strictEqual(span.status.message, status.message); + } +}; + +// Check if childSpan was propagated from parentSpan +export const assertPropagation = ( + childSpan: ReadableSpan, + parentSpan: Span +) => { + const targetSpanContext = childSpan.spanContext; + const sourceSpanContext = parentSpan.context(); + assert.strictEqual(targetSpanContext.traceId, sourceSpanContext.traceId); + assert.strictEqual(childSpan.parentSpanId, sourceSpanContext.spanId); + assert.strictEqual( + targetSpanContext.traceFlags, + sourceSpanContext.traceFlags + ); + assert.notStrictEqual(targetSpanContext.spanId, sourceSpanContext.spanId); +}; From e523aaa8b061e19fdbe37ebccd160dfa0e227a33 Mon Sep 17 00:00:00 2001 From: Naseem Date: Mon, 23 Dec 2019 18:32:12 -0500 Subject: [PATCH 5/6] fix: install required packages --- packages/opentelemetry-test-utils/package.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-test-utils/package.json b/packages/opentelemetry-test-utils/package.json index b120cdfbf5..80a42ddd19 100644 --- a/packages/opentelemetry-test-utils/package.json +++ b/packages/opentelemetry-test-utils/package.json @@ -22,13 +22,14 @@ }, "homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", "devDependencies": { + "@opentelemetry/core": "^0.3.1", + "@opentelemetry/tracing": "^0.3.1", + "@opentelemetry/types": "^0.3.1", "gts": "^1.1.2", "ts-node": "^8.5.4", "tslint-consistent-codestyle": "^1.16.0", "tslint-microsoft-contrib": "^6.2.0", "typescript": "3.7.4" }, - "dependencies": { - "@opentelemetry/tracing": "^0.3.1" - } + "dependencies": {} } From b374500bff37c9f531852a86664a58d8d87789eb Mon Sep 17 00:00:00 2001 From: Naseem Date: Mon, 23 Dec 2019 18:34:42 -0500 Subject: [PATCH 6/6] fix: gts --- .../opentelemetry-plugin-pg/test/pg.test.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts index 4ccb2db947..c508f5840f 100644 --- a/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts +++ b/packages/opentelemetry-plugin-postgres/opentelemetry-plugin-pg/test/pg.test.ts @@ -73,13 +73,7 @@ const runCallbackTest = ( const spans = memoryExporter.getFinishedSpans(); assert.strictEqual(spans.length, spansLength); const pgSpan = spans[spansIndex]; - testUtils.assertSpan( - pgSpan, - SpanKind.CLIENT, - attributes, - events, - status - ); + testUtils.assertSpan(pgSpan, SpanKind.CLIENT, attributes, events, status); if (span) { testUtils.assertPropagation(pgSpan, span); }