diff --git a/plugins/node/instrumentation-fs/test/fs.test.ts b/plugins/node/instrumentation-fs/test/fs.test.ts index c29fb0b40a..e8f97b6ab2 100644 --- a/plugins/node/instrumentation-fs/test/fs.test.ts +++ b/plugins/node/instrumentation-fs/test/fs.test.ts @@ -13,12 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { context, trace, SpanStatusCode, SpanKind } from '@opentelemetry/api'; +import { context, trace } from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { BasicTracerProvider, InMemorySpanExporter, - ReadableSpan, SimpleSpanProcessor, } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; @@ -34,6 +33,7 @@ import { SYNC_FUNCTIONS, } from '../src/constants'; import { indexFs, splitTwoLevels } from '../src/utils'; +import { assertSpans, makeRootSpanName } from './utils'; const TEST_ATTRIBUTE = 'test.attr'; const TEST_VALUE = 'test.attr.value'; @@ -149,16 +149,6 @@ describe('fs instrumentation', () => { ]); }); }; - const makeRootSpanName = (name: FMember): string => { - let rsn: string; - if (Array.isArray(name)) { - rsn = `${name[0]}.${name[1]}`; - } else { - rsn = `${name}`; - } - rsn = `${rsn} test span`; - return rsn; - }; const callbackTest: TestCreator = ( name: FMember, @@ -443,50 +433,3 @@ describe('fs instrumentation', () => { }); }); }); - -const assertSpans = (spans: ReadableSpan[], expected: any) => { - assert.strictEqual( - spans.length, - expected.length, - `Expected ${expected.length} spans, got ${spans.length}(${spans - .map((s: any) => `"${s.name}"`) - .join(', ')})` - ); - - spans.forEach((span, i) => { - assertSpan(span, expected[i]); - }); -}; - -const assertSpan = (span: ReadableSpan, expected: any) => { - assert(span); - assert.strictEqual(span.name, expected.name); - assert.strictEqual( - span.kind, - SpanKind.INTERNAL, - 'Expected to be of INTERNAL kind' - ); - if (expected.parentSpan) { - assert.strictEqual( - span.parentSpanId, - expected.parentSpan.spanContext().spanId - ); - } - if (expected.attributes) { - assert.deepEqual(span.attributes, expected.attributes); - } - if (expected.error) { - assert( - expected.error.test(span.status.message), - `Expected "${span.status.message}" to match ${expected.error}` - ); - assert.strictEqual(span.status.code, SpanStatusCode.ERROR); - } else { - assert.strictEqual( - span.status.code, - SpanStatusCode.UNSET, - 'Expected status to be unset' - ); - assert.strictEqual(span.status.message, undefined); - } -}; diff --git a/plugins/node/instrumentation-fs/test/fsPromises.test.ts b/plugins/node/instrumentation-fs/test/fsPromises.test.ts index 22229b6864..604eb8c61b 100644 --- a/plugins/node/instrumentation-fs/test/fsPromises.test.ts +++ b/plugins/node/instrumentation-fs/test/fsPromises.test.ts @@ -13,12 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { context, trace, SpanStatusCode, SpanKind } from '@opentelemetry/api'; +import { context, trace } from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { BasicTracerProvider, InMemorySpanExporter, - ReadableSpan, SimpleSpanProcessor, } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; @@ -26,7 +25,8 @@ import Instrumentation from '../src'; import * as sinon from 'sinon'; import type * as FSPromisesType from 'fs/promises'; import tests, { FsFunction, TestCase, TestCreator } from './definitions'; -import type { FMember, FPMember, EndHook } from '../src/types'; +import type { FPMember, EndHook } from '../src/types'; +import { assertSpans, makeRootSpanName } from './utils'; const supportsPromises = parseInt(process.versions.node.split('.')[0], 10) >= 14; @@ -67,17 +67,6 @@ if (supportsPromises) { context.disable(); }); - const makeRootSpanName = (name: FMember): string => { - let rsn: string; - if (Array.isArray(name)) { - rsn = `${name[0]}.${name[1]}`; - } else { - rsn = `${name}`; - } - rsn = `${rsn} test span`; - return rsn; - }; - const promiseTest: TestCreator = ( name: FPMember, args, @@ -162,50 +151,3 @@ if (supportsPromises) { }); }); } - -const assertSpans = (spans: ReadableSpan[], expected: any) => { - assert.strictEqual( - spans.length, - expected.length, - `Expected ${expected.length} spans, got ${spans.length}(${spans - .map((s: any) => `"${s.name}"`) - .join(', ')})` - ); - - spans.forEach((span, i) => { - assertSpan(span, expected[i]); - }); -}; - -const assertSpan = (span: ReadableSpan, expected: any) => { - assert(span); - assert.strictEqual(span.name, expected.name); - assert.strictEqual( - span.kind, - SpanKind.INTERNAL, - 'Expected to be of INTERNAL kind' - ); - if (expected.parentSpan) { - assert.strictEqual( - span.parentSpanId, - expected.parentSpan.spanContext().spanId - ); - } - if (expected.attributes) { - assert.deepEqual(span.attributes, expected.attributes); - } - if (expected.error) { - assert( - expected.error.test(span.status.message), - `Expected "${span.status.message}" to match ${expected.error}` - ); - assert.strictEqual(span.status.code, SpanStatusCode.ERROR); - } else { - assert.strictEqual( - span.status.code, - SpanStatusCode.UNSET, - 'Expected status to be unset' - ); - assert.strictEqual(span.status.message, undefined); - } -}; diff --git a/plugins/node/instrumentation-fs/test/utils.ts b/plugins/node/instrumentation-fs/test/utils.ts new file mode 100644 index 0000000000..a3f2cd33f7 --- /dev/null +++ b/plugins/node/instrumentation-fs/test/utils.ts @@ -0,0 +1,77 @@ +/* + * Copyright The 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 { ReadableSpan } from '@opentelemetry/sdk-trace-base'; +import * as assert from 'assert'; +import { SpanKind, SpanStatusCode } from '@opentelemetry/api'; +import type { FMember } from '../src/types'; + +export const assertSpans = (spans: ReadableSpan[], expected: any) => { + assert.strictEqual( + spans.length, + expected.length, + `Expected ${expected.length} spans, got ${spans.length}(${spans + .map((s: any) => `"${s.name}"`) + .join(', ')})` + ); + + spans.forEach((span, i) => { + assertSpan(span, expected[i]); + }); +}; + +const assertSpan = (span: ReadableSpan, expected: any) => { + assert(span); + assert.strictEqual(span.name, expected.name); + assert.strictEqual( + span.kind, + SpanKind.INTERNAL, + 'Expected to be of INTERNAL kind' + ); + if (expected.parentSpan) { + assert.strictEqual( + span.parentSpanId, + expected.parentSpan.spanContext().spanId + ); + } + if (expected.attributes) { + assert.deepEqual(span.attributes, expected.attributes); + } + if (expected.error) { + assert( + expected.error.test(span.status.message), + `Expected "${span.status.message}" to match ${expected.error}` + ); + assert.strictEqual(span.status.code, SpanStatusCode.ERROR); + } else { + assert.strictEqual( + span.status.code, + SpanStatusCode.UNSET, + 'Expected status to be unset' + ); + assert.strictEqual(span.status.message, undefined); + } +}; + +export const makeRootSpanName = (name: FMember): string => { + let rsn: string; + if (Array.isArray(name)) { + rsn = `${name[0]}.${name[1]}`; + } else { + rsn = `${name}`; + } + rsn = `${rsn} test span`; + return rsn; +};