-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instrumentation-fs): require parent span
- Loading branch information
Showing
4 changed files
with
164 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { | ||
BasicTracerProvider, | ||
InMemorySpanExporter, | ||
SimpleSpanProcessor, | ||
} from '@opentelemetry/sdk-trace-base'; | ||
import Instrumentation from '../src'; | ||
import * as assert from 'assert'; | ||
import type * as FSType from 'fs'; | ||
import type { FsInstrumentationConfig } from '../src/types'; | ||
import * as api from '@opentelemetry/api'; | ||
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; | ||
|
||
const supportsPromises = parseInt(process.versions.node.split('.')[0], 10) > 8; | ||
|
||
const provider = new BasicTracerProvider(); | ||
const memoryExporter = new InMemorySpanExporter(); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); | ||
|
||
const tracer = provider.getTracer('default'); | ||
|
||
describe('fs instrumentation: requireParentSpan', () => { | ||
let plugin: Instrumentation; | ||
let fs: typeof FSType; | ||
let context: api.Context; | ||
let endRootSpan: () => void; | ||
let additionalSpans: number; | ||
|
||
const initializePlugin = (pluginConfig: FsInstrumentationConfig) => { | ||
plugin = new Instrumentation(); | ||
plugin.setTracerProvider(provider); | ||
plugin.setConfig(pluginConfig); | ||
plugin.enable(); | ||
fs = require('fs'); | ||
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); | ||
} | ||
|
||
beforeEach(() => { | ||
const contextManager = new AsyncHooksContextManager(); | ||
api.context.setGlobalContextManager(contextManager.enable()); | ||
}) | ||
|
||
afterEach(() => { | ||
plugin.disable(); | ||
memoryExporter.reset(); | ||
}); | ||
|
||
const createSpanTests = (shouldCreateSpan: boolean) => { | ||
const prefix = shouldCreateSpan ? "creates" : "does not create" | ||
const expectedSpans = () => (shouldCreateSpan ? 1 : 0) + additionalSpans | ||
|
||
it(`${prefix} a span with the callback API`, async () => { | ||
await new Promise<void>((resolve) => { | ||
api.context.with(context, () => { | ||
fs.access('./test/fixtures/readtest', fs.constants.R_OK, () => resolve()) | ||
}) | ||
}).then(() => endRootSpan()) | ||
|
||
assert.deepEqual(memoryExporter.getFinishedSpans().length, expectedSpans()); | ||
}) | ||
|
||
it(`${prefix} a span with the synchronous API`, () => { | ||
api.context.with(context, () => { | ||
fs.accessSync('./test/fixtures/readtest', fs.constants.R_OK) | ||
endRootSpan() | ||
}) | ||
|
||
assert.deepEqual(memoryExporter.getFinishedSpans().length, expectedSpans()); | ||
}) | ||
|
||
if (supportsPromises) { | ||
it(`${prefix} a span with the promises API`, async () => { | ||
await new Promise<void>((resolve) => { | ||
api.context.with(context, () => { | ||
fs.promises.access('./test/fixtures/readtest', fs.constants.R_OK) | ||
.finally(() => resolve(endRootSpan())) | ||
}) | ||
}) | ||
|
||
assert.deepEqual(memoryExporter.getFinishedSpans().length, expectedSpans()); | ||
}) | ||
} | ||
} | ||
|
||
const withRootSpan = (fn: () => void) => { | ||
describe('with a root span', () => { | ||
beforeEach(() => { | ||
const rootSpan = tracer.startSpan("root span") | ||
|
||
context = api.trace.setSpan(api.context.active(), rootSpan) | ||
endRootSpan = () => rootSpan.end() | ||
additionalSpans = 1 | ||
}) | ||
|
||
fn() | ||
}) | ||
} | ||
|
||
const withoutRootSpan = (fn: () => void) => { | ||
describe('without a root span', () => { | ||
beforeEach(() => { | ||
context = api.context.active() | ||
endRootSpan = () => {} | ||
additionalSpans = 0 | ||
}) | ||
|
||
fn() | ||
}) | ||
} | ||
|
||
describe('requireParentSpan enabled', () => { | ||
beforeEach(() => { | ||
initializePlugin({requireParentSpan: true}) | ||
}) | ||
|
||
withRootSpan(() => { | ||
createSpanTests(true) | ||
}) | ||
|
||
withoutRootSpan(() => { | ||
createSpanTests(false) | ||
}) | ||
}) | ||
|
||
describe('requireParentSpan disabled', () => { | ||
beforeEach(() => { | ||
initializePlugin({requireParentSpan: false}) | ||
}) | ||
|
||
withRootSpan(() => { | ||
createSpanTests(true) | ||
}) | ||
|
||
withoutRootSpan(() => { | ||
createSpanTests(true) | ||
}) | ||
}) | ||
}) |