Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
43fd59e
docs: add example for esm-http-ts
JamieDanielson Mar 21, 2023
e5215fe
fix: use instr version not node version
JamieDanielson Mar 21, 2023
3c51c6d
deps: add import-in-the-middle
JamieDanielson Mar 21, 2023
8444af3
feat: add iitm for esm instr
JamieDanielson Mar 21, 2023
e4478d9
docs: add readme notes from valentin
JamieDanielson Mar 21, 2023
1434b27
wip: add test based on valentin's test
JamieDanielson Mar 21, 2023
a533f9f
remove changes to ritm init
JamieDanielson Mar 22, 2023
a43089b
update example - use build dir, add readme
JamieDanielson Mar 22, 2023
85a2e8f
add changelog entry
JamieDanielson Mar 22, 2023
e0b5c44
dont use ritm singleton for iitm
JamieDanielson Mar 23, 2023
63477c5
fix up call for new iitm
JamieDanielson Mar 23, 2023
1188c02
Merge branch 'main' into esm-support
JamieDanielson Mar 23, 2023
33e544c
Merge branch 'main' into esm-support
lizthegrey Mar 28, 2023
bfe51f5
wrap function refactor
pkanal Mar 31, 2023
7110c37
clean up
pkanal Apr 3, 2023
3ddb3a9
Merge pull request #1 from honeycombio/purvi/esm-tests
JamieDanielson Apr 4, 2023
3f3d9a2
Merge branch 'main' into esm-support
JamieDanielson Apr 4, 2023
2936a16
update changelog: it takes a village
JamieDanielson Apr 4, 2023
4a10129
small cleanup on test
JamieDanielson Apr 4, 2023
50363c5
version should be node version, not instr version
JamieDanielson Apr 5, 2023
c9922c3
run lint:fix for prettier error
JamieDanielson Apr 5, 2023
b722486
Merge pull request #2 from honeycombio/jamie.http-node-version
pkanal Apr 5, 2023
4ac1ee1
base unwrap functionality
pkanal Apr 5, 2023
a346531
move esm specific wrap logic to node class
pkanal Apr 5, 2023
1ca6b55
fix types
pkanal Apr 6, 2023
4401576
add masswrap & massunwrap
pkanal Apr 6, 2023
86b6837
handle edge cases for masswrap and massunwrap
pkanal Apr 6, 2023
da694c7
Merge pull request #3 from honeycombio/purvi.unwrap
JamieDanielson Apr 7, 2023
1612855
Merge branch 'main' into esm-support
JamieDanielson Apr 7, 2023
e924c99
Ship shimmer types (#5)
pkanal Apr 25, 2023
b894bcd
Merge branch 'main' into esm-support
pkanal Apr 25, 2023
68c43c9
use --experimental-meta-resolve option with hook file (#8)
pkanal Apr 26, 2023
a2a74f8
Merge branch 'main' into esm-support
pkanal May 1, 2023
a529c8c
fix markdown
pkanal May 1, 2023
ddc8dc5
Merge branch 'main' into esm-support
pkanal May 3, 2023
42eb5ba
address PR feedback (#9)
pkanal May 3, 2023
4f71df8
Update experimental/packages/opentelemetry-instrumentation/hook.js
pkanal May 5, 2023
6a052b1
Update experimental/packages/opentelemetry-instrumentation/hook.mjs
pkanal May 5, 2023
58d34c0
change back to process.versions.node (#10)
pkanal May 5, 2023
33409da
address pr feedback (#11)
pkanal May 9, 2023
1f017b0
remove `--experimental-import-meta-resolve` flag (#12)
pkanal May 10, 2023
16109d3
Merge branch 'main' into esm-support
JamieDanielson May 10, 2023
bcaef1c
Merge branch 'main' into esm-support
JamieDanielson May 11, 2023
c590efc
Merge branch 'main' into esm-support
JamieDanielson May 12, 2023
59047bd
remove import limitation from README
pkanal May 12, 2023
0167a10
add esm example to examples readme
pkanal May 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/esm-http-ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { trace } from '@opentelemetry/api';
import { trace, DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
Expand All @@ -10,6 +10,7 @@ import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import http from 'http';

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
const tracerProvider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'esm-http-ts-example',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,28 @@ export abstract class InstrumentationAbstract<T = any>
}

/* Api to wrap instrumented method */
protected _wrap = shimmer.wrap;
protected _wrap = (
moduleExports: any,
name: string,
wrapper: (originalFn: any) => any
) => {
try {
return shimmer.wrap(moduleExports, name, wrapper);
} catch (e) {
// shimmer doesn't handle Proxy objects well
// if there is an error from import in the middle providing
// a Proxy of a Module we have to pass it to shimmer as a regular object
const wrapped: any = shimmer.wrap(
Object.assign({}, moduleExports),
name,
wrapper
);
Object.defineProperty(moduleExports, name, {
value: wrapped,
});
return moduleExports;
}
};
/* Api to unwrap instrumented methods */
protected _unwrap = shimmer.unwrap;
/* Api to mass wrap instrumented method */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export abstract class InstrumentationBase<T = any>
const hookFn: ImportInTheMiddle.HookFn = (exports, name, baseDir) => {
return this._onRequire<typeof exports>(
module as unknown as InstrumentationModuleDefinition<typeof exports>,
Object.assign({}, exports),
exports,
name,
baseDir
);
Expand All @@ -197,11 +197,13 @@ export abstract class InstrumentationBase<T = any>
: this._requireInTheMiddleSingleton.register(module.name, onRequire);

this._hooks.push(hook);
new (ImportInTheMiddle as unknown as typeof ImportInTheMiddle.default)(
[module.name],
{ internals: true },
<HookFn>hookFn
);
const esmHook =
new (ImportInTheMiddle as unknown as typeof ImportInTheMiddle.default)(
[module.name],
{ internals: false },
<HookFn>hookFn
);
this._hooks.push(esmHook);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,73 @@ import * as assert from 'assert';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
} from '../../build/src/index.js';
import * as exported from 'test-esm-module';

describe('when loading esm module', () => {
it('should patch module file', async () => {
class TestInstrumentation extends InstrumentationBase {
constructor() {
super('test-esm-instrumentation', '0.0.1');
class TestInstrumentationWrapFn extends InstrumentationBase {
constructor(config) {
super('test-esm-instrumentation', '0.0.1', config);
}
init() {
console.log('test-esm-instrumentation initialized!');
return new InstrumentationNodeModuleDefinition(
'test-esm-module',
['*'],
moduleExports => {
this._wrap(moduleExports, 'testFunction', () => {
return () => 'a different result';
});
return moduleExports;
},
moduleExports => {
this._unwrap(moduleExports, 'testFunction');
console.log('second');
return moduleExports;
}
init() {
console.log('test-esm-instrumentation initialized!');
return new InstrumentationNodeModuleDefinition(
'test-esm-module',
['*'],
moduleExports => {
console.log('patch');
moduleExports.testConstant = 43;
}
);
);
}
}

class TestInstrumentationSimple extends InstrumentationBase {
constructor(config) {
super('test-esm-instrumentation', '0.0.1', config);
}
init() {
console.log('test-esm-instrumentation initialized!');
return new InstrumentationNodeModuleDefinition(
'test-esm-module',
['*'],
moduleExports => {
moduleExports.testConstant = 43;
return moduleExports;
}
}
const instrumentation = new TestInstrumentation();
);
}
}
describe('when loading esm module', () => {
const instrumentationWrap = new TestInstrumentationWrapFn({
enabled: false,
});

it('should patch module file directly', async () => {
const instrumentation = new TestInstrumentationSimple({
enabled: false,
});
instrumentation.enable();
// const exported = await import('test-esm-module');
// assert.deepEqual(exported.testConstant, 43);
assert.deepEqual(exported.testConstant, 43);
});

// error from import-in-the-middle register
// TypeError: setters.get(...)[name] is not a function
it('should patch a module with the wrap function', async () => {
instrumentationWrap.enable();
assert.deepEqual(exported.testFunction(), 'a different result');
});

// it('should be able to unwrap a patched function', async () => {
// // disable to trigger unwrap
// const exported = await import('test-esm-module');
// instrumentationWrap.enable();
// instrumentationWrap.disable();
// assert.deepEqual(exported.testFunction(), 'test');
// });
});