-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(opencensus-shim) add require-in-the-middle hook to patch @opence…
…nsus/core
- Loading branch information
Showing
5 changed files
with
169 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* 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 { installShim } from './shim'; | ||
|
||
installShim(); |
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,62 @@ | ||
/* | ||
* 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 { diag, trace, Tracer } from '@opentelemetry/api'; | ||
import { Hook } from 'require-in-the-middle'; | ||
import * as oc from '@opencensus/core'; | ||
|
||
import { ShimTracer } from './ShimTracer'; | ||
import { VERSION } from './version'; | ||
|
||
type CoreTracerConstructor = new ( | ||
...args: ConstructorParameters<typeof oc.CoreTracer> | ||
) => oc.Tracer; | ||
|
||
let hook: Hook | null = null; | ||
|
||
interface OpenCensusShimConfig { | ||
/** | ||
* An optional OpenTelemetry tracer to send OpenCensus spans to. If not provided, one will be | ||
* created for you. | ||
*/ | ||
tracer?: Tracer | undefined; | ||
} | ||
|
||
/** | ||
* Patches OpenCensus to redirect all instrumentation to OpenTelemetry. Uses | ||
* require-in-the-middle to override the implementation of OpenCensus's CoreTracer. | ||
* | ||
* Use {@link uninstallShim} to undo the effects of this function. | ||
* | ||
* @param config | ||
*/ | ||
export function installShim({ | ||
tracer = trace.getTracer('@opentelemetry/shim-opencensus', VERSION), | ||
}: OpenCensusShimConfig = {}): void { | ||
diag.info('Installing OpenCensus shim require-in-the-middle hook'); | ||
|
||
hook = new Hook(['@opencensus/core'], exports => { | ||
const CoreTracer: CoreTracerConstructor = ShimTracer.bind(null, tracer); | ||
return { | ||
...exports, | ||
CoreTracer, | ||
}; | ||
}); | ||
} | ||
|
||
export function uninstallShim(): void { | ||
hook?.unhook(); | ||
} |
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,72 @@ | ||
/* | ||
* 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 * as assert from 'assert'; | ||
import { installShim, uninstallShim } from '../src/shim'; | ||
import { ShimTracer } from '../src'; | ||
import { CoreTracer as OrigCoreTracer } from '@opencensus/core'; | ||
import { withTestTracerProvider } from './util'; | ||
import { trace } from '@opentelemetry/api'; | ||
|
||
describe('shim', () => { | ||
beforeEach(uninstallShim); | ||
afterEach(uninstallShim); | ||
afterEach(() => { | ||
trace.disable(); | ||
}); | ||
|
||
describe('installShim', () => { | ||
it('should patch the @opencensus/core CoreTracer to create instances of the ShimTracer', () => { | ||
installShim(); | ||
const { CoreTracer } = require('@opencensus/core'); | ||
assert.notStrictEqual(CoreTracer, OrigCoreTracer); | ||
assert(new CoreTracer() instanceof ShimTracer); | ||
}); | ||
|
||
it('should use the provided Tracer', async () => { | ||
const spans = await withTestTracerProvider(tracerProvider => { | ||
const tracer = tracerProvider.getTracer('test'); | ||
installShim({ tracer }); | ||
const CoreTracer: typeof OrigCoreTracer = | ||
require('@opencensus/core').CoreTracer; | ||
const coreTracer = new CoreTracer(); | ||
coreTracer.startChildSpan().end(); | ||
}); | ||
assert.strictEqual(spans.length, 1); | ||
}); | ||
|
||
it('should use the global OpenTelemetry TracerProvider if none provided', async () => { | ||
installShim(); | ||
const spans = await withTestTracerProvider(tracerProvider => { | ||
trace.setGlobalTracerProvider(tracerProvider); | ||
const CoreTracer: typeof OrigCoreTracer = | ||
require('@opencensus/core').CoreTracer; | ||
const coreTracer = new CoreTracer(); | ||
coreTracer.startChildSpan().end(); | ||
}); | ||
assert.strictEqual(spans.length, 1); | ||
}); | ||
}); | ||
|
||
describe('uninstallShim', () => { | ||
it('should restore the original CoreTracer', () => { | ||
installShim(); | ||
uninstallShim(); | ||
const { CoreTracer } = require('@opencensus/core'); | ||
assert.strictEqual(CoreTracer, OrigCoreTracer); | ||
}); | ||
}); | ||
}); |
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