|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 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 | + * https://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 * as assert from 'assert'; |
| 18 | +import { installShim, uninstallShim } from '../src/shim'; |
| 19 | +import { ShimTracer } from '../src'; |
| 20 | +import { CoreTracer as OrigCoreTracer } from '@opencensus/core'; |
| 21 | +import { withTestTracerProvider } from './util'; |
| 22 | +import { trace } from '@opentelemetry/api'; |
| 23 | + |
| 24 | +describe('shim', () => { |
| 25 | + beforeEach(uninstallShim); |
| 26 | + afterEach(uninstallShim); |
| 27 | + afterEach(() => { |
| 28 | + trace.disable(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('installShim', () => { |
| 32 | + it('should patch the @opencensus/core CoreTracer to create instances of the ShimTracer', () => { |
| 33 | + installShim(); |
| 34 | + const { CoreTracer } = require('@opencensus/core'); |
| 35 | + assert.notStrictEqual(CoreTracer, OrigCoreTracer); |
| 36 | + assert(new CoreTracer() instanceof ShimTracer); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should use the provided Tracer', async () => { |
| 40 | + const spans = await withTestTracerProvider(tracerProvider => { |
| 41 | + const tracer = tracerProvider.getTracer('test'); |
| 42 | + installShim({ tracer }); |
| 43 | + const CoreTracer: typeof OrigCoreTracer = |
| 44 | + require('@opencensus/core').CoreTracer; |
| 45 | + const coreTracer = new CoreTracer(); |
| 46 | + coreTracer.startChildSpan().end(); |
| 47 | + }); |
| 48 | + assert.strictEqual(spans.length, 1); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should use the global OpenTelemetry TracerProvider if none provided', async () => { |
| 52 | + installShim(); |
| 53 | + const spans = await withTestTracerProvider(tracerProvider => { |
| 54 | + trace.setGlobalTracerProvider(tracerProvider); |
| 55 | + const CoreTracer: typeof OrigCoreTracer = |
| 56 | + require('@opencensus/core').CoreTracer; |
| 57 | + const coreTracer = new CoreTracer(); |
| 58 | + coreTracer.startChildSpan().end(); |
| 59 | + }); |
| 60 | + assert.strictEqual(spans.length, 1); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('uninstallShim', () => { |
| 65 | + it('should restore the original CoreTracer', () => { |
| 66 | + installShim(); |
| 67 | + uninstallShim(); |
| 68 | + const { CoreTracer } = require('@opencensus/core'); |
| 69 | + assert.strictEqual(CoreTracer, OrigCoreTracer); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments